码迷,mamicode.com
首页 > 其他好文 > 详细

编译opengl编程指南第八版示例代码通过

时间:2015-06-06 17:53:35      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

最近在编译opengl编程指南第八版的示例代码,如下

 1 #include <iostream>
 2 #include "vgl.h"
 3 #include "LoadShaders.h"
 4 
 5 using namespace std;
 6 
 7 
 8 enum VAO_IDs { Triangles, NumVAOs };
 9 enum Buffer_IDs { ArrayBuffer, NumBuffers };
10 enum Attrib_IDs { vPosition = 0 };
11 GLuint VAOs[NumVAOs];
12 GLuint Buffers[NumBuffers];
13 const GLuint NumVertices = 6;
14 
15 void
16 init(void)
17 {
18     glGenVertexArrays(NumVAOs, VAOs);
19     glBindVertexArray(VAOs[Triangles]);
20     GLfloat vertices[NumVertices][2] = {
21         { -0.90, -0.90 }, // Triangle 1
22         { 0.85, -0.90 },
23         { -0.90, 0.85 },
24         { 0.90, -0.85 }, // Triangle 2
25         { 0.90, 0.90 },
26         { -0.85, 0.90 }
27     };
28     glGenBuffers(NumBuffers, Buffers);
29     glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
30     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
31         vertices, GL_STATIC_DRAW);
32     ShaderInfo shaders[] = {
33         { GL_VERTEX_SHADER, "triangles.vert" },
34         { GL_FRAGMENT_SHADER, "triangles.frag" },
35         { GL_NONE, NULL }
36     };
37     GLuint program = LoadShaders(shaders);
38     glUseProgram(program);
39     glVertexAttribPointer(vPosition, 2, GL_FLOAT,
40         GL_FALSE, 0, BUFFER_OFFSET(0));
41     glEnableVertexAttribArray(vPosition);
42 }
43 
44 void
45 display(void)
46 {
47     glClear(GL_COLOR_BUFFER_BIT);
48     glBindVertexArray(VAOs[Triangles]);
49     glDrawArrays(GL_TRIANGLES, 0, NumVertices);
50     glFlush();
51 }
52 
53 int
54 main(int argc, char** argv)
55 {
56     glutInit(&argc, argv);
57     glutInitDisplayMode(GLUT_RGBA);
58     glutInitWindowSize(512, 512);
59     glutInitContextVersion(4, 3);
60     glutInitContextProfile(GLUT_CORE_PROFILE);
61     glutCreateWindow(argv[0]);
62     if (glewInit()) {
63         cerr << "Unable to initialize GLEW ... exiting" << endl;
64         exit(EXIT_FAILURE);
65     }
66     init();
67     glutDisplayFunc(display);
68     glutMainLoop();
69 }

shader代码如下:

//顶点shader
#version 430 core

layout(location = 0) in vec4 vPosition;

void main(){
    gl_Position = vPosition;
}
//片元shader
#version 430 core

out vec4 fColor;

void main(){
    fColor = vec4(0.0, 0.0, 1.0, 1.0);
}

在编译成功运行的时候老是在glGenVertexArrays和glBindVertexArray两个函数出crash,在stackoverflow上面找到了解决的办法,GLEW has trouble accessing some parts of the opengl core profile by default, so saying "glewExperimental = GL_TRUE;" before your call to glewInit() might help.因此只需要加这么一行代码就可以了

 1 int
 2 main(int argc, char** argv)
 3 {
 4     glutInit(&argc, argv);
 5     glutInitDisplayMode(GLUT_RGBA);
 6     glutInitWindowSize(512, 512);
 7     glutInitContextVersion(4, 3);
 8     glutInitContextProfile(GLUT_CORE_PROFILE);
 9     glutCreateWindow(argv[0]);
10     glewExperimental = GL_TRUE;//add
11     if (glewInit()) {
12         cerr << "Unable to initialize GLEW ... exiting" << endl;
13         exit(EXIT_FAILURE);
14     }
15     init();
16     glutDisplayFunc(display);
17     glutMainLoop();
18 }

运行结果如下

 

技术分享

编译opengl编程指南第八版示例代码通过

标签:

原文地址:http://www.cnblogs.com/zxh1210603696/p/4556879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!