Java 如何在OpenGL/LWJGL显示列表上绘制纹理?
我有一张展示清单。(是的,我知道它们已经去润滑了。我有理由使用它们。) 如何在显示列表上显示纹理 虽然我更喜欢在不同的位置有不同的纹理,但实际上在显示列表上有一个纹理将是一个很好的开始,对于封闭alpha测试的游戏来说,在找到更好的解决方案之前,可能已经足够好了Java 如何在OpenGL/LWJGL显示列表上绘制纹理?,java,opengl,lwjgl,Java,Opengl,Lwjgl,我有一张展示清单。(是的,我知道它们已经去润滑了。我有理由使用它们。) 如何在显示列表上显示纹理 虽然我更喜欢在不同的位置有不同的纹理,但实际上在显示列表上有一个纹理将是一个很好的开始,对于封闭alpha测试的游戏来说,在找到更好的解决方案之前,可能已经足够好了 try { // Load the heightmap-image from its resource file System.out.println("Path: " + new File(System
try {
// Load the heightmap-image from its resource file
System.out.println("Path: " + new File(System.getProperty("user.dir") + "/res/heightmap.bmp").getAbsolutePath());
BufferedImage heightmapImage = ImageIO.read(new File(
System.getProperty("user.dir") + "/res/heightmap.bmp"));
width = heightmapImage.getWidth();
height = heightmapImage.getHeight();
BufferedImage heightmapColour = ImageIO.read(new File(System.getProperty("user.dir") +
"/res/colours.bmp"));
data = new float[heightmapImage.getWidth()][heightmapImage
.getHeight()];
red = new float[heightmapColour.getWidth()][heightmapColour
.getHeight()];
blue = new float[heightmapColour.getWidth()][heightmapColour
.getHeight()];
green = new float[heightmapColour.getWidth()][heightmapColour
.getHeight()];
Color colour;
Color colours;
PNGDecoder decoder = new PNGDecoder(heightmapLookupInputStream);
ByteBuffer buffer = BufferUtils.createByteBuffer(4
* decoder.getWidth() * decoder.getHeight());
decoder.decode(buffer, decoder.getWidth() * 4,
PNGDecoder.Format.RGBA);
buffer.flip();
heightmapLookupInputStream.close();
lookupTexture = glGenTextures();
glBindTexture(GL_TEXTURE_2D, lookupTexture);
// Hand the texture data to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(),
decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
} catch (IOException e) {
e.printStackTrace();
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
heightmapDisplayList = glGenLists(1);
glNewList(heightmapDisplayList, GL_COMPILE);
for (int z = 0; z < data.length - 1; z++) {
// Render a triangle strip for each 'strip'.
glBegin(GL_TRIANGLE_STRIP);
for (int x = 0; x < data[z].length; x++) {
// Take a vertex from the current strip
if (((blue[z][x] / 255) < 0.4))
glColor4f((red[z][x] / 255) / 2
+ (float) (Math.random() / 10), (green[z][x] / 255)
/ 2 + (float) (Math.random() / 10),
(blue[z][x] / 255) / 2
+ (float) (Math.random() / 10), 1);
else {
glColor4f((red[z][x] / 255) / 2
+ (float) (Math.random() / 10), (green[z][x] / 255)
/ 2 + (float) (Math.random() / 10),
(blue[z][x] / 255) / 2
+ (float) (Math.random() / 10), 1);
}
if (data[z][x] >= WATER_LEVEL -10){
glVertex3f(x, data[z][x], z);
glVertex3f(x, data[z + 1][x], z + 1);
}
}
glEnd();
}
glEndList();
试试看{
//从其资源文件加载heightmap图像
System.out.println(“路径:”+新文件(System.getProperty(“user.dir”)+“/res/heightmap.bmp”).getAbsolutePath());
BuffereImage heightmapImage=ImageIO.read(新文件(
System.getProperty(“user.dir”)+“/res/heightmap.bmp”);
宽度=heightmapImage.getWidth();
高度=heightmapImage.getHeight();
BuffereImage HeightMapColor=ImageIO.read(新文件(System.getProperty(“user.dir”)+
“/res/colors.bmp”);
数据=新浮点[heightmapImage.getWidth()][heightmapImage
.getHeight();
红色=新浮点[HeightMapColor.getWidth()][HeightMapColor]
.getHeight();
蓝色=新浮动[HeightMapColor.getWidth()][HeightMapColor]
.getHeight();
绿色=新浮动[HeightMapColor.getWidth()][HeightMapColor]
.getHeight();
颜色;
颜色;
PNGDecoder解码器=新的PNGDecoder(heightmapLookupInputStream);
ByteBuffer buffer=BufferUtils.createByteBuffer(4
*decoder.getWidth()*decoder.getHeight());
decoder.decode(缓冲区,decoder.getWidth()*4,
PNGDecoder.Format.RGBA);
flip();
heightmapLookupInputStream.close();
lookupTexture=glGenTextures();
glBindTexture(GL_TEXTURE_2D,lookupTexture);
//将纹理数据交给OpenGL
glTexImage2D(GL_纹理_2D,0,GL_RGBA,解码器.getWidth(),
解码器.getHeight(),0,GL_RGBA,GL_无符号字节,缓冲区);
}捕获(IOE异常){
e、 printStackTrace();
}
glTexParameteri(GL_纹理2D、GL_纹理最小过滤器、GL_线性);
glTexParameteri(GL_纹理2D、GL_纹理MAG_过滤器、GL_线性);
heightmapDisplayList=GlgenList(1);
glNewList(高度映射显示列表,GL_编译);
对于(intz=0;z=水位-10){
glVertex3f(x,数据[z][x],z);
glVertex3f(x,数据[z+1][x],z+1);
}
}
格伦德();
}
格伦德利斯();
如果你看不懂我格式不好的代码,那么如果有什么不同的话,我会使用GL_三角形条来渲染.bmp文件中的内容
GL\u最近的
/GL\u线性
进行GL\u纹理最小过滤器
。或者提供一些地图。如果不执行这两项操作中的一项,将导致失败glEnable(GL\U纹理\U 2D)
。如果没有这些,OpenGL将继续忽略任何绑定的纹理glVertex()
之前提供一些纹理坐标,可能通过glTexCoord()
您似乎没有提供纹理坐标或启用纹理。@genpfault请告诉我怎么做!我已经尝试了所有的纹理和坐标的东西,但是我不知道它是如何与显示列表一起工作的,或者,因为我使用了一个GL_三角形_条,我甚至会设置坐标到什么(或者如何设置它们)。我已经尝试了我所知道的一切,所以一定有我不知道的东西。和往常一样,谢谢你的时间。我想第三步不可能有一个显示列表?还是这样?另外,我如何在超过1000个顶点的gl三角形条带上设置纹理坐标?很有可能,调用会很好地完成。您考虑的是
glTexCoordPointer()
,它将是。您可以一次设置一个坐标,就像您的glVertex()
调用一样。