'Why is my PyOpenGL program using shaders not displaying anything?
I have the following minimal shader example (running in Python 2.7). I expect to see the entire screen taken up by a shaded quad (that is red). Instead I just see black. This example used to work perfectly on my previous Mac machine but now it seizes to function -- am I missing some OpenGL call that is needed?
from OpenGL.GLU import *
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLUT.freeglut import glutLeaveMainLoop
from OpenGL.arrays import vbo
from OpenGL.GL import shaders
from OpenGL.GL.ARB.color_buffer_float import *
from OpenGL.raw.GL.ARB.color_buffer_float import *
import numpy as np
QUAD_VERTEX_SHADER = """
attribute vec2 points;
varying vec2 coord;
varying vec2 index;
void main() {
index = (points + 1.0) / 2.0;
coord = (points * vec2(1, -1) + 1.0) / 2.0;
gl_Position = vec4(points, 0.0, 1.0);
}
"""
FRAG_SHADER = """
varying vec2 index;
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
"""
def display():
glViewport(0, 0, int(width), int(height))
shaders.glUseProgram(shader)
vbo.bind()
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointerf(vbo)
glDrawArrays(GL_TRIANGLES, 0, 6)
vbo.unbind()
glDisableClientState(GL_VERTEX_ARRAY)
glutSwapBuffers()
def idle():
glutPostRedisplay()
width = 640
height = 480
vertices = [
[ -1, 1, 0 ],
[ -1,-1, 0 ],
[ 1,-1, 0 ],
[ -1, 1, 0 ],
[ 1,-1, 0 ],
[ 1, 1, 0 ]
]
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_ACCUM | GLUT_DEPTH)
glutInitWindowSize(width, height)
window_id = glutCreateWindow('Visualization')
glClearColor(0.,0.,0.,0.)
glClampColor(GL_CLAMP_READ_COLOR, GL_FALSE)
glEnable(GL_CULL_FACE)
glEnable(GL_DEPTH_TEST)
glEnable( GL_PROGRAM_POINT_SIZE )
glMatrixMode(GL_MODELVIEW)
glutIdleFunc(idle)
glutDisplayFunc(display)
vp = shaders.compileShader(QUAD_VERTEX_SHADER, GL_VERTEX_SHADER)
fp = shaders.compileShader(FRAG_SHADER, GL_FRAGMENT_SHADER)
shader = shaders.compileProgram(vp, fp)
vbo = vbo.VBO(np.array(vertices,'float32'))
glutMainLoop()
Solution 1:[1]
You have to specify the array of vertex attributes with glVertexAttribPointer and you have to enable it with glEnableVertexAttribArray:
shader = shaders.compileProgram(vp, fp)
points_loc = glGetAttribLocation(shader, 'points');
glUseProgram(shader)
vbo.bind()
glEnableVertexAttribArray(points_loc);
glVertexAttribPointer(points_loc, 3, GL_FLOAT, GL_FALSE, 0, None)
glDrawArrays(GL_TRIANGLES, 0, 6)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rabbid76 |
