2010-12-07 2 views
4

버텍스 속성을 실행중인 쉐이더 프로그램에 전달할 때 문제가 있습니다. 위치와 RGBA 색상이라는 두 가지 속성을 전달하고 싶습니다. 속성 위치 바인딩은 해당 위치에서 작동합니다. 그러나 색상에 대해서는 작동하지 않습니다. 따라서 모든 지오메트리는 결국 검정색으로 렌더링됩니다.GLSL 1.5 및 OpenGL 3.3 : 버텍스 쉐이더로 색상 전달이 실패한 것 같습니다.

이 내가 할 것입니다 :

GLuint programHandler; 

// create program 
// compile & attach vertex shader 
// compile & attach fragment shader 

glBindAttribLocation(programHandler, 0, "InPosition"); 
glBindAttribLocation(programHandler, 1, "InColor"); 
glBindFragDataLocation(programHandler, 1, "FragColor"); 

glLinkProgram(programHandler); 
glUseProgram(programHandler); 

// initialize uniform variables 

// I'm trying to get the attribute locations now. 
// I expect location "0" for InPosition and location "1" for InColor. 
// In fact, it gets me a "-1" for InColor. I simply cannot see the reason for that behaviour 

GLint positionLocation = glGetAttribLocation(programHandler, "InPosition"); // returns 0 
GLint colorLocation = glGetAttribLocation(programHandler, "InColor"); // returns -1 

glEnableVertexAttribArray(positionLocation); 
glEnableVertexAttribArray(colorLocation); // fails! 

내 버텍스 쉐이더는 아주 기본적인 것입니다.

#version 150 core 
precision highp float; 

// input 
in vec4 PassColor; 

// output 
out vec4 FragColor; 

void main(void) { 
    FragColor = PassColor; 
} 

이유는 무엇입니까 바인딩 "포지션"작업과 "InColor"

#version 150 core 

// input 
uniform mat4 ModelviewMatrix; 
uniform mat4 ProjectionMatrix; 
in vec3 InPosition; 
in vec4 InColor; 

// output 
out vec4 PassColor; 

void main(void) { 
    // passing color through to fragment shader 
    PassColor = InColor; 

    // transformation 
    gl_Position = ProjectionMatrix * ModelviewMatrix * vec4(InPosition, 1.0); 
} 

내 조각 쉐이더가 단순히 색을 반환해야합니다 : 난 정말 할 모든 정점을 변환하고, 프레 그먼트 쉐이더에 색상을 전달입니다 하지 않습니다? GLSL 컴파일러가 쉐이더 코드를 최적화하여 사용하지 않는 변수를 바인딩 할 수 없다는 것을 알고 있습니다. 하지만 왜 여기에 색상을 최적화해야하는지 모르겠습니다. 결국, 조각 쉐이더로 전달하여 사용합니다.

+0

'glGetError()'가 있습니까? 또한 GPU와 드라이버 버전은 무엇입니까? – Kos

답변

1

눈 먼 샷, 나는이 잘못 생각 :

glBindFragDataLocation(programHandler, 1, "FragColor"); 

그것은해야한다 : 조각 쉐이더 여러 색상 평면에 작성해야

glBindFragDataLocation(programHandler, 0, "FragColor"); 
+0

나는 본다. 하지만 여러 렌더 타겟으로 렌더링하고 싶다면 어떻게해야할까요? 현재 프레임 버퍼 객체에 첨부 된 텍스처로 렌더링됩니다. 원래는 컬러 버퍼로 텍스처 하나 이상을 첨부하려고했습니다. 그것이 "1"을 두 번째 인수로 사용하는 이유입니다. gl_FragData [] 배열은 사용되지 않습니다. 어떻게 올바르게 바인딩하고 프래그먼트 쉐이더에서 선언 할 수 있습니까? – Walter

+0

정확 합니다만 두 번째 렌더 대상에 렌더링됩니다 (있는 경우). 렌더링 대상이 두 개가 없으면 오류가 발생합니다. –

