PDA

View Full Version : Current color not getting used? [solved]



Fejimush
06-03-2008, 02:46 PM
I am working my way through the OpenGL superbible (chapter 4 currently) and can't seem to figure out why the earth and moon are not getting colored. The sun shows up as yellow but the earth and moon are both black.

I substituted glutSolidSphere() with gluSphere(). It probably has something to do with this.

prepare_opengl() is called once.
resize() is called after prepare_opengl() and whenever a resize event occurs.
After that draw_solar is called repeatedly.

Any help would be greatly appreciated.

model.cpp
---------
model::model() :
PI(3.1415926535897),
m_sphere_quad(NULL)
{
boost::array<GLfloat,4> white_light = { 0.2f, 0.2f, 0.2f, 1.0f };
boost::array<GLfloat,4> source_light = { 0.8f, 0.8f, 0.8f, 1.0f };
boost::array<GLfloat,4> light_pos = { 0.0f, 0.0f, 0.0f, 1.0f };
m_white_light = white_light;
m_source_light = source_light;
m_light_pos = light_pos;
}

void model::prepare_opengl()
{
cout << format("model::prepare_opengl\n");

// red, green, blue, alpha (RGBA), (0,0,0,1 = black) (1,1,1,1 = white)

// Light values and coordinates
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glEnable(GL_CULL_FACE); // Do not calculate inside of jet

// Enable lighting
glEnable(GL_LIGHTING);

// Setup and enable light 0
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, m_white_light.c_array());
glLightfv(GL_LIGHT0,GL_DIFFUSE, m_source_light.c_array());
glLightfv(GL_LIGHT0,GL_POSITION, m_light_pos.c_array());
glEnable(GL_LIGHT0);

// Enable color tracking
glEnable(GL_COLOR_MATERIAL);

// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

// Background - clearing color
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

void model::resize(float width, float height)
{
cout << format("model::resize width=%f, height=%f\n") % width % height;

GLfloat fAspect;

// Prevent a divide by zero
if (height == 0)
{
height = 1;
}

// Set Viewport to window dimensions
glViewport(0, 0, width, height);

// Calculate aspect ratio of the window
fAspect = (GLfloat)width/(GLfloat)height;

// Set the perspective coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// field of view of 45 degrees, near and far planes 1.0 and 425
gluPerspective(45.0f, fAspect, 1.0, 425.0);

// Modelview matrix reset
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void model::draw_solar()
{
// Earth and Moon angle of revolution
static float fMoonRot = 0.0f;
static float fEarthRot = 0.0f;

m_sphere_quad = gluNewQuadric();

// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Save the matrix state and do the rotations
glMatrixMode(GL_MODELVIEW);
glPushMatrix();

// Translate the whole scene out and into view
glTranslatef(0.0f, 0.0f, -300.0f);

// Set material color, Red
// Sun
glDisable(GL_LIGHTING);
glColor3ub(255, 255, 0);
gluSphere(m_sphere_quad, 15.0f, 30, 17);
glEnable(GL_LIGHTING);

// Move the light after we draw the sun!
glLightfv(GL_LIGHT0,GL_POSITION, m_light_pos.c_array());

// Rotate coordinate system
glRotatef(fEarthRot, 0.0f, 1.0f, 0.0f);

// Draw the Earth
glTranslatef(105.0f,0.0f,0.0f);
m_sphere_quad = gluNewQuadric();
glColor3ub(0,0,255);
gluSphere(m_sphere_quad, 15.0f, 30, 17);

// Rotate from Earth based coordinates and draw Moon
glColor3ub(200,200,200);
glRotatef(fMoonRot,0.0f, 1.0f, 0.0f);
glTranslatef(30.0f, 0.0f, 0.0f);

fMoonRot+= 15.0f;
if (fMoonRot > 360.0f)
{
fMoonRot = 0.0f;
}

gluSphere(m_sphere_quad, 6.0f, 30, 17);

gluDeleteQuadric(m_sphere_quad);

// Restore the matrix state
glPopMatrix(); // Modelview matrix

// Step earth orbit 5 degrees
fEarthRot += 5.0f;
if (fEarthRot > 360.0f)
{
fEarthRot = 0.0f;
}

SDL_Delay(100);
}

Fejimush
06-03-2008, 03:19 PM
On initialization if prepare_opengl() is called after the first resize() call it works fine.

I must be doing something in the resize() function which is nixing the colors.(?)

On a resize event. The earth and moon become black again.

-graham

Fejimush
06-03-2008, 03:46 PM
I was not re-initializing (prepare_opengl) opengl prior to calling resize().

Issue fixed.

-graham