1.GLEW介绍
GLEW是OpenGL常用的扩展库,可以在 http://glew.sourceforge.net/ 进行下载
2.实现
1 #include <stdio.h> 2 3 #include <GL/glew.h> 4 #include <GL/freeglut.h> 5 6 struct Vector3f 7 { 8 float x; 9 float y; 10 float z; 11 12 Vector3f() 13 { 14 } 15 16 Vector3f(float _x, float _y, float _z) 17 { 18 x = _x; 19 y = _y; 20 z = _z; 21 } 22 }; 23 24 GLuint VBO; 25 26 static void RenderSceneCB() 27 { 28 glClear(GL_COLOR_BUFFER_BIT); 29 30 glEnableVertexAttribArray(0); 31 glBindBuffer(GL_ARRAY_BUFFER, VBO); 32 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 33 34 glDrawArrays(GL_POINTS, 0, 1); 35 36 glDisableVertexAttribArray(0); 37 38 glutSwapBuffers(); 39 } 40 41 42 static void InitializeGlutCallbacks() 43 { 44 glutDisplayFunc(RenderSceneCB); 45 } 46 47 static void CreateVertexBuffer() 48 { 49 Vector3f Vertices[1]; 50 Vertices[0] = Vector3f(0.0f, 0.0f, 0.0f); 51 52 glGenBuffers(1, &VBO); 53 glBindBuffer(GL_ARRAY_BUFFER, VBO); 54 glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); 55 } 56 57 58 int main(int argc, char** argv) 59 { 60 glutInit(&argc, argv); 61 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA); 62 glutInitWindowSize(1024, 768); 63 glutInitWindowPosition(100, 100); 64 glutCreateWindow("Tutorial 02"); 65 66 InitializeGlutCallbacks(); 67 68 // Must be done after glut is initialized! 69 GLenum res = glewInit(); 70 if (res != GLEW_OK) { 71 fprintf(stderr, "Error: ‘%s‘\n", glewGetErrorString(res)); 72 return 1; 73 } 74 75 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 76 77 CreateVertexBuffer(); 78 79 glutMainLoop(); 80 81 return 0; 82 }
Vector3f
首选定义一个自己的3D指标结构,方便信息表示
GLuint VBO;
用于存储定点缓存数据
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
设置缓存块,绑定缓存块,填充缓存块
glEnableVertexAttribArray(0);
glDisableVertexAttribArray(0);
在渲染前需要设置开启渲染管道属性,在渲染结束后需要关闭
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_POINTS, 0, 1);//p1:绘制类型,p2:绘制Vertex起始位置,p3:绘制Vertex数目
在渲染前需要预先绑定已有数据,说明数据信息,绘制
时间: 2024-12-27 23:42:46