标题
OpenGLES 1.x相比于OpenGLES 2.x和3.x,不用写着色器,方便许多,今天闲着没事,就写几个类 不过貌似有点问题,难道是我使用方式不对??? 这个先留着当模板,寒假写个OpenGLES 3.x的
代码
Camera.java
package com.Diamond.gl10try;
import android.opengl.*;
import android.renderscript.Float3;
import android.opengl.Matrix;
import android.opengl.GLES11;
import android.renderscript.Matrix4f;
import android.util.Log;
public class Camera {
private Float3 m_eye;
private Float3 m_center;
private Float3 m_up;
public Camera() {
m_eye = new Float3();
m_center = new Float3(0.0f,0.0f,-1.0f);
m_up = new Float3(0.0f,1.0f,0.0f);
}
public Camera(Float3 eye,Float3 center,Float3 up) {
m_eye = eye;
m_center = center;
m_up = up;
}
public Camera setEye(Float3 value) {
m_eye = value;
return this;
}
public Camera setCenter(Float3 value) {
m_center = value;
return this;
}
public Camera setUp(Float3 value) {
m_up = value;
return this;
}
public Float3 getEye() {
return m_eye;
}
public Float3 getCenter() {
return m_center;
}
public Float3 getUp() {
return m_up;
}
public Camera forward(float l) {
m_eye.z = m_eye.z + l;
return this;
}
public Camera back(float l) {
return forward(-l);
}
public Camera up(float l) {
m_eye.y = m_eye.y + l;
return this;
}
public Camera down(float l) {
return up(-l);
}
public Camera left(float l) {
m_eye.x = m_eye.x + l;
return this;
}
public Camera right(float l) {
return left(-l);
}
public static Matrix4f getProjection(float fovy,float aspect,float near,float far) {
Matrix4f matrix = new Matrix4f();
matrix.loadIdentity();
matrix.loadPerspective(fovy,aspect,near,far);
return matrix;
}
public Matrix4f getView() {
float[] matrix = new float[16];
Matrix.setLookAtM(matrix,0,m_eye.x,m_eye.y,m_eye.z,m_center.x,m_center.y,m_center.z,m_up.x,m_up.y,m_up.z);
return new Matrix4f(matrix);
}
public Camera updateCamera() {
Matrix4f matrix = this.getProjection(120.0f,0.5f,0.1f,100.0f);
matrix.multiply(getView());
GLES11.glMatrixMode(GLES11.GL_PROJECTION);
GLES11.glLoadIdentity();
GLES11.glMultMatrixf(matrix.getArray(),0);
return this;
}
}
Object.java
package com.Diamond.gl10try;
import com.Diamond.gl10try.Vertex;
import android.opengl.*;
import android.renderscript.Float3;
import android.renderscript.Matrix4f;
import android.opengl.GLES11;
import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class Object {
private Float3 m_position;
private Float3 m_rotate;
private Float3 m_scale;
private float[] m_vertices;
private float[] m_color;
public Object() {
m_position = new Float3();
m_rotate = new Float3();
m_scale = new Float3();
m_vertices = null;
m_color = null;
}
public Object(float[] ver) {
m_position = new Float3();
m_rotate = new Float3();
m_scale = new Float3();
m_vertices = ver;
m_color = null;
}
public Object(float[] ver,float[] col) {
m_position = new Float3();
m_rotate = new Float3();
m_scale = new Float3();
m_vertices = ver;
m_color = col;
}
public Object setVertices(float[] ver) {
m_vertices = ver;
return this;
}
public float[] getVertices() {
return m_vertices;
}
public Object setColor(float[] col) {
m_color = col;
return this;
}
public float[] getColor() {
return m_color;
}
public Object translate(Float3 value) {
m_position = value;
return this;
}
public Object rotate(Float3 value) {
m_rotate = value;
return this;
}
public Object scale(Float3 value) {
m_scale = value;
return this;
}
public Float3 getTranslate() {
return m_position;
}
public Float3 getRotate() {
return m_rotate;
}
public Float3 getScale() {
return m_scale;
}
public Matrix4f getModelView() {
Matrix4f modelview = new Matrix4f();
modelview.loadIdentity();
modelview.translate(m_position.x,m_position.y,m_position.z);
modelview.rotate(m_rotate.x,1.0f,0.0f,0.0f);
modelview.rotate(m_rotate.y,0.0f,1.0f,0.0f);
modelview.rotate(m_rotate.z,0.0f,0.0f,1.0f);
modelview.scale(m_scale.x,m_scale.y,m_scale.z);
return modelview;
}
public Object setModelView() {
GLES11.glMatrixMode(GLES11.GL_MODELVIEW);
GLES11.glLoadIdentity();
GLES11.glMultMatrixf(getModelView().getArray(),0);
return this;
}
public Object setModelView2() {
GLES11.glMatrixMode(GLES11.GL_MODELVIEW);
GLES11.glLoadIdentity();
GLES11.glTranslatef(m_position.x,m_position.y,m_position.z);
GLES11.glRotatef(m_rotate.x,1.0f,0.0f,0.0f);
GLES11.glRotatef(m_rotate.y,0.0f,1.0f,0.0f);
GLES11.glRotatef(m_rotate.z,0.0f,0.0f,1.0f);
GLES11.glScalef(m_scale.x,m_scale.y,m_scale.z);
return this;
}
public static FloatBuffer getBuffer(float[] array) {
ByteBuffer bb = ByteBuffer.allocateDirect(array.length * 4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(array);
fb.position(0);
return fb;
}
public Object enableCilent() {
GLES11.glEnableClientState(GLES11.GL_VERTEX_ARRAY);
GLES11.glVertexPointer(3,GLES11.GL_FLOAT,0,this.getBuffer(m_vertices));
if(m_color != null) {
GLES11.glEnableClientState(GLES11.GL_COLOR_ARRAY);
GLES11.glColorPointer(4,GLES11.GL_FLOAT,0,this.getBuffer(m_color));
}
return this;
}
public Object disableClient() {
GLES11.glDisableClientState(GLES11.GL_VERTEX_ARRAY);
GLES11.glDisableClientState(GLES11.GL_VERTEX_ARRAY);
return this;
}
}
Vertex.java
package com.Diamond.gl10try;
import android.renderscript.Float3;
import android.renderscript.Float2;
public class Vertex {
public Float3 position;
public Float3 normal;
public Float2 texCoord;
public Vertex() {
position = new Float3();
normal = new Float3();
texCoord = new Float2();
}
public Vertex(Float3 pos) {
position = pos;
normal = new Float3();
texCoord = new Float2();
}
public Vertex(Float3 pos,Float3 nor) {
position = pos;
normal = nor;
texCoord = new Float2();
}
public Vertex(Float3 pos,Float2 tex) {
position = pos;
normal = new Float3();
texCoord = tex;
}
public Vertex(Float3 pos,Float3 nor,Float2 tex) {
position = pos;
normal = nor;
texCoord = tex;
}
}
|