Why wont this code draw a square?

Started by
7 comments, last by empirical2 13 years, 6 months ago
#include <iostream>#include <vector>#include <GLUT/GLUT.h>#include <OpenGL/OpenGL.h>#define X_SIZE 640#define Y_SIZE 480class Square {private:public:	Square() {		GLdouble square[] = {			-1.0,  1.0, -1.0,			 1.0,  1.0, -1.0,			 1.0  -1.0, -1.0,			-1.0,  1.0, -1.0		};			glEnableClientState(GL_VERTEX_ARRAY);					glVertexPointer(3, GL_DOUBLE, 0, square);			glDrawArrays(GL_QUADS, 0, 12);					glDisableClientState(GL_VERTEX_ARRAY);	}};void draw() {	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glEnable(GL_DEPTH_TEST);	Square mySquare;	glutSwapBuffers();}int main (int argc, char **argv) {	glutInit(&argc, argv);	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);	glutInitWindowSize(X_SIZE, Y_SIZE);	glutCreateWindow("Lol");	glutDisplayFunc(draw);	glutMainLoop();    return 0;}


I don't get why this doesn't display a square. I am first initializing glut in int main then, I am drawing the stuff in void draw where I make my object mySquare which by initialization should draw a square. But, I get this weird shaped polygon instead.

Thanks in advance.
Advertisement
I do not know why the square will not draw. But you shouldn't be doing the drawing in the object initialisation (constructor). That's not how it's meant to be used. You should have a draw() function in your Square class:
#include <iostream>#include <vector>#include <GLUT/GLUT.h>#include <OpenGL/OpenGL.h>#define X_SIZE 640#define Y_SIZE 480class Square {public:    Square() {}    void draw() {        GLdouble square[] = {            -1.0,  1.0, -1.0,             1.0,  1.0, -1.0,             1.0  -1.0, -1.0,            -1.0,  1.0, -1.0        };        glEnableClientState(GL_VERTEX_ARRAY);        glVertexPointer(3, GL_DOUBLE, 0, square);        glDrawArrays(GL_QUADS, 0, 12);        glDisableClientState(GL_VERTEX_ARRAY);    }};// Define your object here (globally) or elsewhereSquare mySquare;void draw() {    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glEnable(GL_DEPTH_TEST);    mySquare.draw();    glutSwapBuffers();}...
[Window Detective] - Windows UI spy utility for programmers
Your 4 vertices do not make a square. Double check your coordinates (where's (-1,-1)?)
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Thanks for you help but, it still doesn't work here is my updated code:

[source lang="cpp]#include <iostream>#include <vector>#include <GLUT/GLUT.h>#include <OpenGL/OpenGL.h>#define X_SIZE 640#define Y_SIZE 480class Square {private:public:	Square() {	}	void draw() {        GLdouble square[] = {            -1.0,  1.0, -1.0,			1.0,  1.0, -1.0,			1.0  -1.0, -1.0,            -1.0,  -1.0, -1.0        };        glEnableClientState(GL_VERTEX_ARRAY);        glVertexPointer(3, GL_DOUBLE, 0, square);        glDrawArrays(GL_QUADS, 0, 12);        glDisableClientState(GL_VERTEX_ARRAY);    }	};void draw() {	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glEnable(GL_DEPTH_TEST);	Square mySquare;	mySquare.draw();	glutSwapBuffers();}int main (int argc, char **argv) {	glutInit(&argc, argv);	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);	glutInitWindowSize(X_SIZE, Y_SIZE);	glutCreateWindow("Lol");	glutDisplayFunc(draw);	glutMainLoop();    return 0;}
At least part of the problem is that your vertices are wound clockwise, and OpenGL defaults to counter-clockwise vertex winding. Either order your vertices counter-clockwise, or specify glFrontFace(GL_CW) in your initialisation.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
At least part of the problem is that your vertices are wound clockwise, and OpenGL defaults to counter-clockwise vertex winding. Either order your vertices counter-clockwise, or specify glFrontFace(GL_CW) in your initialisation.


Thanks, I have added glFrontFace(GL_CW) before I call mySquare.draw() but, I am still not getting a square. I'm so lost on what to do.
also depending on ur camera settings you might not see the cube that is there. Its because you are either to zoomed in too far or zoomed out too far.
Bring more Pain
Quote:Original post by owiley
also depending on ur camera settings you might not see the cube that is there. Its because you are either to zoomed in too far or zoomed out too far.


I can see the square if that's what you are referring too.
glDrawArrays(GL_QUADS, 0, 12);

Should be 4.

This topic is closed to new replies.

Advertisement