0

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()
2
  • 1
    Where are your glVertexAttribPointer() call(s)? Alternatively, why aren't you using the fixed-function inter-op built-in variables in your shader code? Commented Feb 23, 2022 at 4:20
  • I don't have any glVertexAttribPointer calls and the code used to work (maybe wrongly). What should those calls look like? What do the inter-op built in variables look like? Any guidance much appreciated. Commented Feb 23, 2022 at 4:49

1 Answer 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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.