+0

그래서 GLSL 조각 셰이더에서 FragColor [1]을 통해 COLOR_ATTACHMENT1을 처리한다고 가정합니다. 나는 그렇게하기 위해 FragColor를 다음과 같이 선언 할 필요가있다. "out vec4 FragColor [8];" (8 개의 컬러 버퍼가 붙어 있다고 가정). 그 맞습니까? 또한 "glBindFragDataLocation (programHandler, i,"FragColor [i] ");"와 같은 각 조각 데이터 위치를 바인딩해야합니까? – Walter

0

경우, 프레임 버퍼가 있어야한다 여러 색상 평면이 첨부되어 있으며 조각을 쓸 수 있습니다.

색상면 어느 버퍼를 렌더링 할 수

GLuint renderbuffer[2]; 
glGenRenderbuffers(2, &renderbuffer[0]); 
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer[0]); 
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height); 
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer[1]); 
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height); 

또는 그들은 질감이 될 수 있습니다.

GLuint colorTexture[2]; 
glGenTextures(2, &colorTexture[0]); 
glBindTexture(GL_TEXTURE_2D, colorTexture[0]); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 
glBindTexture(GL_TEXTURE_2D, colorTexture[1]); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 

텍스처 또는 렌더링 버퍼를 프레임 버퍼에 첨부해야합니다.

물론 버퍼 렌더링 텍스처 혼합 될 수 있고, 하나의 상이한 컬러 평면과 같은 프레임에 부착 될 수 있고, 버퍼

GLuint frameBuffer; 
glGenFramebuffers(1, &frameBuffer); 
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); 
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbuffer[0]); 
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbuffer[1]); 

텍스처를

GLuint frameBuffer; 
glGenFramebuffers(1, &frameBuffer); 
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); 
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture[0], 0); 
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, colorTexture[1], 0); 

참고 렌더링 완충기.
깊이 버퍼 또는 스텐실 버퍼가 필요한 경우에는 GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT 또는 GL_DEPTH_STENCIL_ATTACHMENT을 프레임 버퍼에 첨부해야합니다.

완료되었음을 farme 버퍼 glCheckFramebufferStatus 평가 될 수있는 경우 : 하나의 프레임 버퍼에 부착 될 수있는 컬러 버퍼

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) 
{ 
    // error 
} 

최대 수 OGL 함수 glGetIntegerv 의해 결정될 수 의해 매개 변수 GL_MAX_COLOR_ATTACHMENTS 사용 :

GLint maxColAttchemnts = 0; 
glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxColAttchemnts); 

은 모든 색 첨부해야하는 동시에 활성화된다. 프래그먼트 셰이더 데이터의 출력이 들어갈 버퍼 배열은 glDrawBuffers으로 정의 할 수 있습니다. 프래그먼트 쉐이더

GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT1 }; 
glDrawBuffers(1, &drawBuffers[0]); 

각 액티브 컬러 부착 선언 하나 개 out 변수를 갖는다.

out fragColor0; 
out fragColor1; 

void main() 
{ 
    .... 
    fragColor0 = ....; 
    .... 
    fragColor1 = ....; 
} 

glBindFragDataLocation 명시 적으로 지정 될 수있는 프레임 버퍼 컬러 부착 인덱스에 out 변수의 결합. 셰이더 프로그램이 링크되기 전에이 작업을 수행해야합니다.

GLuint prog = ....; 
glBindFragDataLocation(prog, 0, "fragColor0"); 
glBindFragDataLocation(prog, 1, "fragColor1"); 
glLinkProgram(prog); 

특정 출력이 쓰는 최신 OpenGL 버퍼 색인에서 레이아웃 위치로 명시 적으로 지정할 수 있습니다. 이것은 glBindFragDataLocation을 사용하지 않아도됩니다.

layout(location = 0) out fragColor0; 
layout(location = 1) out fragColor1; 
관련 문제