我要提高我的open gl知识。好吧,我失败得很惨。当我尝试使用我研究中的一些代码与本教程混合渲染一个简单的三角形时,我遇到了崩溃:http ://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/
知道我为什么会崩溃(没有来自 gdb 的更多信息,只是 glDrawArrays 调用导致崩溃)。我在我的 64 位 lubuntu 上安装了一个实验性的 gpu 驱动程序,但是 3D 游戏和其他代码可以工作。不知道我做错了什么。PS:我是为自己做的,没有“功课”之类的。
#include <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtx/string_cast.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <stack>
#include <cmath>
#include <sstream>
#include <vector>
GLuint triangleVertexBufferId;
GLuint attribPointer;
void updateGL()
{
// clear buffer, enable transparency
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glEnable( GL_BLEND );
//glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferId);
std::cout << "Loop: " << triangleVertexBufferId << std::endl;
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferId);
/*glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);*/
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
// swap renderbuffers for smooth rendering //
glutSwapBuffers();
}
void idle()
{
glutPostRedisplay();
}
// Callback function called by GLUT when window size changes
void Reshape(int width, int height)
{
glutPostRedisplay();
}
void Terminate(void)
{
}
int main(int argc, char** argv)
{
std::cout << "Hi" << std::endl;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitContextVersion(3,3);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitWindowSize (1024, 768);
glutInitWindowPosition (10, 10);
glutCreateWindow("Exercise 04 - Camera control and projection");
glutCreateMenu(NULL);
glutDisplayFunc(updateGL);
glutReshapeFunc(Reshape);
glutIdleFunc(idle);
atexit(Terminate);
glewInit();
std::vector<glm::vec3> vertices;
std::vector<glm::vec3> normals;
std::vector<glm::vec2> uvs;
std::vector<glm::vec3> triangle_index;
vertices.push_back(glm::vec3(-1.0f,-1.0f,0.0f));
vertices.push_back(glm::vec3(1.0f,-1.0f,0.0f));
vertices.push_back(glm::vec3(0.0f,1.0f,0.0f));
std::cout << "Main: " << triangleVertexBufferId << std::endl;
glGenVertexArrays(1,&triangleVertexBufferId);
glBindVertexArray(triangleVertexBufferId);
GLfloat rawVertexData[vertices.size()*3];
for(unsigned int i = 0; i < vertices.size();i=i+3)
{
rawVertexData[i] = vertices[i].x;
rawVertexData[i+1] = vertices[i].y;
rawVertexData[i+2] = vertices[i].z;
}
glBufferData(GL_ARRAY_BUFFER, vertices.size()*3*sizeof(GLfloat), &rawVertexData[0], GL_STATIC_DRAW);
std::cout << "Main: " << triangleVertexBufferId << std::endl;
glutMainLoop();
return(0);
}