我正在使用OpenTK,所以我可以在C#中使用OpenGL。我想渲染一个纹理然后再将该纹理渲染到屏幕上。目前我有一个程序,将绿色方块渲染为纹理,GLClearColor设置为黑色。然后将该纹理存储并渲染到屏幕,并将GLClearColor设置为蓝色。但是,我无法让绿色广场出现。我正在尝试按照本教程:http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/
这是我的代码:
int width = 300;
int height = 300;
//generate and bind buffer
int[] fb = { 0 };
GL.GenFramebuffers(1, fb);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, fb[0]);
//generate and bind texture
int RenderTextureTarget = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, RenderTextureTarget);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
//^Rgba and Brga^ setting came from my texture loading function so this texture should be formateed the same as everthing else I render
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
//set texture as colorattatchment0 and make opengl draw to colorattatchment0
GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, RenderTextureTarget, 0);
DrawBuffersEnum[] temp = { DrawBuffersEnum.ColorAttachment0 };
GL.DrawBuffers(1, temp);
//setup veiwport and matricies
GL.Viewport(0, 0, width, height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(-1, 1, 1, -1, 1, -1);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
//set clear color to black so I can see where the texture is being rendered for testing purposes
GL.ClearColor(Color.Black);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
//render a quad that should show up as a green square on the black background with on corner at center
GL.Color3(Color.Green);
GL.Begin(PrimitiveType.Quads);
GL.Vertex2(0, 0);
GL.Vertex2(1, 0);
GL.Vertex2(1, 1);
GL.Vertex2(0, 1);
GL.End();
//set things back to their defaults
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
GL.ClearColor(Color.Blue);
所以纹理在屏幕上的正确位置,我知道一切都在渲染到屏幕功能中运行良好,因为如果我将上面生成的id替换为另一个纹理的id,它会完美渲染。
我的目标是让一个绿色的方块显示在那个黑盒子上。