PDA

View Full Version : help incrementing a square



jaffacakes
11-24-2008, 03:01 PM
hi im a new user to opengl and i needsome help ive written this program in C to display a square pretty basic but need help creating more 2 more squares which will create 3 in total. i was told u use and incrementing code which i cant find. code i am using now. help will be much appreciated with coding

/*
* square.c
*/
#include <GL/glut.h>

void display(void)
{

glClear (GL_COLOR_BUFFER_BIT);


glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();


glFlush ();
}

void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}


int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

matchStickMan
11-24-2008, 09:00 PM
Hi there, by incremental code, would i be correct to say you want to have only one function that draws the squares? If that's the case, maybe the code below might help you. I havent tested it but I guess it should work fine. Let me know if you have issues understanding it.


void DrawSquare( x0, y0, x1, y1 )
{
glBegin( GL_POLYGON );
glVertex3f ( x0, y0, 0.0 );
glVertex3f ( x1, y0, 0.0 );
glVertex3f ( x1, y1, 0.0 );
glVertex3f ( x0, y1, 0.0 );
glEnd();
}


void display(void)
{

glClear (GL_COLOR_BUFFER_BIT);

glMatrixMode( GL_MODELVIEW );

//Draw the first square
glPushMatrix();

glColor3f (1.0, 1.0, 1.0);
DrawSquare(0.25, 0.25, 0.75, 0.75);

glPopMatrix();




//Draw the second square and translate to appropriate position
glPushMatrix();

glColor3f (1.0, 0.0, 0.0);
glTranslated( 0.0, 0.5, 0.0 );
DrawSquare(0.25, 0.25, 0.75, 0.75);

glPopMatrix();




//Draw the third square and translate to appropriate position
glPushMatrix();

glColor3f (0.0, 1.0, 0.0);
glTranslated( 0.5, 0.0, 0.0 );
DrawSquare(0.25, 0.25, 0.75, 0.75);

glPopMatrix();

glFlush ();
}