0

I have a problem with PyOpenGL. this is my code:

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

from math import *


a=[[cos(0.5*pi/180),sin(0.5*pi/180),0],
   [-sin(0.5*pi/180),cos(0.5*pi/180),0],
   [0,0,1]]
def zarb_matris(p,b):
    c=[b[0][0]*p[0][0]+b[0][1]*p[1][0]+b[0][2]*p[2][0],
       b[1][0]*p[0][0]+b[1][1]*p[1][0]+b[1][2]*p[2][0],
       b[2][0]*p[0][0]+b[2][1]*p[1][0]+b[2][2]*p[2][0]]
    return c

verticies= [
    [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,1),
    (0,3),
    (0,4),
    (2,1),
    (2,3),
    (2,7),
    (6,3),
    (6,4),
    (6,7),
    (5,1),
    (5,4),
    (5,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)
    )

##    (0,3,2,1),
##    (6,7,2,3),
##    (4,5,7,6),
##    (4,0,1,5),
##    (5,1,2,7),
##    (4,0,3,6)
##    )
colors = (
    (0.9,0,0),#red
    (0,1,0),#green
    (0.75,0.38,0),#orange
    (0,0,1),#blue
    (1,1,0),#yellow
    (1,1,1),   
    (1,0,0),
    (0,1,0),
    (0.75,0.38,0),
    (0,0,1),
    (1,1,0),
    (0.9,1,1)
    )


def Cube():
    global verticies
    glBegin(GL_QUADS)
    x = 0
    for surface in surfaces:
        x+=1

        for vertex in surface:
            glColor3fv(colors[x])
            glVertex3fv(verticies[vertex])
    glEnd()
    glBegin(GL_LINES)
    glColor3fv((1,1,1))
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()


def main():
    global s
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

    glTranslatef(1,1, -10)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        glRotatef(1, 12, 1, 1)
        for i in range(8):
            s=[]
            for j in verticies[i]:
                s.append([j])
            k=zarb_matris(s,a)
            verticies[i]=k
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)


main()

It just shows a simple cube with colored faces. The colors are not solid and you can see through them. What should I do to fix this problem?

Any help would be greatly appreciated.

p.h
  • 3
  • 2

1 Answers1

0

I can see from your commented out part that you may have suspected the winding order of your quads as the culprit, but that's not the actual problem here. However, it's still a good practice to use the winding order together with back face culling to reduce the unnecessary work the graphics card has to perform. (With all values at their defaults, your second commented set of surfaces is the correct one, apart from the last surface which you still need to turn around.)

The real problem here is that you need to enable features - even very basic ones that you intuitively assume to be there - before they will be used. After set_mode() put this:

glEnable(GL_DEPTH_TEST)

Without that, the primitives are simply drawn in the order they come in without looking at the depth buffer (it's just ignored). Thus some faces and some lines that actually lie behind others will unexpectedly be drawn on top of them due to be rendered later.

Note that you should also enable back face culling (after commenting the first set and uncommenting the second set of surfaces), as it won't be used either by default:

glEnable(GL_CULL_FACE)

Note also that if you didn't have lines (you can try by commenting out that part of your code), the depth test wouldn't strictly be required, as a cube is a convex shape and back face culling alone would do the trick (this won't work for concave shapes or complex scenes, obviously).

blubberdiblub
  • 4,085
  • 1
  • 28
  • 30