OpenGL VBO VAO EBO can run without error but no graphics - python

This is my code:
block_VAO=0
draw=False
block_EBO_buffer_len=0
def print_blocks(x:int,y:int,z:int):
global draw,block_VAO,block_EBO_buffer_len
if not draw:
block_point_buffer=[]
block_color_buffer=[]
block_EBO_buffer=[]
block_point_buffer+=[x-0.5,y+0.5,z-0.5,#V0
x+0.5,y+0.5,z-0.5,#V1
x+0.5,y-0.5,z-0.5,#V2
x-0.5,y-0.5,z-0.5,#V3
x-0.5,y+0.5,z+0.5,#V4
x+0.5,y+0.5,z+0.5,#V5
x+0.5,y-0.5,z+0.5,#V6
x-0.5,y-0.5,z+0.5]#V7
block_EBO_buffer+=[0,1,5,4,
3,2,6,7,
0,3,7,4,
1,2,6,5,
0,1,2,3,
4,5,6,7]
#ADD
block_color_buffer+=[1.0,0.0,1.0,1.0]*24
color_EBO_buffer+=[0]*24
#ADD END
block_VBO=glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,block_VBO)
a=numpy.array(block_point_buffer,dtype='float32')
glBufferData(GL_ARRAY_BUFFER,sys.getsizeof(a),a,GL_STATIC_DRAW)
block_EBO=glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,block_EBO)
a=numpy.array(block_EBO_buffer,dtype='uint32')
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sys.getsizeof(a),a,GL_STATIC_DRAW)
block_EBO_buffer_len=len(a)
#ADD
color_VBO=glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,color_VBO)
a=numpy.array(block_color_buffer,dtype='uint32')
glBufferData(GL_ARRAY_BUFFER,sys.getsizeof(a),a,GL_STATIC_DRAW)
color_EBO=glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,color_EBO)
a=numpy.array(color_EBO_buffer,dtype='uint32')
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sys.getsizeof(a),a,GL_STATIC_DRAW)
#ADD END
block_VAO=glGenVertexArrays(1)
glBindVertexArray(block_VAO)
glBindBuffer(GL_ARRAY_BUFFER,block_VBO)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,block_EBO)
glVertexPointer(3,GL_FLOAT,0,None)
glEnableClientState(GL_VERTEX_ARRAY)
#ADD
glBindBuffer(GL_ARRAY_BUFFER,color_VBO)
glColorPointer(4,GL_FLOAT,0,None)
glEnableClientState(GL_COLOR_ARRAY)
#ADD END
glBindVertexArray(0)
draw=True
glBindVertexArray(block_VAO)
glDrawElements(GL_QUADS,block_EBO_buffer_len,GL_UNSIGNED_INT,None)
glBindVertexArray(0)
function print_blocks is in the mainloop. If I don't bind color to VAO(I mean run without new add code), it can be drawn normally.But after I bind, no graphics will appear.How can I do to make it draw normally?
I really have no thing to say now.Please!Please!Please!Please!Please!Please!

