Java 不使用lwjgl和顶点缓冲区渲染的纹理
我已经对渲染代码进行了几次优化,但是仍然没有渲染任何东西,并且所有可见的都是一个黑色窗口。它以前(使用opengl即时模式渲染)工作过,但我似乎破坏了系统 Texture.javaJava 不使用lwjgl和顶点缓冲区渲染的纹理,java,opengl,buffer,lwjgl,vbo,Java,Opengl,Buffer,Lwjgl,Vbo,我已经对渲染代码进行了几次优化,但是仍然没有渲染任何东西,并且所有可见的都是一个黑色窗口。它以前(使用opengl即时模式渲染)工作过,但我似乎破坏了系统 Texture.java import static org.lwjgl.opengl.ARBBufferObject.*; import static org.lwjgl.opengl.ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB; import static org.lwjgl.opengl.GL11
import static org.lwjgl.opengl.ARBBufferObject.*;
import static org.lwjgl.opengl.ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB;
import static org.lwjgl.opengl.GL11.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.imageio.ImageIO;
import org.lwjgl.BufferUtils;
import org.lwjgl.util.ReadableColor;
public class Texture {
public static Texture font1;
public static Texture smallButton1;
public static Texture smallButton1Hover;
public static Texture background1;
public static Texture title1;
public static Texture smallTextBox1;
public static Texture largeTextBox1;
final String file;
boolean loaded = false;
private ByteBuffer buffer;
public final int width, height;
public static void loadTextures() {
font1 = new Texture("/fonts/font1.png");
background1 = new Texture("/textures/background1.png");
title1 = new Texture("/textures/title1.png");
smallButton1 = new Texture("/textures/smallButton1.png");
smallButton1Hover = new Texture("/textures/smallButton1Hover.png");
smallTextBox1 = new Texture("/textures/smallTextBox1.png");
largeTextBox1 = new Texture("/textures/largeTextBox1.png");
}
public Texture(String path) {
BufferedImage image = null;
try {
image = ImageIO.read(TacAttack.class.getResourceAsStream(path));
int BYTES_PER_PIXEL = 4;
int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * BYTES_PER_PIXEL); //4bytes for RGBA
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int pixel = pixels[y * image.getWidth() + x];
buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
buffer.put((byte) (pixel & 0xFF)); // Blue component
buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component
}
}
buffer.flip();
loaded = true;
System.out.println("Loaded texture: \"" + path + "\"");
} catch (IOException e) {
e.printStackTrace();
ErrorHandler.fatalError(ErrorType.IOError, "Couldn't load texture: " + path);
}
this.file = path;
this.width = image.getWidth();
this.height = image.getHeight();
}
public ByteBuffer getBuffer() {
if (!loaded) {
ErrorHandler.fatalError(ErrorType.textureError, "Buffer requested before texture loaded.");
return null;
} else {
return buffer;
}
}
private void use(int texID, ReadableColor color) {
glEnable(GL_TEXTURE_2D);
glColor4b(color.getRedByte(), color.getGreenByte(), color.getBlueByte(), color.getAlphaByte());
glBindTexture(GL_TEXTURE_2D, texID);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
private void endUse() {
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
public void render(int texID, IntBuffer vBuffer, FloatBuffer tBuffer, boolean changed, int vHandle, int tHandle, ReadableColor color) {
if (!loaded) {
return;
}
System.out.println("");
System.out.println(">>");
System.out.println("apparently rendered at: x" + vBuffer.get(0) + ", y" + vBuffer.get(1) + ", w" + (vBuffer.get(4) - vBuffer.get(0)) + ", h" + (vBuffer.get(5) - vBuffer.get(1)) + " with vHandle - " + vHandle + ", " + file);
System.out.println("apparent texturecoords: x" + tBuffer.get(0) + ", y" + tBuffer.get(1) + ", w" + (tBuffer.get(4) - tBuffer.get(0)) + ", h" + (tBuffer.get(5) - tBuffer.get(1)) + " with tHandle - " + tHandle);
use(texID, color);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vHandle);
if (changed) {
glBufferDataARB(GL_ARRAY_BUFFER_ARB, vBuffer, GL_STATIC_DRAW_ARB);
System.out.println("yeah");
}
glVertexPointer(2, GL_INT, 2 * 4, 0L);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, tHandle);
if (changed) {
glBufferDataARB(GL_ARRAY_BUFFER_ARB, tBuffer, GL_STATIC_DRAW_ARB);
System.out.println("yeah2");
}
glTexCoordPointer(2, GL_FLOAT, 2 * 4, 0L);
glDrawArrays(GL_QUADS, 0, 4);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
endUse();
}
}
或与该行相关:)
好几天都没能弄明白这一点…好吧,在花了大量时间玩这个之后,我应该知道问题不在于缓冲区 问题是Texture.java中的这一行:
glColor4b(color.getRedByte(), color.getGreenByte(), color.getBlueByte(), color.getAlphaByte());
这显然将颜色设置为黑色
然而,当我尝试它时,它起了作用(感谢“正面全裸”帮助我发现了这一点):
ReadableColor返回的字节似乎不是lwjgl需要的那种字节
但是,耶,它现在起作用了 有没有没有没有纹理的渲染四边形?或者你真的看不到任何东西,甚至连四边形都看不到。(您可以通过在glDrawArrays之前调用立即模式着色来测试这一点)错误,不需要这样做。事实上,最近有一次。它可以归结为使用有符号字节的
glColor4b(…)
。它的范围是-128-127。如果尝试使用无符号颜色,如果将其传递给有符号颜色函数,一半的颜色空间将溢出,并将变为负数。负色在很多上下文中都不有用,当存储在帧缓冲区中时,将被替换为0;)--简而言之,使用glColor4ub(…)
。
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.getBuffer());
buffer.flip();
glColor4b(color.getRedByte(), color.getGreenByte(), color.getBlueByte(), color.getAlphaByte());
glColor4f((float) color.getRed() / 255f, (float) color.getGreen() / 255f, (float) color.getBlue() / 255f, (float) color.getAlpha() / 255f);