Jump to content
  • Advertisement
Sign in to follow this  
TyriceClark

OpenGL Screen not showing polygons with arrayelement()

This topic is 2593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make a program where I can draw a number of squares on screen and click one to make it disappear. I figured I need to make an array to hold all the vertices of the rectangles and use arrayelement to draw the polygon at each point that i set. Then I set an array for the midpoints of all the squares so I can use the distance formula for the mouseclick to trigger an action if I click inside the square. However when I run the program, it doesn't show anything on screen.
I made it run perfect with the same code without using arrays, but couldn't figure how to make the squares clickable without it. I was wondering if anyone could look at the code and see why it's not displaying and tell me if it would be effective even if it did display. Maybe do I need to try and figure out how OpenGL picking can be used?

#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);
glEnd();
glDisableClientState(GL_VERTEX_ARRAY);
}

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, click);
hitbox = 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;
}

Share this post


Link to post
Share on other sites
Advertisement
I thought I had to enable client state to make the array vertices() be able to point at ArrayElement(). I guess I'm not understanding the use of it...

Share this post


Link to post
Share on other sites
why not use glDrawArrays

glDrawArrays(GL_QUADS, verts[4*i], 4);? i didint found nothing else :0






maybe you place verts not there where you want to place them or just do not render them from correct perspective...

Share this post


Link to post
Share on other sites
I dont know what tutorial or whatever you are reading but you missed something here:

your draw rectangles function in messed up. For one, I never used glBegin(GL_POLYGON)
do glBegin(GL_QUADS) //draw quads/rectangles

second as suggested, glVertex takes care of everything so like:

I still cant follow your code, its hard to read, you have marbles and then vertices. It seems as if you never had a triangle/rectangle ever rendered on screen and just wrote a ton of code for marbles and what not, without knowing how to draw a rectangle. If you have, then I don't really know what you are doing.

void drawrectangles()
{
vertice();
glColor3f(0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT, GL_LINE);
glBegin(GL_QUADS);
for(int i = 0; i < totalmarbles; i++)
glVertex3f(x..,y..,z..);
glEnd();
}

Share this post


Link to post
Share on other sites
I know it can be confusing. Here's the code that draws the rectangles but I don't know how to implement clicking with it here. It comes with comments also.

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>


using namespace std;


struct GLPoint
{
int x;
int y;
};


void init(void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glShadeModel (GL_SMOOTH);
}


void display(void) //this displays rectangles in a grid like form
{
int x = 0; int y = 0; //this sets the bottom left corner point of a rectangle
int x1, y1, x2, y2, x3, y3, x4, y4; //these are used to find the other corner points
x1 = x; y1 = y; //bottom left
x2 = x + 100; y2 = y; //bottom right
x3 = x + 100; y3 = y + 75; //top right
x4 = x; y4 = y + 75; //top left
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT, GL_LINE);
glBegin(GL_POLYGON);
glVertex2i(x1, y1 );
glVertex2i(x2, y2 );
glVertex2i(x3, y3 );
glVertex2i(x4, y4 );
glEnd();

while(y < 525) //this enables the the bottom left point to shift up
{
while(x <= 700) //this enables the bottom left to shift right
{
x += 100; //shifts the bottom left point to the right which switches the others
x1 = x; y1 = y;
x2 = x + 100; y2 = y;
x3 = x + 100; y3 = y + 75;
x4 = x; y4 = y + 75;
glColor3f(0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT, GL_LINE); //starts to draw each rectangle on screen
glBegin(GL_POLYGON);
glVertex2i(x1, y1 );
glVertex2i(x2, y2 );
glVertex2i(x3, y3 );
glVertex2i(x4, y4 );
glEnd();
}
x = 0; y += 75; //this shifts the x coordinate back to 0 and the y coordinate up
x1 = x; y1 = y;
x2 = x + 100; y2 = y;
x3 = x + 100; y3 = y + 75;
x4 = x; y4 = y + 75;
glColor3f(0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT, GL_LINE);
glBegin(GL_POLYGON);
glVertex2i(x1, y1 );
glVertex2i(x2, y2 );
glVertex2i(x3, y3 );
glVertex2i(x4, y4 );
glEnd();
}

while(x <= 700) //this enables the last row to be drawn correctly
{
x += 100; y = 525;
x1 = x; y1 = y;
x2 = x + 100; y2 = y;
x3 = x + 100; y3 = y + 75;
x4 = x; y4 = y + 75;
glColor3f(0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT, GL_LINE);
glBegin(GL_POLYGON);
glVertex2i(x1, y1 );
glVertex2i(x2, y2 );
glVertex2i(x3, y3 );
glVertex2i(x4, y4 );
glEnd();
}

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]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