The Index Buffer (ELEMENT_ARRAY_BUFFER) binding is stored within the Vertex Array Object. When glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO) is called the element buffer object ID is stored in the currently bound Vertex Array Object. Therefore the VAO must be bound before the element buffer with glBindVertexArray(VAO).
Compared to the Index Buffer, the Vertex Buffer binding (ARRAY_BUFFER) is a global state.
Each attribute which is stated in the VAOs state vector may refer to a different ARRAY_BUFFER. When glVertexAttribPointer is called the buffer which is currently bound to the target ARRAY_BUFFER, is associated to the specified attribute index and the ID of the object is stored in the state vector of the currently bound VAO.
Therefor the VAO needs to be bound before the element buffer is bound and created.
Furthermore the type of the color attribute needs to be floating point:
a=numpy.array(block_color_buffer,dtype='uint32')
a=numpy.array(block_color_buffer,dtype='float32')
Besides that you cannot specify one mesh with multiple index buffers. See Why does OpenGL not support multiple index buffering?. You can just specify 1 array of indices.
Minimal example based on your original code:
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import numpy
rotate = [33, 40, 20]
block_VAO=0
draw=False
block_EBO_buffer_len=0
def create_blocks(x:int, y:int, z:int):
global draw, block_VAO, block_EBO_buffer_len
if draw:
return
draw = True
block_point_buffer=[]
block_color_buffer=[]
block_EBO_buffer=[]
block_point_buffer+=[x-0.5,y+0.5,z-0.5,#V0
x+0.5,y+0.5,z-0.5,#V1
x+0.5,y-0.5,z-0.5,#V2
x-0.5,y-0.5,z-0.5,#V3
x-0.5,y+0.5,z+0.5,#V4
x+0.5,y+0.5,z+0.5,#V5
x+0.5,y-0.5,z+0.5,#V6
x-0.5,y-0.5,z+0.5]#V7
block_EBO_buffer+=[0,1,5,4,
3,2,6,7,
0,3,7,4,
1,2,6,5,
0,1,2,3,
4,5,6,7]
block_color_buffer+=[1.0,0.0,1.0,1.0]*8
block_VBO=glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,block_VBO)
a=numpy.array(block_point_buffer,dtype='float32')
glBufferData(GL_ARRAY_BUFFER,sys.getsizeof(a),a,GL_STATIC_DRAW)
color_VBO=glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,color_VBO)
a=numpy.array(block_color_buffer,dtype='float32')
glBufferData(GL_ARRAY_BUFFER,sys.getsizeof(a),a,GL_STATIC_DRAW)
block_VAO=glGenVertexArrays(1)
glBindVertexArray(block_VAO)
block_EBO=glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,block_EBO)
a=numpy.array(block_EBO_buffer,dtype='uint32')
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sys.getsizeof(a),a,GL_STATIC_DRAW)
block_EBO_buffer_len=len(a)
glBindBuffer(GL_ARRAY_BUFFER,block_VBO)
glVertexPointer(3,GL_FLOAT,0,None)
glEnableClientState(GL_VERTEX_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER,color_VBO)
glColorPointer(4,GL_FLOAT,0,None)
glEnableClientState(GL_COLOR_ARRAY)
glBindVertexArray(0)
def display():
glMatrixMode(GL_MODELVIEW)
glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()
glTranslatef(0, 0, -4.5)
glRotatef(rotate[0], 1, 0.0, 0)
glRotatef(rotate[1], 0, 1, 0)
glRotatef(rotate[2], 0, 0, 1)
glScalef(1, 1, 1)
glBindVertexArray(block_VAO)
glDrawElements(GL_QUADS,block_EBO_buffer_len,GL_UNSIGNED_INT,None)
glBindVertexArray(0)
rotate[1] += 0.1
glutSwapBuffers()
glutPostRedisplay()
def reshape(width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(40.0, width / height, 0.5, 20.0)
glMatrixMode(GL_MODELVIEW)
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
glutInitWindowSize(400, 350)
glutCreateWindow(b"OpenGL Window")
create_blocks(0, 0, 0)
glClearColor(0.0, 0.0, 0.0, 0.0)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
glutDisplayFunc(display)
glutReshapeFunc(reshape)
glutMainLoop()

Related

Set orientation of object in python using quaternions with pyglet, pywavefront and OpenGL

I want to set the orientation of an object in python using quaternions. I get my quaternions periodically via a serial port (this part works). My goal is to create a program similar to the following javascript project : https://github.com/ZaneL/quaternion_sensor_3d_nodejs (but with this object and in python)
Right now I can rotate the object using the keyboard with the following code (notice the rotation is around a non-zero point):
#window.event
def on_key_press(symbol, modifiers):
glTranslated(0, 0, 200)
if symbol == key.Q:
glRotated(22,0,1,0)
if symbol == key.W:
glRotated(-22,0,1,0)
glTranslated(0, 0, -200)
But this rotation is relative and I want to set the absolute orientation (with respect to some initial orientation). And I need to use quaternions, since quaternions specify the desired orientation.
So I want to do something like this:
#window.event
def on_key_press(symbol, modifiers):
if symbol == key.Q:
q = np.array([1,0,0,0])
if symbol == key.W:
q = np.array([0,1,0,0])
#set orientation based on q
Here is my complete code:
import pyglet
import pywavefront
from pywavefront import visualization
from pyglet.gl import *
from pyglet.window import key
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
path = '../models/10475_Rocket_Ship_v1_L3.obj'
window = pyglet.window.Window(resizable=True)
window.projection = pyglet.window.Projection3D(zfar=1000)
scene = pywavefront.Wavefront(path)
#window.event
def on_draw():
# print('draw')
window.clear()
visualization.draw(scene)
#window.event
def on_key_press(symbol, modifiers):
glTranslated(0, 0, 200)
if symbol == key.Q:
glRotated(22,0,1,0)
if symbol == key.W:
glRotated(-22,0,1,0)
glTranslated(0, 0, -200)
if __name__ == "__main__":
glViewport(0, 0, 500,500)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0, 500, 0.0, 500, 0.0, 1.0)
glMatrixMode (GL_MODELVIEW)
glLoadIdentity()
glTranslated(0, 0, 100)
for _ in range(4):
glRotated(25,0,1,0)
glTranslated(35, 0, 0)
glRotated(100,0,1,0)
glTranslated(0, 0, 200)
glRotated(-100,1,0,0)
glTranslated(-100, -275, -250)
glScale(0.75, 0.75, 0.75)
glClearColor(0.85, 0.85, 0.85, 1);
pyglet.app.run()
I also want to simplify the code for setting the initial orientation and position of the object (as this one was found using trial and error). Preferably 1 or 2 operations.
I don't need to use piglet or pywavefront. As long as the object renders properly.
Edit:
I currently have the rotation working. But I want to rotate the entire model around the z-axis to compensate for a non-zero initial jaw angle(my monitor is not perfectly magnetic North of my object). My 6-axis sensor is upside down so the model is also upside down.
from squaternion import Quaternion
import numpy as np
...
q = Quaternion(q['quat_w'],q['quat_x'],q['quat_y'],q['quat_z'])
# delete current matrix and replace with copy of initialized matrix:
glPopMatrix()
glPushMatrix()
e = q.to_euler(degrees=True)
# get initial yaw angle:
global init_yaw
if init_yaw == None:
init_yaw = e[2]
print(q)
print(init_yaw)
glTranslated(0, 0, 200)
# glRotated(init_yaw,0,0,1) #this doesn't work, it rotates using euler angles and it needs to rotate around the z axis
r = np.array(q.to_rot())
r4x4 = np.array([[r[0,0],r[1,0],r[2,0],0],
[r[0,1],r[1,1],r[2,1],0],
[r[0,2],r[1,2],r[2,2],0],
[0,0,0,1]])
glMultMatrixd(r4x4)
glTranslated(0, 0, -200)
I was able to solve it.
I use the squaternion library to store the quaternion. It comes with build-in methods for quaternion multiplication and converting to rotation matrix.
The rotation matrix is 3x3 and needs to be converted to a 4x4 rotation-translation matrix and converted to Column-major order.
First the quaternion needs to be rotated to compensate for the offset in the Yaw angle.
Then the quaternion needs to be rotated around the x axis to compensate for the fact the sensor is mounted upside down.
Then the 4x4 matrix is calculated.
Then then matrix is applied (after translating and before translating back)
In order to simplify the initial rotation and translating operations (which were found using trial and error) all I had to do was print and inspect the matrix. After rounding relatively small values (<0.01) to zero I was able to find a simpler matrix. I used this code for inspection:
a = (GLdouble * 16)()
mvm = glGetDoublev(GL_MODELVIEW_MATRIX, a)
print(list(a))
array = np.array(list(a)).reshape([4,4])
print(array)
Here is my final code:
path = '../models/10475_Rocket_Ship_v1_L3.obj'
import pyglet
import pywavefront
from pywavefront import visualization
from pyglet.gl import *
from pyglet.window import key
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import serial
import json
from squaternion import Quaternion
import numpy as np
ser = serial.Serial('COM4',115200,timeout=0)
window = pyglet.window.Window(resizable=True)
window.projection = pyglet.window.Projection3D(zfar=1000)
scene = pywavefront.Wavefront(path)
buffer = ''
init_yaw = None
def timer(self):
global buffer
len = ser.in_waiting
if len > 0:
string = ser.read(len).decode("utf-8")
buffer_old = str(buffer)
buffer += string
last = buffer.rfind('\n')
if last >= 0:
second_last = buffer[0:last].rfind('\n')
if second_last >= 0:
# extract last full line (starts and ends with newline):
last_line = buffer[second_last+1:last]
try:
q = json.loads(last_line)
q = Quaternion(q['quat_w'],q['quat_x'],q['quat_y'],q['quat_z'])
buffer = buffer[last:] # delete everything before last newline
except:
print('invalid input')
print('buffer:',buffer)
buffer = ''
return
# delete current matrix and replace with initial matrix:
glPopMatrix()
glPushMatrix()
e = q.to_euler(degrees=True)
global init_yaw
if init_yaw == None:
init_yaw = e[2]
print(q)
print(init_yaw)
glTranslated(0, 0, 200)
q_yaw = Quaternion.from_euler(0,0,180-init_yaw,degrees=True)
q = q_yaw*q
#flip model around x axis, because sensor is upside down:
q_flip = Quaternion.from_angle_axis(180, [1,0,0],degrees=True)
q = q*q_flip
r = np.array(q.to_rot())
r4x4 = np.array([[r[0,0],r[1,0],r[2,0],0],
[r[0,1],r[1,1],r[2,1],0],
[r[0,2],r[1,2],r[2,2],0],
[0,0,0,1]])
glMultMatrixd(r4x4)
glTranslated(0, 0, -200)
#window.event
def on_draw():
window.clear()
visualization.draw(scene)
if __name__ == "__main__":
glViewport(0, 0, 500,500)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0, 500, 0.0, 500, 0.0, 1.0)
glMatrixMode (GL_MODELVIEW)
glLoadIdentity()
glClearColor(0.85, 0.85, 0.85, 1);
array = np.array([[0,0,1,0],
[1,0,0,0],
[0,1,0,0],
[0,-150,-600,1]])
glLoadMatrixd(array)
glPushMatrix();
pyglet.clock.schedule_interval(timer, 1/60);
pyglet.app.run()

