Java 基于四边形的Opengl纹理
这是我的纹理类的内容:Java 基于四边形的Opengl纹理,java,opengl,lwjgl,Java,Opengl,Lwjgl,这是我的纹理类的内容: public int id; public Texture(InputStream inputStream) { ByteBuffer buf = null; int tWidth = 0; int tHeight = 0; try { PNGDecoder decoder = new PNGDecoder(inputStream); buf = ByteBuffer.allocateDirect(4*d
public int id;
public Texture(InputStream inputStream) {
ByteBuffer buf = null;
int tWidth = 0;
int tHeight = 0;
try {
PNGDecoder decoder = new PNGDecoder(inputStream);
buf = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight());
decoder.decode(buf, decoder.getWidth()*4, PNGDecoder.TextureFormat.RGBA);
buf.rewind();
inputStream.close();
} catch (IOException exception) {
ErrorHandler.handleError("Failed to load image", exception);
}
id = glGenTextures();
glActiveTexture(id);
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tWidth, tHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
glBindTexture(GL_TEXTURE_2D, 0);
}
我是这样渲染的:
glActiveTexture(background.id);
glBindTexture(GL_TEXTURE_2D, background.id);
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 4*18);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindTexture(GL_TEXTURE_2D, 0);
这是片段着色器:
#version 330
in vec2 textureCoordinate;
out vec4 outputColor;
uniform sampler2D texture_diffuse;
void main() {
outputColor.rgb = vec3(1.0f, 1.0f, 1.0f);
outputColor += texture2D(texture_diffuse, textureCoordinate);
}
我做错了什么?传递给着色器程序的纹理坐标100%正确(我选中)。但我还是有一辆白色的四轮车
注意:我使用png解码器
编辑:
我将每4个字节的浮点值打印到控制台,得到0.00.00.00.0。。。。这是不是意味着纹理被间接加载,或者信息以不同的格式存储到缓冲区?您的片段着色器看起来有误-您设置了白色并添加了纹理中的值,因此它将钳制为白色。就这样做吧
void main() {
outputColor.a = 1.0f;
outputColor.rgb = texture2D(texture_diffuse, textureCoordinate);
}
我不这么认为。我的片段着色器将所有三角形的颜色设置为白色,然后在其上添加纹理。他们试了很多次,一切都很好。无论如何,我试过你的代码来确定,我得到了一个后屏幕。那是因为四边形是绝对透明的。我认为四边形是透明的,因为没有任何东西可以添加到它。。。如何检查png解码器是否获得了正确的信息?@visDEVion:除非使用混合,否则四边形不能是透明的。@Nicolas如果没有为四边形设置任何颜色,则无法看到它。这不是让他透明了吗?或者是黑色的,我看不见,因为背景是黑色的?