PDA

View Full Version : No Drawing & No Error Messages in My OpenGL Programming



ScottChang
01-25-2004, 08:59 AM
Hi All,
I tried to use the attached OpenGL code to draw a Star-shape figure on my Microsoft Visual C++ .NET 2002-Windows XP Pro PC. I did 'Build'in Debug Mode successfully. I clicked the '! Start without Debug' and it did not generate the Star-shape figure on my console and did not have any error messges. Please help and tell me what it is wrong with my coding in this program and how to correct the problem.
Thanks in advance,
Scott Chang
====================================
// ---StarOGLkeys.cpp---
// ---Rotating Star by stricking keys---

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include <gl/glut.h>

// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;

// Called to draw scene
void RenderScene(void)
{
// Clear the window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
}
//*******Add Star Data here********
//******Here's Where We Do All The Drawing
SetupRC()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glBegin(GL_LINE_LOOP); // Drawing a star by using 10 vertices for the outer rims of a star
glVertex3f (0, 0.5, 0); //Specify the next 10 vertices
glVertex3f (-0.2, 0.2, 0); //Specify the next 10 vertices
glVertex3f (-0.5, 0.1, 0);
glVertex3f (-0.2, -0.1, 0);
glVertex3f (-0.3, -0.5, 0);
glVertex3f (0, -0.2, 0);
glVertex3f (0.3, -0.5, 0);
glVertex3f (0.2, -0.1, 0);
glVertex3f (0.5, 0.1, 0);
glVertex3f (0.2, 0.2, 0);
glVertex3f (0, 0.5, 0);
glEnd(); // Finished drawing the star shape
glColor3f (0.0, 1.0, 0.0); //Set the color to green
glBegin(GL_LINES); //Draw 10 lines radiating from (0,0,0) to each vertice
glVertex3f (0, 0.5, 0); //1st vertix to connect the origin to form a line
glVertex3f (0, 0, 0);
glVertex3f (-0.2, 0.2, 0); //2nd vertix
glVertex3f (0, 0, 0);
glVertex3f (-0.5, 0.1, 0);
glVertex3f (0, 0, 0);
glVertex3f (-0.2, -0.1, 0);
glVertex3f (0, 0, 0);
glVertex3f (-0.3, -0.5, 0);
glVertex3f (0, 0, 0);
glVertex3f (0, -0.2, 0);
glVertex3f (0, 0, 0);
glVertex3f (0.3, -0.5, 0);
glVertex3f (0, 0, 0);
glVertex3f (0.2, -0.1, 0);
glVertex3f (0, 0, 0);
glVertex3f (0.5, 0.1, 0);
glVertex3f (0, 0, 0);
glVertex3f (0.2, 0.2, 0);
glVertex3f (0, 0, 0);
glColor3f (0.5, 0.5, 1.0); //Set the color to blue
glVertex3f (0, 0.5, 0);// last vertix
glVertex3f (0, 0, 0);
glEnd(); // Finished drawing the last line

glColor3f (1.0, 0.0, 0.0); //Set the color to red
glBegin(GL_TRIANGLES);
glVertex3f (0, 0, 0);
glVertex3f (0.5, 0.1, 0);
glVertex3f (0.2, 0.2, 0);
glEnd();

//return 0;
}

// Restore transformations
// glPopMatrix();

// Flush drawing commands
// glutSwapBuffers();

//***Stricking Keys here****
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;

if(key == GLUT_KEY_DOWN)
xRot += 5.0f;

if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;

if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;

if(key > 356.0f)
xRot = 0.0f;

if(key < -1.0f)
xRot = 355.0f;

if(key > 356.0f)
yRot = 0.0f;

if(key < -1.0f)
yRot = 355.0f;

// Refresh the Window
glutPostRedisplay();
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("StarOGLkeys-GLUT Shapes");
SetupRC();
return 0;
}

Bob
01-25-2004, 09:13 AM
You don't have any callbacks (display and reshape callbacks are required), nor do you enter the main loop in GLUT.

ScottChang
01-28-2004, 08:57 PM
Hi Bob, Thanks for your response.
As you instructed, I added the disply and reshape callbacks and the main loop in my "int main(int argc.
, char* argv[])" section:
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

// Window position (from top corner), and size (width and hieght)
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 360, 360 );

glutCreateWindow("StarOGLkeys-GLUT Shapes");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
==================================
I executed the program and got the Window without the Star-shape figure!!!??? I need your help again. Please tell me what is still not right and how to get the Star-shape figure drawn on the Window and use the keys to rotate the figure.
Thanks in advance,
Scott Chang

Relic
01-28-2004, 11:20 PM
Where's your projection matrix setup?! The default is an ortho unit cube. Translating an object by glTranslatef(-1.5f,0.0f,-6.0f); falls out of the cube to the left AND the far side.

ScottChang
01-30-2004, 10:14 PM
Thanks, Relic.

I deleted the "glTranslatef(-1.5f,0.0f,-6.0f);" and added the following projection matrix setup(an ortho unit cube) before the main section:

void ChangeSize(int w, int h)
{
GLfloat lightPos[] = { -50.f, 50.0f, 100.0f, 1.0f };
GLfloat nRange = 1.9f;

// Prevent a divide by zero
if(h == 0)
h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);

// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
}

int main(int argc, char* argv[]){
...........

And I executed the program and it still showed the Window without the Star-shape figure. Please help me again and tell me what is still wrong or missing in my coding and how to cure this problem.

Thanks in advance,
Scott Chang

Bob
01-31-2004, 12:46 AM
Maybe you should take a closer look at the display callback. Are you actually drawing anything? The RenderScene function is pretty much empty.