PyOpenGL 3.1.0 with Pygame

I'm learning the OpenGL library with Python, so I use PyOpenGL 3.1.0 with Python 3.6.4 (and pygame 1.9.4 for windowing)
I watched some videos to learn how to render basic triangles with VBOs and VAOs, and so I writed the following code, but I don't understand why my code does not render a simple rectangle from the vertices array...
I think that I missed something about array attribution in vbo but I'm not sure.. Anyone ?
import pygame,numpy
from OpenGL.GL import *
from OpenGL.GLU import *
display = (800,600)
#pygame
pygame.init()
pygame.display.set_mode(display,pygame.DOUBLEBUF|pygame.OPENGL)
#opengl
"""
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 4000)"""
vertices = [-0.5,0.5,0,
-0.5,-0.5,0,
0.5,-0.5,0,
0.5,-0.5,0,
0.5,0.5,0,
-0.5,0.5,0]
vertices = numpy.array(vertices,dtype=numpy.float32)
vao = GLuint()
glGenVertexArrays(1,vao)
glBindVertexArray(vao)
vbo = GLuint()
glGenBuffers(1,vbo)
glBindBuffer(GL_ARRAY_BUFFER,vbo)
glBufferData(GL_ARRAY_BUFFER,len(vertices)*4,vertices,GL_STATIC_DRAW)
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0)
glBindBuffer(GL_ARRAY_BUFFER,0)
glBindVertexArray(0)
a=1
while a:
for event in pygame.event.get():
if event.type == pygame.QUIT:
a = 0
glClearColor(0, 0, 1, 0)
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
glBindVertexArray(vao)
glEnableVertexAttribArray(0)
glDrawArrays(GL_TRIANGLES,0,len(vertices)//3)
glDisableVertexAttribArray(0)
glBindVertexArray(0)
pygame.display.flip()
pygame.time.wait(10)
pygame.quit()
The issue is the call to glVertexAttribPointer.
If a named array buffer object is bound then the last parameter (6th parameter) is treated as a byte offset into the buffer object's data store. The data type of the parameter has to be ctypes.c_void_p.
This means you have to use a ctypes.cast:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.cast(0, ctypes.c_void_p))
or None:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
Thoes days it is common to use a shader program.
If you don't use a shader program, then yuo have to use fixed function attributes (glEnableClientState, glVertexPointer ...).
The only exception is vertex attribute 0. Setting the vertex attribute 0 is completely equivalent to setting the fixed function vertex coordinate array (glVertexPointer).
See also What are the Attribute locations for fixed function pipeline in OpenGL 4.0++ core profile?.

