【绝对经典】opengl 3d卷轴效果

先上个图,以后上源码

完全打开状态

 

over

关键代码
package com.archermind.myRenderer;

import java.nio.IntBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLSurfaceView.Renderer;

import com.archermind.myFigure.Cube;
import com.archermind.myFigure.Square;
import com.archermind.tools.BitmapCreate;

public class myRenderer implements Renderer
{

Cube cube;

Cube lift_cube;

Cube right_cube;

Square square;

public float mAngleX;

public float mAngleY; 

public float mRunX;

public float dx;

public float speed = 0.08f;

float rotateAngle;

private Context mContext;

private int[] texture;



public myRenderer(Context context, Cube lift_cube, Cube right_cube,

Square square,BitmapCreate bitmapCreate)

{

init(context, lift_cube, right_cube, square,bitmapCreate);

}



private void init(Context context, Cube lift_cube, Cube right_cube,

Square square,BitmapCreate bitmapCreate)



this.cube=new Cube(lift_cube.getT(), lift_cube.getX(), 0.2f, context, bitmapCreate);

this.square = square;

this.mContext = context;

this.lift_cube = lift_cube;

this.right_cube = right_cube;

}



@Override

public void onDrawFrame(GL10 gl)

{

//---------------------------画左

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);



gl.glMatrixMode(GL10.GL_MODELVIEW);// 用后来的矩阵操作MODELVIEW栈

//画左边的多面体

gl.glLoadIdentity();// 重置矩阵

// 里移3个单位

gl.glTranslatef(-square.getX() - lift_cube.getX() + mRunX, 0, -4.0f);

// 向里面移动5个单位

gl.glRotatef(mAngleX, 0, 1, 0);// 绕y轴旋转

// 允许设置顶点

// 画多变形

lift_cube.draw(gl, texture,0);



//-----------------------------画右

//画右边的多面体

gl.glLoadIdentity();// 重置矩阵

// 里移3个单位

gl.glTranslatef(square.getX() + right_cube.getX() - mRunX, 0, -4.0f);

// 向里面移动5个单位

gl.glRotatef(-mAngleX, 0, 1, 0);// 绕y轴旋转

// 允许设置顶点

// 画多变形

right_cube.draw(gl, texture,0);





//-------------------------------画中间

gl.glLoadIdentity();

gl.glTranslatef(0, 0, -4.0f);

// 左坐标轴的设置

if (mRunX - square.getMAX_X() >= square.getMAX_X())

{

square.setLiftX(square.getMAX_X());

}

else if (mRunX - square.getMAX_X() <= -square.getMAX_X())

{

square.setLiftX(-square.getMAX_X());

}

else

{

square.setLiftX(mRunX - square.getMAX_X());

}

//右座标轴的设置

if (mRunX - square.getMAX_X() >= square.getMAX_X())

{

square.setRightX(-square.getMAX_X());

}

else if (mRunX - square.getMAX_X() <= -square.getMAX_X())

{

square.setRightX(square.getMAX_X());

}

else

{

square.setRightX(-mRunX + square.getMAX_X());

}

// 画正方形

square.draw(gl, texture,1);





//-------------------------------------画小件

gl.glLoadIdentity();

gl.glTranslatef(-lift_cube.getMAX_X()-square.getMAX_X()+mRunX, square.getY()+cube.getY(), -4.0f);

gl.glRotatef(mAngleX, 0, 1, 0);// 绕y轴旋转

cube.draw(gl, texture,3);

gl.glLoadIdentity();

gl.glTranslatef(+lift_cube.getMAX_X()+square.getMAX_X()-mRunX, square.getY()+cube.getY(), -4.0f);

gl.glRotatef(-mAngleX, 0, 1, 0);// 绕y轴旋转

cube.draw(gl, texture,3);

gl.glLoadIdentity();

gl.glTranslatef(-lift_cube.getMAX_X()-square.getMAX_X()+mRunX, -square.getY()-cube.getY(), -4.0f);

gl.glRotatef(mAngleX, 0, 1, 0);// 绕y轴旋转

cube.draw(gl, texture,3);

gl.glLoadIdentity();

gl.glTranslatef(+lift_cube.getMAX_X()+square.getMAX_X()-mRunX, -square.getY()-cube.getY(), -4.0f);

gl.glRotatef(-mAngleX, 0, 1, 0);// 绕y轴旋转

cube.draw(gl, texture,3);





gl.glDisable(GL10.GL_TEXTURE_2D);



if (mRunX > 0 && mRunX <  square.getX())

{

if (dx > 0)

{

mRunX += speed;

}

else

{

mRunX -= speed;

}

}



else if (mRunX <= 0)

{

if (dx > 0)

{

mRunX += speed;

}

else

{

mRunX = 0;

}

}

else if (mRunX >=  square.getX())

{

if (dx < 0)

{

mRunX -= speed;

}

else

{

mRunX = square.getX();

}

}



}



@Override

public void onSurfaceChanged(GL10 gl, int width, int height)

{

float ratio = (float) width / height;

gl.glViewport(0, 0, width, height);

// 设置投影矩阵

gl.glMatrixMode(GL10.GL_PROJECTION);

// 重置投影矩阵

gl.glLoadIdentity();

// 设置视口的大小

gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);

// 选择模型观察矩阵

gl.glMatrixMode(GL10.GL_MODELVIEW);

// 重置模型观察矩阵

gl.glLoadIdentity();

}



@Override

public void onSurfaceCreated(GL10 gl, EGLConfig config)

{

// 启用阴影平滑

gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

gl.glShadeModel(GL10.GL_SMOOTH);

// 黑色背景

// 设置深度缓存

gl.glClearDepthf(1.0f);

gl.glEnable(GL10.GL_DEPTH_TEST);

gl.glDepthFunc(GL10.GL_LEQUAL);



// 创建纹理

IntBuffer intBuffer = IntBuffer.allocate(4);

gl.glGenTextures(4, intBuffer);

texture = intBuffer.array();

// square.createTexture(gl);

// cube.createTexture(gl);

}


}


卷起状态


原文地址:http://www.eoeandroid.com/thread-72302-1-1.html

你可能感兴趣的:(android学习,炫丽效果)