Slike ( teksture )
-
Rađeno na CyanogenMod 7.2 OS-u
- Sve slike potrebno staviti u folder: res/drawable-nodpi
- Slike veličine potencije broja dva: 2x2, 4x4, 8x8, 16x16, 32x32, 64x64, 128x128, 256x256, ..
OpenGLRenderer
@Override
public void onDrawFrame(GL10 gl) {
// Clears the screen and depth buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -6.0f);
gl.glScalef(0.2f, 0.2f, 0.2f);
gl.glTranslatef(0, -4, 0);
logo.draw(gl, context);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
if (height == 0) {
height = 1;
}
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
// Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glEnable(GL10.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
gl.glShadeModel(GL10.GL_SMOOTH); // Enable Smooth Shading
gl.glClearColor(1f, 1f, 0.878f, 0.5f); // Light Yellow Background
gl.glClearDepthf(1.0f); // Depth Buffer Setup
gl.glEnable(GL10.GL_DEPTH_TEST); // Enables Depth Testing
gl.glDepthFunc(GL10.GL_LEQUAL); // The Type Of Depth Testing To Do
// Really Nice Perspective Calculations
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
Square.java:
private FloatBuffer vertexBuffer; // buffer holding the vertices
private float vertices[] = {
-1.0f, -1.0f, 0.0f, // V1 - bottom left
-1.0f, 1.0f, 0.0f, // V2 - top left
1.0f, -1.0f, 0.0f, // V3 - bottom right
1.0f, 1.0f, 0.0f // V4 - top right
};
Square.java:
private FloatBuffer textureBuffer; // buffer holding the texture coordinates
private float texture[] = { // Mapping coordinates for the vertices
0.0f, 1.0f, // top left (V2)
0.0f, 0.0f, // bottom left (V1)
1.0f, 1.0f, // top right (V4)
1.0f, 0.0f // bottom right (V3)
};
/** The texture pointer */
private int[] textures = new int[1];
Učitavanje teksture
MainActivity.java:
public static int tekstura;
public static int[] teksture = new int[]
{
R.drawable.box,
R.drawable.cigle,
R.drawable.steel,
R.drawable.kod,
R.drawable.brick001,
R.drawable.kameni,
R.drawable.road
};;
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event != null) {
if (teksture != null) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
tekstura = teksture[brojac];
brojac = (short) (++brojac % teksture.length);
}
}
}
return true;
}
Square.java:
public void loadGLTexture(GL10 gl, Context context) {
// loading texture
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), MainActivity.tekstura);
// generate one texture pointer
gl.glGenTextures(1, textures, 0);
// ...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// create nearest filtered texture
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
// Use Android GLUtils to specify a two-dimensional texture image from
// our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
// Clean up
bitmap.recycle();
}
Crtanje na ekran
Square.java
public void draw(GL10 gl) {
// Point to our buffers
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Set the face rotation
gl.glFrontFace(GL10.GL_CW);
// Point to our vertex buffer
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
// Disable the client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
Crtanje pojedine stranice
Cube.java:
public void draw(GL10 gl, Context context) {
square.loadGLTexture(gl, context);
gl.glPushMatrix();
gl.glTranslatef(0, 0, 1);
// front surface
square.draw(gl);
// front surface
gl.glPushMatrix();
gl.glTranslatef(0, 0, -1);
gl.glTranslatef(1, 0, 0);
gl.glRotatef(90, 0, 1, 0);
square.draw(gl);
gl.glPopMatrix();
// back surface
gl.glPushMatrix();
gl.glTranslatef(0, 0, -2);
square.draw(gl);
gl.glPopMatrix();
// left surface
gl.glPushMatrix();
gl.glTranslatef(0, 0, -1);
gl.glTranslatef(-1, 0, 0);
gl.glRotatef(90, 0, 1, 0);
square.draw(gl);
gl.glPopMatrix();
// top surface
gl.glPushMatrix();
gl.glTranslatef(0, 0, -1);
gl.glTranslatef(0, 1, 0);
gl.glRotatef(90, 1, 0, 0);
square.draw(gl);
gl.glPopMatrix();
// bottom surface
gl.glPushMatrix();
gl.glTranslatef(0, 0, -1);
gl.glTranslatef(0, -1, 0);
gl.glRotatef(90, 1, 0, 0);
square.draw(gl);
gl.glPopMatrix();
gl.glPopMatrix();
}
|
Izgled na kraju:
|
|
|
Logo.java
@Override
public void draw(GL10 gl, Context context) {
int temp = MainActivity.tekstura;
MainActivity.tekstura = MainActivity.sky;
// Draw sky
square.loadGLTexture(gl, context);
gl.glPushMatrix();
gl.glTranslatef(0, 0, -20);
gl.glScalef(15.0f, 26.0f, 0.0f);
square.draw(gl);
gl.glPopMatrix();
MainActivity.tekstura = MainActivity.background;
// Draw ground
square.loadGLTexture(gl, context);
gl.glPushMatrix();
gl.glTranslatef(0, 0, -1);
gl.glTranslatef(0, -3, 0);
gl.glRotatef(90, 1, 0, 0);
gl.glScalef(15.0f, 15.0f, 0.0f);
square.draw(gl);
gl.glPopMatrix();
MainActivity.tekstura = temp;
gl.glPushMatrix();
gl.glRotatef(angle, 0, 10, 0);
cube.draw(gl, context);
gl.glTranslatef(0, 2, 0);
cube.draw(gl, context);
gl.glTranslatef(0, 2, 0);
cube.draw(gl, context);
gl.glTranslatef(0, 2, 0);
cube.draw(gl, context);
gl.glTranslatef(0, 2, 0);
cube.draw(gl, context);
gl.glTranslatef(2, 0, 0);
cube.draw(gl, context);
gl.glTranslatef(0, -4, 0);
cube.draw(gl, context);
gl.glTranslatef(4, -4, 0);
cube.draw(gl, context);
gl.glTranslatef(0, 2, 0);
cube.draw(gl, context);
gl.glTranslatef(0, 2, 0);
cube.draw(gl, context);
gl.glTranslatef(0, 4 + visina, 0);
cube.draw(gl, context);
gl.glPopMatrix();
angle += kut;
if(true == gore)
{
visina += 0.1;
if(visina > 2)
{
gore = false;
}
}
else
{
visina -= 0.1;
if(visina < -1)
{
gore = true;
}
}
}