2016-07-30 2 views
-2

쉐이더 경험에서 내가 경험 한 이상한 문제가 발생했습니다. 몇 가지 테스트 쉐이더 코드 (아래 참조)를 작성하여 간단한 텍스처로 실행했습니다.셰이더가 잘못된 단편 색상을 나타냅니다.

기본적으로 쉐이더는 조각의 색상을 확인하고, 특정 범위 내에 있으면 균일 한 색상 변수에 따라 해당 조각을 색칠합니다. 문제는 셰이더가 조각의 색상을 올바르게 인식하지 못한다는 것입니다. 나는 심지어 색깔의 빨간 부분이 1과 같고 그것이 인지 항상 확인하기 위해 갔다. 모든 조각에 대해 항상이 true를 반환한다. 그러나 동일한 쉐이더를 사용하여 원래 텍스처를 그리면 잘 작동합니다.

왜 이런 일이 발생합니까? 셰이더는 오류없이 컴파일됩니다. 나는 명백한 무엇인가를 놓치고있는 것처럼 느낀다 ...

코드 (LibGDX에 접근 할 수 있으면이 코드를 스스로 실행할 수있다).

// Java code. 
// Creating sprite, shader and sprite batch. 
SpriteBatch batch = new SpriteBatch(); 
Sprite sprite = new Sprite(new Texture("testTexture.png")); 
ShaderProgram shader = new ShaderProgram(Gdx.files.internal("vertex.vert"), 
       Gdx.files.internal("fragment.frag")); 
// Check to see if the shader has logged any errors. Prints nothing. 
System.out.println(shader.getLog()); 
// We start the rendering. 
batch.begin(); 
// We begin the shader so we can load uniforms into it. 
shader.begin(); 
// Set the uniform (works fine). 
shader.setUniformf("color", Color.RED); 
// We end the shader to tell it we've finished loading uniforms. 
shader.end(); 
// We then tell our renderer to use this shader. 
batch.setShader(shader); 
// Then we draw our sprite.  
sprite.draw(batch); 
// And finally we tell our renderer that we're finished drawing. 
batch.end(); 

// Dispose to release resources. 
shader.dispose(); 
batch.dispose(); 
sprite.getTexture().dispose(); 

// Vertex 
#version 330 

in vec4 a_position; 
in vec4 a_color; 
in vec2 a_texCoord0; 

out vec4 v_color; 
out vec2 v_texCoord0; 

uniform mat4 u_projTrans; 

void main() { 
    v_color = a_color; 
    v_texCoord0 = a_texCoord0; 
    gl_Position = u_projTrans * a_position; 
} 

// Fragment 
#version 330 

in vec4 v_color; 
in vec2 v_texCoord0; 

out vec4 outColor; 

uniform vec4 color; 
uniform sampler2D u_texture; 

void main() { 
    // This is always true for some reason... 
    if(v_color.r == 1.0) { 
     outColor = color; 
    } else { 
     // But if I run this code it draws the original texture just fine with the correct colors. 
     outColor = v_color * texture2D(u_texture, v_texCoord0); 
    } 
} 
텍스처 : simple texture

+1

그리고 어떤 색상 당신의 정점을해야합니까? 위의 코드를 사용할 수 있다면 분명히 흰색입니다. 따라서 조건이 참으로 놀라운 것은 아닙니다. –

+0

이것은 물론 논리적 인 이유입니다.하지만 렌더링하는 유일한 것이 32x32 텍스처 일 때 꼭지점이 흰색 일 수는 없습니다. – Charanor

+0

정점 속성'a_color'를 전달 중입니다. 버텍스 버퍼에서이 속성에 대해 어떤 데이터를 제공합니까? 텍스처 색상을 비교하려면 먼저 텍스처를 먼저 샘플링해야합니다. –

답변

2

당신이 개 입력 귀하의 프레 그먼트 쉐이더에 대한 색상이 있습니다 질감으로 정점 속성으로 보내 하나 하나를 . 텍스처 색상을 확인하려고하지만 정점 속성으로 전송 된 색상 값을 확인하십시오.

// Vertex 
#version 330 

in vec4 a_position; 
in vec4 a_color; // <--- A color value is passed as a vertex attribute 
in vec2 a_texCoord0; 

out vec4 v_color; 
out vec2 v_texCoord0; 

uniform mat4 u_projTrans; 

void main() { 
    v_color = a_color; // <--- you are sending it to fragment shader 
    v_texCoord0 = a_texCoord0; 
    gl_Position = u_projTrans * a_position; 
} 

// Fragment 
#version 330 

in vec4 v_color; // <--- coming from vertex buffer 
in vec2 v_texCoord0; 

out vec4 outColor; 

uniform vec4 color; 
uniform sampler2D u_texture; 

void main() { 
    // This is always true for some reason... 
    if(v_color.r == 1.0) { // <--- and then checking this vertex attribute color 
     outColor = color; //  instead of what you send from texture 
    } else { 
     ... 
    } 
} 
관련 문제