本文整理汇总了Python中program.Program.get_handle方法的典型用法代码示例。如果您正苦于以下问题:Python Program.get_handle方法的具体用法?Python Program.get_handle怎么用?Python Program.get_handle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类program.Program
的用法示例。
在下文中一共展示了Program.get_handle方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from program import Program [as 别名]
# 或者: from program.Program import get_handle [as 别名]
class GameObject:
def __init__(self, *args, **kwargs):
self.program = Program()
self.vao = GLuint(0)
self.vbo = GLuint(0)
self.ebo = GLuint(0)
# Create Vertex Array Object
glGenVertexArrays(1, self.vao)
# Create a Vertex Buffer Object
glGenBuffers(1, self.vbo)
# Create an element array
glGenBuffers(1, self.ebo)
# use the vao
glBindVertexArray(self.vao)
width = float(100)
height = float(100)
r = 1.0
g = 0.0
b = 0.0
# self.vertices = [
# 0.0, 0.0, r, g, b, # Top-left
# 0.0 + width, 0.0, r, g, b, # Top-right
# 0.0 + width, 0.0 + height, r, g, b, # Bottom-right
# 0.0, 0.0 + height, r, g, b # Bottom-left
# ]
# self.vertices_gl = (GLfloat * len(self.vertices))(*self.vertices)
self.vertices = [
0.0, 0.0,
0.0 + width, 0.0,
0.0 + width, 0.0 + height,
0.0, 0.0 + height,
]
self.vertices_gl = (GLfloat * len(self.vertices))(*self.vertices)
glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
glBufferData(GL_ARRAY_BUFFER, sizeof(self.vertices_gl), self.vertices_gl, GL_STATIC_DRAW)
self.elements = [
0, 1, 2,
2, 3, 0
]
self.elements_gl = (GLuint * len(self.elements))(*self.elements)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ebo)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(self.elements_gl), self.elements_gl, GL_STATIC_DRAW)
self.program.compile_shader_from_string(
"""
layout (location = 0) in vec2 position;
uniform mat4 proj;
uniform mat4 view;
uniform mat4 model;
void main()
{
gl_Position = proj * view * model * vec4(position, 0.0, 1.0);
}
""", 'vertex')
self.program.compile_shader_from_string(
"""
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
""", 'fragment')
if not self.program.link():
return
if not self.program.validate():
return
self.program.bind()
# Specify the layout of the vertex data
position_attribute = glGetAttribLocation(self.program.get_handle(), "position")
if position_attribute > -1:
glEnableVertexAttribArray(position_attribute)
glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0)
# color_attribute = glGetAttribLocation(self.program.get_handle(), "color")
# if color_attribute > -1:
# glEnableVertexAttribArray(color_attribute)
# glVertexAttribPointer(color_attribute, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 2 * sizeof(GLfloat))
# stop the vao
glBindVertexArray(0)
# def new_look_at(self, eye, at, up):
# z = (eye - at).normalized()
# x = up.cross(z).normalized()
# y = z.cross(x)
#.........这里部分代码省略.........
示例2: Mesh
# 需要导入模块: from program import Program [as 别名]
# 或者: from program.Program import get_handle [as 别名]
class Mesh(GameObject):
def __init__(self):
self.program = Program()
self.vao = GLuint(0)
self.vbuf = None
self.ibuf = None
self.vertices = []
self.normals = []
self.indices = []
self.uvs = []
# we should really be getting the camera not creating a new instance..
self.camera = Camera(800, 600)
GameObject.__init__(self)
def set_data(self, *args, **kwargs):
self.vertices = kwargs.get('vertices', [])
self.normals = kwargs.get('normals', [])
self.indices = kwargs.get('indices', [])
self.uvs = kwargs.get('uvs', [])
self.colors = kwargs.get('colors', [])
# create a vao
glGenVertexArrays(1, self.vao)
# use it
glBindVertexArray(self.vao)
# create a vertexbuffer
if self.vertices:
self.vbuf = VertexBuffer(self.vertices)
#self.vbuf.set_attribute(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0)
#self.vbuf.set_attribute(1, 2, GL_FLOAT, GL_FALSE, 2, 0)
# create an indexbuffer
if self.indices:
self.ibuf = IndexBuffer(self.indices)
self.program.compile_shader_from_string(
"""
layout (location = 0) in vec2 position;
layout (location = 1) in vec3 color;
uniform mat4 proj;
uniform mat4 view;
uniform mat4 model;
smooth out vec3 theColor;
void main()
{
theColor = color;
gl_Position = proj * view * model * vec4(position, 0.0, 1.0);
}
""", 'vertex')
self.program.compile_shader_from_string(
"""
smooth in vec3 theColor;
void main()
{
gl_FragColor = vec4(theColor, 1.0);
}
""", 'fragment')
self.program.link()
self.program.validate()
self.program.bind()
#self.vbuf.set_attribute(self.program.get_handle(), "position", 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0)
#self.vbuf.set_attribute(self.program.get_handle(), "color", 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0)
# Specify the layout of the vertex data
position_attribute = glGetAttribLocation(self.program.get_handle(), "position")
if position_attribute > -1:
glEnableVertexAttribArray(position_attribute)
glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0)
color_attribute = glGetAttribLocation(self.program.get_handle(), "color")
if color_attribute > -1:
glEnableVertexAttribArray(color_attribute)
glVertexAttribPointer(color_attribute, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 2 * sizeof(GLfloat))
# stop the vao
glBindVertexArray(0)
def render(self):
GameObject.render(self)
self.program.bind()
view_location = glGetUniformLocation(self.program.get_handle(), "view")
if view_location > -1:
v = self.camera.view
v = v[:]
v_ctype = (GLfloat * len(v))(*v)
glUniformMatrix4fv(view_location, 1, GL_FALSE, v_ctype)
proj_location = glGetUniformLocation(self.program.get_handle(), "proj")
if proj_location > -1:
p = self.camera.projection
p = p[:]
p_ctype = (GLfloat * len(p))(*p)
glUniformMatrix4fv(proj_location, 1, GL_FALSE, p_ctype)
model_location = glGetUniformLocation(self.program.get_handle(), "model")
#.........这里部分代码省略.........