I'm trying to learn PyOpenGL, and I understand some of the concept, except the "pipe" "|" character connecting things. For example, this is the code for a cube:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
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)
)
def Cube():
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verticies[vertex])
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,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)
Cube()
pygame.display.flip()
pygame.time.wait(10)
main()
I'm not exactly sure what the GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT thing means, or the DOUBLEBUF|OPENGL
I know what double buffering is, but I still do not fully understand what this or the other thing means.
They are actually just integer constants. This is a convenient way of getting input from user. Pipe operator (|
) is a bitwise OR of integers. For example 15 | 128 = 143, i.e. 00001111 | 10000000 = 10001111 in binary.
Here is how it actually works:
>>> from OpenGL.GL import *
>>> type(GL_COLOR_BUFFER_BIT)
OpenGL.constant.IntConstant
>>> int(GL_COLOR_BUFFER_BIT)
16384
>>> bin(GL_COLOR_BUFFER_BIT)
'0b100000000000000'
>>> int(GL_DEPTH_BUFFER_BIT)
256
>>> bin(GL_DEPTH_BUFFER_BIT)
'0b100000000'
>>> int(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
16640
>>> bin(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
'0b100000100000000'