-1

I am trying to draw a solid cube in open GL to serve as a background of an interface button. The problem is, it is not being drawn on the screen at all. I have a class OGWindow that handles the drawing and a main class/method. I invoke all of the necessary methods from OGWindow in the main class. What am I doing wrong here?

This is my main class:

OGWindow    theWindow;

int main(int argc, char **argv) {

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);   
glutInitWindowSize( 1000, 600 );
glutInitWindowPosition(0, 0);
glutCreateWindow("OpenGL Demo");
glEnable(GL_DEPTH_TEST);    // enable the depth buffer test

glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
glutMouseFunc(mouseClick);
glutMotionFunc(mouseMotion);
glutPassiveMotionFunc(mousePassiveMotion);
glutIdleFunc(Idle);
theWindow.initGL(); 

glutMainLoop();
}

void DrawGLScene(void) {
   theWindow.myDrawGLScene();
}

This is my OGWindow class:

void OGWindow::initGL(void) {


glClearColor(1.0, 1.0, 1.0, 1.0);

squareX = 1.0;
squareY = 1.0;

squareWidth = 40.0;
squareHeight = 40.0;
squareColour = RED;

squareDraggingP = false;
}


void OGWindow::MyReSizeGLScene(int fwidth, int fheight) 
{
// Store window size  in class variables so it can be accessed in myDrawGLScene() if necessary
wWidth = fwidth;
wHeight = fheight;

// Calculate aspect ration of the OpenGL window
aspect_ratio = (float) fwidth / fheight;

// Set camera so it can see a square area of space running from 0 to 10 
// in both X and Y directions, plus a bit of space around it.
Ymin = 0;
Xmin = 0;

Ymax = 600;
// Choose Xmax so that the aspect ration of the projection
// = the aspect ratio of the viewport
//Xmax = (aspect_ratio * (Ymax -Ymin)) + Xmin;
Xmax = 1000;

glMatrixMode(GL_PROJECTION);        // Select The Projection Stack
glLoadIdentity();
glOrtho(Xmin, Xmax, Ymin, Ymax, -1.0, 1.0);

glViewport(0, 0, wWidth, wHeight);      // Viewport fills the window
}


void OGWindow::myDrawGLScene(GLvoid)        // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the drawing area

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

OGWindow::myDrawModel();
glColor3f(0.0, 0.0, 0.0);
drawToolbar();
drawCreateButton();

glutSwapBuffers(); // Needed if we're running an animation
glFlush();
}

And this is the method that is suppose to draw a solid cube in the scene:

void OGWindow::drawCreateButton(GLvoid){
glPushMatrix();
glColor3f(0.0, 1.0, 0.0);
    glTranslatef(10.0,30.0,0.0);
    glutSolidCube(4);
glPopMatrix();
 }
Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126
  • 2
    You don't need `glutSwapBuffers` AND `glFlush`. Swap buffers do implicitly call flush anyway. Use swap buffers when using double buffers and flush when using a single buffer (which you shouldn't be), so remove `glFlush`. [See this question](http://stackoverflow.com/questions/2143240/opengl-glflush-vs-glfinish) Also, your cube is very small (size 4 on a window with dimensions 1000,600), so perhaps if you increase its size or scale it you'll see something. – aslg Mar 06 '16 at 19:28
  • Thank you for your suggestions, appreciate it. I did try to scale it up, yes without any luck of seeing something.. :/ – Georgi Koemdzhiev Mar 06 '16 at 21:26
  • 1
    It may be something else you're doing in `OGWindow::myDrawModel()` or in `drawToolbar()`. – aslg Mar 06 '16 at 21:35
  • I dont know why but I could not get anything drawn using the "glutSoildCube" method. Instead, I draw the rectangle/cube (I will be seen only front the front, thus the cube will look as a rectangle) with the following:glBegin(GL_QUADS); ...4 verteces.. glEnd(); Any idea why this worked and the other method not? :) – Georgi Koemdzhiev Mar 06 '16 at 21:38
  • Did you still get nothing if you resize your window? – Amadeus Mar 07 '16 at 03:13
  • 2
    It's most probably due to your depth setup here: glOrtho(Xmin, Xmax, Ymin, Ymax, -1.0, 1.0); Try changing the last two numbers to -100.0, 100.0, for example. :) – Dimo Markov Mar 07 '16 at 10:55
  • That's a good point, will play about with those :) – Georgi Koemdzhiev Mar 07 '16 at 10:57
  • I suspect that your cube is too big to see the front and back faces. They get cut by the near and far depth planes. – Dimo Markov Mar 07 '16 at 10:57

1 Answers1

0

Sorry if i'm not answering your question, but it seems you're not using modern OpenGL, functions like glMatrixMode(GL_PROJECTION); and a lot more in your code should be avoided in modern applications.

Instead of glVertex() and glColor() you should use a VBO and a shader.

Here's a modern OpenGL tutorial wich is easy to follow: OpenGL tutorial for beginners

Matth
  • 154
  • 7
  • Thank you for your suggestions. However, this is the version of OpenGL that I am studying at my university and the teacher specifically ask to use it. – Georgi Koemdzhiev Mar 06 '16 at 21:25
  • 1
    Sadly nowadays fixed-pipeline OpenGL is still taught in universities. I can't help so much because I used it long time ago and then I switched to the modern pipeline. Nice Studying! – Matth Mar 06 '16 at 21:27
  • 1
    Sorry if Im being pedantic here, but this is not an answer, but a good advice, that could be put in comments section. – Amadeus Mar 07 '16 at 03:14