欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

opengl之triangel

程序员文章站 2022-07-04 10:21:00
...

  记录下代码

  https://bitbucket.org/rndblnch/opengl-programmable/src/tip/10-gl3.2core.py?fileviewer=file-view-default

  部分代码借用了上述链接

shader.py

# coding: utf-8
from OpenGL.GL import *

import sys
##############################################################################
# Shader
##############################################################################
# Checks for GL posted errors after appropriate calls

shader=None
def printOpenGLError():
    err = glGetError()
    if (err != GL_NO_ERROR):
        print('GLERROR: ', gluErrorString(err))
        #sys.exit()

class Shader(object):

    def initShader(self, vertex_shader_source, fragment_shader_source):
        # create program
        self.program=glCreateProgram()
        print('create program')
        printOpenGLError()

        # vertex shader
        print('compile vertex shader...')
        self.vs = glCreateShader(GL_VERTEX_SHADER)
        glShaderSource(self.vs, [vertex_shader_source])
        glCompileShader(self.vs)
        glAttachShader(self.program, self.vs)
        printOpenGLError()

        # fragment shader
        print('compile fragment shader...')
        self.fs = glCreateShader(GL_FRAGMENT_SHADER)
        glShaderSource(self.fs, [fragment_shader_source])
        glCompileShader(self.fs)
        glAttachShader(self.program, self.fs)
        printOpenGLError()

        print('link...')
        glLinkProgram(self.program)
        printOpenGLError()

    def begin(self):
        if glUseProgram(self.program):
            printOpenGLError()

    def end(self):
        glUseProgram(0)

三角形的坐标设定,shader 代码 。主要了解到ctypes库。pyopengl库中接口的参数传递,数据类型有此库帮助更加易用。

起初一直是黑色屏幕,没有显示出三角形。加入glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)此句清除framebuffer的则可以正常输出了

# coding: utf-8
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys
from ctypes import sizeof, c_float, c_void_p, c_uint, string_at

from shader import *
#https://bitbucket.org/rndblnch/opengl-programmable/src/tip/10-gl3.2core.py?fileviewer=file-view-default

     
myvertices = [
    -0.5, -0.7, 0.0,
     0.5, -0.5, 0.0,
     0.0,  0.5, 0.0
    ]
def mydraw():

    # clear
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    # view
    #glMatrixMode(GL_MODELVIEW)
    #glLoadIdentity()
    
    buffers = glGenBuffers(1)
    float_size = sizeof(c_float)
    vertex_offset    = c_void_p(0 * float_size)
    glBindBuffer(GL_ARRAY_BUFFER, buffers)
    glBufferData(GL_ARRAY_BUFFER, 
            len(myvertices)*4,  # byte size
            (ctypes.c_float*len(myvertices))(*myvertices), 
            GL_STATIC_DRAW)
    
    
    
    glEnableClientState(GL_VERTEX_ARRAY)
    glVertexAttribPointer(0,3,GL_FLOAT, False, 3 * float_size, vertex_offset)
    glEnableVertexAttribArray(0) 
    global shader
    if shader==None:
            shader=Shader()
            shader.initShader('''
   
    layout (location = 0) in vec3 aPos;
    void main()
    {
        gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
       
    }
            ''',
            '''
    out vec4 FragColor;
    void main()
    {
        FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
    }
            ''')
    
    shader.begin()
    glDrawArrays(GL_TRIANGLES, 0, 3)
    #glFlush()
    shader.end()
    glDisableVertexAttribArray(0)

##############################################################################
def disp_func():
    #draw()
    mydraw()
    glutSwapBuffers()

if __name__=="__main__":
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
    glutInitWindowSize(256, 256)
    glutCreateWindow(b"vbo")
    glutDisplayFunc(disp_func)
    glutIdleFunc(disp_func)
    #glutReshapeFunc(reshape_func)

    #initialize()

    glutMainLoop()

 

 

上一篇: RSA加密

下一篇: tomcat优化