PDA

View Full Version : My scene is utterly blank, what am I doing wrong?



indigo0086
04-04-2007, 12:24 PM
I copied a class disign from a book I'm using and I tried to recreate it but it isn't working. I even comparitavely looked at both the classes and don't know why the other one works while mine isn't rendering a thing. Did I set up the projection wrong?


#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.2f, 0.2f, 0.2f, 1.0f};

GLfloat redLightPos[] = {3.0, 2.0, 0.0, 1.0};

GLfloat redLightCol[] = {1.0f, 0.0f, 0.0f, 1.0f};

//albient color for the scene, bluish Green
GLfloat globalAmbience[] = {0.0, 0.2, 0.3, 1.0};
//================================================== =======================

CGfxOpenGL::CGfxOpenGL()
{
flashLightOn = GL_TRUE;
}

CGfxOpenGL::~CGfxOpenGL()
{
}

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, 20.0);

glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT1, GL_POSITION, redLightPos);
glLightfv(GL_LIGHT1, GL_AMBIENT, redLightCol);
glLightfv(GL_LIGHT1, GL_DIFFUSE, redLightCol);
glLightfv(GL_LIGHT1, GL_SPECULAR, redLightCol);

glEnable(GL_COLOR_MATERIAL);


return true;
}

void CGfxOpenGL::setupProjection(int width, int height)
{
//prevent division by 0
if(height = 0)
height = 1;


glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(60.0, (GLfloat)width/(GLfloat)height, 1.0, 1000.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

windowWidth = width;
windowHeight = height;
}

void CGfxOpenGL::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

glLoadIdentity();
gluLookAt(0.0, 0.0, 20.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);

if(flashLightOn)
glEnable(GL_LIGHT0);
else
glDisable(GL_LIGHT0);

GLfloat cubeColor[] = {0.6, 0.7, 1.0, 1.0};
GLfloat cubeSpecular[] = { 1.0, 1.0, 1.0, 1.0};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, cubeColor);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, cubeSpecular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 15.0);

glPushMatrix();
DrawCube(9, 256);
glPopMatrix();

}

bool CGfxOpenGL::shutDown()
{
return true;
}

void CGfxOpenGL::prepare()
{

}

plasmonster
04-05-2007, 02:31 AM
I see the "setupProjection" function, but no indication that it's being called. Instead there's a lookat call in your render method, which I assume is on the modelview stack (not quite sure what's going on there).

Light positions are transformed by the current modelview matrix, so you may be seeing some trouble stemming from your setup in that area.

There are some good cut and paste examples/snippets that work here:
http://fly.cc.fer.hr/~unreal/theredbook/chapter06.html

It's been a while since I used the fixed function lighting stuff, so maybe I missed something else.

indigo0086
04-05-2007, 04:08 AM
Well that is just the class, All the proper calls are in the main method. I've used a similar class with the same main and it works, but something about this is incomplete. I'll check that.