Problem with glEnable(GL_COLOR_MATERIAL) in Python

Hey guys, I’ve been scratching my head over this all day; I hope you can help! :smiley:

Basically, the code runs fine except for one problem.

When I enable material color tracking like this:


glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)

My previously lighted objects become a flat colour.

Before enabling material color tracking:

After enabling material color tracking:

My system and software stats are:
Windows XP
ATI Radeon 3850 HD (Latest drivers as of this writing, 9-5_xp32_dd_ccc_wdm_enu.exe)
Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32
PyGame 1.8
PyOpenGL 3.0.0

Here’s my code:

import sys, os
import pygame

from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *

import numpy
import platform

class Engine():
	
	kill = False
	screen = 0

	def SetupMatrixes(self, _w, _h):
		# Prevent divide by zero
		if _h <= 0: 
			_h = 1
		if _w <= 0: 
			_w = 1
		
		# Set viewport to window dimensions
		glViewport(0, 0, _w, _h) 
		
		# Reset projection matrix stack
		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()
		gluPerspective(60, 1, 1, 100)
		
		# Reset texture view matrix stack
		glMatrixMode(GL_TEXTURE)
		glLoadIdentity()
		
		# Reset model view matrix stack
		glMatrixMode(GL_MODELVIEW) 
		glLoadIdentity()
		# ... and keep it on for rendering

	def SetupRenderingContext(self):
		glClearColor(0.4, 0.0, 0.0, 1.0)
		glClearDepth(1.0)
		
		glEnable(GL_DEPTH_TEST)
		glDepthFunc(GL_LEQUAL)
		glFrontFace(GL_CCW) # counter clockwise polys face out
		
		glAlphaFunc(GL_GREATER, 0.01)
		glEnable(GL_TEXTURE_2D) 
		glEnable(GL_BLEND)
		glEnable(GL_ALPHA_TEST)
		glEnable(GL_CULL_FACE)
		glBlendFunc(GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA)
		
		glPolygonMode(GL_FRONT, GL_FILL)
		glPolygonMode(GL_BACK, GL_LINE)
		
		glShadeModel(GL_SMOOTH)
		
		glEnable(GL_LIGHTING)
		
		# Set up light0
		glLightfv(GL_LIGHT0, GL_AMBIENT, ( 1.0, 1.0, 1.0, 1.0 ))
		glLightfv(GL_LIGHT0, GL_DIFFUSE, ( 1.0, 1.0, 1.0, 1.0 ))
		glLightfv(GL_LIGHT0, GL_SPECULAR,( 0.0, 0.0, 0.0, 1.0 ))
		glLightfv(GL_LIGHT0, GL_POSITION,( 0.0, 40.0, -20.0, 1.0 ))
		
		glEnable(GL_LIGHT0)
		
		# GLOBAL light settings
		glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ( 0.5, 0.5, 0.5, 1 ))
		
		# Material color tracking
		glEnable(GL_COLOR_MATERIAL)
		glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
		
	def CleanObject(self):
		glLoadIdentity()
		glColor4f(1.0, 1.0, 1.0, 1.0) # reverts back to white color so you get no screw-ups	
		glBindTexture(GL_TEXTURE_2D, GL_NONE) # untexture to NULL so u can set color with no crap
		glTranslatef(0.0, 0.0, 0.0)
		
		# apply camera rotations
		gluLookAt(
		0.0, -10, -10.0,
		0.0, 0,  3.0,
		0.0,  1.0,   0.0)

	def __init__(self):
		pygame.init()
		
		self.screen = pygame.display.set_mode([1024,768], pygame.OPENGL|pygame.DOUBLEBUF)	
		
		self.SetupRenderingContext()
		self.SetupMatrixes(1024,768)

		self.Loop() # Start program loop
		
	def Loop(self):
		while self.kill != True:
			
			# Do events
			for event in pygame.event.get():
				if event.type == pygame.QUIT: 
					self.kill = True
				elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: 
					self.kill = True
			
			# Draw a blue torus
			self.CleanObject()
			glTranslatef(0,0,3)
			glScalef(10, 10, 10)
			glColor4f( 0, 0, 1, 1 )
			glutSolidTorus (0.3, 0.5, 6, 6)
			
			# Draw a small red cube in the centre
			self.CleanObject()
			glTranslatef(0,0,3)
			glScalef(2, 2, 2)
			glColor4f( 1, 0, 0, 1 )
			glutSolidCube(1)
			
			glFlush()
			pygame.display.flip()
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

# Check to see if this is the main module. __name__ is the script name.
if __name__ == '__main__':
	engine = Engine()

/headsdesk

Alright the problem was that my glColor4f() parameters were set too high. Ambient lighting is very touchy, and gives the appearance of flat shading if you’re not careful. I should have known better!

However I did discover that the following example is broken in the latest two versions of PyOpenGL for Windows (3.0.0c1 and 3.0.0): http://www.willmcgugan.com/blog/tech/2007/6/4/opengl-sample-code-for-pygame/ That said, there are no problems when using PyOpenGL 3.0.0b8 or earlier with this example.

That example being broken gave me a false impression of what was going on and set me off on a wild goose chase for hours. :expressionless:

That said, I really do love Python, and I really do love OpenGL. So now that i’m set up properly, this should be a barrel of fun! =D