【发布时间】:2018-05-15 07:04:45
【问题描述】:
我开始在 Java 中使用 LWJGL/OpenGL 来创建 2D 游戏,但是我在渲染一个简单的 32x32 四边形(正方形)时遇到了困难。我创建了一个纹理加载系统,并按照正确的纹理说明进行操作,但纹理不会显示。我的代码如下:
devBlock64.bind();
glEnable(GL_TEXTURE_2D);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(32, 0);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(32, 32);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(0, 32);
GL11.glEnd();
glDisable(GL_TEXTURE_2D);
以上是每次调用“render”时的代码。 “devBlock64”只是一个加载了 64x64 纹理的 Texture 对象(但在本例中是 32x32,因为我将其保存为错误的大小)
此外,这是我在加载纹理并生成其纹理 ID 后调用的选项和函数:
this.bind();
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
图片加载正常,但不会显示。 this.bind(); 调用我的函数来执行此操作:glBindTexture(GL_TEXTURE_2D, id):
而且,如果有人好奇的话,我的游戏循环,其他一切都是根据 OpenGL 的设置窗口教程完成的:
public void loop() {
// Binds GLFW with OpenGL
GL.createCapabilities();
glOrtho(0f, 800, 600, 0f, 0f, 1f);
glClearColor(1.0f, 1f, 1f, 1f);
//glLoadIdentity();
world.loadTextures();
while(!hasWindowRequestedClose()) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
world.onUpdate();
world.render();
glfwSwapBuffers(handle);
glfwPollEvents();
}
destroy();
}
【问题讨论】:
-
您希望将四边形安装在窗口中吗?
-
不,我想对四边形进行纹理处理。我编辑了标题以消除任何混乱。
-
使用 BufferUtils 加载的 ByteBuffer。
标签: java opengl textures lwjgl texturing