|
, .
.. 3
1 .. 4
1.1 . 4
1.2 6
2 .. 8
2.1 . 8
2.2 . 9
3 . 15
3.1. . 15
3.2. . 16
3.3 . 18
.. 23
.. 24
.. 25
, . , . – , RGB-, . , – . . n- (x, y), – , – . , , , .
. – . – .
, , ( Jave) OpenGL ES 2.0 Android 2.2 .
, . 4 . 2,3,4 , 3 . .
MainActivity.java
packagepam.param.tram;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.Menu;
importandroid.view.MenuItem;
publicclass MainActivity extends Activity {
privateMyClassSurfaceView mGLSurfaceView;
private profili profiliki;
private float[] p1;
private int n = 3;
private double[]
x = { 0.6, 0.66, 0.74, 0.79, 0.77, 0.74, 0.59, 0.54, 0.5,0.22, 0.17 },
b = { 0.4, 0.44, 0.49, 0.41, 0.35, 0.26, 0.2, 0.3, 0.44, 0.5, 0.64 },
c = { 0.01, 0.5, 0.57, 0.61, 0.66, 0.7, 0.63,0.55, 0.5, 0.45, 0.4 },
d = { 0.64, 0.74, 0.82, 0.85, 0.89, 0.93,0.96, 0.91, 0.93, 0.8, 0.8 },
y = { 0.59, 0.62, 0.65, 0.68, 0.71,0.74, 0.77, 0.8, 0.83, 0.86, 0.89 };
private float[] ap2 = new float[n + 1];
private float[] ap = new float[n + 1];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
profiliki = new profili();
mGLSurfaceView = new MyClassSurfaceView(this);
setContentView(mGLSurfaceView);
}
@Override
protected void onPause() {
super.onPause();
mGLSurfaceView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mGLSurfaceView.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.kn1:
ap=profiliki.Sglaj(x,y,n);
mGLSurfaceView.setP1(ap);
break;
case R.id.kn2:
ap=profiliki.Sglaj(b,y,n);
mGLSurfaceView.setP1(ap);
/*
break;
case R.id.kn3:
ap = profiliki.Sglaj(c, y, n);
mGLSurfaceView.setP1(ap);
break;
case R.id.kn4:
ap = profiliki.Sglaj(d, y, n);
mGLSurfaceView.setP1(ap);
break;
case R.id.step2:
if (item.isChecked())
item.setChecked(true);
else
item.setChecked(false);
n = 2;
ap = profiliki.Sglaj(d, y, n);
mGLSurfaceView.setP1(ap);
return true;
case R.id.step3:
if (item.isChecked())
item.setChecked(true);
else
item.setChecked(false);
n = 3;
ap = profiliki.Sglaj(d, y, n);
mGLSurfaceView.setP1(ap);
return true;
case R.id.step4:
if (item.isChecked())
item.setChecked(true);
else
item.setChecked(false);
n = 4;
ap = profiliki.Sglaj(d, y, n);
mGLSurfaceView.setP1(ap);
return true;
}
return super.onOptionsItemSelected(item);
}
}
MyClassSurfaceView.java
package pam.param.tram;
import android.content.Context;
import android.opengl.GLSurfaceView;
// MyClassSurfaceView GLSurfaceView
public class MyClassSurfaceView extends GLSurfaceView{
private MyClassRenderer renderer;
public MyClassSurfaceView(Context context) {
super(context);
setEGLContextClientVersion(2);
renderer = new MyClassRenderer(context);
setRenderer(renderer);
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
public float[] getP1() {
return renderer.getP1();
}
public void setP1(float[] a0) {
renderer.setP1(a0);
}
}
MyClassRenderer.java
package pam.param.tram;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
//GLES20.glDisable(GLES20.GL_DITHER)
public class MyClassRenderer implements GLSurfaceView.Renderer{
private Context context;
private float xamera, yCamera, zCamera;
//private float a0, a1, a2,a3;
float[] P1={0.6016f, -7.1823f,18.1216f, -11.1600f};
public float[] getP1() {
return P1;
}
public void setP1(float[] a0) {
this.P1 = a0;
}
private float xLightPosition, yLightPosition, zLightPosition;
private float[] modelMatrix;
private float[] viewMatrix;
private float[] modelViewMatrix;
private float[] projectionMatrix;
private float[] modelViewProjectionMatrix;
private FloatBuffer vertexBuffer;
private FloatBuffer normalBuffer;
private FloatBuffer colorBuffer;
private Shader mShader;
public MyClassRenderer(Context context) {
this.context=context;
xLightPosition=0;
yLightPosition=1.1f;
zLightPosition=0.5f;
modelMatrix=new float[16];
viewMatrix=new float[16];
modelViewMatrix=new float[16];
projectionMatrix=new float[16];
modelViewProjectionMatrix=new float[16];
Matrix.setIdentityM(modelMatrix, 0);
//
xamera=0.6f;
yCamera=3.4f;
zCamera=3f;
Matrix.setLookAtM(
viewMatrix, 0, xamera, yCamera, zCamera, 0, 0, 0, 0, 1, 0);
Matrix.multiplyMM(modelViewMatrix, 0, viewMatrix, 0, modelMatrix, 0);
// 1
float vertexArray [] = {-2,1,0, -2,1,1, 0,0.5f,0,
0,0.5f,1, 2,1,0, 2,1,1};
ByteBuffer bvertex = ByteBuffer.allocateDirect(vertexArray.length*4);
bvertex.order(ByteOrder.nativeOrder());
vertexBuffer = bvertex.asFloatBuffer();
vertexBuffer.position(0);
vertexBuffer.put(vertexArray);
vertexBuffer.position(0);
float nx=0;
float ny=1;
float nz=0;
floatnormalArray[] ={nx, ny, nz,nx, ny, nz,nx, ny, nz,nx,ny,nz,nx, ny, nz, nx, ny, nz, nx, ny, nz, nx, ny, nz, };
//
ByteBuffer bnormal = ByteBuffer.allocateDirect(normalArray.length*4);
bnormal.order(ByteOrder.nativeOrder());
normalBuffer = bnormal.asFloatBuffer();
normalBuffer.position(0);
normalBuffer.put(normalArray);
normalBuffer.position(0);
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
float k=0.055f;
float left = -k*ratio;
float right = k*ratio;
float bottom = -k;
float top = k;
float near = 0.1f;
floatfar= 10.0f;
Matrix.frustumM(projectionMatrix, 0, left, right, bottom, top, near, far);
Matrix.multiplyMM(
modelViewProjectionMatrix, 0, projectionMatrix, 0, modelViewMatrix, 0);
}
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
//
GLES20.glEnable(GLES20.GL_CULL_FACE);
GLES20.glHint(
GLES20.GL_GENERATE_MIPMAP_HINT, GLES20.GL_NICEST);
String vertexShaderCode=
"uniform mat4 u_modelViewProjectionMatrix;"+
"uniform vec4 u_profil;"+
"uniform vec3 u_profil_d;"+
"attribute vec3 a_vertex;"+
"attribute vec3 a_normal;"+
"attribute vec4 a_color;"+
"varying vec3 v_vertex;"+
"varying vec3 v_normal;"+
"varying vec4 v_color;"+
"varying vec4 v_profil;"+
"varying vec3 v_profil_d;"+
"void main() {"+
"v_profil_d=u_profil_d;"+
"v_profil=u_profil;"+
"v_vertex=a_vertex;"+
"vec3 n_normal=normalize(a_normal);"+
"v_normal=n_normal;"+
"v_color=a_color;"+
"gl_Position = u_modelViewProjectionMatrix * vec4(a_vertex,1.0);"+
"}";
//
String fragmentShaderCode=
"precision mediump float;"+
"uniform vec3 u_camera;"+
"uniform vec3 u_lightPosition;"+
"varying vec3 v_profil_d;"+
"varying vec3 v_vertex;"+
"varying vec3 v_normal;"+
"varying vec4 v_color;"+
"varying vec4 v_profil;"+
"void main() {"+
"vec3 n_normal=normalize(v_normal);"+
"vec3 lightvector = normalize(u_lightPosition - v_vertex);"+
"vec3 lookvector = normalize(u_camera - v_vertex);"+
// "vec3 one=vec3(0.8,0.8,0.0);" .
"float ambient=0.2;"+
"float k_diffuse=0.8;"+
"float k_specular=0.2;"+
"float diffuse = k_diffuse * max(dot(n_normal, lightvector), 0.0);"+
"vec3 reflectvector = reflect(-lightvector, n_normal);"+
"float specular = k_specular * pow( max(dot(lookvector,reflectvector),0.0), 8.0 );"+
"vec4 one=vec4(0.8,0.8,0.0,1.0);"+
"float a0=0.0; \n"
+"float a1=0.0; \n"
+"float a2=2.0; \n"
+"float dx=(u_lightPosition.x-v_vertex.x)*(u_lightPosition.x-v_vertex.x); \n"
+"float dy=(u_lightPosition.y-v_vertex.y)*(u_lightPosition.y-v_vertex.y); \n"
+"float dz=(u_lightPosition.z-v_vertex.z)*(u_lightPosition.z-v_vertex.z); \n"
+"float d=sqrt(dx+dy+dz); \n"
+"float niz=(a0+a1*d+a2*pow(d,2.0)); \n"
+"float frz=pow(niz,-1.0); \n"
+"vec3 lvobr=normalize( v_vertex-u_lightPosition); \n"
+"vec3 naprsveta=vec3(0.0,-1.0,0.0); \n"
+"float nz=0.9; \n"
+"float cosmej=dot(lvobr,naprsveta); \n"
+"float fyz=pow(cosmej,nz); \n"
+ "float pro= v_profil.x+v_profil.y*cosmej+v_profil.z*pow(cosmej,2.0)+v_profil.w*pow(cosmej,3.0)+v_profil_d.x*pow(cosmej,4.0); \n"
+"vec4 lightColor =max(fyz*frz*pro*(diffuse+specular),0.06)*one;"+
"gl_FragColor =lightColor;"+
"}";
mShader=new Shader(vertexShaderCode, fragmentShaderCode);
mShader.linkVertexBuffer(vertexBuffer);
mShader.linkNormalBuffer(normalBuffer);
mShader.linkColorBuffer(colorBuffer);
}
public void onDrawFrame(GL10 unused) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
mShader.linkModelViewProjectionMatrix(modelViewProjectionMatrix);
mShader.linkCamera(xamera, yCamera, zCamera);
mShader.linkProfil(P1);
mShader.linkLightSource(xLightPosition, yLightPosition, zLightPosition);
mShader.useProgram();
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 6);
/// 6 , , 6
}
}
Shader.java
package pam.param.tram;
import java.nio.FloatBuffer;
import android.opengl.GLES20;
public class Shader {
privateintprogram_Handle;
public Shader(String vertexShaderCode, String fragmentShaderCode) {
createProgram(vertexShaderCode, fragmentShaderCode);
}
private void createProgram(String vertexShaderCode,
String fragmentShaderCode) {
int vertexShader_Handle = GLES20
.glCreateShader(GLES20.GL_VERTEX_SHADER);
GLES20.glShaderSource(vertexShader_Handle, vertexShaderCode);
GLES20.glCompileShader(vertexShader_Handle);
int fragmentShader_Handle = GLES20
.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
GLES20.glShaderSource(fragmentShader_Handle, fragmentShaderCode);
GLES20.glCompileShader(fragmentShader_Handle);
program_Handle = GLES20.glCreateProgram();
GLES20.glAttachShader(program_Handle, vertexShader_Handle);
GLES20.glAttachShader(program_Handle, fragmentShader_Handle);
GLES20.glLinkProgram(program_Handle);
}
public void linkVertexBuffer(FloatBuffer vertexBuffer) {
GLES20.glUseProgram(program_Handle);
int a_vertex_Handle = GLES20.glGetAttribLocation(program_Handle,
"a_vertex");
// a_vertex
GLES20.glEnableVertexAttribArray(a_vertex_Handle);
GLES20.glVertexAttribPointer(a_vertex_Handle, 3, GLES20.GL_FLOAT,
false, 0, vertexBuffer);
}
public void linkNormalBuffer(FloatBuffer normalBuffer) {
GLES20.glUseProgram(program_Handle);
int a_normal_Handle = GLES20.glGetAttribLocation(program_Handle,
"a_normal");
GLES20.glEnableVertexAttribArray(a_normal_Handle);
GLES20.glVertexAttribPointer(a_normal_Handle, 3, GLES20.GL_FLOAT,
false, 0, normalBuffer);
}
public void linkModelViewProjectionMatrix(float[] modelViewProjectionMatrix) {
GLES20.glUseProgram(program_Handle);
int u_modelViewProjectionMatrix_Handle = GLES20.glGetUniformLocation(
program_Handle, "u_modelViewProjectionMatrix");
GLES20.glUniformMatrix4fv(u_modelViewProjectionMatrix_Handle, 1, false,
modelViewProjectionMatrix, 0);
}
public void linkProfil(float[] p) {
GLES20.glUseProgram(program_Handle);
int u_profil_Handle = GLES20.glGetUniformLocation(program_Handle,
"u_profil");
int u_profil_dobav = GLES20.glGetUniformLocation(program_Handle,
"u_profil_d");
if (p.length == 4) {
GLES20.glUniform4f(u_profil_Handle, p[0], p[1], p[2], p[3]);
GLES20.glUniform3f(u_profil_dobav, 0, 0, 0);
} else if (p.length == 3) {
GLES20.glUniform4f(u_profil_Handle, p[0], p[1], p[2], 0);
GLES20.glUniform3f(u_profil_dobav, 0, 0, 0);
} else if (p.length == 5) {
GLES20.glUniform4f(u_profil_Handle, p[0], p[1], p[2], p[3]);
GLES20.glUniform3f(u_profil_dobav, p[4], 0, 0);
}
}
public void linkCamera(float xCamera, float yCamera, float zCamera) {
GLES20.glUseProgram(program_Handle);
int u_camera_Handle = GLES20.glGetUniformLocation(program_Handle,
"u_camera");
GLES20.glUniform3f(u_camera_Handle, xCamera, yCamera, zCamera);
}
public void linkLightSource(float xLightPosition, float yLightPosition,
float zLightPosition) {
GLES20.glUseProgram(program_Handle);
int u_lightPosition_Handle = GLES20.glGetUniformLocation(
program_Handle, "u_lightPosition");
GLES20.glUniform3f(u_lightPosition_Handle, xLightPosition,
yLightPosition, zLightPosition);
}
public void useProgram() {
GLES20.glUseProgram(program_Handle);
}
}
Profile.java
package pam.param.tram;
public class profili {
&