相信大家都会画矩形了,下面我们来画一个房间,大家想想怎么画了,我以前想用索引法把每个面都画出来,但是遇到了很多问题,画不出我想要的结果,其实还有 种方法,那就是通过旋转,和位移变换,把矩形旋转和位移到长方体的各个面,再加工一下,这样就变成小房间了,下面我们来看看效果和代码吧。
GLSurfaceViewActivity.java
- package yy.cal;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.LinearLayout;
-
- public class GLSurfaceViewActivity extends Activity {
- private MySurfaceView mSurfaceView;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mSurfaceView=new MySurfaceView(this);
- mSurfaceView.requestFocus();
- mSurfaceView.setFocusableInTouchMode(true);
- LinearLayout ll=(LinearLayout)this.findViewById(R.id.main_liner);
- ll.addView(mSurfaceView);
- }
-
- @Override
- protected void onPause() {
-
- super.onPause();
- mSurfaceView.onPause();
- }
-
- @Override
- protected void onResume() {
-
- super.onResume();
- mSurfaceView.onResume();
- }
- }
MySurfaceView.java
- package yy.cal;
-
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.content.Context;
- import android.opengl.GLSurfaceView;
- import android.opengl.GLU;
- import android.view.MotionEvent;
-
- public class MySurfaceView extends GLSurfaceView{
- private final float TOUCH_SCALE_FACTOR = 180.0f/320;
- private SceneRenderer mRenderer;
- private float mPreviousX;
-
- public MySurfaceView(Context context) {
- super(context);
- mRenderer = new SceneRenderer();
- setRenderer(mRenderer);
- setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
- }
-
-
- @Override
- public boolean onTouchEvent(MotionEvent e) {
- float x = e.getX();
- switch (e.getAction()) {
- case MotionEvent.ACTION_MOVE:
- float dx = x - mPreviousX;
- mRenderer.angle += dx * TOUCH_SCALE_FACTOR;
-
- requestRender();
- }
- mPreviousX = x;
- return true;
- }
-
- private class SceneRenderer implements GLSurfaceView.Renderer
- {
- Cube cube=new Cube();
- float angle=45;
-
- public void onDrawFrame(GL10 gl) {
-
- gl.glEnable(GL10.GL_CULL_FACE);
-
-
- gl.glShadeModel(GL10.GL_SMOOTH);
-
-
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
-
- gl.glMatrixMode(GL10.GL_MODELVIEW);
-
- gl.glLoadIdentity();
-
- GLU.gluLookAt
- (
- gl,
- 0f,
- 10f,
- 15.0f,
- 0,
- 0f,
- 0,
- 0,
- 1,
- 0
- );
-
-
-
- gl.glRotatef(angle, 0, 1, 0);
-
-
- gl.glPushMatrix();
-
- cube.drawSelf(gl);
- gl.glPopMatrix();
- }
-
- public void onSurfaceChanged(GL10 gl, int width, int height) {
-
- gl.glViewport(0, 0, width, height);
-
- gl.glMatrixMode(GL10.GL_PROJECTION);
-
- gl.glLoadIdentity();
-
- float ratio = (float) height/width ;
-
-
- gl.glFrustumf( -1, 1,-ratio, ratio, 8f, 100);
- }
-
- public void onSurfaceCreated(GL10 gl, EGLConfig config) {
-
- gl.glDisable(GL10.GL_DITHER);
-
- gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST);
-
- gl.glClearColor(0,0,0,0);
-
- gl.glEnable(GL10.GL_DEPTH_TEST);
- }
- }
- }
ColorRect.java
- package yy.cal;
-
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import javax.microedition.khronos.opengles.GL10;
-
- public class ColorRect {
- private FloatBuffer mVertexBuffer;
- private IntBuffer mColorBuffer;
- int vCount=0;
-
- public ColorRect(float width,float height)
- {
-
- vCount=6;
- final float UNIT_SIZE=4.0f;
- float vertices[]=new float[]
- {
- 0,0,0,
- width*UNIT_SIZE,height*UNIT_SIZE,0,
- -width*UNIT_SIZE,height*UNIT_SIZE,0,
- -width*UNIT_SIZE,-height*UNIT_SIZE,0,
- width*UNIT_SIZE,-height*UNIT_SIZE,0,
- width*UNIT_SIZE,height*UNIT_SIZE,0
- };
-
-
-
- ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
- vbb.order(ByteOrder.nativeOrder());
- mVertexBuffer = vbb.asFloatBuffer();
- mVertexBuffer.put(vertices);
- mVertexBuffer.position(0);
-
-
-
-
-
-
- final int one = 65535;
- int colors[]=new int[]
- {
- one,one,one,0,
- 0,0,one,0,
- 0,0,one,0,
- 0,0,one,0,
- 0,0,one,0,
- 0,0,one,0,
- };
-
-
-
-
- ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
- cbb.order(ByteOrder.nativeOrder());
- mColorBuffer = cbb.asIntBuffer();
- mColorBuffer.put(colors);
- mColorBuffer.position(0);
-
-
-
- }
-
- public void drawSelf(GL10 gl)
- {
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
-
-
- gl.glVertexPointer
- (
- 3,
- GL10.GL_FLOAT,
- 0,
- mVertexBuffer
- );
-
-
- gl.glColorPointer
- (
- 4,
- GL10.GL_FIXED,
- 0,
- mColorBuffer
- );
-
-
- gl.glDrawArrays
- (
- GL10.GL_TRIANGLE_FAN,
- 0,
- vCount
- );
- }
- }
Cube.java
- package yy.cal;
-
- import javax.microedition.khronos.opengles.GL10;
- import static yy.cal.Constant.*;
-
- public class Cube {
-
- ColorRect cr=new ColorRect(SCALE,SCALE);
-
- public void drawSelf(GL10 gl)
- {
-
-
- gl.glPushMatrix();
-
-
- gl.glPushMatrix();
- gl.glTranslatef(0, 0, -UNIT_SIZE*SCALE);
- cr.drawSelf(gl);
- gl.glPopMatrix();
-
-
- gl.glPushMatrix();
- gl.glTranslatef(0,-UNIT_SIZE*SCALE,0);
- gl.glRotatef(-90, 1, 0, 0);
- cr.drawSelf(gl);
- gl.glPopMatrix();
-
-
- gl.glPushMatrix();
- gl.glTranslatef(UNIT_SIZE*SCALE,0,0);
- gl.glRotatef(-90, 1, 0, 0);
- gl.glRotatef(-90, 0, 1, 0);
- cr.drawSelf(gl);
- gl.glPopMatrix();
-
-
- gl.glPushMatrix();
- gl.glTranslatef(-UNIT_SIZE*SCALE,0,0);
- gl.glRotatef(90, 0, 1, 0);
- cr.drawSelf(gl);
- gl.glPopMatrix();
-
- gl.glPopMatrix();
- }
- }
Constant.java
- package yy.cal;
-
- public class Constant {
- public static final float UNIT_SIZE=4.0f;
- public static final float SCALE=0.5f;
- }
(dlnuchunge) |