Related
I want to draw a house with Python OpenGL.
It should look like this:
filled:
unfilled:
Further information: The house should rotate via key input around the x- and y-axis. By pressing the F-key, it should switch between filled and unfilled mode.
My Problem: The window opens up, but I don't see anything on it. I'm not sure what exactly is wrong here or what I'm missing for drawing. Can somebody explain it?
This is my code:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
# from OpenGL.GL import shaders
# from OpenGL.arrays import vbo
# import numpy as np
# general all vertices needed
vertices = (
# front wall
(2, 0, -1, 2, 1, -1, -2, 1, -1, -2, 0, -1),
# back wall
(2, 0, 1, 2, 1, 1, -2, 1, 1, -2, 0, 1, 2, 2, 0, -2, 2, 0),
# roof ridge
(2.2, 2, 0, -2.2, 2, 0),
# roof edges
(2.2, 0.9, -1.1, -2.2, 0.9, -1.1, 2.2, 0.9, 1.1, -2.2, 0.9, 1.1),
# chimney
(1, 0, 0.5, 1.25, 0, 0.5, 1.25, 0, 0.25, 1, 0, 0.25,
1, 2.5, 0.5, 1.25, 2.5, 0.5, 1.25, 2.5, 0.25, 1, 2.5, 0.25)
)
def DrawHouse():
# house
# back wall
glBegin(GL_LINES)
glColor3f(0.5, 0.5, 0.5)
glVertex3f(2, 0, -1) # top left
glVertex3f(2, 1, -1) # top right
glVertex3f(-2, 1, -1) # bottom right
glVertex3f(-2, 0, -1) # bottom left
glEnd()
# front wall
glBegin(GL_LINES)
glColor3f(0.5, 0.5, 0.5)
glVertex3f(2, 0, 1) # top left
glVertex3f(2, 1, 1) # top right
glVertex3f(-2, 1, 1) # bottom right
glVertex3f(-2, 0, 1) # bottom right
glVertex3f(2, 2, 0) # rooftop
glVertex3f(-2, 2, 0) # rooftop
glEnd()
# roof
glBegin(GL_LINES)
# roof ridge
glColor3f(0, 0, 0)
glVertex3f(2.2, 2, 0)
glVertex3f(-2.2, 2, 0)
glEnd()
# roof edges
glBegin(GL_TRIANGLES)
glColor3f(0, 0, 0)
glVertex3f(2.2, 0.9, -1.1)
glVertex3f(-2.2, 0.9, -1.1)
glVertex3f(2.2, 0.9, 1.1)
glVertex3f(-2.2, 0.9, 1.1)
glEnd()
# chimney
glBegin(GL_POLYGON)
glColor3f(1, 1, 0)
glVertex3f(1, 0, 0.5)
glVertex3f(1.25, 0, 0.5)
glVertex3f(1.25, 0, 0.25)
glVertex3f(1, 0, 0.25)
glVertex3f(1, 2.5, 0.5)
glVertex3f(1.25, 2.5, 0.5)
glVertex3f(1.25, 2.5, 0.25)
glVertex3f(1, 2.5, 0.25)
glEnd()
def init():
# Switch on z-buffer for calculation of hidden surfaces
glEnable(GL_DEPTH_TEST)
# Display front and back of polygons as border lines only
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
def reshape():
# initialize projection matrix, to 60 degrees horizontal field of view
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(60.0, 1.0, 1.0, 200.0) # angle, aspect, near and far clip
# make modelview matrix the current matrix again
glMatrixMode(GL_MODELVIEW)
def main():
pygame.init()
window = (800, 800)
display = pygame.display.set_mode(window, DOUBLEBUF | OPENGL)
pygame.display.set_caption('Haus')
clock = pygame.time.Clock()
# GLUT.glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE) # request render context with z-buffer, doublebuffer for rgb mode
gluOrtho2D(0, 800, 0, 800)
rotX = 0.0
rotY = 0.0
polygonMode = GL_LINE
while True:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
keys = pygame.key.get_pressed() # checking pressed keys
if keys[pygame.K_d]: # The keys 'a' and 'd' should rotate the house around the y-axis
rotX -= 5
if keys[pygame.K_a]:
rotX += 5
if keys[pygame.K_w]: # the keys 'w' and 'd' should rotate the house around the x-axis
rotY -= 5
if keys[pygame.K_d]:
rotY += 5
if keys[pygame.K_f]: # Key 'f' is to switch between wireframe and filled surfaces
if polygonMode is GL_FILL:
polygonMode = GL_LINE
else:
polygonMode = GL_FILL
# Switch polygon display between outline and filled
glPolygonMode(GL_FRONT_AND_BACK, polygonMode)
# GLUT.glutPostRedisplay() # render image again
# remove Framebuffer and Z-Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# initialise Modelview Matrix
glLoadIdentity()
# execute Modeltransformation
glTranslatef(0, 0, -3) # shift by -3 in z-direction
glRotatef(rotY, 0, 1, 0) # rotate y-achse
glRotatef(rotX, 1, 0, 0) # rotate x-achse
glTranslatef(-0.5, -0.5, -0.5) # shift by -0.5 in all directions
# copy Back-Buffer in Front-Buffer
# swapBuffers()
DrawHouse()
# Show the screen
pygame.display.flip()
# callback functions
# glutReshapeFunc(reshape)
if __name__ == "__main__":
main()
The coordinates of your geometry are in range [-2.2, 2.2]. However you set an orthographic projection in range [0, 800]:
gluOrtho2D(0, 800, 0, 800)
Actually there are 3 reasons why you don't see anything
That results in only a few pixels being drawn on the bottom left of the screen.
gluOrtho2D creates an orthographic projection with a near plane of -1 and a far plane of 1. This will clip your geometry.
You need to choose the GL_PROJECTION for the current matrix. Later in your code, the model view matrix is set to the identity matrix with glLoadIdentity.
def main():
pygame.init()
window = (800, 800)
display = pygame.display.set_mode(window, DOUBLEBUF | OPENGL)
pygame.display.set_caption('Haus')
clock = pygame.time.Clock()
# gluOrtho2D(0, 800, 0, 800)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-5, 5, -5, 5, -5, 5)
glMatrixMode(GL_MODELVIEW)
# [...]
Alternatively you can use the perspective projection from the reshape function:
def main():
pygame.init()
window = (800, 800)
display = pygame.display.set_mode(window, DOUBLEBUF | OPENGL)
pygame.display.set_caption('Haus')
clock = pygame.time.Clock()
# gluOrtho2D(0, 800, 0, 800)
reshape()
# [...]
I am trying to make a game that looks like a retro space shooter, where the lines are 3d wireframe.
To achieve 3D in Python, I am using pygame for the window and PyOpenGL to render the objects.
I want to get OpenGL to render to a surface (not the display surface) and then via pygame scale the surface up and render it onto the display surface. This will hopefully give the effect of a low resolution window while being fit to work on modern screens.
The thing that is stopping me from doing this is that OpenGL renders to the display surface, and I cannot find any option that allows me to change what surface it draws to.
So the process should be: OpenGL renders to small surface, pygame scales surface and draws that to the display screen, repeat.
Here is my current code:
def main():
pygame.init()
display = (500,500)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL) # Create display window
gluPerspective(70, (display[0]/display[1]), 0.1, 50.0) # Setup view
glTranslatef(0.0,0.0, -5) # Set view position
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: # If X is clicked
pygame.quit() # Close
quit()
glRotatef(1, 3, 1, 1) # Rotates view
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) # Clears the screen
Cube() # Renders the cube onto the screen
pygame.display.flip() # Updates the display
pygame.time.wait(10)
main()
I've tried creating a surface with exactly the same settings as the display but OpenGL STILL won't render it to that surface.
You have to create a Framebuffer Object (before the main loop) with a resoultion smaller than the window resolution. See also Framebuffer Object Extension Examples:
fb_size = [50, 50]
depth_buffer_obj = glGenRenderbuffers(1)
glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer_obj)
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, fb_size[0], fb_size[1])
color_buffer_obj = glGenRenderbuffers(1)
glBindRenderbuffer(GL_RENDERBUFFER, color_buffer_obj)
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, fb_size[0], fb_size[1])
fb_obj = glGenFramebuffers(1)
glBindFramebuffer(GL_FRAMEBUFFER, fb_obj)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_buffer_obj)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color_buffer_obj)
status = glCheckFramebufferStatus(GL_FRAMEBUFFER)
if status != GL_FRAMEBUFFER_COMPLETE:
print("incomplete framebuffer object")
glBindFramebuffer(GL_FRAMEBUFFER, 0)
Se the size of the viewport to the size of the framebuffer, clear the frame buffer and render the cube to the Framebuffer:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glBindFramebuffer(GL_FRAMEBUFFER, fb_obj)
glViewport (0, 0, fb_size[0], fb_size[1])
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glRotatef(1, 3, 1, 1)
Cube()
Set the viewport size to the size of the window and use glBlitFramebuffer with the filter parameter GL_NEAREST to copy the pixels from the named framebuffer object to the default framebuffer. Note it is not necessary to clear the default framebuffer, because it is completely overwritten:
while True:
# .....
glBindFramebuffer(GL_READ_FRAMEBUFFER, fb_obj)
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
glViewport(0, 0, 500, 500)
glBlitFramebuffer(
0, 0, fb_size[0], fb_size[1],
0, 0, 500, 500,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,
GL_NEAREST)
pygame.display.flip()
pygame.time.wait(10)
Note, the line glBindFramebuffer(GL_READ_FRAMEBUFFER, fb_obj) is not necessary, because fb_obj is bound for read and draw at this point.
If your system down't support glBlitFramebuffer, the you can create a framebuffer with a texture attached to its color plane:
fb_size = [50, 50]
depth_buffer_obj = glGenRenderbuffers(1)
glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer_obj)
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, fb_size[0], fb_size[1])
#color_buffer_obj = glGenRenderbuffers(1)
#glBindRenderbuffer(GL_RENDERBUFFER, color_buffer_obj)
#glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, fb_size[0], fb_size[1])
color_tex_obj = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, color_tex_obj)
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_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fb_size[0], fb_size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, None)
fb_obj = glGenFramebuffers(1)
glBindFramebuffer(GL_FRAMEBUFFER, fb_obj)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_buffer_obj)
#glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color_buffer_obj)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color_tex_obj, 0)
status = glCheckFramebufferStatus(GL_FRAMEBUFFER)
if status != GL_FRAMEBUFFER_COMPLETE:
print("incomplete framebuffer object")
glBindFramebuffer(GL_FRAMEBUFFER, 0)
glBindTexture(GL_TEXTURE_2D, 0)
Render to the framebuffer and draw a quad with the the texture over the entire window to the default framebuffer:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glBindFramebuffer(GL_FRAMEBUFFER, fb_obj)
glViewport (0, 0, fb_size[0], fb_size[1])
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glRotatef(1, 3, 1, 1)
gluSphere(gluNewQuadric( ), 2.0, 32, 32)
glBindFramebuffer(GL_FRAMEBUFFER, 0)
glViewport(0, 0, 500, 500)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
#glBlitFramebuffer(
# 0, 0, fb_size[0], fb_size[1],
# 0, 0, 500, 500,
# GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,
# GL_NEAREST)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, color_tex_obj)
glBegin(GL_TRIANGLE_FAN)
glTexCoord2f(0,0)
glVertex2f(-1,-1)
glTexCoord2f(1,0)
glVertex2f(1,-1)
glTexCoord2f(1,1)
glVertex2f(1,1)
glTexCoord2f(0,1)
glVertex2f(-1,1)
glEnd()
glDisable(GL_TEXTURE_2D)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
pygame.display.flip()
pygame.time.wait(10)
I load a Candide3D model self.drawFace(vertices) and load a hat 3D model glCallList(obj.gl_list), but when I change the z position of the hat, it never stay on the Candidie3 model.
I also changed the gluPerspective(50,self.w/float(self.h), 1 , 250.0) camera matrix but, in almost every case the hat render in front of Candide3 model.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
setOrtho(self.w, self.h)
glEnable(GL_DEPTH_TEST)
glEnable(GL_TEXTURE_2D)
glDepthMask(GL_FALSE)
self.drawFace(vertices)
glDepthMask( GL_TRUE )
######## render next model ##########
glLightfv(GL_LIGHT0, GL_POSITION, (-40, 50, 100, 0.0))
glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1.0))
glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1.0))
glEnable(GL_LIGHT0)
glEnable(GL_LIGHTING)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_SMOOTH)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(50,self.w/float(self.h), 1 , 250.0)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_MODELVIEW)
glBindTexture(GL_TEXTURE_2D, self.texture_image2)
glTranslate(self.tx,self.ty,-40)
self.tx,self.ty=self.track(shape2D)
glRotate(ry, 1, 0, 0)
glPushMatrix()
glCallList(obj.gl_list)
glPopMatrix()
glEnable(GL_TEXTURE_2D)
I am creating a first-person view RPG and I want to rotate the camera in PyOpenGL when I move the mouse (just like some other games like Minecraft). What function can I use to do this and how?
I tried to use gluLookAt() but I don't understand how it works although I went through different sources. I don't even know if it can help.
import sys,pygame
from OpenGL.GL import *
from OpenGL.GLU import *
cmddown = False
#...
keypress = pygame.key.get_pressed()#Move using WASD
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)
mouse_movement = pygame.mouse.get_rel()#Get mouse event
#This is where the "look around" should be happen
pygame.display.flip()
You can use glRotate to rotate around an axis, by an amount which is given by the relative mouse movement (pygame.mouse.get_rel()):
mouseMove = pygame.mouse.get_rel()
glRotatef(mouseMove[0]*0.1, 0.0, 1.0, 0.0)
But that won't satisfy you, because the solution won't work any more, if the mouse leaves the window.
You've to center the mouse in the middle of the screen by pygame.mouse.set_pos() in every frame. Get the mouse movement by the pygame.MOUSEMOTION event. e.g.:
# 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)
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 event.type == pygame.MOUSEMOTION:
mouseMove = [event.pos[i] - displayCenter[i] for i in range(2)]
if not paused:
pygame.mouse.set_pos(displayCenter)
Note, that operations like glRotate and glTranslate set a matrix and multiply the current matrix by the new matrix.
currentMatrix = currentMatrix * newMatrix
That is perfect for model animations and transformations, but it is the wrong way for a first person movement, where the camera position and point of view has to be changed.
viewMatrix = viewTransformMatrix * viewMatrix
To do operations like that glGetFloatv(GL_MODELVIEW_MATRIX) and glMultMatrixf.
Initialize the view matrix by gluLookAt and load the view matrix to a variable (viewMatrix) by glGetFloatv(GL_MODELVIEW_MATRIX), before the main loop.
In the main loop for each frame:
load the Identity matrix (glLoadIdentity)
do the new transformations to the the view (glRotatef, glTranslatef)
multiply the current view matrix by viewMatrix (glMultMatrixf)
load the new view matrix to viewMatrix for the next frame
glMatrixMode(GL_MODELVIEW)
gluLookAt(0, -8, 0, 0, 0, 0, 0, 0, 1)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glLoadIdentity()
# [...]
run = True
while run:
# [...]
# init the view matrix
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)
# apply the roation
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)
For the look up und down, you've to apply a final rotation around the x axis. The pivot of the rotation depends on the point of view.
The angle has to be summed up, and the rotation must be applied after the view matrix was else the movement would change the ("height") level dependent on the angle.
viewMatrix = viewTransformMatrix * viewMatrix
finlalMatrix = lookUpDownMatrix * viewMatrix
To do so, you've to ally the up and down rotation matrix and multiply it by viewMatrix
up_down_angle = 0.0
run = True
while run:
# [...]
# 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()
# calculate new `viewMatrix`
# [...]
# apply view matrix
glPopMatrix()
glMultMatrixf(viewMatrix)
See the following example program, which demonstrates the process.
Note, the program keeps the mouse in the center of the window, so you can't "move" the mouse any more.
Therefore the application can be stopped by ESC or return.
The application can be paused by pause or p. When the application is paused, the mouse is not centered to the window.
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import math
pygame.init()
display = (400, 300)
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])
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)
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)
# 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()
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'm trying to use the OpenGL module with Python.
Here is my source:
pygame.init()
screen = pygame.display.set_mode((800, 600), HWSURFACE|OPENGL|DOUBLEBUF)
def init():
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_FLAT)
glClearColor(1.0, 1.0, 1.0, 0.0)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glLight(GL_LIGHT0, GL_POSITION, (0, 1, 1, 0))
def draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glColor3fv((25, 123, 180))
glBegin(GL_TRIANGLES)
glVertex3f( 0.0, 10.0, 0.0)
glVertex3f(-10.0,-10.0, 0.0)
glVertex3f( 10.0,-10.0, 0.0)
glEnd()
def run():
init()
while True:
draw()
pygame.display.flip()
run()
Can anyone see what is wrong? I'm just trying to draw a simple 3 pointed vertex, yet nothing shows up on the screen. Occasionally I get a bright pink screen. I'm confident it's a basic error.
You need to setup view and projection matrices.
http://www.opengl.org/resources/faq/technical/transformations.htm#tran0090