PDA

View Full Version : Drawing dynamic boxes not working correctly



jsimps
12-13-2009, 12:17 PM
Hi guys,

I'm pretty new to coding in OpenGL, and I've run into a bit of an issue. Right now I'm programming a game that will draw a row of boxes, the vertices of which are determined randomly and stored in an array. The vertices are then to be passed to a drawing function two at a time to draw the boxes. However, the way the code is written right now, only the first and the last elements of the array are being passed to the drawing function. Here's the code in question, and I'll supply a sample output below that

void drawBox(int x1, int x2, int y1, int y2)
{
glBegin(GL_POLYGON);
glVertex2f(x1, y1);
glVertex2f(x1, y2);
glVertex2f(x2, y2);
glVertex2f(x2, y1);
glEnd();
}

void drawRow1(void)
{
int i = 0;
int length;
row1.y[0] = 600;
row1.y[1] = 580;

initialize();
genLength(&length); \\This function generates a length for the array
genXValue(row1.x, length); \\This populates the array with values
sort(row1.x, length); \\places the values in order from least to greatest


for (i = 0; i < length -1; i++)
{
drawBox(row1.x[i], row1.x[i+1], row1.y[0], row1.y[1]);

}


printf("value of length: %d\n", length);
for (i = 0; i <length; i++) {
printf(" %d", row1.x[i]);
}

}

And here's what sample run through looks like:
value of length: 4
values in array: 29 48 236 585
http://twitpic.com/tdse2

So, as you can see, the array held 4 values, so there should be 2 blocks drawn. However, the drawing function only used the values of 29 and 585 to draw the vertices. Any ideas why that's happening?

jsimps
12-13-2009, 12:23 PM
And if it will be any help, here's the code in its entirety

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <GLUT/GLUT.h>

#define WIDTH 600
#define HEIGHT 600
/* Define Global Variables */



struct point {
int x[10];
int y[10];
} row1, row2, row3, row4;

void initialize(void) //Initializes seed. WORKS
{
srandom((unsigned)time(NULL));
}

void genLength(int *n) //Sets Length of array. WORKS
{

*n = 1 + random() % 10;

while (*n % 2 != 0)
{
*n = 1 + random() % 10;
}

}

void genXValue(int *a, int length) //Generates X Values. WORKS
{
int i;
for (i = 0; i < length; i++)
{
a[i] = random() % (WIDTH +1);
}

}

void swap (int *p, int *q) //Swaps variables. WORKS
{
int tmp;

tmp = *p;
*p = *q;
*q = tmp;
}
void sort(int a[], int n) //Sorts arrays. WORKS
{
int i, j;

for (i = 0; i < n - 1; ++i)
{
for (j = n -1; i < j; --j)
{
if(a[j-1] > a[j])
swap(&amp;a[j-1], &amp;a[j]);
}
}
}

/* OpenGL functions */
void drawBall(void)
{
int i;

glBegin(GL_POLYGON);
for (i=0; i < 20; i++)
{
float x = 25 * cos(i*2.0*M_PI/20);
float y = 25 * sin(i*2.0*M_PI/20);
glVertex2f(x,y);
}
glEnd();
}

void drawBox(int x1, int x2, int y1, int y2)
{
glBegin(GL_POLYGON);
glVertex2f(x1, y1);
glVertex2f(x1, y2);
glVertex2f(x2, y2);
glVertex2f(x2, y1);
glEnd();
}

void drawRow1(void)
{
int i = 0;
int length;
row1.y[0] = 600;
row1.y[1] = 580;

initialize();
genLength(&amp;length);
genXValue(row1.x, length);
sort(row1.x, length);


for (i = 0; i < length -1; i++)
{
drawBox(row1.x[i], row1.x[i+1], row1.y[0], row1.y[1]);

}


printf("value of length: %d\n", length); //Used to ensure I'm getting good
for (i = 0; i <length; i++) { //values from length and array
printf(" %d", row1.x[i]);
}

}

void displayFunction(void)
{

glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glColor3f(0, 0, 0);
drawRow1();
glPopMatrix();

glFlush();
glutSwapBuffers();
}


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

glutInit(&amp;argc, argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Projectile Motion");

glutDisplayFunc(displayFunction);
/*glutKeyboardFunc(keyPressed);

glutMotionFunc(MotionFunction);*/

gluOrtho2D(0, WIDTH, 0, HEIGHT);

glClearColor(1, 1, 1, 0);

/* start main loop */

glutMainLoop();



return 0;
}

david_m
12-14-2009, 07:09 AM
Hi,

If the value of length is 4, then i goes from zero to 2 in your for() loop.

so the boxes you draw are:
drawBox(x[0],x[1],y[0],y[1]);
drawBox(x[1],x[2],y[0],y[1]);
drawBox(x[2],x[3],y[0],y[1]);

So really you are drawing 3 boxes.
In this way, the boxes are lined up next to each other (since the end of the first box is the beginning of the second.) So, it will appear that only one box is drawn, when in reality you have drawn 3.
If you change the y values of each box, this will be obvious.

Hope this helps,
David

jsimps
12-14-2009, 05:49 PM
Thanks David! That cleared it right up. This was definitely a case of staring too long at the same code. Silly error on my part.