Share this post


Link to post
Share on other sites
And here's the one I'm working on with comments. Hopefully it explains what I'm trying to do better.

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>

//I'm trying to make a nim game where rectangles appear on screen and the user can
//click and enter how many marbles they wanna take for their turn
//Right now I'm just trying to display the rectangles and then hopefully make them clickable afterwards.

using namespace std;

struct GLPoint
{
GLint x;
GLint y;
};

GLint totalmarbles = 64;
GLPoint midhold[64]; //used to hold all the midpoints
GLint vertices[512]; //used to hold all the vertices for an array


//Distance formula to find distance from midpoints of rectangle
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) //This is a function to take two points and find the midpoint
{
GLPoint midpt = (GLPoint){((A.x + B.x)/2, (A.y + B.y)/2)};
return midpt;
}

void storemids(int q, GLPoint A)
{
midhold[q] = A; //this puts the midpoint given into the array midpoint
}

//I'm not using the midpoints yet as I want to just draw the rectangles
//When I do use them, I hope that the distance formula can help me make the rectangles
//clickable

void vertice() //this finds all the vertices needed to make a rectangle
{
GLint total = 0;
GLint x = 0; GLint y = 0; //this would be the left bottom corner point of the rectangle
GLint x1, y1, x2, y2, x3, y3, x4, y4; //these are used to find the other points of the rectangle
x1 = x; y1 = y; //bottom left corner point
x2 = x + 100; y2 = y; //bottom right corner point
x3 = x + 100; y3 = y + 100; //top right corner point
x4 = x; y4 = y + 100; //top left corner point
/*GLPoint P = (GLPoint){(x1, y1)}; //originally i had these as point to put in the array but I didn't think arrayelements would use them
GLPoint Q = (GLPoint){(x2, y2)};
GLPoint R = (GLPoint){(x3, y3)};
GLPoint S = (GLPoint){(x4, y4)};*/
vertices[total] = x1; vertices[total+1] = y1; //this puts the points above in the array
vertices[total+2] = x2; vertices[total+3] = y2;//vertices[]
vertices[total+3] = x3; vertices[total+4] = y3;//this allows the array to hold all the
vertices[total+5] = x4; vertices[total+6] = y4;//points in order for drawelements()
total += 7;
while(y < 600) //this while loop enables to the the y to the shift the bottom left up
{ //so rectangles can be drawn in a column
while(x <= 700)
{
x += 100; //this while loop increments x to shift the bottom left corner point to
x1 = x; y1 = y; //the right to enable rectangles to be drawn in a row.
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; //puts the given points in the array
vertices[total+2] = x2; vertices[total+3] = y2;//again
vertices[total+3] = x3; vertices[total+4] = y3;
vertices[total+5] = x4; vertices[total+6] = y4;
total += 7;
}
x = 0; y += 100; //after the x reaches it's last point it shifts y and puts x = 0
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) //after the above draws, this enables the last row to be drawn
{
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;
} //should produce enough points for 64 rectangles to be on screen

}

void drawrectangles()
{
vertice(); //calls the vertices function to run and put the points in array
glEnableClientState(GL_VERTEX_ARRAY); //enables array client state to use
glVertexPointer(2, GL_INT, 0, vertices); //arrayelements()
glColor3f(0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT, GL_LINE);
glBegin(GL_QUADS);
for(int i = 0; i < totalmarbles; i++)
gl(vertices);
glEnd();
glDisableClientState(GL_VERTEX_ARRAY);
}

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, click);
hitbox = 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, 800);
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;
}



I haven't really looked at tutorials as I'm learning this stuff in a class at school so I might be doing things pretty dumb in y'all eyes :P but this is for a project so I can pretty much do whatever I want. I just want to make it work before December so I can relax :rolleyes:
I'll try DrawArrays though, it's just that I thought I had to label each of the vertices as indicies which was even more complicated then I was doing in my eyes, but if it works without having to do that then it's alright.

Share this post


Link to post
Share on other sites
Amazing what rest can do, I wasn't displaying anything because I didn't call the drawrectangles() unless I clicked the hitbox that I haven't even made up yet. So, I can see the screen. Just need to make some adjustments like the array should go up to 8 but I messed and put total +3 twice...

Share this post


Link to post
Share on other sites
The easiest way to make your code better is get rid of all those x1--x4 variables. Create:

Class Point
{
float x,y;
};

Class Rectangle
{
vector<Point> points;
};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!