Openglを使用して2つの三角形-Python注釈版を描画

5040 ワード

環境構築の参考https://www.jianshu.com/p/9527c0435e9c------以前はanacondaの下で環境を構築してエラーを報告していましたが、pycharmを使用して解釈器パスをanacondaの下に設定すればいいのです.
コードソースhttps://www.jianshu.com/p/f1d326bce955
C++ボードの『OpenGLプログラミングガイド(原書第9版).pdf』を読んだ後、理解によっていくつかのコード注釈を書いて、間違いがあったら指摘してください、ありがとうございます

"""
glfw_Triangle02.py
Author: dalong10
Description: Draw a Triagle, learning OPENGL
"""
import glutils    #Common OpenGL utilities,see glutils.py
import sys, random, math
import OpenGL
from OpenGL.GL import *
from OpenGL.GL.shaders import *
import numpy
import numpy as np
import glfw

'''     ,  OpenGL                    '''
#Vertex Shader     ,         ,                     gl_Position
#vec4(position.x, position.y, position.z, 1.0);------XYZW,       
strVS = """
#version 330 core
layout(location = 0) in vec3 position;
void main(){
    gl_Position = vec4(position.x, position.y, position.z, 1.0);
    }
"""
#Fragment Shader     ,        color  
#vec4(1.0, 0.0, 0.0, 1.0);------RGBA       
strFS1 = """
#version 330 core
out vec4 color;
void main(){
    color = vec4(1.0, 0.0, 0.0, 1.0);
    }
"""

strFS2 = """
#version 330 core
out vec4 color;
void main(){
    color = vec4(0.0, 1.0, 0.0, 1.0);
    }
"""

class FirstTriangle:
    def __init__(self, side1, side2, side3):
        self.side1 = side1
        self.side2 = side2
        self.side3 = side3

        # load shaders
        if side3>0:
            strFS=strFS1
        else:
            strFS=strFS2
        #                        ,      GPU          
        self.program = glutils.loadShaders(strVS, strFS)
        glUseProgram(self.program)

        s1 = side1/1.0
        s2 = side2/1.0
        s3 = side3/1.0
        #       
        vertices = [
             s1, -0.5, 0,#xyzz
             s2, -0.5, 0,#xyzz
             s3, 0.5, 0  #xyzz
             ]
        '''      :                     '''
        # set up vertex array object (VAO)  1       ,       ,  CC       
        self.vao = glGenVertexArrays(1)
        #          ,opengl           ,               
        glBindVertexArray(self.vao)
        # set up VBOs
        vertexData = numpy.array(vertices, numpy.float32)
        #  1       ,                。      opengl               
        self.vertexBuffer = glGenBuffers(1)
        #   opengl   ,opengl     ,                GL_ARRAY_BUFFER
        glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)
        #        ,   4*len(vertexData)  ,  vertexData     opengl    
        glBufferData(GL_ARRAY_BUFFER, 4*len(vertexData), vertexData,
                     GL_STATIC_DRAW)
        '''      :           ,                   '''
        #enable arrays     0       
        self.vertIndex = 0
        glEnableVertexAttribArray(self.vertIndex)
        # set buffers
        glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)
        #0:          position     3:     xyz3  
        # GL_FLOAT:          GL_FALSE:        
        #0:            0
        #        position vertexBuffer    
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
        # unbind VAO opengl             
        glBindVertexArray(0)
    '''      '''
    def render(self):
        #   program  opengl        
        glUseProgram(self.program)
        # bind VAO  vao  opengl       
        glBindVertexArray(self.vao)
        # draw
        glDrawArrays(GL_TRIANGLES, 0, 3)
        # unbind VAO
        glBindVertexArray(0)

if __name__ == '__main__':
    import sys
    import glfw
    import OpenGL.GL as gl
    def on_key(window, key, scancode, action, mods):
        if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
            glfw.set_window_should_close(window,1)

    # Initialize the library   GLFW 
    if not glfw.init():
        sys.exit()

    # Create a windowed mode window and its OpenGL context                 ,               
    window = glfw.create_window(640, 480, "glfw_Triangle02", None, None)
    if not window:
        glfw.terminate()
        sys.exit()

    # Make the window's context current    window    OPENGL     
    glfw.make_context_current(window)
    # Install a key handler
    glfw.set_key_callback(window, on_key)

    # Loop until the user closes the window
    while not glfw.window_should_close(window):
        # Render here
        width, height = glfw.get_framebuffer_size(window)
        ratio = width / float(height)
        gl.glViewport(0, 0, width, height)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        gl.glClearColor(0.0,0.0,0.0,0.0)
        firstTriangle0 = FirstTriangle(-0.9,-0.0,-0.45)
        firstTriangle1 = FirstTriangle(0,0.9,0.45)
        # render
        firstTriangle0.render()
        firstTriangle1.render()

        # Swap front and back buffers
        glfw.swap_buffers(window)

        # Poll for and process events
        glfw.poll_events()

    glfw.terminate()