0

I'm trying to make it where I can draw an square with the values in the global array. I made it work without vertex array before but now I need to save it in array so I can possibly delete a square with just a mouse click. However I can't get the squares to show up on screen, it just appears blank. Any suggestions?

#include <GL/glut.h>
    #include <stdlib.h>
    #include <math.h>
     
     
    using namespace std;
     
    struct GLPoint
    {
    GLint x;
    GLint y;
    };
     
    GLint totalmarbles = 64;
    GLPoint midhold[64];
    GLint vertices[64];
     
    double dist(GLPoint a, GLPoint b)
    {
    double distance = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
    return distance;
    }
     
     
    void init(void)
    {
    glClearColor (1.0, 1.0, 1.0, 0.0);
    glShadeModel (GL_SMOOTH);
    }
     
     
     
    GLPoint mid(GLPoint A, GLPoint B)
    {
    GLPoint midpt = (GLPoint){((A.x + B.x)/2, (A.y + B.y)/2)};
    return midpt;
    }
     
    void storemids(int q, GLPoint A)
    {
    midhold[q] = A;
    }
     
    void vertice()
    {
    GLint total = 0;
    GLint x = 0; GLint y = 0;
    GLint x1, y1, x2, y2, x3, y3, x4, y4;
    x1 = x; y1 = y;
    x2 = x + 100; y2 = y;
    x3 = x + 100; y3 = y + 100;
    x4 = x; y4 = y + 100;
    /*GLPoint P = (GLPoint){(x1, y1)};
      GLPoint Q = (GLPoint){(x2, y2)};
      GLPoint R = (GLPoint){(x3, y3)};
      GLPoint S = (GLPoint){(x4, y4)};*/
    vertices[total] = x1; vertices[total+1] = y1;
    vertices[total+2] = x2; vertices[total+3] = y2;
    vertices[total+3] = x3; vertices[total+4] = y3;
    vertices[total+5] = x4; vertices[total+6] = y4;
    total += 7;
    while(y < 600)
    {
    while(x <= 700)
    {
    x += 100;
    x1 = x; y1 = y;
    x2 = x + 100; y2 = y;
    x3 = x + 100; y3 = y + 100;
    x4 = x; y4 = y + 100;
    /*GLPoint P = (GLPoint){(x1, y1)};
      GLPoint Q = (GLPoint){(x2, y2)};
      GLPoint R = (GLPoint){(x3, y3)};
      GLPoint S = (GLPoint){(x4, y4)};*/
    vertices[total] = x1; vertices[total+1] = y1;
    vertices[total+2] = x2; vertices[total+3] = y2;
    vertices[total+3] = x3; vertices[total+4] = y3;
    vertices[total+5] = x4; vertices[total+6] = y4;
    total += 7;
    }
    x = 0; y += 75;
    x1 = x; y1 = y;
    x2 = x + 100; y2 = y;
    x3 = x + 100; y3 = y + 100;
    x4 = x; y4 = y + 100;
    /*GLPoint P = (GLPoint){x1, y1};
      GLPoint Q = (GLPoint){x2, y2};
      GLPoint R = (GLPoint){x3, y3};
      GLPoint S = (GLPoint){x4, y4};*/
    vertices[total] = x1; vertices[total+1] = y1;
    vertices[total+2] = x2; vertices[total+3] = y2;
    vertices[total+3] = x3; vertices[total+4] = y3;
    vertices[total+5] = x4; vertices[total+6] = y4;
    total += 7;
     
    }
     
    while(x <= 700)
    {
    x += 100; y = 700;
    x1 = x; y1 = y;
    x2 = x + 100; y2 = y;
    x3 = x + 100; y3 = y + 100;
    x4 = x; y4 = y + 100;
    /*GLPoint P = (GLPoint){x1, y1};
      GLPoint Q = (GLPoint){x2, y2};
      GLPoint R = (GLPoint){x3, y3};
      GLPoint S = (GLPoint){x4, y4};*/
    vertices[total] = x1; vertices[total+1] = y1;
    vertices[total+2] = x2; vertices[total+3] = y2;
    vertices[total+3] = x3; vertices[total+4] = y3;
    vertices[total+5] = x4; vertices[total+6] = y4;
    total += 7;
    }
     
    }
     
    void drawrectangles()
    {
    vertice();
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_INT, 0, vertices);
    glColor3f(0.0, 0.0, 0.0);
    glPolygonMode(GL_FRONT, GL_LINE);
    glBegin(GL_POLYGON);
    for(int i = 0; i < totalmarbles; i++)
    glArrayElement(vertices[i]);
    glEnd();
    }
     
    void myMouse(int button, int state, int x, int y)
    {
    GLPoint click = (GLPoint){x, y};
    double hitbox[totalmarbles];
    for(int i = 0; i < totalmarbles; i++)
    {
    double box = dist(midhold[i], click);
    hitbox[i] = box;
    }
     
    if(state == GLUT_DOWN)
    {
    if(button == GLUT_LEFT_BUTTON)
    {
    for(int j = 0; j < totalmarbles; j++)
    {
    if(hitbox[j] < 100)
    {
    vertices[63];
    drawrectangles();
    }
    }
     
    }
    }
    else if(button == GLUT_RIGHT_BUTTON)
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
    }
    }
     
     
     
    void display(void)
    {
    glFlush();
    }
     
     
    void reshape (int w, int h)
    {
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
    }
     
     
    int main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (800, 600);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    glutMouseFunc(myMouse);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
    init ();
    glutMainLoop();
    return 0;
    }
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by gusano79
0

I'm trying to make it where I can draw an square with the values in the global array. I made it work without vertex array before but now I need to save it in array so I can possibly delete a square with just a mouse click. However I can't get the squares to show up on screen, it just appears blank. Any suggestions?

Have you checked your compiler warnings? Using GCC, I'm getting two:

  1. In function 'GLPoint mid(GLPoint, GLPoint)':
    warning: left-hand operand of comma has no effect
    GLPoint mid(GLPoint A, GLPoint B)
    {
        GLPoint midpt = (GLPoint) {
            ((A.x + B.x) / 2, (A.y + B.y) / 2)
        };
        return midpt;
    }

    Try removing the outer brackets: (A.x + B.x) / 2, (A.y + B.y) / 2

  2. In function 'void myMouse(int, int, int, int)':
    warning: statement has no effect
    if(state == GLUT_DOWN)
    {
        if(button == GLUT_LEFT_BUTTON)
        {
            for(int j = 0; j < totalmarbles; j++)
            {
                if(hitbox[j] < 100)
                {
                    vertices[63];
                    drawrectangles();
                }
            }
        }
    }

    What are you trying to do with that line?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.