复纹理图。最后,用宽度和高度创建一个 2 维图。数组中的数据类型由 GL.GL_UNSIGNED_BYTE 定义。下面,将看到如何给枪的每个顶点加上纹理图。
在枪上使用纹理图
定义了纹理图之后,需要指定对枪的哪个顶点进行贴图。请修改 Gun 类的构造函数,如清单 16 所示。
清单 16. 修改 Gun 类的构造函数
public Gun() {
texture();
GL.glNewList(this.getListIndex(), GL.GL_COMPILE);
GL.glColor4fv(RED);
GL.glBegin(GL.GL_QUADS);
GL.glTexCoord2d(0.0f,0.0f); GL.glVertex3fv(v1);
GL.glTexCoord2d(1.0f,0.0f); GL.glVertex3fv(v3);
GL.glTexCoord2d(1.0f,1.0f); GL.glVertex3fv(v4);
GL.glTexCoord2d(0.0f,1.0f); GL.glVertex3fv(v9);
GL.glTexCoord2d(0.0f,0.0f); GL.glVertex3fv(v3);
GL.glTexCoord2d(1.0f,0.0f); GL.glVertex3fv(v4);
GL.glTexCoord2d(1.0f,1.0f); GL.glVertex3fv(v5);
GL.glTexCoord2d(0.0f,1.0f); GL.glVertex3fv(v6);
GL.glTexCoord2d(0.0f,0.0f); GL.glVertex3fv(v10);
GL.glTexCoord2d(1.0f,0.0f); GL.glVertex3fv(v6);
GL.glTexCoord2d(1.0f,1.0f); GL.glVertex3fv(v5);
GL.glTexCoord2d(0.0f,1.0f); GL.glVertex3fv(v7);
GL.glTexCoord2d(0.0f,0.0f); GL.glVertex3fv(v4);
GL.glTexCoord2d(1.0f,0.0f); GL.glVertex3fv(v5);
GL.glTexCoord2d(1.0f,1.0f); GL.glVertex3fv(v7);
GL.glTexCoord2d(0.0f,1.0f); GL.glVertex3fv(v8);
GL.glTexCoord2d(0.0f,0.0f); GL.glVertex3fv(v2);
GL.glTexCoord2d(1.0f,0.0f); GL.glVertex3fv(v10);
GL.glTexCoord2d(1.0f,1.0f); GL.glVertex3fv(v6);
GL.glTexCoord2d(0.0f,1.0f); GL.glVertex3fv(v3);
GL.glTexCoord2d(0.0f,0.0f); GL.glVertex3fv(v10);
GL.glTexCoord2d(1.0f,0.0f); GL.glVertex3fv(v7);
GL.glTexCoord2d(1.0f,1.0f); GL.glVertex3fv(v13);
GL.glTexCoord2d(0.0f,1.0f); GL.glVertex3fv(v14);
GL.glTexCoord2d(0.0f,0.0f); GL.glVertex3fv(v8);
GL.glTexCoord2d(1.0f,0.0f); GL.glVertex3fv(v9);
GL.glTexCoord2d(1.0f,1.0f); GL.glVertex3fv(v12);
GL.glTexCoord2d(0.0f,1.0f); GL.glVertex3fv(v13);
GL.glTexCoord2d(0.0f,0.0f); GL.glVertex3fv(v1);
GL.glTexCoord2d(1.0f,0.0f); GL.glVertex3fv(v9);
GL.glTexCoord2d(1.0f,1.0f); GL.glVertex3fv(v12);
GL.glTexCoord2d(0.0f,1.0f); GL.glVertex3fv(v11);
GL.glTexCoord2d(0.0f,0.0f); GL.glVertex3fv(v1);
GL.glTexCoord2d(1.0f,0.0f); GL.glVertex3fv(v2);
GL.glTexCoord2d(1.0f,1.0f); GL.glVertex3fv(v14);
GL.glTexCoord2d(0.0f,1.0f); GL.glVertex3fv(v11);
|