PDA

View Full Version : Multiple Triangles



DeclanMcgee11
02-26-2015, 06:51 AM
Hello,

I am trying to display 10 different sized triangles on screen and at the moment nothing appears when I run the code.
Some help would be great

#include<ctime>
#include<cstdlib>
#include<GL/glut.h>
#include<cmath>
using namespace std;

void start_random()
{
static bool seeded = false;
if (!seeded){
srand((unsigned)time(NULL));
seeded = true;
}
}

// Generate a random float in the range -1 to +1...
float rnd(){
start_random();
return (-1) + (float)rand() / 16384;
}

class Triangle
{
public:
Triangle(float X, float Y, float width, float height);
void Show();
private:
float x, y, w, h;
};

Triangle::Triangle(float X, float Y, float width, float height)
{
x = rnd(); y = rnd(); w = rnd(); h = rnd();
}

void Triangle::Show()
{
glVertex2f(x, y);
glVertex2f(x - 0.5*w, y - h);
glVertex2f(x + 0.5*w, y - h);

}

void CreateShapes(float x, float y, float w, float h)
{
for (int i = 0; i < 100; i++)
{
Triangle t(x, y, w, h);
t.Show();
}

}


void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
void CreateShapes(float x,float y,float w,float h);
glFlush();


}

int main(int argc, char**argv)
{

glutInit(&argc, argv);
glutCreateWindow("Random Shapes");
glClearColor(1, 1, 1, 1);
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}

tkausl
02-26-2015, 07:34 AM
You are not drawing anything.

void CreateShapes(float x,float y,float w,float h); is a function-definition, not a call. I didn't even knew this would compile. What you're looking for is more like
CreateShapes(0,0,100,100);

DeclanMcgee11
02-26-2015, 08:04 AM
Thanks man! That has worked!

It now display a randomly sized triangle but where shoud I put the for loop so that is does this lets say 10 times?

Betrayal
02-26-2015, 10:29 AM
Thanks man! That has worked!

It now display a randomly sized triangle but where shoud I put the for loop so that is does this lets say 10 times?

Create 10 triangles once when your program is loading, not in every callback of display().
Put the triangles in an array or std::vector.
Put the loop in display and call the show method of every triangle.


Well, maybe learn C++ first instead of doing random changes at some example code?