- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对渲染代码进行了一些优化,但是仍然没有渲染任何内容,并且所有可见的都是一个黑色的窗口。它以前工作过(使用opengl立即模式渲染,但我似乎破坏了系统。
纹理.java
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();
}
}
TextureObject.java
import static org.lwjgl.opengl.ARBBufferObject.glGenBuffersARB;
import static org.lwjgl.opengl.GL11.*;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL12;
import org.lwjgl.util.Color;
import org.lwjgl.util.ReadableColor;
public class TextureObject {
private final Texture texture;
public final int width, height;
private final int texID;
private IntBuffer vBuffer;
private FloatBuffer tBuffer;
private boolean changed = true;
private IntBuffer ib = BufferUtils.createIntBuffer(2);
private final int vHandle;
private final int tHandle;
public TextureObject(Texture texture) {
this.texture = texture;
this.width = texture.width;
this.height = texture.height;
texID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texID);
//Setup wrap mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
//Setup texture scaling filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//Send tex data to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.getBuffer());
vBuffer = BufferUtils.createIntBuffer(8);
tBuffer = BufferUtils.createFloatBuffer(8);
vBuffer.put(0).put(0).put(0).put(0).put(0).put(0).put(0).put(0);
tBuffer.put(0).put(0).put(0).put(0).put(0).put(0).put(0).put(0);
glGenBuffersARB(ib);
vHandle = ib.get(0);
tHandle = ib.get(1);
}
public void render(int sx, int sy, int swidth, int sheight) {
updateBuffers(sx, sy, swidth, sheight, 0, 0, 1, 1);
texture.render(texID, vBuffer, tBuffer, changed, vHandle, tHandle, Color.WHITE);
}
public void render(int sx, int sy, int swidth, int sheight, ReadableColor color) {
updateBuffers(sx, sy, swidth, sheight, 0, 0, 1, 1);
texture.render(texID, vBuffer, tBuffer, changed, vHandle, tHandle, color);
}
public void render(int sx, int sy, int swidth, int sheight, float tx, float ty, float twidth, float theight) {
updateBuffers(sx, sy, swidth, sheight, tx, ty, twidth, theight);
texture.render(texID, vBuffer, tBuffer, changed, vHandle, tHandle, Color.WHITE);
}
public void render(int sx, int sy, int swidth, int sheight, float tx, float ty, float twidth, float theight, ReadableColor color) {
updateBuffers(sx, sy, swidth, sheight, tx, ty, twidth, theight);
texture.render(texID, vBuffer, tBuffer, changed, vHandle, tHandle, color);
}
private void updateBuffers(int sx, int sy, int swidth, int sheight, float tx, float ty, float twidth, float theight) {
IntBuffer tempVertexBuffer = BufferUtils.createIntBuffer(8);
FloatBuffer tempTextureCoordsBuffer = BufferUtils.createFloatBuffer(8);
tempVertexBuffer.clear();
tempTextureCoordsBuffer.clear();
tempVertexBuffer.put(sx).put(sy);
tempVertexBuffer.put(sx + swidth).put(sy);
tempVertexBuffer.put(sx + swidth).put(sy + sheight);
tempVertexBuffer.put(sx).put(sy + sheight);
float ttx = tx / (float) texture.width;
float tty = ty / (float) texture.height;
tempTextureCoordsBuffer.put(ttx).put(tty);
tempTextureCoordsBuffer.put(ttx + twidth).put(tty);
tempTextureCoordsBuffer.put(ttx + twidth).put(tty + theight);
tempTextureCoordsBuffer.put(ttx).put(tty + theight);
for (int i = 0; i < 8; i++) {
if (vBuffer.get(i) != tempVertexBuffer.get(i)) {
vBuffer.clear();
tempVertexBuffer.flip();
vBuffer.put(tempVertexBuffer);
vBuffer.flip();
changed = true;
break;
} else {
changed = false;
}
}
for (int i = 0; i < 8; i++) {
if (tBuffer.get(i) != tempTextureCoordsBuffer.get(i)) {
tBuffer.clear();
tempTextureCoordsBuffer.flip();
tBuffer.put(tempTextureCoordsBuffer);
tBuffer.flip();
changed = true;
break;
} else {
changed = false;
}
}
}
}
所有纹理都是通过调用“TextureObject.render(args)”渲染的,并且“Texture.render(args)”的控制台输出都是正确的。
我没有收到任何错误,但没有任何渲染,所以我怀疑纹理本身以某种方式变成了一堆零。
也许这行来自 TextueObject.java?
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.getBuffer());
或与此行相关? :)
buffer.flip();
这几天一直没搞明白……
最佳答案
好吧,在花费了大量时间研究这个之后,我应该知道问题不可能出在缓冲区上。
问题出在Texture.java中的这一行:
glColor4b(color.getRedByte(), color.getGreenByte(), color.getBlueByte(), color.getAlphaByte());
这显然将颜色设置为黑色。
然而,当我尝试它时,它起作用了(感谢“Full Frontal Nudity”帮助我发现了这一点):
glColor4f((float) color.getRed() / 255f, (float) color.getGreen() / 255f, (float) color.getBlue() / 255f, (float) color.getAlpha() / 255f);
看起来 ReadableColor 返回的字节不是 lwjgl 需要的字节类型...
但是,现在可以了!
关于java - 纹理不使用 lwjgl 和顶点缓冲区渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19619344/
如何以编程方式创建最大化的 lwjgl 窗口或使已创建的显示最大化? 注意:我不是问如何为显示器设置全屏模式。 最佳答案 Display.setResizable(true) 这将启用最大化按钮。 关
我对使用外部 Java 库非常陌生,我从基于 LWJGL 构建的 Slick2D 开始。我编写了一个简单的 StateBasedGame,它只在屏幕上打印三个选项。它给了我一个关于某些(jar?)文件
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.libr ary.path at
我正在 Itellij 中制作一个依赖 org.lwjgl.input 代码的 Minecraft mod。该库已从 LWJGL 3 中删除,因此我必须使用 LWJGL 2。我将正确的 LWJGL 2
我安装了 lwjgl 库和 natives 并将其放入/lib 文件夹。在 Eclipse Java 构建路径上设置库位置和 native 库位置。当我尝试在 Eclipse 上运行它时,它可以工作,
所以我已经使用 LWJGL 开发一个程序很长一段时间了,现在我希望能够轻松地向其他人展示它。但是,当我尝试将其导出为可运行的 jar 文件并在控制台中运行时,出现上述错误。 我还尝试将其导出为普通的
我尝试使用 LWJGL 编译 ( NetBeans ) Java 项目。我单击了项目 -> 属性,然后单击库 -> 编译。我添加了 Jars 位置、源文件位置和 javadoc 位置。 当我尝试构建项
在 NEHE 第 06 课 (http://nehe.gamedev.net/tutorial/texture_mapping/12038/) 中,您可以加载图像并用它来纹理对象。我目前使用的是LWJ
我正在尝试向我的程序添加全屏功能,但我无法让它工作。我想 Display.setFullscreen(true); 我尝试将其位置更改为创建显示的位置或设置显示模式的位置上方,但仍然无法正常工作。有什
我刚刚开始使用 LWJGL 和 OpenGL,但遇到了问题。我正在尝试弄清楚如何使用我找到的游戏引擎为我正在制作的游戏绘制一条简单的线条。 这是我正在使用的引擎的链接:https://github.c
我正在尝试在 java lwjgl 2.9 中编译着色器,如果我运行我的游戏,它无法编译着色器。问题是为什么着色器不重新识别 const 关键字,如果我删除 const 那么它会抛出错误 float
我在使用 Java 和 lwjgl 导入时遇到问题: import org.lwjgl.input.Keyboard; 行不通。我已经添加了 jar : lwjgl-glfw.jar lwjgl-op
我在 OpenGL 上下文中移动实体时遇到问题:当我尝试放置一个实体时,位置似乎是正确的,但是当实体开始移动时,一切都会出错,并且碰撞不起作用。我是 OpenGL 新手,我怀疑我的世界矩阵或模型矩阵是
我正在做一项家庭作业,使用 LightWeight Java 游戏库修改教授给出的代码。问题是我无法完全加载测试代码来开始测试修改。我已经按照修改中的说明链接了 jar 文件,但仍然存在一个挥之不去的
我是 Java 新手,我想将基本的 LWJGL 小程序上传到我的网站并在浏览器中播放。我不知道该怎么做,我想确保在继续开发游戏之前我知道如何让它工作。请帮助我,因为我不知道如何将 JWJGL 小程序或
我正在尝试旋转屏幕上的两个对象之一。我该怎么做?我只能让它旋转屏幕上的所有对象。代码: glColor3f(1, 0, 0); glBegin(GL_QUADS
我目前正在渲染一个天空盒,如下所示: //Front Face GL11.glBindTexture(GL11.GL_TEXTURE_2D, skybox[0].getText
我正在编写类似 Minecraft 的游戏。为了加载纹理和字体(目前没有声音),我使用 slick。 Slick 不再开发,maven 存储库不可用。是否有其他库(LibGDX 除外)可用于加载 lw
我正在尝试使用opengl在lwjgl显示屏上显示具有自定义字体的文本。目前,我正在使用自定义位图字体类从png文件加载字体,并以与tileet相同的方式显示它们。它可以正常工作,但是当我将文本绘制到
我正在使用 VBO 来渲染游戏中的所有立方体(因为会有很多立方体),并且我希望它们都带有纹理。当我运行我的代码时,立方体没有纹理(相反,它看起来有点红色,因为我的纹理是砖墙),并且只有当我非常接近立方
我是一名优秀的程序员,十分优秀!