Java LWJGL纹理不渲染
我一直在尝试用lwjgl编写一个游戏引擎,我遇到了一个渲染问题,我想得到一些帮助。基本上,我遵循了lwjgl basics(GitHub上的mattdesl)中的一些步骤,并在渲染器中生成了一个纹理类和一个部分,除了它们不渲染纹理外,其他部分都可以工作。这是密码Java LWJGL纹理不渲染,java,graphics,rendering,lwjgl,Java,Graphics,Rendering,Lwjgl,我一直在尝试用lwjgl编写一个游戏引擎,我遇到了一个渲染问题,我想得到一些帮助。基本上,我遵循了lwjgl basics(GitHub上的mattdesl)中的一些步骤,并在渲染器中生成了一个纹理类和一个部分,除了它们不渲染纹理外,其他部分都可以工作。这是密码 public void drawTexturedRectangle(float x1, float y1, float w, float h, Texture t){ t.bind(); GL11.glC
public void drawTexturedRectangle(float x1, float y1, float w, float h, Texture t){
t.bind();
GL11.glColor4f(1.0f, 0, 1.0f, 1.0f);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(x1, y1);
GL11.glTexCoord2f(1.0f, 0);
GL11.glVertex2f(x1 + w, y1);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex2f(x1 + w, y1 + h);
GL11.glTexCoord2f(0, 1.0f);
GL11.glVertex2f(x1, y1 + h);
GL11.glEnd();
}
这就是它呈现的地方。下面是纹理类:
package com.nightfall.morningside;
import java.io.*;
import java.net.URL;
import java.nio.ByteBuffer;
import de.matthiasmann.twl.utils.*;
import org.lwjgl.opengl.*;
import org.lwjgl.opengl.GL12.*;
import org.lwjgl.BufferUtils;
public class Texture {
public int width;
public int height;
public int id;
public Texture(URL pathToTexture){
try {
InputStream pngstream = pathToTexture.openStream();
PNGDecoder pngdecoder = new PNGDecoder(pngstream);
width = pngdecoder.getWidth();
height = pngdecoder.getHeight();
int bpp = 4;
ByteBuffer buffer = BufferUtils.createByteBuffer(bpp * width * height);
pngdecoder.decode(buffer, width * bpp, PNGDecoder.Format.RGBA);
buffer.flip();
id = GL11.glGenTextures();
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void bind(){
GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
}
}
TexturedRectangle类:
package com.nightfall.morningside.geometry;
import com.nightfall.morningside.Texture;
import com.nightfall.morningside.Textured;
public class TexturedRectangle extends Rectangle implements Textured{
public Texture texture;
public TexturedRectangle(float xone, float yone, float w, float h, Texture t) {
super(xone, yone, w, h);
texture = t;
}
public Texture getTexture() {
return texture;
}
}
package com.nightfall.morningside.geometry;
public class Rectangle {
private float x1;
private float y1;
private float width;
private float height;
public Rectangle(float xone, float yone, float w, float h){
x1 = xone;
y1 = yone;
width = w;
height = h;
}
public float getX1(){
return x1;
}
public float getY1(){
return y1;
}
public float getHeight(){
return height;
}
public float getWidth(){
return width;
}
public Point getLocation(){
return new Point(x1, y1);
}
}
矩形类:
package com.nightfall.morningside.geometry;
import com.nightfall.morningside.Texture;
import com.nightfall.morningside.Textured;
public class TexturedRectangle extends Rectangle implements Textured{
public Texture texture;
public TexturedRectangle(float xone, float yone, float w, float h, Texture t) {
super(xone, yone, w, h);
texture = t;
}
public Texture getTexture() {
return texture;
}
}
package com.nightfall.morningside.geometry;
public class Rectangle {
private float x1;
private float y1;
private float width;
private float height;
public Rectangle(float xone, float yone, float w, float h){
x1 = xone;
y1 = yone;
width = w;
height = h;
}
public float getX1(){
return x1;
}
public float getY1(){
return y1;
}
public float getHeight(){
return height;
}
public float getWidth(){
return width;
}
public Point getLocation(){
return new Point(x1, y1);
}
}
和纹理界面:
package com.nightfall.morningside;
public interface Textured {
public Texture getTexture();
}
以及Point类:
package com.nightfall.morningside.geometry;
public class Point {
private float x1;
private float y1;
public Point(float x, float y){
x1 = x;
y1 = y;
}
public float getX(){
return x1;
}
public float getY(){
return y1;
}
}
我希望这就是一切。我相信这是我渲染的方式上的一个问题,但可能在加载纹理的位置上存在问题。
如果你还需要什么,我会帮你准备的。
谢谢。默认情况下禁用纹理。如果要渲染带纹理的基本体(例如带纹理的四边形),必须使用激活纹理
glEnable(GL_TEXTURE2D);
glBindTexture(GL_TEXTURE_2D, textureId);
第一行告诉openGL使用纹理,第二行指定要使用的纹理
要停止使用纹理,可以:
- 将零绑定到GL_纹理_2D:
glBindTexture(GL_纹理_2D,0)代码>或
- 禁用GL_纹理_2D:
glDisable(GL_纹理_2D)代码>
编辑,我忘记了所有的
GLxx.
前缀(在我的代码中静态导入)哦,我只是想澄清一下,它是lwjgl 2.90的。如果你要制作一个引擎,那么就不要使用所有不推荐的OpenGL方法。我这样做只是为了学习-一旦我确信我真的能做到,我会尽快替换所有东西。这对学习很有好处,虽然当你学会了做一个新的引擎时不要把所有的东西都替换掉,因为替换每一个不推荐的方法都会是一场噩梦。以下是一些不推荐使用的方法glMatrixMode
,glLoadIdentity
,glTranslate
,glRotate
,glScale
,glBegin
,glVertex
,glNormal
,glTexCoord
,glColor
,等等。你不能只给glBindTexture
0作为句柄,你需要给它一个实际的句柄。实际上你可以,它现在已经被弃用,但它意味着取消绑定纹理。OP使用即时模式,glBindTexture(X,0)是我可以接受的,工作良好,甚至可以为我解决问题。看。哦,我虽然你告诉他那是绑定纹理的方法,但我没有看到/认为你在暗示如何解开它,很抱歉误解了。嗯。我重读了我的帖子,它让人非常困惑。你说得对!我会编辑它!