底下是第1~5课的
全部代码Eclipse 3.2下测试成功
这课是我根据Pepe & Lizzie’s Adventures第6课Texture mapping
结合第4课写的
1.首先要引入3个文件:BitmapLoader.java
功能:负责读取bmp图片(只对8位和24位)转化为BufferedImage
ResourceRetriever.java
功能:读取资源从jar包或当前路径
也就是针对java中读取文件时要使用绝对路径的问题
TextureReader.java
功能:把BufferedImage转化为使OpenGL容易识别的数据结构
2.包含TextureReader.java 用于载入bmp图片
import common.TextureReader;
3.包含私有成员
private float xrot;//绕X轴
private float yrot;//绕Y轴
private float zrot;//绕Z轴
private int texture;//纹理
4.在init(GLAutoDrawable drawable)方法里面添加
//--------------新添加代码-------
gl.glEnable(GL.GL_TEXTURE_2D);//启动纹理映射
texture = genTexture(gl);//返回操作的未被使用的纹理对象的索引
gl.glBindTexture(GL.GL_TEXTURE_2D, texture);//指定和绑定纹理(这里是绑定纹理)
TextureReader.Texture texture1 = null;
try {
texture1 = TextureReader.readTexture("data/Crate.bmp");//bmp图片
//texture1 = TextureReader.readTexture("data/glass.png");
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
glu.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, GL.GL_RGB8, texture1.getWidth(), texture1.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, texture1.getPixels());
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
//--------------
5.在类中添加一个方法
//--------------新添加代码------------
//返回操作的未被使用的纹理对象的索引
private int genTexture(GL gl) {
final int[] tmp = new int[1];
gl.glGenTextures(1, tmp, 0);
return tmp[0];
}
6.display方法的全部代码
//-----------------------
public void display(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT); //清除颜色缓冲
gl.glLoadIdentity(); //重置矩阵
gl.glTranslatef(0.0f, 0.0f, -6.0f); //向内(Z轴负方向)移动6
gl.glTranslatef(0.0f, 0.0f, -6.0f); //向内(Z轴负方向)移动6
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture);//指定和绑定纹理(这里是指定纹理)
gl.glBegin(GL.GL_QUADS);
// 前面
gl.glTexCoord2f(0.0f, 0.0f);gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f);gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f);gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 1.0f);gl.glVertex3f(-1.0f, 1.0f, 1.0f);
// 后面
gl.glTexCoord2f(1.0f, 0.0f);gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(1.0f, 1.0f);gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 1.0f);gl.glVertex3f(1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f);gl.glVertex3f(1.0f, -1.0f, -1.0f);
// 上面
gl.glTexCoord2f(0.0f, 1.0f);gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f);gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f);gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f);gl.glVertex3f(1.0f, 1.0f, -1.0f);
// 下面
gl.glTexCoord2f(1.0f, 1.0f);gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 1.0f);gl.glVertex3f(1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f);gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f);gl.glVertex3f(-1.0f, -1.0f, 1.0f);
// 右面
gl.glTexCoord2f(1.0f, 0.0f);gl.glVertex3f(1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(1.0f, 1.0f);gl.glVertex3f(1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 1.0f);gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 0.0f);gl.glVertex3f(1.0f, -1.0f, 1.0f);
// 左面
gl.glTexCoord2f(0.0f, 0.0f);gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(1.0f, 0.0f);gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f);gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 1.0f);gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glEnd();
xrot += 0.3f;
yrot += 0.2f;
zrot += 0.4f;
}