MFC - no diffuse light?

Hi

I try to run OpenGL in a MFC-dialog based Application. It works fine except for diffuse light control. It simply does not want to work at all, that means no diffuse light at all.

The same code for initialization and drawing used in a win32 window application works fine! Of course the place where you call the init routines is different from win32 to mfc

I don´t use textures but use Color-Material. Again, the same calls and light initializations, viewport, view, normals and objects (actually only 2 dummy triangles) and so on for both applications: Win32 app works fine for diffuse light control, MFC dialog base no diffuse light at all for color material. Any idea?

Thx for any help
Stefan

Put glGetError in various places in both win32 and MFC version of you application. Sometimes moving the code can cause such problems.
I had such error once. Rendering to texture didn’t work (I could clear it but I couldtn’t render any polygon) because some of my previous code generated an error. When I moved rendering to texture before that code everything started to work again, so I placed glGetError after that suspicious block of code and got GL_INVALID_OPERATION returned.
Sometimes, even if everything is working fine you may have errors in your code that can affect your application after some minor changes that shouldn’t normally have any consequences.

HI

Thx 4 the hint. The problem is solved

Actually it isnt´t solved, it just works now :slight_smile:

what i did is to place

GLenum ErrID = glGetError();

right before starting the rendering. Then it works.

What is strange is that a call like this worked too:
if ( ErrID = glGetError() ) Break();

but this failed (!!) :
if ( glGetError() ) Break();

So you have to assign glGetError to a GLenum to solve the problem somehow, strange strange…

Originally posted by Stefan_L_01:
[b]
What is strange is that a call like this worked too:
if ( ErrID = glGetError() ) Break();

but this failed (!!) :
if ( glGetError() ) Break();
[/b]
Such behavior often indicate error in the program which was masked when compiler modified layout of local variables. For example something like damaged stack or use of uninitialized variable.

HI

Actually the program is so simple that there can hardly occur an error.
It´s interesting that Break() isn´t called, but without the code line the code doesn´t work.

It´s getting even more unpredictable if you jump for drawings in other functions, for example

CWorld::Draw()
{

MyObject.Draw();

}
where additional opengl drawing calls are done. So far i can´t see a defined behaviour.

Hi again

Ok i found the problem

I assumed that a light call like this
glLightfv(GL_LIGHT0,GL_DIFFUSE, fDif);

will immediatly update the light by the info in fDif, but there is probalby only the POINTER fDif stored. The documentation makes no remark about it.

So far i declared fDif local in the function, like:

float fDif[3] = {0.2f,0.2f,0.2f}
glLightfv(GL_LIGHT0,GL_DIFFUSE, fDif);

Now after leaving the function, fDif was not valid anymore because it was deleted. All later drawing calls failed this way, or at least the diffuse drawing.
Using a member variable for fDif, keeping it valid and thus accessible for OpenGL all time, solved the problem finally

Originally posted by Stefan_L_01:

Actually the program is so simple that there can hardly occur an error.

This is very bad assumption. Even more so in cases of programs that utilize complex frameworks like MFC.

[b]
I assumed that a light call like this
glLightfv(GL_LIGHT0,GL_DIFFUSE, fDif);

will immediatly update the light by the info in fDif, but there is probalby only the POINTER fDif stored. The documentation makes no remark about it.
[/b]
The glLightfv immediately updates the light parameters. It does not store the pointer. The error is somewhere else and changing the variable to the member one only masked it.

Maybe it´s updated immediatly, maybe not.

Why does ambient work, coded the very same way, and diffuse not?

Originally posted by Stefan_L_01:
Maybe it´s updated immediatly, maybe not.

It is done immediately unless you have very broken drivers.

float fDif[3] = {0.2f,0.2f,0.2f}
glLightfv(GL_LIGHT0,GL_DIFFUSE, fDif);

Needs to be 4 components like
float fDif[4] = {0.2f,0.2f,0.2f,1.0}

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.