OpenGL - Show Texture Only

I'm working on a 2D isometric game, using pygame and pyopengl.
I'm drawing sprites as quads, with a texture. I managed to get the alpha transparency to work for the texture, but the quad itself is still filled in a solid color (whatever colour gl pipeline is set with at the time).
How do I hide the quad shape, and just show the texture?
Here is a pic showing the problem (gl pipeline set to pink/purple color):
The code is a bit messy, and I've been blindly copy 'n pasting gl calls hoping it solves the problem so there are bound to be quite a few calls in the wrong place or duplicated (or both).
GL Setup code (called once at start of script)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glViewport(0, 0, screen_size[0], screen_size[1])
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0, screen_size[0], 0.0, screen_size[1], 0.0, 1.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glDisable(GL_LIGHTING)
glEnable(GL_TEXTURE_2D)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
Drawing setup code (called once at the start of each frame)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glViewport(0, 0, screen_size[0], screen_size[1])
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0, screen_size[0], 0.0, screen_size[1], 0.0, 1.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glDisable(GL_LIGHTING)
glEnable(GL_TEXTURE_2D)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
Quad draw code (called for every sprite draw call):
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glEnable(GL_TEXTURE_2D)
glEnable(GL_ALPHA_TEST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
# Start new transformation matrix
glPushMatrix()
# Apply translation
glTranslatef(self.rect.centerx, self.rect.centery, 0.0)
# Start gl drawing cursor
glColor4f(1.0, 0.0, 1.0, 1.0)
# Bind the texture to this draw session
glBindTexture(GL_TEXTURE_2D, self.texture.id)
# Start drawing a quad
glBegin(GL_QUADS)
# Grab new copy of rect, and move to the origin
r = self.rect.copy()
r.center = (0, 0)
# Draw top left point
glTexCoord2f(1.0, 0.0)
glVertex2f(*r.topleft)
# Draw top right point
glTexCoord2f(0.0, 0.0)
glVertex2f(*r.topright)
# Draw bottom right point
glTexCoord2f(0.0, 1.0)
glVertex2f(*r.bottomright)
# Draw bottom left point
glTexCoord2f(1.0, 1.0)
glVertex2f(*r.bottomleft)
# End quad
glEnd()
# Apply transformation matrix
glPopMatrix()
The colored background behind your tiles is probably due to this line when you set up your texture:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
Just remove this as the default texture environment settings are probably fine for standard tile rendering. As an example of what messing with these parameters can do, if you wanted glColor calls to "tint" your texture instead, then replace GL_DECAL with GL_BLEND.
There is no need for any of those lighting calls included in your code as far as I can tell unless you are working with 3d models and ancient per-vertex lighting (I assume you are not since this is a 2d isometric game). Also you only need blending for this, no need for alpha testing. Assuming you are working with images with alpha (RGBA format), here is a simple demo that displays two tiles with a transparent background (supply your own image of course instead of ./images/grass.png):
import pygame
from pygame.locals import *
from OpenGL.GL import *
import sys
class Sprite(object):
def __init__(self):
self.x = 0
self.y = 0
self.width = 0
self.height = 0
self.texture = glGenTextures(1)
def load_texture(self, texture_url):
tex = pygame.image.load(texture_url)
tex_surface = pygame.image.tostring(tex, 'RGBA')
tex_width, tex_height = tex.get_size()
glBindTexture(GL_TEXTURE_2D, self.texture)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_surface)
glBindTexture(GL_TEXTURE_2D, 0)
self.width = tex_width
self.height = tex_height
def set_position(self, x, y):
self.x = x
self.y = y
def render(self):
#glColor(1, 1, 1, 1)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glBindTexture(GL_TEXTURE_2D, self.texture)
glBegin(GL_QUADS)
glTexCoord(0, 0)
glVertex(self.x, self.y, 0)
glTexCoord(0, 1)
glVertex(self.x, self.y + self.height, 0)
glTexCoord(1, 1)
glVertex(self.x + self.width, self.y + self.height, 0)
glTexCoord(1, 0)
glVertex(self.x + self.width, self.y, 0)
glEnd()
glBindTexture(GL_TEXTURE_2D, 0)
def init_gl():
window_size = width, height = (550, 400)
pygame.init()
pygame.display.set_mode(window_size, OPENGL | DOUBLEBUF)
glEnable(GL_TEXTURE_2D)
glMatrixMode(GL_PROJECTION)
glOrtho(0, width, height, 0, -1, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
if __name__ == "__main__":
init_gl()
tile1 = Sprite()
tile1.load_texture("./images/grass.png")
tile1.set_position(50, 100)
tile2 = Sprite()
tile2.load_texture("./images/grass.png")
tile2.set_position(80, 130)
tiles = [tile1, tile2]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
glClear(GL_COLOR_BUFFER_BIT)
glColor(1, 0, 0, 1)
for tile in tiles:
tile.render()
pygame.display.flip()
Let me know if this helps!
Well, either use blending, so that the alpha value actually has effect on opacity. Or use alpha testing, so that incoming fragments with an alpha below/above a certain threshold are discarded.
Blending requires to sort geometry back to front. And given what you want to do alpha testing may be the easier, more straightforward solution.
Update:
Either way it's imperative that the texture's alpha value makes it through to the fragment. If you were using shaders this would be as simple as making sure that the fragment output alpha would receive its value from the texture. But you're using the fixed function pipeline and the mess that's the texture environment state machine.
Using only a single texture your best bet would be a GL_REPLACE texture mode (completely ignores the vertex color). Or GL_MODULATE that takes the vertex color into account. Right now you're assumingly using GL_DECAL mode.
My suggestion: Drop the fixed function pipeline and use shaders. Much easier to get things related to texturing working. Also you'll hard pressed to find hardware that's not using shaders anyway (unless you're planning to run your program on stuff that's been built before 2004).

How to update data with a VBO and Pyglet

I would like to make a mesh with Pyglet that is changing every frame. Therefore I need to update the vertices very often and I thought that a VBO would be the fastest way to go here (correct me if I am wrong). Below an example for Points. Is this the correct way of doing it? I read that the number of glBindBuffer calls should be minimised, but here it is called every frame. also GL_DYNAMIC_DRAW is enabled, but if I change it to GL_STATIC_DRAW it is still working. It makes me wondering if this is a correct setup for fast computation
import pyglet
import numpy as np
from pyglet.gl import *
from ctypes import pointer, sizeof
vbo_id = GLuint()
glGenBuffers(1, pointer(vbo_id))
window = pyglet.window.Window(width=800, height=800)
glClearColor(0.2, 0.4, 0.5, 1.0)
glEnableClientState(GL_VERTEX_ARRAY)
c = 0
def update(dt):
global c
c+=1
data = (GLfloat*4)(*[500+c, 100+c,300+c,200+c])
glBindBuffer(GL_ARRAY_BUFFER, vbo_id)
glBufferData(GL_ARRAY_BUFFER, sizeof(data), 0, GL_DYNAMIC_DRAW)
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(data), data)
pyglet.clock.schedule(update)
glPointSize(10)
#window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(0, 0, 0)
glVertexPointer(2, GL_FLOAT, 0, 0)
glDrawArrays(GL_POINTS, 0, 2)
pyglet.app.run()
You don't need to call glBufferData every single time in update - create and fill the VBO once (see setup_initial_points) and only update it with glBufferSubData. In case you are only working with a single VBO, you can also comment out the glBindBuffer call in update() (see code below).
GL_DYNAMIC_DRAW vs GL_STATIC_DRAW won't make a big difference in this example since you are pushing very few data onto the GPU.
import pyglet
from pyglet.gl import *
from ctypes import pointer, sizeof
window = pyglet.window.Window(width=800, height=800)
''' update function '''
c = 0
def update(dt):
global c
c+=1
data = calc_point(c)
# if there's only on VBO, you can comment out the 'glBindBuffer' call
glBindBuffer(GL_ARRAY_BUFFER, vbo_id)
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(data), data)
pyglet.clock.schedule(update)
''' draw function '''
#window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(0, 0, 0)
glVertexPointer(2, GL_FLOAT, 0, 0)
glDrawArrays(GL_POINTS, 0, 2)
''' calculate coordinates given counter 'c' '''
def calc_point(c):
data = (GLfloat*4)(*[500+c, 100+c, 300+c, 200+c])
return data
''' setup points '''
def setup_initial_points(c):
vbo_id = GLuint()
glGenBuffers(1, pointer(vbo_id))
data = calc_point(c)
glBindBuffer(GL_ARRAY_BUFFER, vbo_id)
glBufferData(GL_ARRAY_BUFFER, sizeof(data), 0, GL_DYNAMIC_DRAW)
return vbo_id
############################################
vbo_id = setup_initial_points(c)
glClearColor(0.2, 0.4, 0.5, 1.0)
glEnableClientState(GL_VERTEX_ARRAY)
glPointSize(10)
pyglet.app.run()

