2017-01-25 42 views
0

我試圖做一個簡單的程序,繞窗口立方體正確呈現。當我運行它,它看起來像立方體的表面正在以正確的順序在第一渲染,但一旦他們不停地旋轉到後面他們表現出通過前,而不是其他的。我使用glEnable(GL_DEPTH_TEST),glenable(GL_CULL_FACE)和glcullface(GL_BACK),我認爲會照顧深度測試,但它仍然沒有工作。這裏是我的腳本,`立方體的表面不被窗口

int windowwidth = 1920; 
int windowheight = 1080; 
GLfloat aspectratio = 16/9.0; 
double horizontalposition = 0; 
double verticalposition = 0; 
double depthposition = 0; 

GLfloat projTop = 1; 
GLfloat projBottom = 0; 
GLfloat floatOne = 1.0; 
GLfloat oneFourth = 1.0/4; 
GLfloat projLeft = 0; 
GLfloat projRight = 1; 
GLfloat zNear = .1; 
GLfloat zFar = 1; 
GLfloat zRange = zNear - zFar; 
GLuint puddletexture; 
GLuint sampler = 0; 
GLuint samplerUniform; 
GLuint shaderProgram; 
GLuint textureunit = 0; 
GLuint positionUniform, colourUniform, rotationViewUniform, projectionUniform,translationUniform, rotationUniform, timeUniform, transformUniform; 
GLuint colourAttribute,secondColourAttribute, positionAttribute, textureAttribute, indexAttribute; 
GLuint vertexArrayObject, 
vertexBuffer; 
GLuint vao, vbo[3]; 
GLuint triangleVao, triangleVbo[2]; 
GLuint elementbuffer; 
GLuint triangleArrayObject, triangleVertexBuffer; 

GLfloat diamond[] = { 
    -1, 1, 1, // vertex[0] 
    1, 1, 1, // vertex[1] 
    1, -1, 1, // vertex[2] 
    -1, -1, 1, // vertex[3] 
    -1, 1, -1, // vertex[4] 
    1, 1, -1, // vertex[5] 
    1, -1, -1, // vertex[6] 
    -1, -1, -1, // vertex[7] 
}; 

GLuint cubeindicies[] = { 
    0, 1, 2, 2, 3, 0, // front face 
    3, 2, 6, 6, 7, 3, // top face 
    7, 6, 5, 5, 4, 7, // back face 
    4, 0, 3, 3, 7, 4, // left face 
    0, 1, 5, 5, 4, 0, // bottom face 
    1, 5, 6, 6, 2, 1, 
}; 


GLfloat colors[4][4] = { 
    { 1.0, 0.0, 0.0, 0.0}, /* Red */ 
    { 1.0, 1.0, 0.0, 0.0}, /* Green */ 
    { 0.0, 0.0, 1.0, 0.0}, /* Blue */ 
    { 1.0, 1.0, 1.0, 0.0}, 

}; 

GLfloat picturetexturecoordinates[] = { 
    0,1,0, 
    0,0,0, 
    1,0,0, 
    1,1,0, 
}; 



GLuint loadTexture(Image* image) { 
    GLuint textureId; 
    glGenTextures(1, &textureId); //Make room for our texture 
    glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glTexImage2D(GL_TEXTURE_2D,    //Always GL_TEXTURE_2D 
     0,       //0 for now 
     GL_RGB,      //Format OpenGL uses for image 
     image->width, image->height, //Width and height 
     0,       //The border of the image 
     GL_RGB, //GL_RGB, because pixels are stored in RGB format 
     GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored 
          //as unsigned numbers 
     image->pixels);    //The actual pixel data 
    return textureId; //Returns the id of the texture 
} 

void reshape(int w, int h) { 
    glViewport(0, 0, w, h); 
    gluPerspective(45.0f, (float)w/(float)h, 1.0f, 100.0f); 

    windowwidth = glutGet(GLUT_WINDOW_WIDTH); 
    windowheight = glutGet(GLUT_WINDOW_HEIGHT); 
    aspectratio = (double)windowwidth/windowheight; 
} 

void animate() { 
    glutPostRedisplay(); 
} 

void visible(int vis) { 
    if (vis == GLUT_VISIBLE) 
     glutIdleFunc(animate); 
    else 
     glutIdleFunc(0); 
} 

void loadShader(); 
void display(); 


void loadShader() { 
    shaderProgram = InitShader("beelsebob.vert", "beelsebob.frag", "fragColour"); 
    positionUniform = glGetUniformLocation(shaderProgram, "p"); 
    if (positionUniform < 0) { 
     std::cerr << "Shader did not contain the 'p' uniform" << std::endl; 
    } 
    colourUniform = glGetUniformLocation(shaderProgram, "c"); 
    if (colourUniform < 0) { 
     std::cerr << "Shader did not contain the 'c' uniform" << std::endl; 
    } 
    timeUniform = glGetUniformLocation(shaderProgram, "timer"); 
    if (timeUniform < 0) { 
     std::cerr << "Shader did not contain the 'timer' uniform" << std::endl; 
    } 
    samplerUniform = glGetUniformLocation(shaderProgram, "textSampler"); 
    if (samplerUniform < 0) { 
     std::cerr << "Shader did not contain the 'textSampler' uniform" << std::endl; 
    } 
    projectionUniform = glGetUniformLocation(shaderProgram, "projectionMatrix"); 
    if (projectionUniform < 0) { 
     std::cerr << "Shader did not contain the 'projectMatrix' uniform" << std::endl; 
    } 
    rotationViewUniform = glGetUniformLocation(shaderProgram, "rotationViewMatrix"); 
    if (rotationViewUniform < 0) { 
     std::cerr << "Shader did not contain the 'rotationViewMatrix' uniform" << std::endl; 
    } 
    transformUniform = glGetUniformLocation(shaderProgram, "transformMatrix"); 
    if (transformUniform < 0) { 
     std::cerr << "Shader did not contain the 'transformMatrix' uniform" << std::endl; 
    } 
    translationUniform = glGetUniformLocation(shaderProgram, "translationMatrix"); 
    if (translationUniform < 0) { 
     std::cerr << "Shader did not contain the 'translationMatrix' uniform" << std::endl; 
    } 
    translationUniform = glGetUniformLocation(shaderProgram, "translationMatrix"); 
    if (translationUniform < 0) { 
     std::cerr << "Shader did not contain the 'translationMatrix' uniform" << std::endl; 
    } 
    colourAttribute = glGetAttribLocation(shaderProgram, "colour"); 
    if (colourAttribute < 0) { 
     std::cerr << "Shader did not contain the 'colour' attribute." << std::endl; 
    } 
    secondColourAttribute = glGetAttribLocation(shaderProgram, "secondColour"); 
    if (secondColourAttribute < 0) { 
     std::cerr << "Shader did not contain the 'secondColour' attribute." << std::endl; 
    } 
    positionAttribute = glGetAttribLocation(shaderProgram, "position"); 
    if (positionAttribute < 0) { 
     std::cerr << "Shader did not contain the 'position' attribute." << std::endl; 
    } 
    textureAttribute = glGetAttribLocation(shaderProgram, "textcoord"); 
    if (textureAttribute < 0) { 
     std::cerr << "Shader did not contain the 'textcoord' attribute." << std::endl; 
    } 

} 


void display() { 
    glClearColor(0.0, 0.0, .2, 1.0); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glEnable(GLUT_MULTISAMPLE); 
    glLoadIdentity(); 
    const float timeScale = 0.0008f; 

    glUseProgram(shaderProgram); 
    GLfloat timeValue = glutGet(GLUT_ELAPSED_TIME)*timeScale; 
    vec3 p3(.5f*sin(timeValue) ,0 , 0.5f*cosf(timeValue)); 
    //vec4 c4(.5f*sinf(timeValue), .1f*sinf(timeValue), 9*cosf(timeValue), 1.0); 
    vec4 c4(.7f, .5f, 0.01, 0.0); 
    mat4 rotationMat(
    { cos(timeValue), sin(timeValue), 0,0 }, 

    { 0,1,0,0 }, 
    { -sin(timeValue), cos(timeValue), 1,0 }, 
    { 0,0,0,1 } 
    ); 
    mat4 transMat(
    {1,0,0,0}, 
    {0,1,0,0}, 
    {0,0,(GLfloat)(windowheight*1.0/windowwidth),0}, 
    {0,0,0,1} 
    ); 
    mat4 projMat(
    {zFar/aspectratio, 0, 0, 0 }, 
    { 0, zFar, 0, 0 }, 
    { 0,0, (zFar+zNear)/(zNear-zFar), (2*zFar*zNear)/(zNear-zFar) }, 
    { 0,0,0,1} 
    ); 

    mat4 completeTransformMatrix = projMat*transMat*rotationMat; 
    glUniformMatrix4fv(projectionUniform, 1, GL_FALSE, projMat); 
    glUniformMatrix4fv(rotationViewUniform, 1, GL_FALSE, rotationMat); 
    glUniformMatrix4fv(translationUniform, 1, GL_FALSE, transMat); 
    glUniformMatrix4fv(transformUniform, 1, GL_FALSE, completeTransformMatrix); 
    glUniform3fv(positionUniform,1,(const GLfloat*)&p3); 
    glUniform4fv(colourUniform, 1, (const GLfloat*)&c4); 
    glUniform1i(samplerUniform, 0); 
    glUniform1fv(timeUniform, 1, (const GLfloat*)&timeValue); 
    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, squareTexture); 
    glGenSamplers(1, &sampler); 
    glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glBindSampler(0, sampler); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); 
    glBindVertexArray(vao); 
    glDrawElements(GL_TRIANGLES, sizeof(cubeindicies)/sizeof(GLuint), GL_UNSIGNED_INT, 0); 
    glBindVertexArray(0); 
    glBindTexture(GL_TEXTURE_2D, 0); 
    glUseProgram(0); 
    glutSwapBuffers(); 

} 


double r = 0; 
void loadBufferData() { 
    glEnable(GL_DEPTH_TEST); 
    glEnable(GL_CULL_FACE); 
    glCullFace(GL_BACK); 
    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 
    glEnable(GL_NORMALIZE); 
    glEnable(GL_COLOR_MATERIAL); 
    glEnable(GLUT_MULTISAMPLE); 
    glEnable(GL_PERSPECTIVE_CORRECTION_HINT); 
    Image* squareImage = loadBMP("puddletexture.bmp"); 
    squareTexture = loadTexture(squareImage); 
    delete squareImage; 

    glGenVertexArrays(1, &vao); 
    glBindVertexArray(vao); 
    glGenBuffers(4, vbo); 

    glEnableVertexAttribArray(positionAttribute); 
    glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(diamond), diamond, GL_STATIC_DRAW); 
    glVertexAttribPointer(positionAttribute, 3, GL_FLOAT, GL_FALSE, 0, 0); 

    glEnableVertexAttribArray(colourAttribute); 
    glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW); 
    glVertexAttribPointer(colourAttribute, 4, GL_FLOAT, GL_FALSE, 0, 0); 

    glEnableVertexAttribArray(textureAttribute); 
    glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(picturetexturecoordinates), picturetexturecoordinates, GL_STATIC_DRAW); 
    glVertexAttribPointer(textureAttribute, 3, GL_FLOAT, GL_FALSE, 0, 0); 

    glEnableVertexAttribArray(indexAttribute); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cubeindicies), cubeindicies, GL_STATIC_DRAW); 

} 


int main(int argc, char* argv[]) 
{ 
    glutInit(&argc, argv); 

    glutInitContextVersion(3, 2); 
    glutInitContextProfile(GLUT_CORE_PROFILE); 
    glutSetOption(
     GLUT_ACTION_ON_WINDOW_CLOSE, 
     GLUT_ACTION_GLUTMAINLOOP_RETURNS 
    ); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE); 
    glutCreateWindow("02561"); 
    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); 
    glutVisibilityFunc(visible); 
    glutIdleFunc(animate); 
    glutReshapeWindow(600, 400); 

    glewExperimental = GL_TRUE; 
    GLint GlewInitResult = glewInit(); 
    if (GlewInitResult != GLEW_OK) { 
     printf("ERROR: %s\n", glewGetErrorString(GlewInitResult)); 
    } 

    loadShader(); 
    loadBufferData(); 
    glutMainLoop(); 
} 

頂點着色器:

#version 330 

uniform vec3 p; 
uniform mat4 transformMatrix; 
uniform vec4 c; 
in vec4 position; 
in vec4 colour; 
out vec4 colourV; 
in vec4 secondColour; 
out vec4 secondColourV; 
in vec2 textcoord; 
out vec2 textcoordV; 


void main (void) 
{ 
    colourV = colour; 
    colourV += c; 
    textcoordV = textcoord; 
    gl_Position = transformMatrix*position + vec4(p, 3.0); 
} 

和片段着色器:

#version 330 

uniform sampler2D textSampler; 
in vec4 colourV; 
in vec4 secondColourV; 
out vec4 fragColour; 
in vec2 textcoordV; 
out vec2 fragTexture; 
void main(void) 
{ 
    fragColour = colourV*texture(textSampler, textcoordV); 
} 

這裏是顯示當我運行的窗口,一對夫婦幀的圖像它,

windowframes 所以現在我卡住了,有沒有人知道什麼cou ld是問題嗎?

回答

1

您的所有基質都似乎是錯誤的。

rotationMat不是有效的旋轉矩陣。它們都具有共同點,即與旋轉軸對應的行和列是未觸及的。 (x旋轉將第一行和第一列設置爲[1,0,0,0],y旋轉將第二行和第二列設置爲[0,1,0,0])。在你的矩陣中,第二行和第三列未被設置。 Source

transMat雖然使用看起來好像應該是一個,但並不是絕對的翻譯矩陣。目前執行沿着z軸的縮放。

projMat是完全錯誤的。目前,這是一種奇怪的正射投影,但以某種方式根據遠平面值投影x和y軸?如果要編寫透視投影矩陣,則前兩行中的zFar應爲cotan(field_of_view/2),第四行應爲[0, 0, -1, 0]Source

因爲你似乎有很多的問題,編寫正確的矩陣,我會強烈建議您使用庫爲(例如glm)。

此外,您在設置中不着色器存在制服(例如translationMatrix),你在安裝這些代碼是否制服存在檢查,但隨後嘗試設置他們display不管這種檢查的,應導致OpenGL錯誤(檢查glGetError())。