I recently took on to learn OpenGL via pyOpenGL, and I've been struggling with a lot of notions, especially lighting.
I managed to create a 3D cube thanks to this tutorial, and I would like to make it look better with appropriate lighting. I'm especially found of the spot light specular light creates, which gives a glossy aspect to the cube.
Desired output (one of these, taken here)
Here is what I do with the lights
glLight(GL_LIGHT0, GL_POSITION, (1.1, 0., .3, 1)) # point light
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
... draw cube ...
glMaterialfv(GL_FRONT, GL_SPECULAR, (1, 1, 1, 1))
glMaterialfv(GL_FRONT, GL_SHININESS, 1)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
If tried increasing the shininess but above ~30 all light effect disappears. Any help would be greatly appreciated, or simply good documentation (preferably using pyOpenGL, and without using vertex / fragment shaders).
Thanks!
Full code if needed:
import pygame
from OpenGL.GL import shaders
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
verticies = (
( 1, -1, -1), # 0
( 1, 1, -1), # 1
(-1, 1, -1), # 2
(-1, -1, -1), # 3
( 1, -1, 1), # 4
( 1, 1, 1), # 5
(-1, -1, 1), # 6
(-1, 1, 1), # 7
)
surfaces = (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6),
)
normals = [
( 0, 0, -1), # surface 0
(-1, 0, 0), # surface 1
( 0, 0, 1), # surface 2
( 1, 0, 0), # surface 3
( 0, 1, 0), # surface 4
( 0, -1, 0) # surface 5
]
colors = [(1, 0.2, 0)] * 12
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7),
)
def Cube():
glBegin(GL_QUADS)
for i_surface, surface in enumerate(surfaces):
x = 0
glNormal3fv(normals[i_surface])
for vertex in surface:
x += 1
glColor3fv(colors[x])
glVertex3fv(verticies[vertex])
glEnd()
glColor3fv(colors[0])
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verticies[vertex])
glEnd()
glMaterialfv(GL_FRONT, GL_SPECULAR, (1, 1, 1, 1))
glMaterialfv(GL_FRONT, GL_SHININESS, 1)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
def main():
global surfaces
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
clock = pygame.time.Clock()
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0, 0, -5)
# glRotatef(20, 1, 0, 0)
glLight(GL_LIGHT0, GL_POSITION, (1.1, 0., .3, 1)) # point light
# glLightfv(GL_LIGHT0, GL_AMBIENT, (0, 0, 1, 1))
# glLightfv(GL_LIGHT0, GL_DIFFUSE, (1, 1, 1, 1))
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_DEPTH_TEST)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glRotatef(0.2, 0, 1, 0)
Cube()
pygame.display.flip()
clock.tick(60)
main()