import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;
import android.view.KeyEvent;public class GLRender implements Renderer
{
int one = 0x10000;
float step = 0.4f;
boolean key;
boolean light = true;
float xrot = 0, yrot = 0;
float xspeed, yspeed;
float z = -2.0f;
float radio = (float)320 / 480;
FloatBuffer lightAmbient = FloatBuffer.wrap(new float[]{0.5f,0.5f,0.5f,1.0f}); FloatBuffer lightDiffuse = FloatBuffer.wrap(new float[]{1.0f,1.0f,1.0f,1.0f});
FloatBuffer lightPosition = FloatBuffer.wrap(new float[]{0.0f,0.0f,6.0f,1.0f}); int filter = 1; int [] texture;
FloatBuffer vertices = FloatBuffer.wrap(new float[]{
-radio,-1,radio,
radio,-1,radio,
radio,1,radio,
-radio,1,radio,
-radio,-1,-radio,
-radio,1,-radio,
radio,1,-radio,
radio,-1,-radio,
-radio,1,-radio,
-radio,1,radio,
radio,1,radio,
radio,1,-radio,
-radio,-1,-radio,
radio,-1,-radio,
radio,-1,radio,
-radio,-1,radio,
radio,-1,-radio,
radio,1,-radio,
radio,1,radio,
radio,-1,radio,
-radio,-1,-radio,
-radio,-1,radio,
-radio,1,radio,
-radio,1,-radio,
});
IntBuffer normals = IntBuffer.wrap(new int[]{
0,0,one,
0,0,one,
0,0,one,
0,0,one,
0,0,-one,
0,0,-one,
0,0,-one,
0,0,-one,
0,one,0,
0,one,0,
0,one,0,
0,one,0,
0,-one,0,
0,-one,0,
0,-one,0,
0,-one,0,
one,0,0,
one,0,0,
one,0,0,
one,0,0,
-one,0,0,
-one,0,0,
-one,0,0,
-one,0,0,
});
IntBuffer texCoords = IntBuffer.wrap(new int[]{
one,0,0,0,0,one,one,one,
0,0,0,one,one,one,one,0,
one,one,one,0,0,0,0,one,
0,one,one,one,one,0,0,0,
0,0,0,one,one,one,one,0,
one,0,0,0,0,one,one,one,
});
ByteBuffer indices1 = ByteBuffer.wrap(new byte[]{
0,1,3,2,
});
ByteBuffer indices2 = ByteBuffer.wrap(new byte[]{
4,5,7,6,
});
ByteBuffer indices3 = ByteBuffer.wrap(new byte[]{
8,9,11,10,
});
ByteBuffer indices4 = ByteBuffer.wrap(new byte[]{
12,13,15,14,
});
ByteBuffer indices5 = ByteBuffer.wrap(new byte[]{
16,17,19,18,
});
ByteBuffer indices6 = ByteBuffer.wrap(new byte[]{
20,21,23,22,
});
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glEnable(GL10.GL_LIGHTING);
gl.glTranslatef(0.0f, 0.0f, z);
//gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
//gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[filter]);
gl.glNormalPointer(GL10.GL_FIXED, 0, normals);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoords); gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glColor4f(0.5f, 0, 0, 1.0f);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices1);
gl.glColor4f(0.5f, 0.5f, 0, 1.0f);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices2);
gl.glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices3);
gl.glColor4f(0, 0.5f, 0, 1.0f);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices4);
gl.glColor4f(0, 0, 0.5f, 1.0f);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices5);
gl.glColor4f(0, 0.5f, 0.5f, 1.0f);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE, indices6); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
if ( key )
{
xrot+=xspeed;
yrot+=yspeed;
}
} public void onSurfaceChanged(GL10 gl, int width, int height)
{
Log.d("GLRender","onSurfaceChanged");
Log.d("GLRENDER","width:"+String.valueOf(width));
Log.d("GLRENDER","height:"+String.valueOf(height));
float ratio = (float) width / height; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity();
//gl.glOrthof(-ratio * 1.5f,ratio * 1.5f,-1.5f,1.5f,1,10);
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 5); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity();
} public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
Log.d("GLRender","onSurfaceCreated");
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); gl.glClearColor(0, 0, 0, 0);
gl.glEnable(GL10.GL_CULL_FACE); gl.glShadeModel(GL10.GL_SMOOTH); gl.glEnable(GL10.GL_DEPTH_TEST);
IntBuffer textureBuffer = IntBuffer.allocate(3); gl.glGenTextures(3, textureBuffer);
texture = textureBuffer.array();
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);
gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST); // ( NEW )
gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST); // ( NEW )
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]);
gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR); // ( NEW )
gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR); // ( NEW )
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]);
gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST); // ( NEW )
gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR); // ( NEW )
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap, 0);
gl.glClearDepthf(1.0f);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
//gl.glEnable(GL10.GL_TEXTURE_2D); gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, lightAmbient);
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, lightDiffuse);
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, lightPosition);
gl.glEnable(GL10.GL_LIGHT1);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货