Reputation: 23
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
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))
edges = ((0, 4, 3), (6, 4, 3), (1, 3, 2), (5, 7, 2), (0, 4, 1), (7, 1, 4),
(3,6, 2), (5, 1, 6), (0, 3, 1), (2, 3, 5), (6, 5, 4), (7, 2, 4))
def draw_cube():
glBegin(GL_TRIANGLES)
for edge in edges:
for index in edge:
glVertex3fv(vertices[index])
glEnd()
def main():
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0, 0, -5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
draw_cube()
pygame.display.flip()
pygame.time.wait(20)
main()
Output:
Desired Output:
Upvotes: 2
Views: 736
Reputation: 1
Hi so check out this link Python Color Constants Module then create a variable
pinkcolor = (255,0,255)
or insert it wherever u build the triangle
and then lets say its a platform:
pinkPlatform = pygame.draw.rect(screen,pinkcolor,
pygame.Rect(pinkPlatformX, pinkPlatformY, pinkPlatformWidth, pinkPlatformHeight))
it should work i think
Upvotes: 0
Reputation: 211126
Define an list of colors with one color for side of the cube:
colors = [(1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 1, 1), (0, 0, 1), (1, 0, 1)]
Use glColor3fv
to set the color:
def draw_cube():
glBegin(GL_TRIANGLES)
for faceIndex, face in enumerate(faces):
glColor3fv(colors[faceIndex // 2])
for index in face:
glVertex3fv(vertices[index])
glEnd()
Enable the Depth Test:
glEnable(GL_DEPTH_TEST)
See also PyGame and OpenGL immediate mode
Note, there is also a problem with your face indices. Minimal correct example:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
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)]
faces = [(0,1,2), (0,2,3), (5,4,7), (5,7,6), (4,0,3), (4,3,7),
(1,5,6), (1,6,2), (4,5,1), (4,1,0), (3,2,6), (3,6,7)]
colors = [(1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 1, 1), (0, 0, 1), (1, 0, 1)]
def draw_cube():
glBegin(GL_TRIANGLES)
for faceIndex, face in enumerate(faces):
glColor3fv(colors[faceIndex // 2])
for index in face:
glVertex3fv(vertices[index])
glEnd()
def main():
pygame.init()
display = (300, 200)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0, 0, -5)
glEnable(GL_DEPTH_TEST)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
draw_cube()
pygame.display.flip()
pygame.time.wait(20)
main()
Upvotes: 1