【一步一步学习OpenGL 1】你好顶点(Pyopengl)

原文链接(C语言环境)

我用python实现的代码:

 1 __author__ = "WSX"
 2 from OpenGL.GLUT.freeglut import *   #新版本的glut
 3 import ctypes
 4 from OpenGL.GLUT import *
 5 from OpenGL.GL import *
 6 from OpenGL.GLU import *
 7 from OpenGL.GL import shaders
 8 import numpy as np
 9 
10 def Draw():
11     glClear(GL_COLOR_BUFFER_BIT)
12     glEnableVertexAttribArray(0)
13     glBindBuffer(GL_ARRAY_BUFFER, VBO)
14     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0)
15     glDrawArrays(GL_POINTS, 0, 1)
16     glDisableVertexAttribArray(0)
17     glutSwapBuffers()
18 
19 def buffer():
20     global VBO
21     vertices = np.array([0.0 , 0.0 , 0.0],dtype="float32")
22     VBO = glGenBuffers(1)    #创建对象
23     glBindBuffer(GL_ARRAY_BUFFER , VBO)  #绑定
24     glBufferData(GL_ARRAY_BUFFER , vertices.nbytes , vertices , GL_STATIC_DRAW)  #输入数据
25 
26 def main():
27     glutInit([])
28     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)  # 显示模式 双缓存
29     glutInitWindowPosition(100, 100)  # 窗口位置
30     glutInitWindowSize(500, 500)  # 窗口大小
31     glutCreateWindow("point")  # 创建窗口
32     glutInitContextVersion(4,3)   #为了兼容
33     glutInitContextProfile(GLUT_CORE_PROFILE)   #为了兼容
34     glutDisplayFunc(Draw)  # 回调函数
35     glClearColor(0.0, 0.0, 0.0, 0.0)
36     buffer()
37     glutMainLoop()
38 
39 
40 main()

猜你喜欢

转载自www.cnblogs.com/WSX1994/p/9077394.html