我的程序的目標是顯示一個簡單的Y軸上旋轉的彩色三角形,沒有任何平移。我正在使用GLM庫。問題是我的三角形的轉換不正確:它有一個奇怪而不合邏輯的行爲。這裏是我的C++代碼以及頂點着色器和片段着色器代碼:使用openGL GLM和着色器不正確的旋轉
C++代碼:
float angle = 0.0f;
struct Vertex
{
float x, y, z;
};
static Vertex vertices[9] =
{
-1.000000f, 0.000000f, 0.000000f,
0.000000f, 1.000000f, 0.000000f,
1.000000f, 0.000000f, 0.000000f
};
static Vertex colors[9] =
{
1.000000f, 0.000000f, 0.000000f,
0.000000f, 1.000000f, 0.000000f,
0.000000f, 0.000000f, 1.000000f
};
[...]
int
main(int ac, char **av)
{
bool continuer = true;
SDL_Event event;
GLuint vboID[2];
GLuint programID = 0;
//SDL window initialization
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("VBO tests",NULL);
SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL);
//Viewport initialization
glViewport(0, 0, WIDTH, HEIGHT);
//Projection initialization
glm::mat4 projection = glm::mat4(1.0f);
projection = glm::perspective<GLfloat>(60.0f, (float)(WIDTH/HEIGHT), 1.0f, 1000.0f);
//View initialization
glm::mat4 view = glm::lookAt<GLfloat>(glm::vec3(0.0f, 0.0f, 8.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glewInit();
//Shaders initialization
programID = initShaders("triangle.vert", "triangle.frag");
//Main loop
while (continuer)
{
eventListener(&event, &continuer);
glClearDepth(1.0f);
glClearColor(0.13f, 0.12f, 0.13f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
//Model transformations
glm::mat4 model = glm::mat4(1.0f);
model *= glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f));
model *= glm::rotate(model, angle, glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 ModelViewMatrix = model * view;
glm::mat4 ModelViewProjectionMatrix = projection * ModelViewMatrix;
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, colors);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glUniformMatrix4fv(glGetUniformLocation(programID, "ModelView"), 1, GL_TRUE, glm::value_ptr(ModelViewMatrix));
glUniformMatrix4fv(glGetUniformLocation(programID, "MVP"), 1, GL_TRUE, glm::value_ptr(ModelViewProjectionMatrix));
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
glUseProgram(0);
angle += 0.020f;
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return (0);
}
頂點着色器代碼:
#version 330
in vec3 VertexPosition;
in vec3 VertexColor;
uniform mat4 ModelViewMatrix;
uniform mat4 MVP;
out vec3 Color;
void main()
{
Color = VertexColor;
gl_Position = MVP * vec4(VertexPosition, 1.0f);
}
片段着色器代碼:
#version 330
in vec3 Color;
out vec4 FragColor;
void main() {
FragColor = vec4(Color, 1.0);
}
當我爲模型聲明瞭一個簡單的矩陣旋轉,我將它發送給頂點着色器,並將其用作M副總裁,它的作品。但是當我添加投影矩陣+視圖矩陣時,它無法正常工作。這裏有一個畫面:
通常情況下,三角應該是在屏幕的中央。
有人能幫助我嗎?
你是什麼意思,當你說「當我添加投影矩陣+視圖矩陣它不能正常工作」?添加到什麼?什麼工作不正確?你在屏幕上看到什麼? – PowerApp101 2013-04-05 02:45:36
我想說當我多次將視圖,投影和模型矩陣放在一起時,我的照片上面出現了糟糕的渲染效果。但是當我刪除代碼中的視圖和投影時,我只保留模型轉換矩陣。這個三角形當然更接近因爲我沒有視圖和投影,但它正確地轉動了Y軸。我想要具有相同的行爲,但投影和視圖矩陣。我的代碼對你來說似乎是否正確? (C++和頂點着色器) – user1364743 2013-04-05 09:37:27
您可以將代碼發佈到您創建視圖和投影矩陣的位置以及將矩陣相乘的位置嗎? – bwroga 2013-04-05 12:41:12