绘制到纹理不起作用 LWJGL
Posted
技术标签:
【中文标题】绘制到纹理不起作用 LWJGL【英文标题】:Drawing to Textures does not work LWJGL 【发布时间】:2015-07-12 18:37:54 【问题描述】:我正在尝试绘制 2d 纹理并在 3D 场景中绘制该纹理。
初始化帧缓冲对象和纹理的代码:
public static void initFBO()
renderEngine.framebuffers = new int[7];
renderEngine.monitorTextures = new int[7];
for(int i = 0;i < 7;i++)
renderEngine.framebuffers[i] = glGenFramebuffers();
renderEngine.monitorTextures[i] = glGenTextures();
glBindFramebuffer(GL_FRAMEBUFFER, renderEngine.framebuffers[i]);
glBindTexture(GL_TEXTURE_2D, renderEngine.monitorTextures[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer)null);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderEngine.monitorTextures[i], 0);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
System.err.println("Error while creating FBO");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
将四边形渲染到帧缓冲区的代码: glUseProgram(0);
for(int i = 0;i < 6;i++)
glBindFramebuffer(GL_FRAMEBUFFER, framebuffers[i]);
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, 512, 512);
glClearColor(0f, 0f, 0f, 1f);
glClear(GL_COLOR_BUFFER_BIT);
//glActiveTexture(GL_TEXTURE0);
//glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 512, 512, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glUseProgram(0);
glColor4f(1f, 0f, 0f, 1f);
glBegin(GL_QUADS);
glVertex2f(20, 20);
glVertex2f(420, 20);
glVertex2f(420, 420);
glVertex2f(20, 420);
glEnd();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glPopAttrib();
如果我更改透明颜色,纹理在我的 3D 场景中渲染时会获得该颜色。但我看不到四边形。我做错了什么吗?
【问题讨论】:
【参考方案1】:如果找到解决方案。因为我使用的是 3D 环境并启用了 GL_CULL_FACE 并设置为 CCW(逆时针),所以未显示顺时针绘制的四边形。
我只是在绘制到帧缓冲区之前禁用了它,然后重新启用它。
glDisable(GL_CULL_FACE);
glEnable(GL_CULL_FACE);
【讨论】:
以上是关于绘制到纹理不起作用 LWJGL的主要内容,如果未能解决你的问题,请参考以下文章