Sign in to follow this  
thyrgle

Why wont this code draw a square?

Recommended Posts

thyrgle    116
#include <iostream>
#include <vector>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
#define X_SIZE 640
#define Y_SIZE 480

class 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.

Share this post


Link to post
Share on other sites
XTAL256    106
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 480

class 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 elsewhere
Square mySquare;

void draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
mySquare.draw();
glutSwapBuffers();
}

...

Share this post


Link to post
Share on other sites
thyrgle    116
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 480

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

Share this post


Link to post
Share on other sites
thyrgle    116
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.

Share this post


Link to post
Share on other sites
owiley    257
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.

Share this post


Link to post
Share on other sites
thyrgle    116
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this