2015-11-09 59 views
0

我對openGL的使用經驗並不好,所以這對你來說可能看起來很sl but,但我有一個使用opengl渲染物體的項目,使用我自己的着色器。然後,我應該可以使用我的鼠標和WASD鍵在相機周圍自由漫遊。我看不到對象,而我的按鍵和鼠標不會影響對象。我一直盯着這個小時改變一些小東西,但似乎沒有任何工作,我真的需要一雙新的眼睛來看待這個。我知道vmath已經老了,而且過剩了,但這是我們的教授希望我們使用的。 這些是我相信問題所在的代碼。 這是我的主:OpenGL程序/ w着色器,相機沒有正確移動

int main(int argc, char **argv) 
{ 

    glutInit(&argc, argv); 

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
    glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH) - 750)/2, (glutGet(GLUT_SCREEN_HEIGHT) - 750)/2); 
    glutInitWindowSize(width, height); 
    glutCreateWindow("Assignment Two"); 

    glewExperimental = GL_TRUE; 
    glewInit(); 

    glViewport(0, 0, width, height); 

    glutPassiveMotionFunc(mouseMove); 
    glutKeyboardFunc(keyboard); 
    glutDisplayFunc(display); 
    glutIdleFunc(display); 
    glEnable(GL_DEPTH_TEST); 

    glGenVertexArrays(1, &VAO); 
    glGenBuffers(1, &VBO); 
    glBindVertexArray(VAO); 

    glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0); 
    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); 
    glEnableVertexAttribArray(2); 

    glBindVertexArray(0); 

    glutMainLoop(); 
    return 1; 
} 

這是在過剩閒置和顯示功能,用我的顯示功能:

void display() 
{ 
    Shader ourShader("Shader.vs", "Shader.frag"); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f); 
    ourShader.Use(); 
    vmath::mat4 view; 
    view = vmath::lookat(cPosition, cFront + cPosition, cUp); 
    vmath::mat4 projection; 
    projection = vmath::perspective(45.0f, (GLfloat)width/(GLfloat)height, nearC, farC); 

    GLint modelLoc = glGetUniformLocation(ourShader.Program, "model"); 
    GLint viewLoc = glGetUniformLocation(ourShader.Program, "view"); 
    GLint projLoc = glGetUniformLocation(ourShader.Program, "projection"); 

    glUniformMatrix4fv(viewLoc, 1, GL_FALSE, view); 
    glUniformMatrix4fv(projLoc, 1, GL_FALSE, projection); 

    glBindVertexArray(VAO); 
    for (GLuint i = 0; i < 10; i++) 
    { 
     vmath::mat4 model; 
     model *= vmath::translate(cubePositions[i]); 
     GLfloat angle = 20.0f * i; 
     model *= vmath::rotate(angle, cubePositions[i]); 
     glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model); 

     glDrawArrays(renderType, 0, 36); 
    } 
    glBindVertexArray(0); 

    glFlush(); 
    glutSwapBuffers(); 
} 

這是鍵盤被稱爲在我glutkeyboardfunc:

void keyboard(unsigned char key, int xx, int yy) 
{ 
    GLfloat cameraSpeed = 0.01f; 
    int i, j; 
    std::string userInput; 
    if (key == 'q' || key == 'Q') 
    { 
     glDeleteVertexArrays(1, &VAO); 
     glDeleteBuffers(1, &VBO); 
     exit(0); 
    } 
    else if (key == 'w' || key == 'W') 
    { 
     cPosition += cameraSpeed * cFront; 
    } 
    else if (key == 's' || key == 'S') 
    { 
     cPosition -= cameraSpeed * cFront; 
    } 
    else if (key == 'a' || key == 'A') 
    { 
     cPosition -= vmath::normalize(vmath::cross(cFront, cUp)) * cameraSpeed; 
    } 
    else if (key == 'd' || key == 'D') 
    { 
     cPosition += vmath::normalize(vmath::cross(cFront, cUp)) * cameraSpeed; 
    } 
} 

然後這是我在我的glutPassiveMotionFunc中調用的mouseMove函數:

void mouseMove(int posX, int posY) 
{ 
    GLfloat xOffset; 
    GLfloat yOffset; 
    GLfloat sensitivity = 0.2f; 
    if (firstTime) 
    { 
     lastX = (float)posX; 
     lastY = (float)posY; 
     firstTime = false; 
    } 
    xOffset = posX - lastX; 
    yOffset = lastY - posY; 
    lastX = (GLfloat)posX; 
    lastY = (GLfloat)posY; 

    xOffset *= sensitivity; 
    yOffset *= sensitivity; 

    yaw += xOffset; 
    pitch += yOffset; 

    if (pitch > 89.0f) 
    { 
     pitch = 89.0f; 
    } 
    if (pitch < -89.0f) 
    { 
     pitch = -89.0f; 
    } 

    vmath::vec3 front; 
    front[0] = (GLfloat)(cos((M_PI/180)*yaw) * cos((M_PI/180)*pitch)); 
    front[1] = (GLfloat)(sin((M_PI/180)*pitch)); 
    front[2] = (GLfloat)(sin((M_PI/180)*yaw) * cos((M_PI/180)*pitch)); 
    cFront = vmath::normalize(front); 
} 

這裏是我的Shader.vs:

layout (location = 0) in vec3 position; 

uniform mat4 model; 
uniform mat4 view; 
uniform mat4 projection; 

void main() 
{ 
    gl_Position = projection * view * model * vec4(position, 1.0f); 
} 

我想必須有這樣一個事實:自己是不是被更改正確,或者無法正確更新的錯誤。如果您希望我發佈任何內容或讓我知道其他任何內容與我的代碼,請告訴我。

+0

檢查你的頂點着色器,可能是有一些錯誤觀點矩陣乘法。 –

+0

@MichaelNastenko我會將.vs着色器添加到我的帖子中 – Doncks

回答

0

您忘記了在模型循環中初始化vmath::mat4 model

你可以這樣做:

for (GLuint i = 0; i < 10; i++) 
{ 
    vmath::mat4 model(vmath::translate(cubePositions[i])); 
    GLfloat angle = 20.0f * i; 
    model *= vmath::rotate(angle, cubePositions[i]); 
    glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model); 

    glDrawArrays(renderType, 0, 36); 
} 
相關問題