2016-07-12 6 views
0

데이터 집합의 각 값을 [0, 1]으로 정규화 한 3 차원 데이터 집합이 있습니다. 텍스처와 블렌딩을 사용하여이 데이터 세트를 시각화하고 싶습니다. 그러나 나는 그것을 작동하게 만들 수 없다.블렌딩 작업을 수행 할 수 없습니다.

//All inputs >=0.6 is transformed to highly transparent white color. 
glm::vec4 transformValueToColor(GLclampf tValue) 
{ 
    if (tValue >= 0.6f) { 
     return glm::vec4(255, 255, 255, 10); 
    } 
    else { 
     auto val = round(255 * tValue); 
     auto valOp = round(255 * (1 - tValue)); 
     return glm::vec4(val, val, val, valOp); 
    }  
} 

내 버텍스 쉐이더 :

int main(){ 
    ... 
    //building an image for each rectangular slice of data 
    vector<Texture> myTextures; 
    for (GLint rowIndex = 0; rowIndex < ROW_NUM; rowIndex++) 
    { 
     auto pathToImage = "images/row" + to_string(rowIndex) + FILE_EXT; 
     FIBITMAP *img = FreeImage_Allocate(SLICE_DIMENSION, SLICE_NUM, 32); //32 = RGBA 

     for (GLint depthIndex = 0; depthIndex < DEPTH_NUM; depthIndex++) 
     { 
      for (GLint colIndex = 0; colIndex < COL_NUM; colIndex++) 
      { 
       auto value = my3DData[depthIndex][rowIndex][colIndex]; 

       //transform tValue to a source color 
       glm::vec4 source = transformValueToColor(value); 

       //note that here I am also setting the opacity. 
       RGBQUAD linRgb = { source.b, source.g, source.r, source.a }; 
       FreeImage_SetPixelColor(img, colIndex, depthIndex, &linRgb); 
      } 
     } 

     //Saving images. Saved images shows transparency. 
     FreeImage_Save(FIF_PNG, img, pathToImage.c_str()); 
     myTextures.push_back(Texture(pathToImage.c_str(), GL_TEXTURE0)); 
    } 
    //create VAO, VBO, EBO for a unit quad. 

    glEnable(GL_DEPTH_TEST); 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

    //game loop 
    while (!glfwWindowShouldClose(window)) 
    { 
     ... 
     for (int i = 0; i < myTextures.size(); i++) 
     { 
      GLint index = myTextures.size() - i - 1; 
      myTextures[index].bind(); //does glActiveTexture(...), and glBindTexture(...); 

      glm::mat4 model; 

      //translate 
      model = glm::translate(model, glm::vec3(0.0f, 0.0f, -index*0.003f)); 

      //scale 
      model = glm::scale(model, glm::vec3(1.2f)); 

      glUniformMatrix4fv(glGetUniformLocation(ourShader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model)); 

      glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 
     } 
    } 
} 

transformValueToColor 색상 값에 [0,1]에 데이터 값을 변환하기위한 다음은

내가 지금까지 한 일이다

#version 330 core 

layout(location = 0) in vec3 position; 
layout(location = 1) in vec2 texCoord; 

out vec2 TexCoord; 

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

void main() 
{ 
    gl_Position = projection * view * model * vec4(position, 1.0f); 
    TexCoord = vec2(texCoord.s, 1-texCoord.t); 
} 

내 조각 쉐이더 :

#version 330 core 

in vec2 TexCoord; 

out vec4 color; 

uniform sampler2D sliceTexture; 

void main() 
{ 
    vec4 texColor = texture(sliceTexture, TexCoord); 
    color = texColor; 
} 

블렌딩이 작동하는 데 필요한 코드라고 생각합니다. 이미지가 올바르게 생성되고 쿼드에 텍스처로 올바르게 적용됩니다. 그러나 전면에있는 사각형은 완전히 불투명하게 보이지만 생성 된 이미지 (심지어 앞에 나타나는 이미지)는 투명 영역을 나타냅니다.

어디서 잘못 가고 있는지 잘 모르겠습니다. 제안 요청.

감사합니다.