mixing 2d and 3d in opengl (using pyglet)

I am trying to mix 2d and 3d in opengl in pyglet, i.e. draw a 3d scene then switch to orthographic projection and draw stuff over the top. I draw the 3d stuff, push the projection matrix to the
stack, do a glOrtho projection matrix, draw the 2d stuff, then pop the previous matrix off the stack.
The 3d stuff draws fine but for some reason the 2d part isn't drawing at all, even on its own.
Here's the code:
class Window(pyglet.window.Window):
# resolution
width, height = 1024, 786
def __init__(self, width, height):
# initialise window
super(Window, self).__init__(width, height)
# set title
self.set_caption("OpenGL Doss")
# call update() at 30fps
pyglet.clock.schedule_interval(self.update, 1 / 30.0)
glEnable(GL_TEXTURE_2D) # enable textures
glShadeModel(GL_SMOOTH) # smooth shading of polygons
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glDepthFunc(GL_LEQUAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # make stuff look nice
self.world = World() # initialise world
self.label = pyglet.text.Label('Hello, world',
font_name='Times New Roman',
font_size=20,
width=10, height=10)
def on_resize(self, width, height):
print 'on resize'
if height == 0:
height = 1
glViewport(0, 0, width, height) # specify viewport
# load perspective projection matrix
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, 1.0 * width / height, 0.1, 100.0)
#glLoadIdentity()
def on_draw(self):
self.set3d()
# draw 3d stuff
self.world.draw()
self.set2d()
# draw 2d stuff
self.draw2d()
self.unSet2d()
def update(self, dt):
"called at set interval during runtime"
#maze = self.world.maze
maze_platform = self.world.maze_platform
pacman = maze_platform.maze.pacman
maze_platform.update()
# send it world pointer
pacman.update(self.world)
def on_key_press(self, symbol, modifiers):
control.press(symbol, modifiers)
def on_key_release(self, symbol, modifiers):
control.release(symbol, modifiers)
def set3d(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_DEPTH_TEST) # enable depth testing
# reset modelview matrix
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def set2d(self):
glDisable(GL_DEPTH_TEST)
# store the projection matrix to restore later
glMatrixMode(GL_PROJECTION)
glPushMatrix()
# load orthographic projection matrix
glLoadIdentity()
#glOrtho(0, float(self.width),0, float(self.height), 0, 1)
far = 8192
glOrtho(-self.width / 2., self.width / 2., -self.height / 2., self.height / 2., 0, far)
# reset modelview
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
#glClear(GL_COLOR_BUFFER_BIT)
def unSet2d(self):
# load back the projection matrix saved before
glMatrixMode(GL_PROJECTION)
glPopMatrix()
def draw2d(self):
z=-6
n=100
glTranslatef(0, 0.0, -z)
glBegin(GL_TRIANGLES)
glVertex3f(0.0, n, 0.0)
glVertex3f(-n, -n, 0)
glVertex3f(n, -n, 0)
glEnd()
def main():
window = Window(Window.width, Window.height)
pyglet.app.run()
print 'framerate:', pyglet.clock.get_fps(), '(error checking = %s)' % pyglet.options['debug_gl']
if __name__ == '__main__': main()
#command = 'main()'
#cProfile.run(command)
I would recommend that you fully reset the modelview and projection matrices on each render, and then don't use push/pop when you go from 3d to 2d.
However, I suspect that you are using bad coordinates so the scene is drawing outside the clip planes. In partciular I am a tad suspicious of putting the near clipping plane at zero. Normally 2d elements are drawn with z=0.
Try putting the near clip-plane at -1.
I'm also a bit unsure why you're calling glTranslatef(0, 0.0, -z) in draw2d, I wouldn't bother.
In draw2d() try glDisable(GL_TEXTURE_2D) and glColor3ub(255,255,255) before drawing your triangle.
Make sure to re-glEnable(GL_TEXTURE_2D) before calling world.draw() again if it uses textured geometry.

Categories