How to set a ligth fixed when a sphere is spinning? - python

I have a scene with a sphere spinning, just like Earth and the Sun. I can activate the lighting over the object but i notice that the light is moving with the object when spin, illuminating the same face ever. I want to set the light on a fixed point so, the illumination will be fixed while the "Earth" spin, giving the effect of day and night.
I know that if an object is rotated, all the "coordinate system" moves, not the object, so, i think that the ligth is fixed to the system. I tried to find the point where initialy the light is placed using trigonometry but without positive results. How can i place the light in a fixed spot to reach the day/night effect?
This is my code:
import pygame
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
#<some other libraries imports>
def sup_texture(surf):
rgbsurf = pygame.image.tostring(surf, 'RGB')
textID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texID)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
surfrc = surf.get_rect()
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, surfrc[2], surfrc[3], 0, GL_RGB, GL_UNSIGNED_BYTE, rgbsurf)
return textID
def texture(arch,arch2):
textID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, textID)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,arch2[0], arch2[1], 0, GL_RGB, GL_UNSIGNED_BYTE, arch)
glGenerateMipmap(GL_TEXTURE_2D)
return textID
def oglprint(sup):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glDisable(GL_LIGHTING)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, sup)
glBegin(GL_QUADS)
glTexCoord2f(0, 0); glVertex2f(-1, 1)
glTexCoord2f(0, 1); glVertex2f(-1, -1)
glTexCoord2f(1, 1); glVertex2f(1, -1)
glTexCoord2f(1, 0); glVertex2f(1, 1)
glEnd()
glDisable(GL_TEXTURE_2D)
def esfep(vesf,resol,texture,rotpt,punt,tama):
light_ambient = [0.0, 0.0, 0.0, 1.0]
light_diffuse = [1.0, 1.0, 1.0, 1.0]
light_position = [1, 1, 0, 0.0]
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient)
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
glLightfv(GL_LIGHT0, GL_POSITION, light_position)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
rotpt = rotpt + vesf
glOrtho(0,resol[0],resol[1],0,-resol[0],resol[1])
glTranslatef(float(punt[0]),float(punt[1]),-resol[1])
glRotatef(270, 1, 0, 0)
glRotatef(rotpt, 0, 0, 1)
glScalef(1*tama/resol[1],1*tama/resol[1],1*tama/resol[1])
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE )
esf = gluNewQuadric()
gluQuadricTexture(esf, GL_TRUE)
glDisable(GL_DEPTH_TEST)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, texture)
gluSphere(esf,round(resol[1]/2), 50, 50)
gluDeleteQuadric(esf)
glDisable(GL_TEXTURE_2D)
glDisable(GL_DEPTH_TEST)
return rotpt
pygame.init()
resol = (1366,768)
opcp = pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.OPENGL | pygame.FULLSCREEN
displayG = pygame.display.set_mode(resol,opcp)
display = pygame.Surface(resol)
imgpl = pygame.image.load('texture.jpg')
imgplt = imgpl.get_size()
imgpl = pygame.transform.smoothscale(imgpl,(1000,500))
imgpltd = pygame.image.tostring(imgpl,'RGB',False)
planpres = texture(imgpltd,imgplt)
rotpt = randint(270,360)
timer = pygame.time.Clock()
RE = tab(display)
while True:
#<some pygame stuff and display blitting>
opsurf = pygame.Surface.copy(display)
pantsp = sup_texture(opsurf)
botact = 1
while botact == 1:
timer.tick(20)
oglprint(pantsp)
rotpt = esfep(0.05,resol,planpres,rotpt,(0,resol[1] + resol[1]/4.5),1000) #this is the printing of the sphere.
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
x_mouse, y_mouse = pygame.mouse.get_pos()
dot = (x_mouse,y_mouse)
for x in range(len(RE)):
if RE[x].collidepoint(dot):
#<some stuff>
botact = 0
glDeleteTextures(pantsp)

The light position (GL_POSITION) an direction (GL_SPOT_DIRECTION) is transformed by the current modelview matrix when glLighv is called. Make sure that the model view matrix is the Identity matrix, before calling glLight, but set the transformations to the model view matrix:
def esfep(vesf,resol,texture,rotpt,punt,tama):
light_ambient = [0.0, 0.0, 0.0, 1.0]
light_diffuse = [1.0, 1.0, 1.0, 1.0]
light_position = [1, 1, 0, 0.0]
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient)
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse)
# set projection matrix
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0,resol[0],resol[1],0,-resol[0],resol[1])
# set model view identity matrix
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
# set light position (multiplied by identity matrix)
glLightfv(GL_LIGHT0, GL_POSITION, light_position)
# set model view transformations
rotpt = rotpt + vesf
glTranslatef(float(punt[0]),float(punt[1]),-resol[1])
glRotatef(270, 1, 0, 0)
glRotatef(rotpt, 0, 0, 1)
glScalef(1*tama/resol[1],1*tama/resol[1],1*tama/resol[1])
# [...]

Related

My pygame/pyopengl code seems to apply a texture to every surface

