Created
March 9, 2017 19:50
-
-
Save olymk2/b156f7490e3f5662e2fa0cf42b8c7c63 to your computer and use it in GitHub Desktop.
glutexample.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import sys | |
import OpenGL | |
# OpenGL.USE_ACCELERATE = False | |
# OpenGL.ERROR_CHECKING = False | |
from OpenGL.GLUT import * | |
from OpenGL.GLU import * | |
from OpenGL import GL as GL | |
from ctypes import * | |
import gi | |
from OpenGL.arrays import vbo | |
from OpenGL.GL import shaders | |
from OpenGL.raw.GL.ARB.vertex_array_object import glGenVertexArrays, \ | |
glBindVertexArray | |
from numpy import array | |
import numpy as np | |
VERTEX_SHADER = """ | |
#version 330 | |
in vec4 position; | |
void main() | |
{ | |
gl_Position = position; | |
}""" | |
FRAGMENT_SHADER = """ | |
#version 330 | |
out vec4 fragColor; | |
void main() | |
{ | |
fragColor = vec4(1.0, 0.0, 0.0, 1.0); | |
} | |
""" | |
class application_gui(object): | |
def __init__(self): | |
self.OnInit() | |
def OnInit(self): | |
self.vertices = [ | |
0.6, 0.6, 0.0, 1.0, | |
-0.6, 0.6, 0.0, 1.0, | |
0.0, -0.6, 0.0, 1.0] | |
self.vertices = np.array(self.vertices, dtype=np.float32) | |
glutInit([]) | |
# make a window | |
glutInitContextVersion(4, 0) | |
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE) | |
# glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE) | |
glutInitContextProfile(GLUT_CORE_PROFILE) | |
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) | |
glutInitWindowSize(640, 480) | |
glutCreateWindow("pyopengl with glut") | |
self.initialize() | |
self.setup_data() | |
glutDisplayFunc(self.Render) | |
glutMainLoop() | |
def initialize(self): | |
vs = shaders.compileShader(VERTEX_SHADER, GL.GL_VERTEX_SHADER) | |
fs = shaders.compileShader(FRAGMENT_SHADER, GL.GL_FRAGMENT_SHADER) | |
self.shader = shaders.compileProgram(vs, fs) | |
def setup_data(self): | |
self.vertex_array_object = glGenVertexArrays(1) | |
GL.glBindVertexArray( self.vertex_array_object ) | |
# Generate buffers to hold our vertices | |
self.vertex_buffer = GL.glGenBuffers(1) | |
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vertex_buffer) | |
# Get the position of the 'position' in parameter of our shader and bind it. | |
self.position = GL.glGetAttribLocation(self.shader, 'position') | |
GL.glEnableVertexAttribArray(self.position) | |
# Describe the position data layout in the buffer | |
GL.glVertexAttribPointer(self.position, 4, GL.GL_FLOAT, False, 0, ctypes.c_void_p(0)) | |
# Send the data over to the buffer | |
GL.glBufferData(GL.GL_ARRAY_BUFFER, 48, self.vertices, GL.GL_STATIC_DRAW) | |
# Unbind the VAO first (Important) | |
GL.glBindVertexArray( 0 ) | |
# Unbind other stuff | |
GL.glDisableVertexAttribArray(self.position) | |
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0) | |
return True | |
def Render(self): | |
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) | |
GL.glUseProgram(self.shader) | |
self.setup_data() | |
GL.glBindVertexArray( self.vertex_array_object ) | |
GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3) | |
GL.glBindVertexArray( 0 ) | |
GL.glUseProgram(0) | |
glFlush() | |
glutSwapBuffers() | |
app = application_gui() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment