简体   繁体   中英

How do I incorporate Vertex Buffer Array into my graphics program?

I am writing a 3d graphics toolkit for Python and PyQt5, using PyOpenGL. I am writing my own shaders to go with it, if that helps. What I am trying to do is go from using glBegin to using a Vertex Buffer Array. I have found the following on using VBOs:

http://www.songho.ca/opengl/gl_vbo.html - I could only scrape together a bit of information from this because it is in C/C++.

How to get VBOs to work with Python and PyOpenGL - This was in Python2 and was fairly limiting as a result.

I cannot, however, piece together what I need to take the vertexes of each of my shape objects and compile them into a scene VBO. I also have no idea how the data in an array is laid out. My initGL and paintGL functions are below, as are my vertex and fragment shaders' GLSL code.

    def initGL(self):
        self.vertProg = open(self.vertPath, 'r')
        self.fragProg = open(self.fragPath, 'r')
        self.vertCode = self.vertProg.read()
        self.fragCode = self.fragProg.read()
        self.vertShader = shaders.compileShader(self.vertCode, GL_VERTEX_SHADER)
        self.fragShader = shaders.compileShader(self.fragCode, GL_FRAGMENT_SHADER)
        self.shader = shaders.compileProgram(self.vertShader, self.fragShader)

#paintGL uses shape objects, such as cube() or mesh(). Shape objects require the following:
#a list named 'vertices'  - This list is a list of points, from which edges and faces are drawn.
#a list named 'wires'     - This list is a list of tuples which refer to vertices, dictating where to draw wires.
#a list named 'facets'    - This list is a list of tuples which refer to vertices, ditating where to draw facets.
#a bool named 'render'    - This bool is used to dictate whether or not to draw the shape.
#a bool named 'drawWires' - This bool is used to dictate whether wires should be drawn.
#a bool named 'drawFaces' - This bool is used to dictate whether facets should be drawn.

    def paintGL(self):
        shaders.glUseProgram(self.shader)
        glLoadIdentity()
        gluPerspective(45, self.sizeX / self.sizeY, 0.1, 110.0)    #set perspective?
        glTranslatef(0, 0, self.zoomLevel)    #I used -10 instead of -2 in the PyGame version.
        glRotatef(self.rotateDegreeV + self.vOffset, 1, 0, 0)    #I used 2 instead of 1 in the PyGame version.
        glRotatef(self.rotateDegreeH, 0, 0, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

        for s in self.shapes:
            if s.drawWires:
                glBegin(GL_LINES)
                for w in s.wires:
                    for v in w:
                        glVertex3fv(s.vertices[v])
                glEnd()

            if s.drawFaces:
                glBegin(GL_QUADS)
                for f in s.facets:
                    for v in f:
                        glVertex3fv(s.vertices[v])
                glEnd()

Vertex shader:

#version 120    
void main() {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Fragment shader:

#version 120
void main() {
    gl_FragColor = vec4( 0, 1, 0, 1 );
}

In the final form of this project, I want to have information in my buffer for vertex positions, color, and maybe even glow. (That will be achieved when I put this to ray marching eventually.) I also need a way to specify whether or not I should draw the wires and faces.

How do I set up and configure one or more VBOs to transfer all this information to the GPU and OpenGL?

Python 3.7.6, Windows 10

After a while longer researching, I decided to try using less specific search terms. I eventually stumbled upon this site: https://www.metamost.com/opengl-with-python/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM