조명

2014-04-03 4 views
0

가 여기 내 쉐이더 프로그램 쉐이더 프로그램 오작동 :조명

#version 330 core 

// Input vertex data, different for all executions of this shader. 
layout(location = 0) in vec3 vertexPosition_modelspace; 
layout(location = 1) in vec3 vertexNormal_modelspace; 


// Values that stay constant for the whole mesh. 
uniform mat4 MVP; 
uniform mat4 V; 
uniform mat4 M; 
uniform mat3 blNormalMatrix; 
uniform vec3 lightPos; 
out vec4 forFragColor; 
const vec3 diffuseColor = vec3(0.55, 0.09, 0.09); 

void main(){ 

    // Output position of the vertex, in clip space : MVP * position 
    gl_Position = MVP * vec4(vertexPosition_modelspace,1); 
    vec3 MaterialAmbientColor = vec3(0.1,0.1,0.1) * diffuseColor; 
    // all following gemetric computations are performed in the 
    // camera coordinate system (aka eye coordinates) 
    vec3 vertexNormal_cameraspace = (V*M*vec4(vertexNormal_modelspace,0)).xyz; 
    vec4 vertexPosition_cameraspace4 = V*M* vec4(vertexPosition_modelspace,1); 
    vec3 vertexPosition_cameraspace = vec3(vertexPosition_cameraspace4).xyz; 
    vec3 lightDir = normalize(lightPos - vertexPosition_cameraspace); 

    float lambertian = clamp(dot(lightDir,vertexNormal_cameraspace), 0.0,1.0); 
    forFragColor = vec4(lambertian*diffuseColor , 1.0); 

} 

내 문제는 이것이 기존의 OpenGL을 프로필에 "일"이다, 심지어 버전 번호가 없었어요, 나는 주위에 생각 OpenGL 2.1 또는 그 주요 변경 사항은 원래 정상적으로 = gl_normalMatrix * gl_normal이었고 모든 것이 효과가있었습니다. 나는 3.3 이상 핵심 프로필 업데이트되었습니다 어쩌면 지금이 왼쪽하고 사용되지 않는 기능을 고정 후 교수님의 코드에 기반하지만

:

https://drive.google.com/file/d/0B6oLZ_d7S-U7cVpkUXpVXzdaZEk/edit?usp=sharing이의 동영상 링크 내 프로그램의 동작.

광원은 움직이지 않아야하는 (0,0,3) 지점 광이어야합니다. 하지만 특히 논리적 인 행동 패턴을 따르지 않는 경우, 나는 그것을 이해할 수 없습니다.

모델 행렬의 역행렬을 전달하여 대체 normalMatrix로 사용하려고 시도했지만 내 법선을 파멸 시켰습니다. 그래서 나는 모른다.

내 normalMatrix이었다

glm::mat3 MyNormalMatrix = glm::mat3(glm::transpose(glm::inverse(ModelMatrix))); 

편집 : 여기 내 디스플레이 코드 : 문제는 궁극적으로 내 교수에 의해 제공되는 모델 로더에 문제가 밝혀졌다

glClearColor(0.0f, 0.0f, 0.4f, 0.0f); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glEnable(GL_DEPTH_TEST); 

     // Use our shader 
    glUseProgram(programID); 

    // Get our transformations iff we move the camera around. 
    glm::mat4 MyModelMatrix = ModelMatrix * thisTran * ThisRot; 

    MVP = ProjectionMatrix * ViewMatrix * MyModelMatrix; 
    glm::mat4 ModelView = ViewMatrix * MyModelMatrix; 
    glm::mat3 MyNormalMatrix = glm::mat3(glm::transpose(glm::inverse(ModelView))); 
    glm::vec3 newLightPos = lightPos; 
     // Send our transformation to the currently bound shader, 
     // in the "MVP" uniform 
    glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); 
    glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &MyModelMatrix[0][0]); 
    glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &ViewMatrix[0][0]); 
    glUniformMatrix4fv(BlNormalMatrix,1,GL_FALSE, &MyNormalMatrix[0][0]); 
    glUniformMatrix4fv(BlRotations, 1, GL_FALSE, &ThisRot[0][0]); 
    glUniform3f(BlCamera, cameraLoc.x, cameraLoc.y, cameraLoc.z); 
    glUniform3f(lPosition, newLightPos.x,newLightPos.y,newLightPos.z); 

    // VBO buffer: vertices 
     // 1rst attribute buffer : vertices 
    glEnableVertexAttribArray(0); 
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 
    glVertexAttribPointer(
     0,     // attribute 
     3,     // size 
     GL_FLOAT,   // type 
     GL_FALSE,   // normalized? 
     0,     // stride 
     (void*)0   // array buffer offset 
    ); 

    // 2rd attribute buffer : normals 
    glEnableVertexAttribArray(1); 
    glBindBuffer(GL_ARRAY_BUFFER, normalbuffer); 
    glVertexAttribPointer(
     1,        // attribute 
     3,        // size 
     GL_FLOAT,       // type 
     GL_FALSE,       // normalized? 
     0,        // stride 
     (void*)0       // array buffer offset 
    ); 

    // draw object using opengl 3.3 shit 
    glDrawArrays(GL_TRIANGLES, 0, vertices.size()); 
+0

. 당신은 정점 셰이더의 모델 뷰 매트릭스에 의한 정점의 곱셈과 프래그먼트 셰이더의 lightinh 계산과 같은 작업을 수행해야합니다. –

+0

왜 셰이더 조각에 불이 들어 옵니까? 꼭 필요한가 아니면 버텍스 쉐이더에서 할 수 있습니까? 예전의 OpenGL에서 본 대부분의 예제는 버텍스 쉐이더에 모든 것을 넣었으므로 문제가 어떻게 발생하는지 확신 할 수 없습니다. – RaenirSalazar

답변

0

이었다 어떻게 든 현대의 OpenGL과 호환되지 않으며 좌/우 법선이 분명히 빠졌거나 유효하지 않은 값을 가지고 있다는 점에서 "주로"작동합니다. Assimp 구현을 사용하여 해결되었습니다. 연결 assimp와

코드는 다음과 같이이다 :

void blInitResWAssimp() { 
    cout << "blInitResWAssimp" << endl; 
    blCreateModelViewProjectionMatrix(); 

    //loads object 
    bool res = loadAssImp("Resources/RCSS-subdiv.obj", indices, indexed_vertices, indexed_uvs, indexed_normals); 
    //bool res = loadAssImp("Resources/cheb.obj", indices, indexed_vertices, indexed_uvs, indexed_normals); 

    glGenVertexArrays(1, &VertexArrayID); 
    glBindVertexArray(VertexArrayID); 

    // Load it into a VBO 
    glGenBuffers(1, &vertexbuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 
    glBufferData(GL_ARRAY_BUFFER, indexed_vertices.size() * sizeof(glm::vec3), &indexed_vertices[0], GL_STATIC_DRAW); 

    // Normal buffer 
    glGenBuffers(1, &normalbuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, normalbuffer); 
    glBufferData(GL_ARRAY_BUFFER, indexed_normals.size() * sizeof(glm::vec3), &indexed_normals[0], GL_STATIC_DRAW); 

    // Generate a buffer for the indices as well 
    glGenBuffers(1, &elementbuffer); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW); 

    //ModelMatrix = ModelMatrix * glm::translate(glm::mat4(1.0f), glm::vec3(-0.5, -0.5, 0)); 

    } 

Assimp 물건 당신은 함께 재료를 혼합하는

bool loadAssImp(
    const char * path, 
    std::vector<unsigned short> & indices, 
    std::vector<glm::vec3> & vertices, 
    std::vector<glm::vec2> & uvs, 
    std::vector<glm::vec3> & normals 
    ){ 

    Assimp::Importer importer; 

    const aiScene* scene = importer.ReadFile(path, 0/*aiProcess_JoinIdenticalVertices | aiProcess_SortByPType*/); 
    if (!scene) { 
     fprintf(stderr, importer.GetErrorString()); 
     getchar(); 
     return false; 
    } 
    const aiMesh* mesh = scene->mMeshes[0]; // In this simple example code we always use the 1rst mesh (in OBJ files there is often only one anyway) 
    const aiMaterial* material = scene->mMaterials[0]; 

    // Fill vertices positions 
    vertices.reserve(mesh->mNumVertices); 
    for (unsigned int i = 0; i<mesh->mNumVertices; i++){ 
     aiVector3D pos = mesh->mVertices[i]; 
     vertices.push_back(glm::vec3(pos.x, pos.y, pos.z)); 
    } 

    // Fill vertices texture coordinates 
    /* 
    uvs.reserve(mesh->mNumVertices); 
    for (unsigned int i = 0; i<mesh->mNumVertices; i++){ 
     aiVector3D UVW = mesh->mTextureCoords[0][i]; // Assume only 1 set of UV coords; AssImp supports 8 UV sets. 
     uvs.push_back(glm::vec2(UVW.x, UVW.y)); 
    }*/ 

    // Fill vertices normals 
    normals.reserve(mesh->mNumVertices); 
    for (unsigned int i = 0; i<mesh->mNumVertices; i++){ 
     aiVector3D n = mesh->mNormals[i]; 
     //aiVector3D n = mesh->mVertices[i]; 
     normals.push_back(glm::vec3(n.x, n.y, n.z)); 
    } 


    // Fill face ind5ices 
    indices.reserve(3 * mesh->mNumFaces); 
    for (unsigned int i = 0; i<mesh->mNumFaces; i++){ 
     // Assume the model has only triangles. 
     indices.push_back(mesh->mFaces[i].mIndices[0]); 
     indices.push_back(mesh->mFaces[i].mIndices[1]); 
     indices.push_back(mesh->mFaces[i].mIndices[2]); 
    } 

    // The "scene" pointer will be deleted automatically by "importer" 

    }