How does Modern Opengl work?

Asked

Viewed 85 times

0

I already studied Opengl, and now I was reading Modern Opengl, I was watching some videos on Youtube and some tutorials on the internet and I wrote this code:

import pyglet, numpy, ctypes
from OpenGL.GL import *
import OpenGL.GL.shaders as shader

a = pyglet.window.Window()

t = [-.5,-.5,0,.5,-.5,0,.5,.5,0]
t = numpy.array(t, dtype=numpy.float32)

v = """
void main(){
    gl_Position = ftransform();
}
"""

f = """
void main(){
    gl_FragColor = vec4(1,1,1,1);
}
"""

s = shader.compileProgram(shader.compileShader(v,GL_VERTEX_SHADER),shader.compileShader(f,GL_FRAGMENT_SHADER))

VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, len(t), t, GL_STATIC_DRAW)

glUseProgram(s)

@a.event
def on_draw():
    glClear(GL_COLOR_BUFFER_BIT)
    glBindVertexArray(0)
    glDrawArrays(GL_TRIANGLES, 0, 3)

def SRO(dt):
    on_draw()

pyglet.clock.schedule_interval(SRO, 1/30)

pyglet.app.run()

A White triangle was supposed to appear, but the window just opens and there’s a black screen, I searched some codes, added a glVertexAttribPointer did not work(or at least bugged the code enough) and a glBindVertexArray which also didn’t make much difference. Someone there knows where I’m going wrong?

1 answer

1


Have you studied Opengl version 3.2 (or higher) or any version prior to this? if you used the glVertex function it is because it was in the version below the 3.2, which is totally different from how it is done today.

Your code is incomplete, you used glBufferData, which is a function that uploads bytes from CPU to GPU, then you still need to tell how these bytes are organized with glVertexAttribPointer.

You should also explicitly allocate a VAO, but for your triangle to appear alone on the screen it is not required to use VAO.

In his job on_draw you call the glBindVertexArray that links with the default VAO and then makes the call of the drawing function, this part is right, if you allocated the VAO explicitly vc would have to say which is the Handler of the VAO.

Programmable Opengl is not something so trivial as to start writing random code and check what happens on the run. I recommend you read the whole chapter of Getting Started from learnopengl (https://learnopengl.com/Getting-started/OpenGL), before leaving writing Opengl code.

Obs: do not use the ftransform function (it is in your Vertex Shader), unless you know what you are doing.

More Obs: Most of the examples and courses/tutorials you will find will be in C++, all I know of high quality are in C++. Nothing stops you from writing your python code, but if you really want to learn Opengl you’ll have to learn C/C++ as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.