I'm throwing some code together to help me better understand python, pygame, pyopengl, and 3D rendering. I've used code from two different places and I'm integrating them by writing my own code as I go. I've textured cubes in one program and made camera movement work in another. But when I put them together, the colors are wrong and surfaces that I don't intend to texture are affected. I'm sure I'm missing something, but I can't figure it out.
Here is what the two programs look like seperately.
,
But when I put them together, I get this.
Here is my code, sorry I couldn't figure out how to attach it as a file!
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import math
def tex_coord(x, y, n=4):
""" Return the bounding vertices of the texture square.
"""
m = 1.0 / n
dx = x * m
dy = y * m
return dx, dy, dx + m, dy, dx + m, dy + m, dx, dy + m
def tex_coords(top, bottom, side):
""" Return a list of the texture squares for the top, bottom and side.
"""
top = tex_coord(*top)
bottom = tex_coord(*bottom)
side = tex_coord(*side)
result = [
(top),
(bottom),
(side),
(side),
(side),
(side),
]
"""result = []
result.extend(top)
result.extend(bottom)
result.extend(side * 4)"""
return result
#block type names and location on template go here
BLOCK1 = tex_coords((3, 0), (3, 0), (3, 0))
def verts(x, y, z, n):
vertices = (
(1+(2*x), -1+(2*y), -1+(2*z)),
(1+(2*x), 1+(2*y), -1+(2*z)),
(-1+(2*x), 1+(2*y), -1+(2*z)),
(-1+(2*x), -1+(2*y), -1+(2*z)),
(1+(2*x), -1+(2*y), 1+(2*z)),
(1+(2*x), 1+(2*y), 1+(2*z)),
(-1+(2*x), -1+(2*y), 1+(2*z)),
(-1+(2*x), 1+(2*y), 1+(2*z))
)
return(vertices)
print(verts(0, 0, 0, 1))
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)
)
colors = (
(1,0,0),
(0,1,0),
(0,0,1),
(0,1,0),
(1,1,1),
(0,1,1),
(1,0,0),
(0,1,0),
(0,0,1),
(1,0,0),
(1,1,1),
(0,1,1),
)
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)
)
forced = False
def Cube(vx,vy,vz,block):
if not forced:
glBegin(GL_QUADS)
y = 0
for surface in surfaces:
x = 0
y+=1
for vertex in surface:
x+=1
#glColor3fv(colors[x])
glTexCoord2f(block[y-1][2*(x-1)], block[y-1][(2*x)-1])
#print(block[y-1][2*(x-1)], block[y-1][(2*x)-1])
glVertex3fv(verts(vx,vy,vz,1)[vertex])
glEnd()
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verts(vx,vy,vz,1)[vertex])
glEnd()
else:
texX = 0.75
texY = 0.25
glBegin(GL_QUADS)
glTexCoord2f(0.0+texX, 0.0)
glVertex3f(-1.0, -1.0, 1.0)
glTexCoord2f(0.25+texX, 0.0)
glVertex3f(1.0, -1.0, 1.0)
glTexCoord2f(0.25+texX, 0.25)
glVertex3f(1.0, 1.0, 1.0)
glTexCoord2f(0.0+texX, 0.25)
glVertex3f(-1.0, 1.0, 1.0)
glEnd()
def loadTexture():
textureSurface = pygame.image.load('texture2.png')
textureData = pygame.image.tostring(textureSurface, "RGBA", 1)
width = textureSurface.get_width()
height = textureSurface.get_height()
glEnable(GL_TEXTURE_2D)
texid = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texid)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
0, GL_RGBA, GL_UNSIGNED_BYTE, textureData)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
return texid
pygame.init()
display = (800, 600)
scree = pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glShadeModel(GL_SMOOTH)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
glEnable(GL_LIGHT0)
glLightfv(GL_LIGHT0, GL_AMBIENT, [0.5, 0.5, 0.5, 1])
glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1])
"""
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glDepthMask(GL_TRUE)
glDepthFunc(GL_LESS)
glEnable(GL_DEPTH_TEST)
#glEnable(GL_CULL_FACE)
#glCullFace(GL_FRONT)
##glFrontFace(GL_CCW)
##glShadeModel(GL_SMOOTH)
glDepthRange(0.0,1.0)
"""
sphere = gluNewQuadric()
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
gluLookAt(0, -8, 0, 0, 0, 0, 0, 0, 1)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glLoadIdentity()
# init mouse movement and center mouse on screen
displayCenter = [scree.get_size()[i] // 2 for i in range(2)]
mouseMove = [0, 0]
pygame.mouse.set_pos(displayCenter)
loadTexture()
up_down_angle = 0.0
paused = False
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN:
run = False
if event.key == pygame.K_PAUSE or event.key == pygame.K_p:
paused = not paused
pygame.mouse.set_pos(displayCenter)
if not paused:
if event.type == pygame.MOUSEMOTION:
mouseMove = [event.pos[i] - displayCenter[i] for i in range(2)]
pygame.mouse.set_pos(displayCenter)
if not paused:
# get keys
keypress = pygame.key.get_pressed()
#mouseMove = pygame.mouse.get_rel()
# init model view matrix
glLoadIdentity()
# apply the look up and down
up_down_angle += mouseMove[1]*0.1
glRotatef(up_down_angle, 1.0, 0.0, 0.0)
# init the view matrix
glPushMatrix()
glLoadIdentity()
# apply the movment
if keypress[pygame.K_w]:
glTranslatef(0,0,0.1)
if keypress[pygame.K_s]:
glTranslatef(0,0,-0.1)
if keypress[pygame.K_d]:
glTranslatef(-0.1,0,0)
if keypress[pygame.K_a]:
glTranslatef(0.1,0,0)
if keypress[pygame.K_LSHIFT]:
glTranslatef(0,0.5,0)
if keypress[pygame.K_SPACE]:
glTranslatef(0,-0.5,0)
# apply the left and right rotation
glRotatef(mouseMove[0]*0.1, 0.0, 1.0, 0.0)
# multiply the current matrix by the get the new view matrix and store the final vie matrix
glMultMatrixf(viewMatrix)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
# apply view matrix
glPopMatrix()
glMultMatrixf(viewMatrix)
#glLightfv(GL_LIGHT0, GL_POSITION, [1, -1, 1, 0])
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPushMatrix()
Cube(0,0,0,BLOCK1)
Cube(1,0,0,BLOCK1)
Cube(0,1,0,BLOCK1)
Cube(0,0,1,BLOCK1)
Cube(-2,0,0,BLOCK1)
glColor4f(0.5, 0.5, 0.5, 1)
glBegin(GL_QUADS)
glVertex3f(-10, -10, -2)
glVertex3f(10, -10, -2)
glVertex3f(10, 10, -2)
glVertex3f(-10, 10, -2)
glEnd()
glTranslatef(-1.5, 0, 0)
glColor4f(0.5, 0.2, 0.2, 1)
gluSphere(sphere, 1.0, 32, 16)
glTranslatef(3, 0, 0)
glColor4f(0.2, 0.2, 0.5, 1)
gluSphere(sphere, 1.0, 32, 16)
glPopMatrix()
pygame.display.flip()
pygame.time.wait(10)
pygame.quit()
I'd be very thankful if someone could explain this to me!
Edit: Thank you Rabbid76!!!
Here is a picture, and my working code.
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import math
def tex_coord(x, y, n=4):
""" Return the bounding vertices of the texture square.
"""
m = 1.0 / n
dx = x * m
dy = y * m
return dx, dy, dx + m, dy, dx + m, dy + m, dx, dy + m
def tex_coords(top, bottom, side):
""" Return a list of the texture squares for the top, bottom and side.
"""
top = tex_coord(*top)
bottom = tex_coord(*bottom)
side = tex_coord(*side)
result = [
(top),
(bottom),
(side),
(side),
(side),
(side),
]
"""result = []
result.extend(top)
result.extend(bottom)
result.extend(side * 4)"""
return result
#block type names and location on template go here
BLOCK1 = tex_coords((3, 0), (3, 0), (3, 0))
def verts(x, y, z, n):
vertices = (
(1+(2*x), -1+(2*y), -1+(2*z)),
(1+(2*x), 1+(2*y), -1+(2*z)),
(-1+(2*x), 1+(2*y), -1+(2*z)),
(-1+(2*x), -1+(2*y), -1+(2*z)),
(1+(2*x), -1+(2*y), 1+(2*z)),
(1+(2*x), 1+(2*y), 1+(2*z)),
(-1+(2*x), -1+(2*y), 1+(2*z)),
(-1+(2*x), 1+(2*y), 1+(2*z))
)
return(vertices)
print(verts(0, 0, 0, 1))
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)
)
colors = (
(1,0,0),
(0,1,0),
(0,0,1),
(0,1,0),
(1,1,1),
(0,1,1),
(1,0,0),
(0,1,0),
(0,0,1),
(1,0,0),
(1,1,1),
(0,1,1),
)
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)
)
forced = False
def Cube(vx,vy,vz,block):
if not forced:
glBegin(GL_QUADS)
y = 0
for surface in surfaces:
x = 0
y+=1
for vertex in surface:
x+=1
#glColor3fv(colors[x])
glTexCoord2f(block[y-1][2*(x-1)], block[y-1][(2*x)-1])
#print(block[y-1][2*(x-1)], block[y-1][(2*x)-1])
glVertex3fv(verts(vx,vy,vz,1)[vertex])
glEnd()
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verts(vx,vy,vz,1)[vertex])
glEnd()
else:
texX = 0.75
texY = 0.25
glBegin(GL_QUADS)
glTexCoord2f(0.0+texX, 0.0)
glVertex3f(-1.0, -1.0, 1.0)
glTexCoord2f(0.25+texX, 0.0)
glVertex3f(1.0, -1.0, 1.0)
glTexCoord2f(0.25+texX, 0.25)
glVertex3f(1.0, 1.0, 1.0)
glTexCoord2f(0.0+texX, 0.25)
glVertex3f(-1.0, 1.0, 1.0)
glEnd()
def loadTexture():
textureSurface = pygame.image.load('texture2.png')
textureData = pygame.image.tostring(textureSurface, "RGBA", 1)
width = textureSurface.get_width()
height = textureSurface.get_height()
glColor3f(0.5, 0.5, 0.5)
glEnable(GL_TEXTURE_2D)
texid = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texid)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
0, GL_RGBA, GL_UNSIGNED_BYTE, textureData)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
return texid
glDisable(GL_TEXTURE_2D)
pygame.init()
display = (800, 600)
scree = pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glShadeModel(GL_SMOOTH)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
glEnable(GL_LIGHT0)
glLightfv(GL_LIGHT0, GL_AMBIENT, [0.5, 0.5, 0.5, 1])
glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1])
"""
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glDepthMask(GL_TRUE)
glDepthFunc(GL_LESS)
glEnable(GL_DEPTH_TEST)
#glEnable(GL_CULL_FACE)
#glCullFace(GL_FRONT)
##glFrontFace(GL_CCW)
##glShadeModel(GL_SMOOTH)
glDepthRange(0.0,1.0)
"""
sphere = gluNewQuadric()
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
gluLookAt(0, -8, 0, 0, 0, 0, 0, 0, 1)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glLoadIdentity()
# init mouse movement and center mouse on screen
displayCenter = [scree.get_size()[i] // 2 for i in range(2)]
mouseMove = [0, 0]
pygame.mouse.set_pos(displayCenter)
loadTexture()
up_down_angle = 0.0
paused = False
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN:
run = False
if event.key == pygame.K_PAUSE or event.key == pygame.K_p:
paused = not paused
pygame.mouse.set_pos(displayCenter)
if not paused:
if event.type == pygame.MOUSEMOTION:
mouseMove = [event.pos[i] - displayCenter[i] for i in range(2)]
pygame.mouse.set_pos(displayCenter)
if not paused:
# get keys
keypress = pygame.key.get_pressed()
#mouseMove = pygame.mouse.get_rel()
# init model view matrix
glLoadIdentity()
# apply the look up and down
up_down_angle += mouseMove[1]*0.1
glRotatef(up_down_angle, 1.0, 0.0, 0.0)
# init the view matrix
glPushMatrix()
glLoadIdentity()
# apply the movment
if keypress[pygame.K_w]:
glTranslatef(0,0,0.1)
if keypress[pygame.K_s]:
glTranslatef(0,0,-0.1)
if keypress[pygame.K_d]:
glTranslatef(-0.1,0,0)
if keypress[pygame.K_a]:
glTranslatef(0.1,0,0)
if keypress[pygame.K_LSHIFT]:
glTranslatef(0,0.5,0)
if keypress[pygame.K_SPACE]:
glTranslatef(0,-0.5,0)
# apply the left and right rotation
glRotatef(mouseMove[0]*0.1, 0.0, 1.0, 0.0)
# multiply the current matrix by the get the new view matrix and store the final vie matrix
glMultMatrixf(viewMatrix)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
# apply view matrix
glPopMatrix()
glMultMatrixf(viewMatrix)
#glLightfv(GL_LIGHT0, GL_POSITION, [1, -1, 1, 0])
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glEnable(GL_TEXTURE_2D)
Cube(0,0,0,BLOCK1)
Cube(1,0,0,BLOCK1)
Cube(0,1,0,BLOCK1)
Cube(0,0,1,BLOCK1)
Cube(-2,0,0,BLOCK1)
glDisable(GL_TEXTURE_2D)
glColor4f(0.5, 0.5, 0.5, 1)
glBegin(GL_QUADS)
glVertex3f(-10, -10, -2)
glVertex3f(10, -10, -2)
glVertex3f(10, 10, -2)
glVertex3f(-10, 10, -2)
glEnd()
glTranslatef(-1.5, 0, 0)
glColor4f(0.5, 0.2, 0.2, 1)
gluSphere(sphere, 1.0, 32, 16)
glTranslatef(3, 0, 0)
glColor4f(0.2, 0.2, 0.5, 1)
gluSphere(sphere, 1.0, 32, 16)
glColor3f(1, 1, 1)
glPopMatrix()
pygame.display.flip()
pygame.time.wait(10)
pygame.quit()
OpenGL is a state engine. A state is kept until it is changed again. Two-dimensional texturing can be enabled and disabled, see glEnable.
When texturing is activated, by default the color of the pixel is multiplied by the current color, because by default the texture environment mode (GL_TEXTURE_ENV_MODE) is GL_MODULATE. See glTexEnv.
This causes that the color of the piles of the texture is "mixed" by the last color which you have set by glColor4f.
Set a "white" color and enable texturing before you render an object with a texture. Disable texturing before you draw an object with colors:
glColor3f(1.0f, 1.0f, 1.0f)
glEnable(GL_TEXTURE_2D)
# draw object with texture
# [...]
glDisable(GL_TEXTURE_2D)
# draw object with color
# [...]
Changes in your code:
forced = False
def Cube(vx,vy,vz,block):
glColor4f(1, 1, 1, 1) # <--
glEnable(GL_TEXTURE_2D)
if not forced:
glBegin(GL_QUADS)
for y, surface in enumerate(surfaces):
for x, vertex in enumerate(surface):
glTexCoord2f(block[y-1][2*(x-1)], block[y-1][(2*x)-1])
glVertex3fv(verts(vx,vy,vz,1)[vertex])
glEnd()
glDisable(GL_TEXTURE_2D) # <--
glColor4f(0, 0, 0, 1)
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verts(vx,vy,vz,1)[vertex])
glEnd()
# [...]
run = True
while run:
# [...]
Cube(0,0,0,BLOCK1)
Cube(1,0,0,BLOCK1)
Cube(0,1,0,BLOCK1)
Cube(0,0,1,BLOCK1)
Cube(-2,0,0,BLOCK1)
glDisable(GL_TEXTURE_2D) # <--
glColor4f(0.5, 0.5, 0.5, 1)
glBegin(GL_QUADS)
glVertex3f(-10, -10, -2)
glVertex3f(10, -10, -2)
glVertex3f(10, 10, -2)
glVertex3f(-10, 10, -2)
glEnd()
# [...]

