![](/img/trans.png)
[英]Garbled Triangles from VBO/Shader/MVP Matrix using OpenGL 3.3
[英]Using Matrices in OpenGL — MVP matrix not working
今天,我試圖按照以下教程進行操作: http : //www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ ,我覺得我幾乎完全遵循了該教程,但是我似乎無法得到相同的結果。 我確實使用了一些我定義的類來幫助處理基本操作,但是這些並沒有錯,因為我之前使用過它們。
當前結果什么都沒有-我在任何地方都看不到三角形。 如果我擺脫了vert shader的MVP *部分,那就可以了
本教程復制並粘貼了很多代碼。
我排除的問題:1.錯誤的設置(我下載並編譯了本教程的源代碼。)2.錯誤的初始化代碼(如果將MVP *部分從頂點着色器中取出,則效果很好)
感謝您能獲得的所有幫助。
頂點着色器(Basic.vertexshader):
#version 400 core
layout(location = 0) in vec3 location;
uniform mat4 MVP;
void main()
{
gl_Position = MVP * vec4(location,1);
// gl_Position = vec4(location, 1); *** this works fine, without the transformations
//coord = texCoord;
}
這是初始化代碼(僅調用一次)
glClearColor(0.2f, 0.2f, 0.2f, 1.f);
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
// load program
pro = LoadShaders("Basic.vertexshader", "Basic.fragmentshader");
MVPID = glGetUniformLocation(pro, "MVP");
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
Projection = glm::perspective(45.0f, 4.f / 3.f, 0.1f, 100.f);
// Or, for an ortho camera :
//glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates
// Camera matrix
View = glm::lookAt(
glm::vec3(4, 3, 0), // Camera is at (4,3,3), in World Space
glm::vec3(0, 0, 0), // and looks at the origin
glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
);
// Model matrix : an identity matrix (model will be at the origin)
Model = glm::mat4(1.0f);
// Our ModelViewProjection : multiplication of our 3 matrices
// Our ModelViewProjection : multiplication of our 3 matrices
MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
GLfloat verts[] =
{// ---LOCATION----
-1.f, -1.f, 0.f,
1.f, -1.f, 0.f,
0.f, 1.f, 0.f,
};
// init vbo
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
return 0;
最后是繪制代碼(每幀):
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(pro);
glUniformMatrix4fv(MVPID, 1, GL_FALSE, &MVP[0][0]);
// bind location
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
如果我錯過了一些重要的代碼,則可以在此處下載整個源代碼:
https://www.dropbox.com/sh/zb3r85rj7lgfzs2/AADh0nJSVw5dO2fUAJdrnrTua?dl=0
編輯:如果有關系,我正在使用Visual Studio 2013編譯器鏈接到的.lib文件是glfw3.lib,opengl32.lib,glew32.lib
您正在看三角形的邊緣。 該三角形在x / y平面中,因為所有z坐標均為零:
GLfloat verts[] =
{
-1.f, -1.f, 0.f,
1.f, -1.f, 0.f,
0.f, 1.f, 0.f,
};
攝像機也位於x / y平面中,並指向原點:
View = glm::lookAt(
glm::vec3(4, 3, 0),
glm::vec3(0, 0, 0),
glm::vec3(0, 1, 0)
);
由於三角形和相機位於同一平面上,因此三角形在變換后會退化,並且不會渲染任何像素。
最簡單的解決方法是通過將相機位置設置為非零的z坐標,將相機位置移動到x / y平面之外。 例如:
View = glm::lookAt(
glm::vec3(4.0f, 3.0f, 10.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f)
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.