PDA

View Full Version : Rectangle repetition



dvwood
11-04-2008, 11:55 AM
Hi All,

I am trying to make like a chess board, and i was wondering if there was an easy way to repeat these rectangles, instead of writing all coordinates and figuring out where they will have to be in space... thats a lot of work, unfortunately.

Any suggestions? I am pretty bad at coding, so i am trying to figure it out piece by piece... i would appreciate code examples :-)



void setupDrawing() {
glColor3f(0.10f, 0.86f, 0.30f); // .......green
glRectf(-0.75f,-0.75f, 0.75f, 0.75f);
glEnd();
}

trinitrotoluene
11-04-2008, 01:12 PM
This should be very easy. The following should draw a big rectangle (0.0,0.0,size*NUM_RECT_X,size*NUM_RECT_Y) made of smaller rectangle.


GLfloat size = 1.0;

for(int j = 0 ; j < NUM_RECT_Y ; j++)
{
for(int i = 0; i < NUM_RECT_X ; i++)
{
Edit:
if( (i+j)%2 == 1)
{
glColor3f(0.0,0.0,0.0);
}
else
{
glColor3f(1.0,1.0,1.0);
}
End Edit
glRectf(i*size,j*size,size + i*size,size + j*size);
}
}


Disclaimer: I don't test this code so maybe is all wrong but it give you a start for a solution.

Reedit: It works! But doing it with triangle strip should be more efficient.

dvwood
11-04-2008, 02:26 PM
Thank you for the code.
I tried inserting it, but i dont know if i am doing the correct thing here.

I only get a green rectangle...




#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>
#include <stdlib.h>



#define ESC_KEY 27

int size = 0;
int NUM_RECT_Y;
int NUM_RECT_X;

void setupDrawing();
void display ( void ) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //drawing commands


glPushMatrix(); // ----> SAVE
glTranslatef(-2.0f, -3.0f, -2.f);
setupDrawing(); //DRAW CUBE
glPopMatrix(); // ---------> RESTORE

}

void setupDrawing() {

GLfloat size = 1.0;

for(int j = 0 ; j < NUM_RECT_Y ; j++)
{
for(int i = 0; i < NUM_RECT_X ; i++)
{

if( (i+j)%2 == 1)
{
glColor3f(0.0,0.0,0.0);
}
else
{
glColor3f(1.0,1.0,1.0);
}

glRectf(i*size,j*size,size + i*size,size + j*size);
}
}




glColor3f(0.10f, 0.86f, 0.30f); // .......green color
glRectf(-0.75f,-0.75f, 0.75f, 0.75f);
glEnd();



glutPostRedisplay();
glutSwapBuffers(); //

}





//WINDOW!!!
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (height==0)
gluPerspective ( 155, float(width), 1.0, 500.0 ); //75 er en y-størrelse, af FOW, dvs kamera/aksen bliver ændre og ikke objektet
else
gluPerspective ( 155, float(width)/height, 1.0, 500.0); //-1.0 tæt på kamera
glMatrixMode(GL_MODELVIEW);
}
//WINDOW OVER!!!


void keyboard ( unsigned char key, int x, int y )

{

glLoadIdentity();
switch ( key )
{
case ESC_KEY:
exit(1);
break;

glutPostRedisplay();
break;

default:
break;
}
glutPostRedisplay();
}




int main(int argc, char* argv[])
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize(600, 600 );
glutCreateWindow("Game of Life");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glEnable(GL_DEPTH_TEST);

glutMainLoop();//
return 0;

}

trinitrotoluene
11-04-2008, 02:34 PM
Remove the glEnd() call at the end of setupDrawing(). For reference,here is my test code:




#include <iostream>
#include <GL/glut.h>


void draw(void);
void init(void);
void reshape(int width,int height);


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

glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_STEN CIL|GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutCreateWindow("glrectf");
glutReshapeFunc(reshape);
glutDisplayFunc(draw);

init();
glutMainLoop();
return(EXIT_SUCCESS);
}


void draw(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL _STENCIL_BUFFER_BIT);
glLoadIdentity();
static GLfloat size = 1.0;
static const int NUM_RECT_X = 10;
static const int NUM_RECT_Y = 10;

for(int j = 0 ; j < NUM_RECT_Y ; j++)
{
for(int i = 0; i < NUM_RECT_X ; i++)
{
if( (i+j)%2 == 1)
{
glColor3f(0.0,0.5,0.0);
}
else
{
glColor3f(1.0,1.0,1.0);
}

glRectf(i*size,j*size,size + i*size,size + j*size);
}
}

glutSwapBuffers();
}

void init(void)
{
glEnable(GL_DEPTH_TEST);
}


void reshape(int width,int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0,20.0,-5.0,20.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

dvwood
11-04-2008, 02:49 PM
YEEEEEEEEEEEEES!! IT WORKED!!

Thank you soo much!!