Adding different textures to differents objects using PyOpenGL

I feel kind of stupid for asking this, but I've tried almost every example and proved every solution.
I'm trying to create a endless runner game using pyOpenGL and pygames, and facing many troubles, one of them is the following: I'm trying to render 2 textures for differents objects using pyOpenGL glBindTexture(GL_TEXTURE_2D, id). I have a main object, that is a cube, called cube and I also have an array of cube. I want to render a texture in cube and a different one all the cubes in the array, but everytime I try to do this the last texture "rendered" overlaps the previus one. What could I do?
Here is my code:
# Execute with Python 3
import os
import pygame
import random
import math
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
camera_x = 0
camera_z = 0
camera_y = 0
x = 0
Vertices = [
[1, -1, 1],
[-1, -1, 1],
[-1, -1, -1],
[1, -1, -1],
[1, 1, -1],
[-1, 1, -1],
[-1, 1, 1],
[1, 1, 1]
]
textureSurface = pygame.image.load('./textures/ursa.png'), pygame.image.load('./textures/caja.png')
textureData = pygame.image.tostring(textureSurface[0], "RGBA"), pygame.image.tostring(textureSurface[1], "RGBA")
width = textureSurface[0].get_width(), textureSurface[1].get_width()
height = textureSurface[0].get_height(), textureSurface[1].get_height()
class Ground:
global camera_z
def __init__(self):
self.ground_vertices = (
(-11, -2.01, 20),
(11, -2.01, 20),
(11, -2.01, -300),
(-11, -2.01, -300)
)
def draw(self):
glPushMatrix()
glTranslatef(0, 0, camera_z)
glBegin(GL_QUADS)
for vertex in self.ground_vertices:
glColor3fv((0, 0.5, 0.5))
glVertex3fv(vertex)
glEnd()
glPopMatrix()
class Cube:
def __init__(self, texture=False):
self.vertices = [
[1, -1, 1],
[-1, -1, 1],
[-1, -1, -1],
[1, -1, -1],
[1, 1, -1],
[-1, 1, -1],
[-1, 1, 1],
[1, 1, 1]
]
self.surfaces = (
(0, 1, 6, 7),
(0, 1, 2, 3),
(0, 3, 4, 7),
(1, 2, 6, 5),
(2, 3, 4, 5),
(4, 5, 6, 7)
)
self.colors = (
(105 / 255, 210 / 255, 231 / 255),
(167 / 255, 219 / 255, 216 / 255),
(224 / 255, 228 / 255, 204 / 255),
(243 / 255, 134 / 255, 48 / 255)
)
self.vertices_texture = (
(0.0, 0.0),
(1.0, 0.0),
(1.0, 1.0),
(0.0, 1.0),
)
self.texture = texture
self.center = [0, 0, 0]
def draw(self):
if self.texture:
glEnable(GL_TEXTURE_2D)
glColor3f(1, 1, 1)
glBegin(GL_QUADS)
if self.texture:
for surface in self.surfaces:
for x, vertex in enumerate(surface):
glTexCoord2fv(self.vertices_texture[x])
glVertex3fv(self.vertices[vertex])
else:
for surface in self.surfaces:
for x, vertex in enumerate(surface):
glColor3fv(self.colors[x])
glVertex3fv(self.vertices[vertex])
self.center = [
(self.vertices[2][0]+self.vertices[7][0])/2,
(self.vertices[2][1]+self.vertices[7][1])/2,
(self.vertices[2][2]+self.vertices[7][2])/2
]
glEnd()
if self.texture:
glDisable(GL_TEXTURE_2D)
def set_vertices(self, max_distance, min_distance=-40):
x_value_change = random.randrange(-10, 10)
y_value_change = -1
z_value_change = random.randrange(-1 * max_distance, min_distance)
new_vertices = []
for vertex in Vertices:
new_vertex = []
new_x = vertex[0] + x_value_change
new_y = vertex[1] + y_value_change
new_z = vertex[2] + z_value_change
new_vertex.append(new_x)
new_vertex.append(new_y)
new_vertex.append(new_z)
new_vertices.append(new_vertex)
self.vertices = new_vertices
def rotate(self):
glPushMatrix()
glRotatef(25, 1, 0, 0)
glPopMatrix()
def loadTexture(self, file):
glEnable(GL_TEXTURE_2D)
id = [0]*2
glGenTextures(2, id)
if file == 0:
glBindTexture(GL_TEXTURE_2D, id[0])
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width[0], height[0], 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData[0])
if file == 1:
glBindTexture(GL_TEXTURE_2D, id[1])
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width[1], height[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData[1])
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glDisable(GL_TEXTURE_2D)
#return texid
def leave(event):
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
quit()
def main():
global camera_x, camera_y, camera_z, x
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL | OPENGLBLIT)
max_distance = 300
gluPerspective(45, (display[0] / display[1]), 0.1, max_distance)
glEnable(GL_DEPTH_TEST)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glTranslatef(0, -10 / 2.4, -50)
ground = Ground()
cube = Cube(True)
cube.loadTexture(0)
my_cubes = []
for i in range(20):
tmp_cube = Cube(True)
tmp_cube.loadTexture(1)
tmp_cube.set_vertices(max_distance)
my_cubes.append(tmp_cube)
while True:
for event in pygame.event.get():
leave(event)
# movInverse(event)
M = glGetDoublev(GL_MODELVIEW_MATRIX)
# print(M)
camera_x = M[3][0]
camera_y = M[3][1]
camera_z = M[3][2]
glTranslatef(0, 0, 1.5)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
ground.draw()
glPushMatrix()
if(math.fabs(x) < 11):
glTranslatef(x, -1.5, camera_z-20)
glScalef(1 / 2, 1 / 2, 0)
cube.draw()
glPopMatrix()
for tmp_cube in my_cubes:
tmp_cube.draw()
print(tmp_cube.center)
for tmp_cube in my_cubes:
if camera_z <= tmp_cube.vertices[0][2]:
new_max = int(-1 * (camera_z - max_distance * 2))
tmp_cube.set_vertices(new_max, int(camera_z - max_distance))
pygame.display.flip()
if __name__ == '__main__':
main()
The method loadTexture is supposed to choose what texture is going to apply, but it doesn't, I don't know why. I hope someone could help me. Thanks!
When each cube has its own texture, then add an attribute self.id , which holds the texture for the cube.
Create this texture object and load the texture in the method loadTexture():
class Cube:
# [...]
def loadTexture(self, file):
self.id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self.id)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width[file], height[file], 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData[file])
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
Bind the texture before the cube is draw (glBindTexture):
class Cube:
# [...]
def draw(self):
if self.texture:
glEnable(GL_TEXTURE_2D)
glColor3f(1, 1, 1)
glBindTexture(GL_TEXTURE_2D, self.id) # <-----
glBegin(GL_QUADS)
# [...]

