2014-07-12 3 views
1

나는 다음과 같은 코드를 가지고 : 내 장면을 그려 그 두 사이OpenGL (OpenTK)에서 간단한 이미지를 표시하는 가장 간단한 방법은 무엇입니까?

public void BeginDraw() 
    { 
     if(bufferMaterial != null) 
     { 
      bufferMaterial.Textures[0].Surface.BindFramebuffer(); 
      beganDraw = true; 
     } 
    } 
    public void EndDraw() 
    { 
     if (beganDraw) 
     { 
      beganDraw = false; 
      GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); 
      if (bufferMaterial != null) 
      { 
       GL.BindBuffer(BufferTarget.ArrayBuffer, screenMesh.VBO); 
       GL.BindVertexArray(VAO); 
       GL.BindBuffer(BufferTarget.ElementArrayBuffer, screenMesh.VEO); 
       bufferMaterial.Use(); 
       screenMesh.ApplyDrawHints(bufferMaterial.Shader); 
       GL.DrawElements(PrimitiveType.Triangles, 2, DrawElementsType.UnsignedInt, 0); 
       GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); 
       GL.BindVertexArray(0); 
       GL.BindBuffer(BufferTarget.ArrayBuffer, 0); 
       GL.UseProgram(0); 
      } 
     } 
    } 

, 그래서 이미지가 bufferMaterial.Textures [0] .Surface 그려해야합니다.

은 이제 "screenMesh"는 다음과 같이 생성됩니다

 screenMesh = new Mesh(); 
     screenMesh.SetVertices(new float[] { 
      0,0, 
      1,0, 
      1,1, 
      0,1 
     }); 
     screenMesh.SetIndices(new uint[] { 
      0,3,2, 
      0,1,2 
     }); 
     screenMesh.SetDrawHints(new VertexObjectDrawHint("pos", 2, 2, 0)); 

-> 쉐이더는 다음과 같습니다

오프셋 0 2 개 구성 요소 2의 보폭 : 이제

++++++++ 
Screenbuffer 
++++++++ 

[Shader vertexScreen] 
#version 150 core 

in vec2 pos; 
uniform float _time; 
uniform sampler2D tex; 
void main() { 
    gl_Position = vec4(pos, -1, 1); 
} 
[Shader fragmentScreen] 
#version 150 core 
#define PI 3.1415926535897932384626433832795 

out vec4 outColor; 
uniform float _time; 
uniform sampler2D tex; 
//texture2D(tex, 
void main() { 
    outColor = vec4(1,1,1,1); 
} 

,이 화면에 흰색 사각형을 그릴 것이라고 기대할 수 있지만 화면이 검은 색으로 유지됩니다. 나는 화면의 인덱스를 가지고 놀았습니다. 그래서 조금씩 망가질 수도 있었지만, 결코 볼 수 없었습니다 ...

이처럼 쉐이더가있는 영사 매트릭스는 필요하지 않습니까?

메쉬 클래스 : http://pastebin.com/PcwEYqGH

편집 : 좋아, 버퍼가, 이제 렌더링하지만 난 여전히 깊이 버퍼가 작동 얻을 필요가!

public void Create(int width, int height, SurfaceFormat format) 
    { 
     Width = width; 
     Height = height; 
     textureHandle = GL.GenTexture(); 
     //bind texture 
     GL.BindTexture(TextureTarget.Texture2D, textureHandle); 
     Log.Error("Bound Texture: " + GL.GetError()); 
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); 
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); 
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)format.WrapMode); 
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)format.WrapMode); 

     Log.Error("Created Texture Parameters: " + GL.GetError()); 
     GL.TexImage2D(TextureTarget.Texture2D, 0, format.InternalFormat, Width, Height, 0, format.PixelFormat, format.SourceType, format.Pixels); 
     Log.Error("Created Image: " + GL.GetError()); 
     //unbind texture 
     GL.BindTexture(TextureTarget.Texture2D, 0); 
     //create depthbuffer 
     if (format.DepthBuffer) 
     { 
      GL.GenRenderbuffers(1, out dbHandle); 
      GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, dbHandle); 
      GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent24, Width, Height); 
     } 

     //create fbo 
     fboHandle = GL.GenFramebuffer(); 
     GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboHandle); 
     GL.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, textureHandle, 0); 
     if(format.DepthBuffer) 
      GL.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, dbHandle); 
     GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); 
     Log.Error("Created Framebuffer: " + GL.GetError()); 

    } 

을 그리고 이것은 바인딩 코드입니다 : 표면 생성 코드는 다음과 같습니다

public void BindTexture(TextureUnit slot = TextureUnit.Texture0) 
    { 
     GL.ActiveTexture(slot); 
     GL.BindTexture(TextureTarget.Texture2D, textureHandle); 

    } 
    public void BindFramebuffer() 
    { 
     GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboHandle); 
    } 

을하지만 깊이 버퍼를 사용하는 경우, 화면이 다시 검은 색 간다.

편집 : 깊이 버퍼를 삭제하는 것을 잊어 버렸습니다!

GL.DrawElements(PrimitiveType.Triangles, 2, DrawElementsType.UnsignedInt, 0); 

두 번째 인수가 렌더링에 사용되는 인덱스의 수는 다음과 같습니다

+0

오른쪽, 여기에 투영 매트릭스가 필요합니다. 'gl_Position'의 z 좌표를'-1'로 설정하면 꼭지점을 클립 볼륨의 가장자리에 놓습니다. 지오메트리가 잘리지 않도록'0.0'을 사용하는 것이 좋습니다. 그것 이외에, 나는이 OpenTK 클래스를 모른다. 그래서 무엇이 잘못 될 수 있는지 말하기는 어렵다. –

+0

OpenTK는 OpenGL의 래퍼이며, 다른 모든 클래스는 내 자신의 것입니다. 어떤 코드에 관심이 있습니까? 프레임 버퍼와 동일한 버퍼를 사용한 다음 동일한 프레임의 텍스처를 사용하는 것이 문제가 될 수 있습니까? – pixartist

+0

'메쉬'클래스는 어디서 오는 걸까요? 나는 그것이 OpenTK의 일부가되어야한다고 생각했다. 그러나 나는 문서 전체를 찾고 있었고 어디서나 찾을 수 없었다. 문서에 대한 링크를 제공 할 수 있다면 매우 유용 할 것입니다. –

답변

3

은 그리기 호출에 문제가 있습니다. 단 2 개의 인덱스만으로는 완전한 삼각형이 충분하지 않습니다. 당신은 당신의 인덱스 배열의 6 개 인덱스를 가지고, 그래서해야한다 : 그 너머

GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, 0); 

, 당신이 사용하고있는 Mesh 클래스에 대한 코드 또는 문서를 찾을 수 없어, 그래서 나는 경우 말할 수 없다 메쉬에 저장된 정점 데이터가 올바르게 설정됩니다.

+0

메쉬 클래스 : http : // pastebin.com/PcwEYqGH – pixartist

+0

현재 작동하지만 깊이 버퍼에 문제가 있습니다. 원래 게시물의 편집 참조 – pixartist

관련 문제