Mein Object ist hier als klasse definiert, wo auch die Uniforms behandelt werden
Code: Alles auswählen
import sys
import fileinput
import string
import os
import glfw
from OpenGL.GL import *
from glm.gtc.matrix_transform import *
import pyrr
import numpy as np
class MyObject:
BYTE_SIZE = 4
_vertices = None
_normals = None
_textures = None
_indices = None
_vao = None
_vertexVBO = None
_indexVBO = None
transformMatrix = None
def __init__(self, shaderProgram):
self._shaderProgram = shaderProgram
def makeTriangle(self):
self._vertices = [-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.0, 0.5, 0.0]
self._indices = [0,1,2]
self._vertices = np.array(self._vertices, dtype=np.float32)
self._indices = np.array(self._indices, dtype=np.uint32)
self._vao = glGenVertexArrays(1)
glBindVertexArray(self._vao)
self._vertexVBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self._vertexVBO)
glBufferData(GL_ARRAY_BUFFER, self.BYTE_SIZE * self._vertices.size, self._vertices, GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
glEnableVertexAttribArray(0)
self._indexVBO = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self._indexVBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, self.BYTE_SIZE * self._indices.size, self._indices, GL_STATIC_DRAW)
# - Matrices test - #
self._transformMatrix = translate(tmat4x4([]), tvec3([0.0, 0.0, 0.0]))
tmpMatrix = pyrr.Matrix44.from_x_rotation(np.pi * glfw.get_time())
try:
glUniformMatrix4fv(self._transformMatrixID, 1, GL_FALSE, self._transformMatrix)
except TypeError:
print("Exception :: " + str(sys.exc_info()[0]))
def readObjectFile(self):
pass
def render(self):
glUseProgram(self._shaderProgram._programID)
glBindVertexArray(self._vao)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self._indexVBO)
glDrawElements(GL_TRIANGLES, self._indices.size, GL_UNSIGNED_INT, None)
glUseProgram(0)
Die shader sehen so aus:
Code: Alles auswählen
#version 440
in layout(location=0) vec3 vertexPosition;
uniform mat4 transformMatrix;
void main(void)
{
gl_Position = transformMatrix * vec4(vertexPosition, 1.0f);
}
fragment shader:
Code: Alles auswählen
#version 440
out vec4 outColor;
void main(void)
{
outColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}
Und das alles wird in einem Fenster angezeigt:
Code: Alles auswählen
import sys
import os
import time
import threading
import math
import glfw
from OpenGL.GL import *
import numpy as np
import pyrr
from Game.object import MyObject
from Game.shaderprogram import ShaderProgram
class MyWindow:
def __init__(self):
self._height = 400
self._width = 400
self._title = "OpenGL Game (1)"
self._window = None
def _window_size_callback(self, window, width, height):
self._height = height
self._width = width
glViewport(0, 0, width, height)
def initWindow(self):
if not glfw.init():
return
self._window = glfw.create_window(self._width, self._height, self._title, None, None)
if not self._window:
glfw.terminate()
return
glfw.make_context_current(self._window)
# ** Callbacks ** #
glfw.set_window_size_callback(self._window, self._window_size_callback)
def start(self):
sp = ShaderProgram("","")
object = MyObject(sp)
object.makeTriangle()
glClearColor(0.2, 0.3, 0.2, 1.0)
while(glfw.window_should_close(self._window) != True):
glfw.poll_events()
glClear(GL_COLOR_BUFFER_BIT or GL_COLOR_BUFFER_BIT)
object.render()
glfw.swap_buffers(self._window)
glfw.terminate()