Why my texture is not showing PyOpenGL

I tried everything but still I don't get my error. I am trying to put a texture on my sphere object.
"""
Minimal texture on sphere demo
This is demo for showing how to put image
on sphere as texture in PyOpenGL.
"""
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from PIL import Image
import numpy
def run_scene():
glutInit()
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutCreateWindow("Minimal sphere OpenGL")
lightning()
glutDisplayFunc(draw_sphere)
glMatrixMode(GL_PROJECTION)
gluPerspective(40, 1, 1, 40)
glMatrixMode(GL_MODELVIEW)
gluLookAt(0, 0, 10,
0, 0, 0,
0, 1, 0)
glPushMatrix()
glutMainLoop()
return
def lightning():
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glEnable(GL_BLEND)
glLightfv(GL_LIGHT0, GL_POSITION, [10, 4, 10, 1])
glLightfv(GL_LIGHT0, GL_DIFFUSE, [0.8, 1, 0.8, 1])
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1)
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05)
glEnable(GL_LIGHT0)
return
def draw_sphere():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()
texture_id = read_texture('mars.png')
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, texture_id)
glEnable(GL_TEXTURE_GEN_S)
glEnable(GL_TEXTURE_GEN_T)
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP)
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP)
glutSolidSphere(1, 50, 50)
glDisable(GL_TEXTURE_2D)
glPopMatrix()
glutSwapBuffers()
return
def read_texture(filename):
img = Image.open(filename)
img_data = numpy.array(list(img.getdata()), numpy.int8)
texture_id = glGenTextures(1)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0,
GL_RGB, GL_UNSIGNED_BYTE, img_data)
return texture_id
if __name__ == '__main__':
run_scene()
This is result of this code, simple green sphere as this:
But this is not my expected result. I want to simply show this texture mars.png on sphere surface:
Help me with this I cannot solve this on my own it seems.
In read_texture() after you generate a texture name you don't bind it. So the subsequent texture related calls are going to the default texture and not your newly created texture.
texture_id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture_id) # This is what's missing
Also since your image is a PNG image. Then list(img.getdata()) will be a list of tuples containing (R, G, B, A). So when calling glTexImage2D you need to tell that your data is GL_RGBA and not GL_RGB.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0,
GL_RGBA, GL_UNSIGNED_BYTE, img_data)
You can also automate it.
format = GL_RGB if img.mode == "RGB" else GL_RGBA
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0,
format, GL_UNSIGNED_BYTE, img_data)
You can also convert it to not have an alpha channel.
img = img.convert("RGB")
Which of course needs to be done before calling img.getdata().
Now you should see the following:
Also an important note. You're calling read_texture() in draw_sphere(). This means that every time draw_sphere() is called, the image is loaded and a new texture is created. You really don't want to do that. Instead before calling glutMainLoop() call read_texture() and store the result as a global name.

