我试图用图像纹理简单的三角形来解决相当常见的问题。这是代码:
设置openGL:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70,(float)width/(float)height, 0.3f, view);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_VERTEX_ARRAY);
glEnable(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
网格类:
public class Mesh {
protected int TextureID=0;
protected int VertID=-1,TrisID=-1,TexID=-1;
protected Vector3 position,rotation,scale;
protected ArrayList<Vector3> verts;
protected ArrayList<Vector3> tris;
protected ArrayList<Vector3> uvs;
public Mesh(Vector3 position,Vector3 rotation,Vector3 scale){
verts= new ArrayList<Vector3>();
tris= new ArrayList<Vector3>();
uvs= new ArrayList<Vector3>();
this.position = position;
this.rotation=rotation;
this.scale=scale;
}
public void draw(){
glPushMatrix();{
// Translate Submatrix
glScalef(scale.X, scale.Y, scale.Z);
glRotatef(rotation.X,1,0,0);
glRotatef(rotation.Y,0,1,0);
glRotatef(rotation.Z,0,0,1);
glTranslatef(position.X, position.Y, position.Z);
//bind and pointer buffers and textures
TextureLoader.textureList.get(TextureID).bind();
glBindTexture(GL_TEXTURE_2D,TextureID);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, TexID);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VertID);
glVertexPointer(3, GL_FLOAT, 0, 0);
int mode = GL_TRIANGLES;
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, TrisID);
//draw
glDrawElements(mode,tris.size()*3, GL_UNSIGNED_INT, 0);
}glPopMatrix();
}
public Mesh generate(){
updateVertBuffer();
updateTrisBuffer();
updateUvBuffer();
return this;
}
public void updateVertBuffer(){
if(VertID==-1)VertID= GL15.glGenBuffers();
FloatBuffer buffer = BufferUtils.createFloatBuffer(verts.size()*3);
for(int i=0;i<verts.size();i++){
buffer.put(verts.get(i).X);
buffer.put(verts.get(i).Y);
buffer.put(verts.get(i).Z);
}
buffer.flip();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,VertID);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
}
public void updateTrisBuffer(){
if(TrisID==-1)TrisID = GL15.glGenBuffers();
IntBuffer buffer = BufferUtils.createIntBuffer(tris.size()*3);
for(int i=0;i<tris.size();i++){
buffer.put((int)tris.get(i).X);
buffer.put((int)tris.get(i).Y);
buffer.put((int)tris.get(i).Z);
}
buffer.flip();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, TrisID);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer,GL15.GL_STATIC_DRAW);
}
public void updateUvBuffer(){
if(TexID==-1)TexID= GL15.glGenBuffers();
FloatBuffer buffer = BufferUtils.createFloatBuffer(uvs.size()*2);
for(int i=0;i<uvs.size();i++){
buffer.put(uvs.get(i).Y);
buffer.put(uvs.get(i).Z);
}
buffer.flip();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, TexID);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
}
绘制网格物体:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(rot.X,1,0,0);
glRotatef(rot.Y,0,1,0);
glRotatef(rot.Z,0,0,1);
glTranslatef(pos.X,pos.Y,pos.Z);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
for(Mesh a : MHandler)a.draw(); // Simply drawing all meshes
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
Display.update();
示例网格有效,但没有纹理:
Mesh m = new Mesh(new Vector3(0,0,0),new Vector3(0,0,0),new Vector3(10,10,10));
m.addVert(new Vector3(0,0,0),new Vector3(0,0,0));
m.addVert(new Vector3(0,1,0),new Vector3(0,1,0));
m.addVert(new Vector3(0,1,1),new Vector3(0,1,1));
m.addTriangle(new Vector3(0,1,2));
MHandler.add(m);
我知道代码太多了,但我真的不知道问题究竟在哪里。由于我是openGL的新手,因此任何关于代码的批评都会有所帮助。 如果有帮助,我使用Slick加载Textures。