openGL - No output. Please help!

Started by
6 comments, last by sergzone 16 years, 2 months ago
Hey guys. I'm having really crappy time trying to figure out why nothing is displaying in my openGL window. I'm simply trying to draw a cylinder but get a blank screen. My cylinder has to be 300 in diameter. I tried using glOrtho() and glPerspective() but everything fails. To be honest, i don't know what the hell i'm doing. I've got the drawCylinder function from somewhere else and it appears to work using small units. When i change the units to 300 and attempt to change my viewing volume nothing gets drawn. Could someone please help me out on this? I've been looking at examples and reading tutorials for the past 5 days and still stuck on the same darn thing. Here is the code below. I couldn't find tags so i just did copy/paste. Appreciate any response on this. #include <gl/glut.h> #include <iostream> #include <math.h> void mDisplay(); void mReshape(int prmWidth, int prmHeight); void mInit(); void drawCylinder(GLfloat arc, GLfloat radius, GLfloat height, GLint slices, GLint stacks); void mKey(unsigned char prmKey, int prmX, int prmY); int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(600, 600); glutCreateWindow("Clock"); mInit(); glutDisplayFunc(mDisplay); glutReshapeFunc(mReshape); glutKeyboardFunc(mKey); glEnable(GL_DEPTH_TEST); glutMainLoop(); return 0; } void mDisplay() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glFlush(); glutSwapBuffers(); } void mReshape(int prmWidth, int prmHeight) { glViewport(0, 0, prmWidth, prmWidth); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (prmWidth < prmHeight) { // Set Ortho } else { // Set Ortho } glMatrixMode(GL_MODELVIEW); mDisplay(); } void mInit() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-300, 300, -300, 300, 300, -300); drawCylinder(360.0,300.0,3.0,40,40); glMatrixMode(GL_MODELVIEW); } void mKey(unsigned char prmKey, int prmX, int prmY) { if (prmKey == 'q') exit(0); } void drawCylinder(GLfloat arc, GLfloat radius, GLfloat height, GLint slices, GLint stacks) { GLint i,j; // loop variables GLfloat currentAngle,currentHeight; GLfloat stackHeight=height/stacks; // to reduce computing in loops const GLfloat pi180=0.017453293; // pi/180 for (j=0;j<stacks;j++) // loop for stacks { currentHeight=height*j/stacks; glBegin(GL_QUAD_STRIP); for (i=0;i<=slices;i++) // loop for slices { currentAngle=arc*i/slices * pi180; glNormal3f(cos(currentAngle), sin(currentAngle), 0); glVertex3d(cos(currentAngle)*radius, sin(currentAngle)*radius,currentHeight); glVertex3d(cos(currentAngle)*radius, sin(currentAngle)*radius,currentHeight+stackHeight ); } glEnd(); } }
Advertisement
You should add a call to glOrtho() in mReshape().
At this point i'm not really worried about reshape method. I'm not really changing window size while testing or anything like that. In fact, i removed it from my main :)
I think it's called when the application starts, so if you register it with glutReshapeFunc(), it must have a proper implementation (but if you don't, I assume there's a default function).
The tags are 'source', not 'code' on this forum :)

First, you're not drawing anything in your display loop, so nothing gets really displayed, call your drawing routine as:

void mDisplay(){  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  glLoadIdentity();  drawCylinder(360.0,300.0,100.0,40,40);   glFlush();  glutSwapBuffers();}


Second, only the body of the cylinder is rendered (not top and bottom caps), along the z axis, so basically it's invisible for your position. You may want to rotate it to see it:

  glRotatef(90,0,1,0); // rotate by 90 degrees around Y axis  drawCylinder(360.0,300.0,100.0,40,40);

I did notice that my draw function was in my init method. I tried rotation the object but i'm still getting a blank screen. I tried transposing on the z axis and get the same result - blank screen. Here is a cleaned up version of the above code i posted previously.

#include <gl/glut.h>#include <iostream>#include <math.h>void mDisplay();//void mReshape(int prmWidth, int prmHeight);void mInit();void drawCylinder(GLfloat arc, GLfloat radius, GLfloat height, GLint slices, GLint stacks);void mKey(unsigned char prmKey, int prmX, int prmY);int main(int argc, char **argv){    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);    glutInitWindowSize(700, 700);    glutCreateWindow("Test");    mInit();    glutDisplayFunc(mDisplay);    glutKeyboardFunc(mKey);    glutMainLoop();    return 0;}void mDisplay(){    glClearColor(0,0,0,0);    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glLoadIdentity();    glRotatef(90,0,1,0);    drawCylinder(360.0,300.0,10.0,40,40);    glFlush();    glutSwapBuffers();}//void mReshape(int prmWidth, int prmHeight)//{//    glViewport(0, 0, prmWidth, prmWidth);//    glMatrixMode(GL_PROJECTION);//    glLoadIdentity();//    if (prmWidth < prmHeight)//    {//        // Set Ortho//    }//    else//    {//        // Set Ortho//    }////    glMatrixMode(GL_MODELVIEW);//    mDisplay();//}void mInit(){    glEnable(GL_DEPTH_TEST);    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    glMatrixMode(GL_MODELVIEW);}void mKey(unsigned char prmKey, int prmX, int prmY){    if (prmKey == 'q') exit(0);}void drawCylinder(GLfloat arc, GLfloat radius, GLfloat height, GLint slices, GLint stacks){    GLint i,j; // loop variables    GLfloat currentAngle,currentHeight;    GLfloat stackHeight=height/stacks; // to reduce computing in loops    const GLfloat pi180=0.017453293; // pi/180    for (j=0;j<stacks;j++) // loop for stacks    {        currentHeight=height*j/stacks;        glBegin(GL_QUAD_STRIP);        for (i=0;i<=slices;i++) // loop for slices        {            currentAngle=arc*i/slices * pi180;            glNormal3f(cos(currentAngle), sin(currentAngle), 0);            glVertex3d(cos(currentAngle)*radius,            sin(currentAngle)*radius,currentHeight);            glVertex3d(cos(currentAngle)*radius,            sin(currentAngle)*radius,currentHeight+stackHeight );        }        glEnd();    }}
You forgot to set the projection(glOrtho or gluPerspective) in this version - when identity (default) projection matrix is used the screen is actually (-1.0..1.0) in width (and height), so when you'll draw cylinder that is 300.0 units in diameter you'll see nothing again.


yes!!
It's showing really weird. But at least i have an output.
Thank you all!

This topic is closed to new replies.

Advertisement