Laborator 4

Laborator 4

import javax.microedition.khronos.opengles.GL10;

import javax.microedition.khronos.opengles.GL11;

import java.nio.ByteBuffer;

import java.nio.ByteOrder;

import java.nio.FloatBuffer;

public class Cube {

private FloatBuffer mFVertexBuffer;

private ByteBuffer mColorBuffer,mTFan1,mTFan2;

private ByteBuffer mIndexBuffer;

private FloatBuffer mNormalBuffer;

public Cube(){

float vertices[] =

{

-1.0f, 1.0f, 1.0f,

1.0f, 1.0f, 1.0f,

1.0f, -1.0f, 1.0f,

-1.0f, -1.0f, 1.0f,

-1.0f, 1.0f, -1.0f,

1.0f, 1.0f, -1.0f,

1.0f, -1.0f, -1.0f,

-1.0f, -1.0f, -1.0f

};

byte maxColor=(byte)255;

byte colors[] =

{

maxColor, 0, 0, maxColor,

maxColor, 0, 0, maxColor,

maxColor, 0, 0, maxColor,

maxColor, 0, 0, maxColor,

0, 0, 0, maxColor,

0, 0, 0, maxColor,

0, 0, 0, maxColor,

0, 0, 0, maxColor,

};

byte tFan1[] =

{

1,0,3,

1,3,2,

1,2,6,

1,6,5,

1,5,4,

1,4,0

};

byte tFan2[] =

{

7,4,5,

7,5,6,

7,6,2,

7,2,3,

7,3,0,

7,0,4

};

float[] normals =

{

-1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3),

1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3),

1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3),

-1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3),

-1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3),

1.0f/(float)Math.sqrt(3), 1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3),

1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3),

-1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3), -1.0f/(float)Math.sqrt(3)

};

ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);

vbb.order(ByteOrder.nativeOrder());

mFVertexBuffer = vbb.asFloatBuffer();

mFVertexBuffer.put(vertices);

mFVertexBuffer.position(0);

mColorBuffer = ByteBuffer.allocateDirect(colors.length);

mColorBuffer.put(colors);

mColorBuffer.position(0);

mTFan1 = ByteBuffer.allocateDirect(tFan1.length);

mTFan1.put(tFan1);

mTFan1.position(0);

mTFan2 = ByteBuffer.allocateDirect(tFan2.length);

mTFan2.put(tFan2);

mTFan2.position(0);

ByteBuffer nbb = ByteBuffer.allocateDirect(normals.length * 4);

nbb.order(ByteOrder.nativeOrder());

mNormalBuffer = nbb.asFloatBuffer();

mNormalBuffer.put(normals);

mNormalBuffer.position(0);

}

public void draw(GL10 gl){

gl.glVertexPointer(3, GL11.GL_FLOAT, 0, mFVertexBuffer);

gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer);

gl.glDrawElements( gl.GL_TRIANGLE_FAN, 6 * 3, gl.GL_UNSIGNED_BYTE, mTFan1);

gl.glDrawElements( gl.GL_TRIANGLE_FAN, 6 * 3, gl.GL_UNSIGNED_BYTE, mTFan2);

gl.glNormalPointer(GL10.GL_FLOAT, 0, mNormalBuffer);

gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);

}

}

import android.opengl.GLSurfaceView;

import javax.microedition.khronos.egl.EGLConfig;

import javax.microedition.khronos.opengles.GL10;

import java.nio.ByteBuffer;

import java.nio.ByteOrder;

import java.nio.FloatBuffer;

public class CubeRenderer implements GLSurfaceView.Renderer {

private Cube mCube;

private float mTransY,mAngle;

public final static int SS_SUNLIGHT = GL10.GL_LIGHT0;

public CubeRenderer(){

mCube = new Cube();

}

public void onDrawFrame(GL10 gl){

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

gl.glMatrixMode(GL10.GL_MODELVIEW);

gl.glLoadIdentity();

gl.glTranslatef(0.0f, (float) Math.sin(mTransY), -7.0f);

mTransY += 0.075f;

gl.glRotatef(mAngle, 0.0f, 1.0f, 0.0f);

gl.glRotatef(mAngle, 1.0f, 0.0f, 0.0f);

mAngle += 0.4;

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

mCube.draw(gl);

}

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

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

gl.glMatrixMode(GL10.GL_PROJECTION);

gl.glLoadIdentity();

float fieldOfView = 30.0f/57.3f;

float aspectRatio = (float)width/(float)height;

float zNear = 0.1f;

float zFar = 1000;

float size = zNear * (float)(Math.tan((double)(fieldOfView/2.0f)));

gl.glFrustumf(-size, size, -size/aspectRatio, size/aspectRatio, zNear, zFar);

gl.glMatrixMode(GL10.GL_MODELVIEW);

}

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_CULL_FACE);

gl.glShadeModel(GL10.GL_SMOOTH);

gl.glEnable(GL10.GL_DEPTH_TEST);

//gl.glDepthMask(false);

initLighting(gl);

}

public void initLighting(GL10 gl){

float[] green = {0.0f, 1.0f, 0.0f, 1.0f};

//float[] position = {0.0f, 5.0f, 0.0f, 1.0f};

float[] position = {10.0f,0.0f,3.0f,1.0f};

float[] red = {1.0f, 0.0f, 0.0f, 1.0f};

float[] blue = {0.0f,0.0f,1.0f,1.0f};

float[] colorVector={0.2f, 0.2f, 0.2f, 1.0f};

// float[] yellow={1.0f,1.0f,0.0f,0.1f};

float[] direction= {1.0f, 0.0f, 0.0f};

gl.glLightfv(SS_SUNLIGHT, GL10.GL_POSITION, makeFloatBuffer(position));

gl.glLightfv(SS_SUNLIGHT, GL10.GL_DIFFUSE, makeFloatBuffer(green));

gl.glShadeModel(GL10.GL_SMOOTH);

gl.glEnable(GL10.GL_LIGHTING);

gl.glEnable(SS_SUNLIGHT);

gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, makeFloatBuffer(red));

gl.glLightfv(SS_SUNLIGHT,GL10.GL_SPECULAR, makeFloatBuffer(red));

gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, makeFloatBuffer(red));

gl.glMaterialf(GL10.GL_FRONT_AND_BACK,GL10.GL_SHININESS, 5);

gl.glLightfv(SS_SUNLIGHT, GL10.GL_AMBIENT, makeFloatBuffer(blue));

gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, makeFloatBuffer(blue));

gl.glLightModelfv(GL10.GL_LIGHT_MODEL_AMBIENT, makeFloatBuffer(colorVector));

// gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_EMISSION, makeFloatBuffer(yellow));

gl.glEnable(GL10.GL_COLOR_MATERIAL);

gl.glLightf(SS_SUNLIGHT, GL10.GL_LINEAR_ATTENUATION, 0.025f);

gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPOT_DIRECTION, makeFloatBuffer(direction));

}

protected static FloatBuffer makeFloatBuffer(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;

}

}

import android.opengl.GLSurfaceView;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.WindowManager;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

GLSurfaceView view = new GLSurfaceView(this);

view.setRenderer(new CubeRenderer());

setContentView(view);

}

}