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

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

 #include <iostream>
#include "vgl.h"
#include "LoadShaders.h" using namespace std; enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = };
GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
const GLuint NumVertices = ; void
init(void)
{
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);
GLfloat vertices[NumVertices][] = {
{ -0.90, -0.90 }, // Triangle 1
{ 0.85, -0.90 },
{ -0.90, 0.85 },
{ 0.90, -0.85 }, // Triangle 2
{ 0.90, 0.90 },
{ -0.85, 0.90 }
};
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
vertices, GL_STATIC_DRAW);
ShaderInfo shaders[] = {
{ GL_VERTEX_SHADER, "triangles.vert" },
{ GL_FRAGMENT_SHADER, "triangles.frag" },
{ GL_NONE, NULL }
};
GLuint program = LoadShaders(shaders);
glUseProgram(program);
glVertexAttribPointer(vPosition, , GL_FLOAT,
GL_FALSE, , BUFFER_OFFSET());
glEnableVertexAttribArray(vPosition);
} void
display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, , NumVertices);
glFlush();
} int
main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(, );
glutInitContextVersion(, );
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[]);
if (glewInit()) {
cerr << "Unable to initialize GLEW ... exiting" << endl;
exit(EXIT_FAILURE);
}
init();
glutDisplayFunc(display);
glutMainLoop();
}

shader代码如下:

//顶点shader
#version core layout(location = ) in vec4 vPosition; void main(){
gl_Position = vPosition;
}
//片元shader
#version core out vec4 fColor; void main(){
fColor = vec4(0.0, 0.0, 1.0, 1.0);
}

在编译成功运行的时候老是在glGenVertexArrays和glBindVertexArray两个函数出crash,在*上面找到了解决的办法,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.因此只需要加这么一行代码就可以了

 int
main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(, );
glutInitContextVersion(, );
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[]);
glewExperimental = GL_TRUE;//add
if (glewInit()) {
cerr << "Unable to initialize GLEW ... exiting" << endl;
exit(EXIT_FAILURE);
}
init();
glutDisplayFunc(display);
glutMainLoop();
}

运行结果如下

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

上一篇:A brief introduction to weakly supervised learning(简要介绍弱监督学习)


下一篇:2021.09.18 膜你赛