Having a problem with glut rendering from an object.
Basically I revised my experiment programs to use a class, and rather than use a specific system api I'm using GLUT. The thing is when I converted all the rendering and such to an object I must have missed something and now my scene doesn't show anything. It captures the keyboard properly so I know it's in the proper loop, but other than that it's just a black screen. Here's my main source, and the class header and source
Code:
//main.cpp
#define GLUT_NO_LIB_PRAGMA
#define GLUT_NO_WARNING_DISABLE
#include <GL/glut.h>
#include "cgfxopengl.h"
//=========================================================================
int width = 800, height = 600;
CGfxOpenGL* glRender = new CGfxOpenGL;
//=========================================================================
void renderScene(void)
{
glRender->prepare();
glRender->render();
glutSwapBuffers();
}
void preserveRatio(int width, int height)
{ glRender->setupProjection(width, height); }
void specialKeys(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_F1:
glRender->toggleFlashLight();
break;
default:
break;
}
}
void normalKeys(unsigned char key, int x, int y)
{
if(key == 27)
exit(0);
}
//main
int main(int argc, char **argv)
{
//Initializes and Creates Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(width , height);
glutCreateWindow("My GLUT Window");
//displayfunctions
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(preserveRatio);
//keyboard function
glutSpecialFunc(specialKeys);
glutKeyboardFunc(normalKeys);
//rendering object
glRender->intialize();
glutMainLoop();
return 0;
}
Code:
//cgfxopengl.h
#ifndef CGFXOPENGL_H
#define CGFXOPENGL_H
#include "includes.h"
class CGfxOpenGL
{
public:
CGfxOpenGL() : flashLightOn(true) {}
virtual ~CGfxOpenGL() {}
//sets up all variables and necessary OpenGL functions
bool intialize();
//projection function
void setupProjection(int width, int height);
//cleans up all OpenGL Internal states
bool shutDown();
//prepares the scene for rendering (if animation needed)
void prepare();
//renders the scene
void render();
//special functions
void toggleFlashLight() { flashLightOn = !flashLightOn; }
private:
int windowWidth;
int windowHeight;
bool flashLightOn;
};
#endif // CGFXOPENGL_H
Code:
//cgfxopengl.cpp
#include "cgfxopengl.h"
#include "shapes.h"
//=========================================================================
GLfloat lightAngle;
GLfloat flashLightPos[] = {0.0, 0.0, 0.0, 1.0};
GLfloat flashLightDir[] = {0.0, 0.0, -1.0};
GLfloat flashLightCol[] = {0.2, 0.2, 0.2, 1.0};
GLfloat bigLightPos[] = {0.0, 1.0, 1.0, 0.0};
GLfloat bigLightCol[] = {0.3, 0.3, 0.3, 1.0};
//albient color for the scene, bluish Green
GLfloat globalAmbience[] = {0.0, 0.2, 0.3, 1.0};
//=========================================================================
bool CGfxOpenGL::intialize()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbience);
//set up the flashLight
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, flashLightPos);
glLightfv(GL_LIGHT0, GL_AMBIENT, flashLightCol);
glLightfv(GL_LIGHT0, GL_DIFFUSE, flashLightCol);
glLightfv(GL_LIGHT0, GL_SPECULAR, flashLightCol);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, flashLightDir);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 15.0);
//set up the large light
return true;
}
void CGfxOpenGL::setupProjection(int width, int height)
{
//prevent division by 0
if(height = 0)
height = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, width, height);
gluPerspective(60.0, (GLfloat)width/(GLfloat)height, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, -1.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
windowWidth = width;
windowHeight = height;
}
void CGfxOpenGL::render()
{
glPushMatrix();
GLfloat coneColor[] = {0.5, 0.8, 1.0, 1.0};
GLfloat coneSpecular[] = { 1.0, 1.0, 1.0, 1.0};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, coneColor);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, coneSpecular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 15.0);
glTranslatef(0, 0, 12);
DrawCube(2, 12);
glPopMatrix();
}
bool CGfxOpenGL::shutDown()
{
return true;
}
void CGfxOpenGL::prepare()
{
if(flashLightOn)
glEnable(GL_LIGHT0);
else
glDisable(GL_LIGHT0);
}
At first I thought it was the position of the camera, but I changed it and relocated the cube (which DrawCube is defined elsewhere and works properly), but it doesn't show it. It worked find normally, but after I put it in a class it just stopped.