Skip to content

Commit

Permalink
Show a textured triangle #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanl committed Jan 21, 2012
1 parent c6d0ef7 commit 08b6e68
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 152 deletions.
4 changes: 2 additions & 2 deletions assets/hellogl_with_mesh_vertex_shader.glsl
@@ -1,10 +1,10 @@
uniform mat4 uMVPMatrix;
attribute vec4 vPosition;
attribute vec4 aPosition;
attribute vec4 SourceColor;

varying vec4 DestinationColor;

void main() {
DestinationColor = SourceColor;
gl_Position = uMVPMatrix * vPosition;
gl_Position = uMVPMatrix * aPosition;
}
31 changes: 25 additions & 6 deletions src/com/osesm/randy/framework/gl/Mesh.java
Expand Up @@ -5,6 +5,8 @@
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

import android.util.Log;

public abstract class Mesh {

private boolean hasColor;
Expand All @@ -16,6 +18,7 @@ public abstract class Mesh {
private ShortBuffer indices;
private ShaderCompiler shaderCompiler;
private int program;
private Texture texture;

protected static final int FLOAT_SIZE = 4;

Expand All @@ -28,6 +31,8 @@ public Mesh(int maxVertices, int maxIndices, boolean hasColor, boolean hasTexCoo
: 0)) * FLOAT_SIZE;
tmpBuffer = new int[maxVertices * vertexSize / FLOAT_SIZE];

Log.d("Randy", "vertex size = " + vertexSize);

ByteBuffer buffer = ByteBuffer.allocateDirect(maxVertices * vertexSize);
buffer.order(ByteOrder.nativeOrder());
vertices = buffer.asIntBuffer();
Expand Down Expand Up @@ -84,13 +89,27 @@ protected IntBuffer getVertices() {
protected ShortBuffer getIndices() {
return indices;
}

public abstract void prepare(float[] viewProjectionMatrix);
public abstract void draw();
public abstract void reset();



public void setTexture(Texture texture) {
this.texture = texture;
}

public Texture getTexture() {
return texture;
}

public boolean hasTexture() {
return hasTexCoords;
}

public boolean hasColor() {
return hasColor;
}

public boolean hasNormals() {
return hasNormals;
}

public abstract void prepare(float[] viewProjectionMatrix);
public abstract void draw();
}
77 changes: 77 additions & 0 deletions src/com/osesm/randy/framework/gl/Texture.java
@@ -0,0 +1,77 @@
package com.osesm.randy.framework.gl;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLUtils;
import android.util.Log;

public class Texture {
private static final String TAG = "Texture";
private int rawResource;
private Context context;
private int textureID;

public Texture(Context context, int rawResource) {
this.context = context;
this.rawResource = rawResource;
load();
}

private void load() {
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
textureID = textures[0];

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);
checkGlError("bind texture");

GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
GLES20.GL_NEAREST);
checkGlError("set min filter");
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,
GLES20.GL_LINEAR);
checkGlError("set mag filter");

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
GLES20.GL_REPEAT);
checkGlError("set wrap s");
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
GLES20.GL_REPEAT);
checkGlError("set wrap t");

InputStream is = context.getResources().openRawResource(rawResource);
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(is);
} finally {
try {
is.close();
} catch (IOException e) {
// Ignore.
}
}

GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}

public void bind() {
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
checkGlError("activate texture");
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);
checkGlError("bind texture");
}

private void checkGlError(String op) {
int error;
while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
Log.e(TAG, op + ": glError " + error);
throw new RuntimeException(op + ": glError " + error);
}
}
}
8 changes: 0 additions & 8 deletions src/com/osesm/randy/lab/HelloGLWithMeshTest.java
Expand Up @@ -213,14 +213,6 @@ public void draw() {
public void setAngle(float angle) {
mAngle = angle;
}


@Override
public void reset() {
// TODO Auto-generated method stub

}

}

}

0 comments on commit 08b6e68

Please sign in to comment.