Advertisement

[glEnable(GL_COLOR_MATERIAL]

Started by March 28, 2022 02:03 PM
17 comments, last by JoeJ 2 years, 8 months ago

Hello,

So I'm new to OpenGL and programming using shaders.

I tried to render a textured object and I wanted to add a coordinate system with colored axes.

At first the coordinate system'axes had the same color as the object so I had to use texture→release() to unbind the texture before drawing the coordinate system, so the axes became all black.

In the function that draws the coordinate system i used glColor3f to specify each axis' color.

I read that I should enable GL _ COLOR _ MATERIAL and LIGHTING before calling that function and I tried it but it didn't work.

Any idea on how to fix this problem please ?


What version of openGL are you using?

NBA2K, Madden, Maneater, Killing Floor, Sims

Advertisement

ogldev1 said:
At first the coordinate system'axes had the same color as the object so I had to use texture→release() to unbind the texture before drawing the coordinate system, so the axes became all black.

You also need to disable shader programs, in case you forgot.
And i think you want to disable lighting instead enabling it.

@dpadam450 I think its 3.1 .

@JoeJ Well, After using texture → release() I added program.release() to disable the shader then I disabled the lighting with glDisable(GL_LIGHTING) then I drew the coordinate system. Now the object is no longer visible and only two axes of the frame visible on the screen.

ogldev1 said:
Now the object is no longer visible and only two axes of the frame visible on the screen.

Seems your state management is messed up. I would clearly separate object and axes rendering, each enabling all states it needs and disabling them after drawn. Then see both work in isolation, finally make both work at the same time.

One day i had very strange issues using core profile and AMD GPU. Compute shaders did not work properly. It turned out i have to remove every single legacy GL call like glVertex or glColor. Only after everything was using modern stuff like VBO, it worked.

Edit: Also make sure to check glGetError.

I'm confused, Logically if I disable the shader program that was created only to draw the object, every single other object drawn after won't be affected right ? and that's kinda what happened, the coordinate system is visible however the object disappeared.

I'm really sorry but I'm new to OpenGL and it's been like a month I'm searching and reading and seeing tutorials.

Thing is I'm not working using the opengl libraries like GLUT,GLEW etc.. but I'm using the funtions that were integrated in Qt6 like the functions in QMatrix4x4 (view,projection, transformations etc..)

Like for example gluLookAt corresponds to LookAt in the QMatrix4x4 class in Qt, that's why I'm facing a lot of difficulties and I'm trying to solve 'em.

Advertisement

ogldev1 said:
I'm confused, Logically if I disable the shader program that was created only to draw the object, every single other object drawn after won't be affected right ? and that's kinda what happened, the coordinate system is visible however the object disappeared.

Yeah, thus i guess the problem is related to state management. Something like the shader not being enabled again next frame when drawing the object. Such state issues are constant source of frustration in my experience, as you get just a black screen but no hint what's wrong, for example.
Calling glGetError in debug build very often can help. If you're lucky, some error shows up and you can track it down.
Qt is probably not related. But maybe you forgot to set the model view matrix for the object every frame.

Probably you should post some code, hoping it's not too much.

Okay, in the code there are functions that initialize both vertex and textures.

Now here is the paintGL function:

void MainWidget::paintGL()

{

qDebug()<<__func__;

// Clear color and depth buffer

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

texture->bind();

//! [6]

// Calculate model view transformation

QMatrix4x4 matrix;

matrix.translate(0.0, 0.0, -5.0);

matrix.scale(0.5,0.5,0.5);

matrix.rotate(rotation);

program.setUniformValue("mvp_matrix", projection * matrix);

// Use texture unit 0 which contains cube.png

program.setUniformValue("texture", 0);

// Draw cube geometry

geometries->drawCubeGeometry(&program);

draw_frame();

}

and here is the draw_frame function:

void MainWidget::draw_frame()

{

glLineWidth(8.0f);

glBegin(GL_LINES);

//X_axis

glColor3f(1.0, 0.0, 0.0);

glVertex3f(0.0, 0.0, 0.0);

glVertex3f(4.0, 0.0, 0.0);

//Y_axis

glColor3f (0.0, 1.0, 0.0);

glVertex3f(0.0, 0.0, 0.0);

glVertex3f(0.0, 4.0, 0.0);

//Z_axis

glColor3f (0.0, 0.0, 1.0);

glVertex3f(0.0, 0.0, 0.0);

glVertex3f(0.0, 0.0, 4.0);

glEnd();

}

I only wanted to use the shaders just to render the object with a texture:

When I compile this code: I get this result:

The axes have the same texture as the object. However when I unbind the texture, the axes turn all black.

I'll try to use glGetError()

Ah yes, looks like it's related to shader programs, which are missing from your code, so i guess you enable them just once at application startup.

It should look like this, somehow:

DrawObject ()
{
shaderProgram.bind();
enable and bind texture
set uniforms
draw stuff
release and disable texture
shaderProgram.release();
}

DrawAxis ()
{
// no state, just draw as you did
}

PaintGL ()
{
DrawObject ();
#ifedf DEBUG
check glError
#endif

DrawAxis ();
#ifedf DEBUG
check glError
#endif
}

Okay so I tried doing as you said but two things happened:

the z axis isn't visible and the coordinate system doesn't seem to be attached to the object however I drew it right after drawing the object.

In the previous code where the coordinate system's axes have the same texture as the object, the axes were attached to the object.

When the object rotates, the axes rotate too but now they seem to be detached from the object.

Also here is the new code:

void MainWidget::paintGL()

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//! [6]

// Calculate model view transformation

QMatrix4x4 matrix;

matrix.lookAt(QVector3D(1.0, 2.0, 0.0), QVector3D(0.0,0.0,-5.0),QVector3D(0.0,1.0,0.0));

matrix.translate(0.0, 0.0, -5.0);

matrix.scale(0.5,0.5,0.5)

program.setUniformValue("mvp_matrix", projection * matrix);

draw_object();

draw_frame();

}

void MainWidget::draw_object()

{

program.bind();

texture->bind();

program.setUniformValue("texture", 0);

geometries->drawCubeGeometry(&program);

texture->release();

program.release();

}

Draw_Frame()

{

glLineWidth(8.0f);

glBegin(GL_LINES);

//X_axis

glColor3f(1.0, 0.0, 0.0);

glVertex3f(0.0, 0.0, 0.0);

glVertex3f(4.0, 0.0, 0.0);

//Y_axis

glColor3f (0.0, 1.0, 0.0);

glVertex3f(0.0, 0.0, 0.0);

glVertex3f(0.0, 4.0, 0.0);

//Z_axis

glColor3f (0.0, 0.0, 1.0);

glVertex3f(0.0, 0.0, 0.0);

glVertex3f(0.0, 0.0, 4.0);

glEnd();

}

This topic is closed to new replies.

Advertisement