편집 : (RGBA 이미지를로드하고 그에서 RGBA 질감을 생성에 관련된 부분 만) Texture 클래스의 세부 정보 :

int width, height, channelCount; 
unsigned char* image = SOIL_load_image(pathToImage, &width, &height, &channelCount, SOIL_LOAD_RGBA); 
...  
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); 

Edit2가 : 카메라 클래스의 추가 세부 사항. Camera::getViewMatrix()view 매트릭스를 제공한다.

Camera::Camera(GLFWwindow* window, glm::vec3 position, glm::vec3 worldUpDirection, GLfloat yaw, GLfloat pitch) 
    :mouseSensitivity(0.25f), fov(45.0f), cameraSpeed(1.0f) 
{ 
    this->position = this->originalPosition = position; 
    this->worldUpDirection = worldUpDirection; 
    this->yaw = this->originalYaw = yaw; 
    this->pitch = this->originalPitch = pitch; 

    updateCameraVectors(); 
} 

void Camera::updateCameraVectors() 
{ 
    glm::mat4 yawPitchRotMat; 

    yawPitchRotMat = glm::rotate(yawPitchRotMat, glm::radians(yaw), glm::vec3(0.0f, 1.0f, 0.0f));  //y-ais as yaw axis 
    yawPitchRotMat = glm::rotate(yawPitchRotMat, glm::radians(pitch), glm::vec3(1.0f, 0.0f, 0.0f));  //x-axis as pitch axis 

    frontDirection = glm::normalize(-glm::vec3(yawPitchRotMat[2].x, yawPitchRotMat[2].y, yawPitchRotMat[2].z)); 

    rightDirection = glm::normalize(glm::cross(frontDirection, worldUpDirection)); 
    upDirection = glm::normalize(glm::cross(rightDirection, frontDirection)); 
} 

glm::mat4 Camera::getViewMatrix() 
{ 
    return glm::lookAt(position, position + frontDirection, upDirection); 
} 

답변

2
myTextures.push_back(Texture(pathToImage.c_str(), GL_TEXTURE0)); 

이것은 당신의 Texture 클래스와 어떻게 이미지 파일을 구문 분석하는 것에 대한 정보가 포함되어 있지 않습니다. 파일 자체에 투명도가 표시되는 경우에도 이미지를로드 할 때 텍스처로드 코드가 알파 채널을 삭제할 수 있습니다.

model = glm::translate(model, glm::vec3(0.0f, 0.0f, -index*0.003f)); 

앞면에서 뒷면으로 렌더링하는 것처럼 보입니다. 투명한 오브젝트를 렌더링하려면 서로 독립적으로 투명도 순서를 지정하는 알고리즘을 사용해야합니다. 그렇지 않으면 뒤에서 앞으로 렌더링해야합니다.

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

당신도 할 수 있습니다/알파 채널의 혼합이 다르게 수행되도록 glBlendFuncSeparate를 사용할 필요가 있습니다. 나는 이것에 대해 확실하지 않다.

단일 레이어 GL_TEXTURE_3D를 채우고 각 레이어에 대해 일련의 사각형을 렌더링하는 대신 조각 쉐이더에서 모든 혼합 작업을 수행하여 큐브로 렌더링하는 것이 좋습니다.

+0

답장을 보내 주셔서 감사합니다. 문제의'Texture' 클래스와 관련된 세부 사항을 추가했습니다. 또한 순서에 관해서는 가장 먼 쿼드를 가장 먼저 그리고 가장 가까운 쿼드를 마지막으로 그리는 것으로 생각합니다. 'z' 값이 더 많이 앞당겨 질수록 감소한다고 생각합니다. 너는 무엇을 제안 하는가? –

+1

내가 본 대부분의 OpenGL 설정은 앞으로 나아갈수록 Z 값이 감소합니다. 그것이 당신에게 어떻게 작용할지는 당신이 당신의 View 매트릭스를 어떻게 만들 었는지에 달려 있지만 당신은 그것도 보지 못합니다. – Jherico

+0

의견을 보내 주셔서 감사합니다. 뷰 매트릭스에 대한 '카메라'클래스 세부 정보가 추가되었습니다. –