PDA

View Full Version : glColor3f not working



TimOgunmakin
04-11-2013, 09:46 AM
I am having problems with my glColor3f not working :S Whenever I draw glutSolidSphere, it fails to colour it in the specific colour and just colours it in black. However other objects colour fine. It was working before but has stopped working for some reason

This is occuring within the draw mehod within my planet class:


void Planet::draw()
{

glPushMatrix();

glColor3f(r,g,b);
glClearColor(1.0, 0.0, 1.0, 1.0); //remove this when planet color3f bug is fixed.
glTranslated(loop[(int)(ceil (speed))].x, loop[(int)(ceil (speed))].y, loop[(int)(ceil (speed))].z);
glRotated(60, 0, 0, 1);
glScalef(scale, scale, scale);
glutSolidSphere(3.0, 20, 20);
glPopMatrix();

}

here is where the method is being called:


void display() {



glClear(GL_COLOR_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // reset the matrix
gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z,
cam.lookAt.x, cam.lookAt.y, cam.lookAt.z,
cam.up.x, cam.up.y, cam.up.z);

drawAxes();


//Draw Player
player.draw();

// Draw planets
for (int i = 0; i < planetNum; i++)
{
//This will draw planets and Planet HUD (shows planet name on their position)
planet[i]->draw();
glColor3f(255.0, 255.0, 255.0);
stringstream ss;
ss << planet[i]->getname() << endl;
outputText(planet[i]->getPlanetPos().x, planet[i]->getPlanetPos().y, planet[i]->getPlanetPos().z, ss.str());

}

glFlush();

}

mobeen
04-11-2013, 09:55 AM
What values do you pass to r, g, b? May be you are not passing them correctly?
have you tried passing a constant value, for example just modify this line



glColor3f(r,g,b);
to this



glColor3f(1,0,0);
and see if all planets turn red.

TimOgunmakin
04-11-2013, 10:19 AM
I had already tried this, no luck </3

Aleksandar
04-11-2013, 01:38 PM
There are a lot of meaningless statements in your code. Please consider reading chapter 2 from 7th revision of the Red Book, or some similar article about fundamentals of legacy OpenGL.Maybe answering to the following questing will help us to find where the problem is:1. What are the values of r,g and b variables?2. glClearColor() has no effect in your code. Do you know what's the purpose of that command?
glClearColor(1.0, 0.0, 1.0, 1.0); //remove this when planet color3f bug is fixed.3. What color you think is set with glColor3f(255.0, 255.0, 255.0)? This is not a valid color. In fact, it is, but after clamping. The valid range for glColor*f is [0..1].4. Is there some missing code that you have deleted from the post as "not important"?

TimOgunmakin
04-11-2013, 02:19 PM
There are a lot of meaningless statements in your code. Please consider reading chapter 2 from 7th revision of the Red Book, or some similar article about fundamentals of legacy OpenGL.Maybe answering to the following questing will help us to find where the problem is:1. What are the values of r,g and b variables?2. glClearColor() has no effect in your code. Do you know what's the purpose of that command?3. What color you think is set with glColor3f(255.0, 255.0, 255.0)? This is not a valid color. In fact, it is, but after clamping. The valid range for glColor*f is [0..1].4. Is there some missing code that you have deleted from the post as "not important"?


I was using glClearColor to set the background pink so I am able to view my planets. Without it, my background is black and I can't see my planets (as they are black also). It was just there temporarily so I could view my planets while this problem exists.

R, G and B are unique values for each planet (there are 5 of them). The problem will not be that as I replaced them with "255,0,0" which should give me the colour red for every planet. This did not happen and they still stayed black.

glClearColor() does infact have an affect within my code as commenting it out caused my background black.

glColor3f(255.0, 255.0, 255.0) should give white.

I have not deleted anything from the post, just given both relevant methods.

Aleksandar
04-11-2013, 02:53 PM
I was using glClearColor to set the background pink so I am able to view my planets. Without it, my background is black and I can't see my planets (as they are black also). It was just there temporarily so I could view my planets while this problem exists.glClearColor() affects next glClear(). This should be done once per frame, or even better in some PrepareScene() function (once per application execution).
R, G and B are unique values for each planet (there are 5 of them). The problem will not be that as I replaced them with "255,0,0" which should give me the colour red for every planet. This did not happen and they still stayed black.Nope! glColor3f(1.0, 0.0, 0.0) is red! use glColor3ub if you want [0..255] segment.
glClearColor() does infact have an affect within my code as commenting it out caused my background black. Move it before glClear(). In the first frame it does nothing, but remain in the subsequent frames hence the effect.
glColor3f(255.0, 255.0, 255.0) should give white.Nope! glColor3f(1.0, 1.0, 1.0) is white.

TimOgunmakin
04-11-2013, 04:02 PM
Updated, same result.

draw method:

void Planet::draw()
{

glPushMatrix();
glColor3f(1.0,0.0,0.0);
glTranslated(loop[(int)(ceil (speed))].x, loop[(int)(ceil (speed))].y, loop[(int)(ceil (speed))].z);
glRotated(60, 0, 0, 1);
glScalef(scale, scale, scale);
glutSolidSphere(3.0, 20, 20);
glPopMatrix();

}

where it is being called:


void display() {



glClear(GL_COLOR_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // reset the matrix
gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z,
cam.lookAt.x, cam.lookAt.y, cam.lookAt.z,
cam.up.x, cam.up.y, cam.up.z);

drawAxes();


//Draw Player
player.draw();

// Draw planets
for (int i = 0; i < planetNum; i++)
{
//This will draw planets and Planet HUD (shows planet name on their position)

planet[i]->draw();
glColor3f(1.0, 1.0, 1.0);
stringstream ss;
ss << planet[i]->getname() << endl;
outputText(planet[i]->getPlanetPos().x, planet[i]->getPlanetPos().y, planet[i]->getPlanetPos().z, ss.str());

}

glFlush();

}

mobeen
04-11-2013, 08:59 PM
There is not enough information here. Please post the complete thing otherwise we have no clue what you might be doing in some other part of code.
Would you mind setting up a simple code first to test if it works for rendering one simple sphere with solid color. Wrapping it up into a class should be the next step after you are certain it works for the basic case.