May anyone explain to me why the texture doesn't show? [PyOpenGL]

The relevant methods to see are: init, genTexture, onDisplayEvent and table_leg_model.
The 'wood1.png' is a 64x64 image.
Code :
import Image
from math import cos
from math import pi
from math import sin
from numpy import array
from numpy import uint8
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from sys import argv
# Classes ----------------------------------------------------------------------
class AppGL(object):
def __init__(self, title, position=(100, 100), size=(400, 400)):
# Properties -----------------------------------------------------------
self.angle_delta = 4.0
self.position = position
self.rotateX = [0, 1.0, 0.0, 0.0]
self.rotateY = [0, 0.0, 1.0, 0.0]
self.size = size
self.translate = [0.0, 0.0, 0.0]
self.textures_index = {
'wood1': 0,
}
self.textures = array([0] * len(self.textures_index), uint8)
#-----------------------------------------------------------------------
# General initialization
glClearColor(0.0, 0.0, 0.0, 1.0)
glEnable(GL_DEPTH_TEST)
glEnable(GL_TEXTURE_2D)
glGenTextures(len(self.textures), self.textures)
self.genTexture('wood1', 'textures/wood1.png')
#-----------------------------------------------------------------------
# GLUT initialization --------------------------------------------------
glutInit(argv)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowPosition(*self.position)
glutInitWindowSize(*self.size)
glutCreateWindow(title)
#-----------------------------------------------------------------------
# Callbacks ------------------------------------------------------------
glutDisplayFunc(self.onDisplayEvent)
glutKeyboardFunc(self.onKeyEvent)
glutMotionFunc(self.onMouseMotionEvent)
glutMouseFunc(self.onMouseButtonEvent)
glutReshapeFunc(self.onReshapeEvent)
glutSpecialFunc(self.onSpecialKeyEvent)
#-----------------------------------------------------------------------
# Arrays ---------------------------------------------------------------
data = []
#data.extend(table_model(0.4, 40))
data.extend(table_leg_model())
#data.extend(table_model(0.2, 40))
glInterleavedArrays(GL_T2F_V3F, 0, array(data, "f"))
#-----------------------------------------------------------------------
def genTexture(self, index, path):
img = Image.open(path)
texture_data = array(list(img.getdata()), uint8)
glBindTexture(GL_TEXTURE_2D, self.textures[self.textures_index[index]])
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, texture_data)
def onDisplayEvent(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
gluLookAt(0, 0, 4, 0, 0, -1, 0, 1, 0)
glTranslatef(*self.translate)
glRotatef(*self.rotateX)
glRotatef(*self.rotateY)
# Visual scene ---------------------------------------------------------
glPushMatrix()
# Table -----------------------~
#glDrawElements(GL_POLYGON, 39, GL_UNSIGNED_INT, array(xrange(0, 80, 2), "d"))
#glDrawElements(GL_POLYGON, 39, GL_UNSIGNED_INT, array(xrange(1, 80, 2), "d"))
#glDrawElements(GL_QUAD_STRIP, 80, GL_UNSIGNED_INT, array(xrange(0, 80), "d"))
getMatrixHead()
glTranslatef(-0.1, -0.1, 0)
#glDrawElements(GL_QUAD_STRIP, 10, GL_UNSIGNED_INT, array(xrange(80, 90), "d"))
glDrawElements(GL_QUAD_STRIP, 10, GL_UNSIGNED_INT, array(xrange(0, 10), "d"))
#getMatrixHead()
#glTranslatef(0, 0, 1)
#glDrawElements(GL_POLYGON, 39, GL_UNSIGNED_INT, array(xrange(90, 170, 2), "d"))
#glDrawElements(GL_POLYGON, 39, GL_UNSIGNED_INT, array(xrange(91, 170, 2), "d"))
#glDrawElements(GL_QUAD_STRIP, 80, GL_UNSIGNED_INT, array(xrange(90, 170), "d"))
glPopMatrix()
#-----------------------------------------------------------------------
glutSwapBuffers()
def onKeyEvent(self, key, x, y):
change = True
if key == 'a':
self.translate[0] += 1
elif key == 'd':
self.translate[0] -= 1
elif key == 's':
self.translate[1] += 1
elif key == 'w':
self.translate[1] -= 1
elif key == 'q':
self.translate[2] += 1
elif key == 'e':
self.translate[2] -= 1
else:
change = False
if change: glutPostRedisplay()
def onMouseMotionEvent(self, x, y):
pass
def onMouseButtonEvent(self, button, state, x, y):
pass
def onReshapeEvent(self, width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(60.0, width / height, 1.0, 100.0)
glMatrixMode(GL_MODELVIEW)
def onSpecialKeyEvent(self, key, x, y):
change = True
if key == GLUT_KEY_UP:
self.rotateX[0] += self.angle_delta
elif key == GLUT_KEY_DOWN:
self.rotateX[0] -= self.angle_delta
elif key == GLUT_KEY_LEFT:
self.rotateY[0] += self.angle_delta
elif key == GLUT_KEY_RIGHT:
self.rotateY[0] -= self.angle_delta
else:
change = False
if change: glutPostRedisplay()
def run(self):
glutMainLoop()
#-------------------------------------------------------------------------------
# Functions --------------------------------------------------------------------
def getMatrixHead():
glPopMatrix()
glPushMatrix()
def table_model(radius, number_of_points):
delta = 2 * pi / (number_of_points - 1)
points = []
for i in xrange(number_of_points):
points.extend((radius * cos(i * delta), radius * sin(i * delta), 0.05))
points.extend((radius * cos(i * delta), radius * sin(i * delta), 0))
return points
def table_leg_model():
return (
0, 0, 0, 0.2, 0,
0, 1, 0, 0.2, 1.0,
1, 0, 0, 0, 0,
1, 1, 0, 0, 1.0,
#0, 0, 0.2, 0, 0,
#0, 1, 0.2, 0, 1.0,
#1, 0, 0.2, 0.2, 0,
#1, 1, 0.2, 0.2, 1.0,
#0, 0, 0, 0.2, 0,
#0, 1, 0, 0.2, 1.0,
)
#-------------------------------------------------------------------------------
# Main -------------------------------------------------------------------------
if __name__ == '__main__':
AppGL("Moe's Tavern", (300, 100), (800, 600)).run()
#-------------------------------------------------------------------------------
The source code can also be found here ~> https://github.com/EPadronU/TavernGL/blob/master/appgl.py
I'll just run down the list of what stroke me curious:
The function getMatrixHead makes no sense, at least the naming. At best it will just drop what's been on the stack and make a copy of what's below.
Then you make the typical newbie error of trying to "initialize" OpenGL. Don't do that. Except for loading textures or VBO data make OpenGL calls only from the display function and nowhere else. If you made a backtrace whenever a OpenGL function is called it should only happen because ultimately because display was called. If you had done it that way you'd not have made OpenGL called before there was an actual OpenGL context: Only after a OpenGL context has been created OpenGL calls have an effect. If using GLUT that's after calling glutCreateWindow
Now look at that code:
glEnable(GL_DEPTH_TEST)
glEnable(GL_TEXTURE_2D)
glGenTextures(len(self.textures), self.textures)
self.genTexture('wood1', 'textures/wood1.png')
#-----------------------------------------------------------------------
# GLUT initialization --------------------------------------------------
glutInit(argv)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowPosition(*self.position)
glutInitWindowSize(*self.size)
glutCreateWindow(title)
You're making OpenGL calls before there is a context. If you had written it in the following way it would have worked (I also took the liberty at giving it a nice informative loading screen)
import Image
from math import cos
from math import pi
from math import sin
from numpy import array
from numpy import uint8
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from sys import argv
# Classes ----------------------------------------------------------------------
class AppGL(object):
def __init__(self, title, position=(100, 100), size=(400, 400)):
# Properties -----------------------------------------------------------
self.angle_delta = 4.0
self.position = position
self.rotateX = [0, 1.0, 0.0, 0.0]
self.rotateY = [0, 0.0, 1.0, 0.0]
self.size = size
self.translate = [0.0, 0.0, 0.0]
self.textures = dict()
#-----------------------------------------------------------------------
# GLUT initialization --------------------------------------------------
glutInit(argv)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowPosition(*self.position)
glutInitWindowSize(*self.size)
glutCreateWindow(title)
#-----------------------------------------------------------------------
# Callbacks ------------------------------------------------------------
glutDisplayFunc(self.onDisplayEvent)
glutKeyboardFunc(self.onKeyEvent)
glutMotionFunc(self.onMouseMotionEvent)
glutMouseFunc(self.onMouseButtonEvent)
glutReshapeFunc(self.onReshapeEvent)
glutSpecialFunc(self.onSpecialKeyEvent)
#-----------------------------------------------------------------------
# Arrays ---------------------------------------------------------------
self.tablelegmodel = array( list(table_leg_model()), "f")
#-----------------------------------------------------------------------
def loadTexture(self, path):
if path in self.textures.keys():
glBindTexture(GL_TEXTURE_2D, self.textures[path])
return
glDrawBuffer(GL_FRONT)
glClearColor(0.25, 0.25, 0.75, 1.)
glClear(GL_COLOR_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
glRasterPos3f(-0.9, 0.9, 0)
glDisable(GL_TEXTURE_2D)
glDisable(GL_LIGHTING)
glutBitmapString(GLUT_BITMAP_HELVETICA_18, "Loading Texture " + path)
glFinish()
glDrawBuffer(GL_BACK)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
img = Image.open(path)
texture_data = array(list(img.getdata()), uint8)
texID = array([0], uint8)
glGenTextures(1, texID)
texID = texID[0]
self.textures[path] = texID
glBindTexture(GL_TEXTURE_2D, texID)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, texture_data)
def onDisplayEvent(self):
width, height = self.size
aspect = float(width) / float(height)
glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(60.0, aspect, 1.0, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(0, 0, 4, 0, 0, -1, 0, 1, 0)
glTranslatef(*self.translate)
glRotatef(*self.rotateX)
glRotatef(*self.rotateY)
# Visual scene ---------------------------------------------------------
glEnable(GL_DEPTH_TEST)
glPushMatrix()
glTranslatef(-0.1, -0.1, 0)
self.loadTexture('textures/wood1.png')
glEnable(GL_TEXTURE_2D)
glInterleavedArrays(GL_T2F_V3F, 0, self.tablelegmodel)
glDrawElements(GL_QUAD_STRIP, 10, GL_UNSIGNED_INT, array(xrange(0, 10), "d"))
glPopMatrix()
#-----------------------------------------------------------------------
glutSwapBuffers()
def onKeyEvent(self, key, x, y):
if key == 'a':
self.translate[0] += 1
elif key == 'd':
self.translate[0] -= 1
elif key == 's':
self.translate[1] += 1
elif key == 'w':
self.translate[1] -= 1
elif key == 'q':
self.translate[2] += 1
elif key == 'e':
self.translate[2] -= 1
else:
return
glutPostRedisplay()
def onMouseMotionEvent(self, x, y):
pass
def onMouseButtonEvent(self, button, state, x, y):
pass
def onReshapeEvent(self, width, height):
self.size = width, height
glutPostRedisplay()
def onSpecialKeyEvent(self, key, x, y):
if key == GLUT_KEY_UP:
self.rotateX[0] += self.angle_delta
elif key == GLUT_KEY_DOWN:
self.rotateX[0] -= self.angle_delta
elif key == GLUT_KEY_LEFT:
self.rotateY[0] += self.angle_delta
elif key == GLUT_KEY_RIGHT:
self.rotateY[0] -= self.angle_delta
else:
return
glutPostRedisplay()
def run(self):
glutMainLoop()
#-------------------------------------------------------------------------------
def table_model(radius, number_of_points):
delta = 2 * pi / (number_of_points - 1)
points = []
for i in xrange(number_of_points):
points.append((radius * cos(i * delta), radius * sin(i * delta), 0.05))
points.append((radius * cos(i * delta), radius * sin(i * delta), 0))
return points
def table_leg_model():
return (
0, 0, 0, 0.2, 0,
0, 1, 0, 0.2, 1.0,
1, 0, 0, 0, 0,
1, 1, 0, 0, 1.0,
#0, 0, 0.2, 0, 0,
#0, 1, 0.2, 0, 1.0,
#1, 0, 0.2, 0.2, 0,
#1, 1, 0.2, 0.2, 1.0,
#0, 0, 0, 0.2, 0,
#0, 1, 0, 0.2, 1.0,
)
#-------------------------------------------------------------------------------
# Main -------------------------------------------------------------------------
if __name__ == '__main__':
AppGL("Moe's Tavern", (300, 100), (800, 600)).run()
#-------------------------------------------------------------------------------

pyopengl set multipass texture blending mode

im trying to get multi texturing working and have so far got miltiple textures to load using this function
def loadTexture(name):
img = PIL.Image.open(name) # .jpg, .bmp, etc. also work
img_data = numpy.array(list(img.getdata()), numpy.int8)
id = glGenTextures(1)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glBindTexture(GL_TEXTURE_2D, id)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
return id
And I can set the texture to use with this code
glBindTexture(GL_TEXTURE_2D, 1)
glEnable(GL_TEXTURE_2D)
My first attempt has been this:
glBindTexture(GL_TEXTURE_2D, 1)
glEnable(GL_TEXTURE_2D)
glBegin(GL_TRIANGLES)
....
glEnd()
glBindTexture(GL_TEXTURE_2D, 3)
glEnable(GL_TEXTURE_2D)
glBegin(GL_TRIANGLES)
....
glEnd()
So i render the polys twice and select a different texture each time, this seems to work in as much as calling glBindTexture(GL_TEXTURE_2D, n) will select the relivant texture and it will render but there is no blending going on per se, i just see the last selected texture in the render. I've tried adding glEnable(GL_BLEND), but that doesn't seem to do anything.
What I would like to do is to add pixels of the two passes together
How would I go about this?
Are you sure that you need multiple passes? Here is an example of plain old OpenGL multitexturing:
import pygame
from pygame.locals import *
import numpy
import numpy.linalg
from OpenGL.GL import *
from OpenGL.GL.shaders import *
RESOLUTION = (800,600)
POSITIONS = numpy.array([[-1.0, -1.0], [+1.0, -1.0], [-1.0, +1.0], [+1.0, +1.0]], dtype=numpy.float32)
TEXCOORDS = numpy.array([[0.0, 1.0], [1.0, 1.0], [0.0, 0.0], [1.0, 0.0]], dtype=numpy.float32)
from PIL import Image
tex0 = 0
tex1 = 0
def loadTexture(path):
img = Image.open(path)
img = img.convert("RGBA")
img_data = numpy.array(list(img.getdata()), numpy.int8)
texture = glGenTextures(1)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glBindTexture(GL_TEXTURE_2D, texture)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.size[0], img.size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data)
return texture
def init():
global tex0
global tex1
tex0 = loadTexture("texture0.png")
tex1 = loadTexture("texture1.png")
glViewport(0, 0, *RESOLUTION)
aspect = RESOLUTION[0]/float(RESOLUTION[1])
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-aspect, +aspect, -1.0, +1.0, -1.0, +1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0, 0.0, 0.0, 1.0);
glVertexPointer(2, GL_FLOAT, 0, POSITIONS);
glEnableClientState(GL_VERTEX_ARRAY);
glClientActiveTexture(GL_TEXTURE0);
glTexCoordPointer(2, GL_FLOAT, 0, TEXCOORDS);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glClientActiveTexture(GL_TEXTURE1);
glTexCoordPointer(2, GL_FLOAT, 0, TEXCOORDS);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
def draw():
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex0);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex1);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
def main():
pygame.init()
screen = pygame.display.set_mode(RESOLUTION, OPENGL | DOUBLEBUF)
init()
while True:
for event in pygame.event.get():
if event.type == QUIT: return
draw()
pygame.display.flip()
if __name__ == "__main__":
main()
Have a look at Texture Combiners, they may enable the type of effect you are looking for. Of course, the state of the art for this kind of thing nowadays are shaders.

Categories

Resources