Results 1 to 4 of 4

Thread: Drawing dynamic boxes not working correctly

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2009
    Posts
    3

    Drawing dynamic boxes not working correctly

    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
    [img:left]http://twitpic.com/tdse2[/img]

    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?

  2. #2
    Junior Member Newbie
    Join Date
    Dec 2009
    Posts
    3

    Re: Drawing dynamic boxes not working correctly

    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;
    }

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2009
    Posts
    9

    Re: Drawing dynamic boxes not working correctly

    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

  4. #4
    Junior Member Newbie
    Join Date
    Dec 2009
    Posts
    3

    Re: Drawing dynamic boxes not working correctly

    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.

Similar Threads

  1. VBO not working correctly?
    By rcapote in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 08-23-2014, 11:30 AM
  2. animation not working correctly
    By qawded in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-08-2013, 11:05 AM
  3. Lighting and normals not working correctly
    By PascalM123 in forum OpenGL: GLSL
    Replies: 12
    Last Post: 08-24-2010, 09:15 AM
  4. gluUnProject not working correctly
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-13-2003, 02:39 AM
  5. GL_CLAMP not working correctly
    By iNsaNEiVaN in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-21-2003, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean