0
我一直在嘗試將頂點數據從矢量上傳到VBO以渲染帶紋理的彩色正方形。當我運行該程序時,它在「glDrawArrays(...)」中斷,並且我在「Tutorial.exe中的nvoglv64.dll:讀取位置的訪問衝突...」中得到「第一次機會異常...」確定我做錯了什麼。雖然我的主要目標是在精靈批量使用此,這裏的簡單的例子:如何在OpenGL中使用向量和結構上傳頂點數據?
Game::Game()
{
Oreg::Core::init();
//INIT VAO
tex = Oreg::FileSys::getTex("Feels.png");
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_VERTEX_ARRAY, vbo);
GLuint posAtt = Oreg::DefShader::texture2D.getAtt("vert");
GLuint texAtt = Oreg::DefShader::texture2D.getAtt("texcoords");
GLuint colAtt = Oreg::DefShader::texture2D.getAtt("color");
glEnableVertexAttribArray(posAtt);
glEnableVertexAttribArray(texAtt);
glEnableVertexAttribArray(colAtt);
glVertexAttribPointer(posAtt, 2, GL_FLOAT, GL_FALSE, sizeof(Det::Vert), (GLvoid*)offsetof(Det::Vert, pos));
glVertexAttribPointer(texAtt, 2, GL_FLOAT, GL_FALSE, sizeof(Det::Vert), (GLvoid*)offsetof(Det::Vert, uv));
glVertexAttribPointer(colAtt, 4, GL_FLOAT, GL_FALSE, sizeof(Det::Vert), (GLvoid*)offsetof(Det::Vert, col));
glBindVertexArray(0);
std::cout<<"Size of GLfloat: "<<sizeof(GLfloat)<<"\n"; //4
std::cout<<"Size of Vert: "<<sizeof(Det::Vert)<<"\n"; //32
std::cout<<"Offset of Vert's pos: "<<offsetof(Det::Vert, pos)<<"\n"; //0
std::cout<<"Offset of Vert's uv: "<<offsetof(Det::Vert, uv)<<"\n"; //8
std::cout<<"Offset of Vert's col: "<<offsetof(Det::Vert, col)<<"\n"; //16
//UPLOAD VERTS
std::vector<Det::Vert> verts;
verts.resize(6);
verts.at(0).setPos(0, 0);
verts.at(0).setUV(0, 0);
verts.at(0).setCol(1, 0, 1, 1);
verts.at(1).setPos(50, 0);
verts.at(1).setUV(1, 0);
verts.at(1).setCol(1, 0, 1, 1);
verts.at(2).setPos(50, 50);
verts.at(2).setUV(1, 1);
verts.at(2).setCol(1, 0, 1, 1);
verts.at(3).setPos(50, 50);
verts.at(3).setUV(1, 1);
verts.at(3).setCol(1, 0, 1, 1);
verts.at(4).setPos(0, 50);
verts.at(4).setUV(0, 1);
verts.at(4).setCol(1, 0, 1, 1);
verts.at(5).setPos(0, 0);
verts.at(5).setUV(0, 0);
verts.at(5).setCol(1, 0, 1, 1);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, verts.size()*sizeof(Det::Vert), verts.data(), GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Game::update()
{
//BEGIN RENDER
Oreg::Window::clear(0.0f, 0.06f, 0.09f, 1.0f);
Oreg::Window::renderSettings();
Oreg::DefShader::texture2D.use();
glm::mat4 mat = glm::ortho(0.0f, (float)Oreg::Window::getRes().x, (float)Oreg::Window::getRes().y, 0.0f, -1.0f, 1.0f);
glUniform1i(Oreg::DefShader::texture2D.getUni("tex"), 0);
glUniformMatrix4fv(Oreg::DefShader::texture2D.getUni("matrix"), 1, GL_FALSE, glm::value_ptr(mat));
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
//RENDER
glBindVertexArray(vao);
glBindTexture(GL_TEXTURE_2D, tex->id);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
//END RENDER
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glUseProgram(0);
Oreg::Window::showBuffer();
}
Game::~Game(){ Oreg::Core::dest(); }
謝謝你,那是錯的。我之前調用過glGetError(),但自從調用「glewExperimental = GL_TRUE;」自動觸發GL_INVALID_ENUM錯誤,我不幸將它刷掉。 – SelymAI 2014-10-12 03:09:34