1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
// Render all scene
for( int i=0; i<_meshList.size(); i++ )
{
OBJMesh m = (OBJMesh)_meshList.get( i );
// If current material has texture, bind it
if( m.material.texId > 0 )
{
gl.glEnable( GL.GL_TEXTURE_2D );
gl.glBindTexture( GL.GL_TEXTURE_2D, m.material.texId );
}
else
{
gl.glDisable( GL.GL_TEXTURE_2D );
gl.glBindTexture( GL.GL_TEXTURE_2D, 0 );
}
gl.glMaterialfv( GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, new float[]{m.material.ambient.x, m.material.ambient.y, m.material.ambient.z, m.material.alpha}, 0 );
gl.glMaterialfv( GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, new float[]{m.material.diffuse.x, m.material.diffuse.y, m.material.diffuse.z, m.material.alpha}, 0 );
gl.glMaterialfv( GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, new float[]{m.material.specular.x, m.material.specular.y, m.material.specular.z, m.material.alpha}, 0 );
gl.glMateriali( GL.GL_FRONT_AND_BACK,GL.GL_SHININESS, 128 );
// render triangles.. this is too basic. should be optimized
gl.glBegin( GL.GL_TRIANGLES );
gl.glColor4f( m.material.diffuse.x, m.material.diffuse.y, m.material.diffuse.z, m.material.alpha );
boolean has_texture=false;
if(_texcoordList!=null&&_texcoordList.size()>0)
{
has_texture=true;
}
for( int fi=0; fi<m.faceList.size(); fi++ )
{
OBJFace f = (OBJFace)m.faceList.get( fi );
p1 = (Vector3f)_vertexList.get(f.a);
p2 = (Vector3f)_vertexList.get(f.b);
p3 = (Vector3f)_vertexList.get(f.c);
n1 = (Vector3f)_normalList.get(f.na);
n2 = (Vector3f)_normalList.get(f.nb);
n3 = (Vector3f)_normalList.get(f.nc);
//these should only be drawn if there are texture 2d coordinates
if(has_texture){
tc1 = (Vector3f)_texcoordList.get(f.ta);
tc2 = (Vector3f)_texcoordList.get(f.tb);
tc3 = (Vector3f)_texcoordList.get(f.tc);
}
gl.glNormal3f( n1.x, n1.y, n1.z );
if(has_texture){
gl.glTexCoord2f( tc1.x, tc1.y );
}
gl.glVertex3f( p1.x, p1.y, p1.z );
gl.glNormal3f( n2.x, n2.y, n2.z );
if(has_texture){
gl.glTexCoord2f( tc2.x, tc2.y );
}
gl.glVertex3f( p2.x, p2.y, p2.z );
gl.glNormal3f( n3.x, n3.y, n3.z );
if(has_texture){
gl.glTexCoord2f( tc3.x, tc3.y );
}
gl.glVertex3f( p3.x, p3.y, p3.z );
}
gl.glEnd();
}
} |
Partager