Nevada
2007.02.19, 09:19 PM
I'm having some trouble switching textures with glBindTexture() in PyOpenGL. When I load the texture image, I bind it there and only there. If I do it anywhere else, I have problems. This would work fine if I only needed one texture, but, of course, I need more. Whenever I put glBindTexture() in my render loop (even if it's just one), the program locks up before even swapping the first buffer. I really don't know why it's doing this. Could someone help me out?
import os, sys
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import pygame
from pygame.locals import *
import GLTexture
# The lower right corners of every tile
TILE_POS = (
# Bottom Row
(-2.0, 0.0, 2.0), (-1.0, 0.0, 2.0), (0.0, 0.0, 2.0), (1.0, 0.0, 2.0),
# Bottom Middle Row
(-2.0, 0.0, 1.0), (-1.0, -1.0, 1.0), (0.0, -1.0, 1.0), (1.0, 0.0, 1.0),
# Top Middle Row
(-2.0, 0.0, 0.0), (-1.0, -1.0, 0.0), (0.0, -1.0, 0.0), (1.0, 0.0, 0.0),
# Top Row
(-2.0, 0.0, -1.0), (-1.0, 0.0, -1.0), (0.0, 0.0, -1.0), (1.0, 0.0, -1.0),
# Bottom Panels
(-1.0, 0.0, 2.0), (0.0, 0.0, 2.0),
# Left Panels
(-1.0, 0.0, 1.0), (-1.0, 0.0, 0.0),
# Top Panels
(-1.0, -1.0, 0.0), (0.0, -1.0, 0.0),
# Right Panels
(1.0, -1.0, 0.0), (1.0, -1.0, 1.0)
)
# These points tell the tile which direction to "step" in when rendering the tile.
# It can be used to find the normal and it's used to orient the tile.
TILE_EDGES = (
# Bottom Row
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
# Bottom Middle Row
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
# Top Middle Row
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
# Top Row
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
# Bottom Panels
((0.0, -1.0, 0.0), (1.0, 0.0, 0.0)),
((0.0, -1.0, 0.0), (1.0, 0.0, 0.0)),
# Left Panels
((0.0, 0.0, 1.0), (0.0, -1.0, 0.0)),
((0.0, 0.0, 1.0), (0.0, -1.0, 0.0)),
# Top Panels
((0.0, 1.0, 0.0), (1.0, 0.0, 0.0)),
((0.0, 1.0, 0.0), (1.0, 0.0, 0.0)),
# Right Panels
((0.0, 0.0, 1.0), (0.0, 1.0, 0.0)),
((0.0, 0.0, 1.0), (0.0, 1.0, 0.0))
)
# The app
class Table:
def init(self):
self.camerax_rot = 45.0
self.cameray_rot = 0.0
self.cameraz_rot = 0.0
self.camerax = 0.0
self.cameray = 0.0
self.cameraz = -10.0
self.texture1 = 0
self.texture2 = 0
self.panelList = 0
self.pixelList = 0
def loadTextures(self):
# Load Textures
self.texture2 = GLTexture.loadTexture("pixel.tga", 8, 8)
self.texture1 = GLTexture.loadTexture("texture.tga", 256, 256)
# Here is where it actually works
#glBindTexture( GL_TEXTURE_2D, self.texture1 )
def update(self):
# Rotate the Camera
self.cameray_rot = self.cameray_rot + .1
def render(self):
a = 1.0
s = .05
tiles = zip(TILE_POS, TILE_EDGES)
# Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
# Orient the camera
glLoadIdentity()
glTranslatef(self.camerax, self.cameray, self.cameraz)
glRotatef(self.camerax_rot, 1, 0, 0)
glRotatef(self.cameray_rot, 0, 1, 0)
glRotatef(self.cameraz_rot, 0, 0, 1)
# The first culprit
glBindTexture( GL_TEXTURE_2D, self.texture1 )
# Draw the tiles
glBegin(GL_QUADS)
# Render all the tiles
for tile in tiles:
pos, edges = tile
step1, step2 = edges
x, y, z = pos
x1, y1, z1 = step1
x2, y2, z2 = step2
glColor3f(1.0, 1.0, 1.0)
glTexCoord2d(0.0, 0.0)
glVertex3f(x, y, z)
glTexCoord2d(0.0, 1.0)
glVertex3f(x + x1, y + y1, z + z1)
glTexCoord2d(1.0, 1.0)
glVertex3f(x + x1 + x2, y + y1 + y2, z + z1 + z2)
glTexCoord2d(1.0, 0.0)
glVertex3f(x + x2, y + y2, z + z2)
# The program never even reaches here
glBindTexture( GL_TEXTURE_2D, self.texture2 )
for tile in tiles:
pos, edges = tile
step1, step2 = edges
x, y, z = pos
x1, y1, z1 = step1
x2, y2, z2 = step2
glColor3f(0.0, 0.0, 1.0)
glVertex3f(x, y+.01, z)
glVertex3f(x + s*x1, y + s*y1+.01, z + s*z1)
glVertex3f(x + s*x1 + s*x2, y + s*y1 + s*y2+.01, z + s*z1 + s*z2)
glVertex3f(x + s*x2, y + s*y2+.01, z + s*z2)
a = a - 0.75/24
glEnd()
def main():
app = Table()
app.init()
# Initialize PyGame and OpenGL stuff
pygame.init()
pygame.display.set_mode((800, 600), OPENGL | DOUBLEBUF)
glEnable(GL_DEPTH_TEST)
glEnable(GL_TEXTURE_2D)
glMatrixMode(GL_PROJECTION)
gluPerspective(43.0, 800/600, 0.1, 100.0)
app.loadTextures()
# The event loop
while True:
event = pygame.event.poll()
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
break
app.update()
app.render()
pygame.display.flip()
pygame.time.wait(1)
if __name__ == '__main__': main()
import os, sys
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import pygame
from pygame.locals import *
import GLTexture
# The lower right corners of every tile
TILE_POS = (
# Bottom Row
(-2.0, 0.0, 2.0), (-1.0, 0.0, 2.0), (0.0, 0.0, 2.0), (1.0, 0.0, 2.0),
# Bottom Middle Row
(-2.0, 0.0, 1.0), (-1.0, -1.0, 1.0), (0.0, -1.0, 1.0), (1.0, 0.0, 1.0),
# Top Middle Row
(-2.0, 0.0, 0.0), (-1.0, -1.0, 0.0), (0.0, -1.0, 0.0), (1.0, 0.0, 0.0),
# Top Row
(-2.0, 0.0, -1.0), (-1.0, 0.0, -1.0), (0.0, 0.0, -1.0), (1.0, 0.0, -1.0),
# Bottom Panels
(-1.0, 0.0, 2.0), (0.0, 0.0, 2.0),
# Left Panels
(-1.0, 0.0, 1.0), (-1.0, 0.0, 0.0),
# Top Panels
(-1.0, -1.0, 0.0), (0.0, -1.0, 0.0),
# Right Panels
(1.0, -1.0, 0.0), (1.0, -1.0, 1.0)
)
# These points tell the tile which direction to "step" in when rendering the tile.
# It can be used to find the normal and it's used to orient the tile.
TILE_EDGES = (
# Bottom Row
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
# Bottom Middle Row
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
# Top Middle Row
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
# Top Row
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
((0.0, 0.0, 1.0), (1.0, 0.0, 0.0)),
# Bottom Panels
((0.0, -1.0, 0.0), (1.0, 0.0, 0.0)),
((0.0, -1.0, 0.0), (1.0, 0.0, 0.0)),
# Left Panels
((0.0, 0.0, 1.0), (0.0, -1.0, 0.0)),
((0.0, 0.0, 1.0), (0.0, -1.0, 0.0)),
# Top Panels
((0.0, 1.0, 0.0), (1.0, 0.0, 0.0)),
((0.0, 1.0, 0.0), (1.0, 0.0, 0.0)),
# Right Panels
((0.0, 0.0, 1.0), (0.0, 1.0, 0.0)),
((0.0, 0.0, 1.0), (0.0, 1.0, 0.0))
)
# The app
class Table:
def init(self):
self.camerax_rot = 45.0
self.cameray_rot = 0.0
self.cameraz_rot = 0.0
self.camerax = 0.0
self.cameray = 0.0
self.cameraz = -10.0
self.texture1 = 0
self.texture2 = 0
self.panelList = 0
self.pixelList = 0
def loadTextures(self):
# Load Textures
self.texture2 = GLTexture.loadTexture("pixel.tga", 8, 8)
self.texture1 = GLTexture.loadTexture("texture.tga", 256, 256)
# Here is where it actually works
#glBindTexture( GL_TEXTURE_2D, self.texture1 )
def update(self):
# Rotate the Camera
self.cameray_rot = self.cameray_rot + .1
def render(self):
a = 1.0
s = .05
tiles = zip(TILE_POS, TILE_EDGES)
# Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
# Orient the camera
glLoadIdentity()
glTranslatef(self.camerax, self.cameray, self.cameraz)
glRotatef(self.camerax_rot, 1, 0, 0)
glRotatef(self.cameray_rot, 0, 1, 0)
glRotatef(self.cameraz_rot, 0, 0, 1)
# The first culprit
glBindTexture( GL_TEXTURE_2D, self.texture1 )
# Draw the tiles
glBegin(GL_QUADS)
# Render all the tiles
for tile in tiles:
pos, edges = tile
step1, step2 = edges
x, y, z = pos
x1, y1, z1 = step1
x2, y2, z2 = step2
glColor3f(1.0, 1.0, 1.0)
glTexCoord2d(0.0, 0.0)
glVertex3f(x, y, z)
glTexCoord2d(0.0, 1.0)
glVertex3f(x + x1, y + y1, z + z1)
glTexCoord2d(1.0, 1.0)
glVertex3f(x + x1 + x2, y + y1 + y2, z + z1 + z2)
glTexCoord2d(1.0, 0.0)
glVertex3f(x + x2, y + y2, z + z2)
# The program never even reaches here
glBindTexture( GL_TEXTURE_2D, self.texture2 )
for tile in tiles:
pos, edges = tile
step1, step2 = edges
x, y, z = pos
x1, y1, z1 = step1
x2, y2, z2 = step2
glColor3f(0.0, 0.0, 1.0)
glVertex3f(x, y+.01, z)
glVertex3f(x + s*x1, y + s*y1+.01, z + s*z1)
glVertex3f(x + s*x1 + s*x2, y + s*y1 + s*y2+.01, z + s*z1 + s*z2)
glVertex3f(x + s*x2, y + s*y2+.01, z + s*z2)
a = a - 0.75/24
glEnd()
def main():
app = Table()
app.init()
# Initialize PyGame and OpenGL stuff
pygame.init()
pygame.display.set_mode((800, 600), OPENGL | DOUBLEBUF)
glEnable(GL_DEPTH_TEST)
glEnable(GL_TEXTURE_2D)
glMatrixMode(GL_PROJECTION)
gluPerspective(43.0, 800/600, 0.1, 100.0)
app.loadTextures()
# The event loop
while True:
event = pygame.event.poll()
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
break
app.update()
app.render()
pygame.display.flip()
pygame.time.wait(1)
if __name__ == '__main__': main()