Weird Shader/Material Problem

Started by
2 comments, last by Geometrian 12 years, 8 months ago
Hi,

I have a problem with materials in a simple Phong shader. I simplified the problem, so now, I:

1. Bind a shader (compiles, links, etc. with no errors or warnings)
2. Draw a simple quad:[source lang="python"] glBegin(GL_QUADS)
glVertex3f(0,0,0)
glVertex3f(1,0,0)
glVertex3f(1,0,1)
glVertex3f(0,0,1)
glEnd()[/source]3. Call a display list.
4. Unbind the shader (back to fixed function)

The display list contains code that alters material properties with glMaterialf(...), as well as an object. The problem is that, though the object draws, the values of gl_FrontMaterial in the fragment program are incorrect, so it doesn't look as it should.

Now, properties of the issue:
-If I remove both steps 1. and 4., then the materials work correctly (though the rendering of the object isn't as nice).
-If I instead remove step 2., then the shader works correctly (but then, of course, the simple quad isn't drawn).
-If I add another step between 2. and 3., where I unbind and rebind the shader (i.e., steps are: 1, 2, 4,1, 3, 4), then the program works as expected (both the quad draws, and the materials work properly when the object is drawn--but then of course I'm binding and unbinding the shader when it really shouldn't be necessary).

I get the impression that this has a great deal to do with OpenGL's internal state. Can anyone explain what's going on?

Thanks,
-G

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Advertisement
Sounds like a driver bug. Try to update all your code to not use immediate more and not use display lists. Just rendering everything with IBO/VBO.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Sounds like a driver bug.[/quote]Ack . . . Well I did just update by driver to the newest. I see if rolling back the driver fixes it.
Try to update all your code to not use immediate more and not use display lists. Just rendering everything with IBO/VBO.[/quote]I have. This is a backwards compatibility module.
Thanks,

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

I['ll] see if rolling back the driver fixes it.
Well, I installed the previous two drivers, and that didn't fix it. I've made a self-contained Python script demonstrating the problem:[source lang="python"]from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.GL.ARB.shader_objects import *
from OpenGL.GL.ARB.vertex_shader import *
from OpenGL.GL.ARB.fragment_shader import *
import pygame
from pygame.locals import *
from math import *
import sys, traceback

pygame.init()

Screen = [800,600]

pygame.display.set_mode(Screen,OPENGL|DOUBLEBUF)

glInitShaderObjectsARB()
glInitVertexShaderARB()
glInitFragmentShaderARB()

glEnable(GL_LIGHTING)

glLightfv(GL_LIGHT0,GL_POSITION,[0,5,0, 1])
glEnable(GL_LIGHT0)

dl = glGenLists(1)
glNewList(dl,GL_COMPILE)
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,[1,0.1,0.1,1])
glBegin(GL_QUADS)
glNormal3f(0,1,0)
glVertex3f(1,0,0)
glVertex3f(2,0,0)
glVertex3f(2,0,1)
glVertex3f(1,0,1)
glEnd()
glEndList()

program = glCreateProgramObjectARB()

vert = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)
glShaderSourceARB(vert, ["void main() { gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex; }"])
glCompileShaderARB(vert)
glAttachObjectARB(program,vert)

frag = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB)
glShaderSourceARB(frag, ["void main() { gl_FragData[0] = vec4(gl_FrontMaterial.diffuse.rgb,1.0); }"])
glCompileShaderARB(frag)
glAttachObjectARB(program,frag)

glValidateProgramARB(program)
glLinkProgramARB(program)

glDeleteObjectARB(vert)
glDeleteObjectARB(frag)

print glGetInfoLogARB(program)

draw_base = False
camera_rotation = [90,23]
camera_radius = 8.0

def GetInput():
global camera_radius, draw_base
mrel = pygame.mouse.get_rel()
mpress = pygame.mouse.get_pressed()
for event in pygame.event.get():
if event.type == QUIT: return False
if event.type == KEYDOWN:
if event.key == K_d: draw_base = not draw_base
elif event.key == K_ESCAPE: return False
if event.type == MOUSEBUTTONDOWN:
if event.button == 5: camera_radius += .2
if event.button == 4: camera_radius -= .2
if mpress[0]:
camera_rotation[0] += mrel[0]
camera_rotation[1] += mrel[1]
def Draw():
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

glViewport(0,0,Screen[0],Screen[1])
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45,float(Screen[0])/float(Screen[1]),0.1,20.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

camerapos = [camera_radius*cos(radians(camera_rotation[0]))*cos(radians(camera_rotation[1])),
camera_radius*sin(radians(camera_rotation[1])),
camera_radius*sin(radians(camera_rotation[0]))*cos(radians(camera_rotation[1]))]
gluLookAt(camerapos[0],camerapos[1],camerapos[2], 0,0,0, 0,1,0)

glLightfv(GL_LIGHT0,GL_POSITION,[0,5,0, 1])

glUseProgramObjectARB(program)

glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE, [1,1,1, 1])
if draw_base:
glBegin(GL_QUADS)
glNormal3f(0,1,0)
glVertex3f(1,0,-1)
glVertex3f(-1,0,-1)
glVertex3f(-1,0,1)
glVertex3f(1,0,1)
glEnd()

glPushMatrix()
glTranslatef(0,1,0)
glCallList(dl)
glPopMatrix()

glUseProgramObjectARB(0)

pygame.display.flip()

def main():
Clock = pygame.time.Clock()
while True:
if GetInput() == False: break
Draw()
Clock.tick(60)
pygame.quit()
sys.exit()
if __name__ == '__main__':
try:
main()
except Exception, e:
tb = sys.exc_info()[2]
traceback.print_exception(e.__class__, e, tb)
pygame.quit()
raw_input()
sys.exit()[/source]Press "d" to turn the bottom quad on or off; changing the color of the display list quad.

Thanks,
-G

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

This topic is closed to new replies.

Advertisement