Last active
August 29, 2015 14:01
-
-
Save larsoner/5ff8ffca0111ea3d81bf to your computer and use it in GitHub Desktop.
Testing use of OpenGL 3.2 and 2.1 shaders simultaneously
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
# -*- coding: utf-8 -*- | |
import numpy as np | |
from vispy.gloo import gl | |
from vispy.app import Canvas | |
v1 = """ | |
#version 120 | |
attribute vec2 pos; attribute vec3 color; varying vec4 f_color; | |
void main() { | |
gl_Position = vec4(pos, 0, 1.0); | |
f_color = vec4(color, 1.0); | |
} | |
""" | |
v2 = """ | |
#version 150 | |
in vec2 pos; in vec3 color; out vec4 f_color; | |
void main() { | |
gl_Position = vec4(pos + vec2(1, 0), 0, 1.0); | |
f_color = vec4(color, 1.0); | |
} | |
""" | |
f1 = ("#version 120\n" | |
"attribute vec4 f_color; void main() {gl_FragColor = f_color;}") | |
f2 = ("#version 150\n" | |
"in vec4 f_color; void main() {gl_FragColor = f_color;}") | |
c = Canvas(close_keys='escape', show=True) | |
c.__enter__() | |
gl.use('pyopengl') | |
def create_prog(v, f): | |
hprog = gl.glCreateProgram() | |
for tt, src in ((gl.GL_VERTEX_SHADER, v), (gl.GL_FRAGMENT_SHADER, f)): | |
shader = gl.glCreateShader(tt) | |
gl.glShaderSource_compat(shader, src) | |
gl.glCompileShader(shader) | |
gl.glAttachShader(hprog, shader) | |
gl.glLinkProgram(hprog) | |
gl.glValidateProgram(hprog) | |
return hprog | |
prog1 = create_prog(v1, f1) | |
prog2 = create_prog(v2, f2) | |
pos = np.array([(-1, -1), (0, 1), (1, -1)], np.float32) | |
color = np.array([(1, 0, 0), (0, 1, 0), (0, 0, 1)], np.float32) | |
pos_buf = gl.glCreateBuffer() | |
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, pos_buf) | |
gl.glBufferData(gl.GL_ARRAY_BUFFER, pos, gl.GL_STATIC_DRAW) | |
col_buf = gl.glCreateBuffer() | |
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, col_buf) | |
gl.glBufferData(gl.GL_ARRAY_BUFFER, color, gl.GL_STATIC_DRAW) | |
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0) | |
assert gl.glGetError() == 0 | |
@c.events.paint.connect | |
def paint(event): | |
for prog in (prog1, prog2): | |
gl.glUseProgram(prog) | |
loc = gl.glGetAttribLocation(prog, 'pos') | |
gl.glEnableVertexAttribArray(loc) | |
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, pos_buf) | |
gl.glVertexAttribPointer(loc, 2, gl.GL_FLOAT, gl.GL_FALSE, 0, 0) | |
loc = gl.glGetAttribLocation(prog, 'color') | |
gl.glEnableVertexAttribArray(loc) | |
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, col_buf) | |
gl.glVertexAttribPointer(loc, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, 0) | |
gl.glDrawArrays(gl.GL_TRIANGLES, 0, 3) | |
assert gl.glGetError() == 0 | |
c.show() | |
c.update() | |
c.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment