draw spinning quad with a texture instead of smoothed colors

This commit is contained in:
Josh Holtrop 2012-11-19 22:15:09 -05:00
parent d34c51acf9
commit 758435fd63
6 changed files with 47 additions and 20 deletions

View File

@ -1,7 +1,8 @@
varying vec3 color_i;
varying vec2 tex_coord_i;
uniform sampler2D tex;
void main()
{
gl_FragColor = vec4(color_i, 1);
gl_FragColor = texture2D(tex, tex_coord_i);
}

View File

@ -2,11 +2,11 @@
uniform mat4 projection;
uniform mat4 modelview;
attribute vec3 pos;
attribute vec3 color;
varying vec3 color_i;
attribute vec2 tex_coord;
varying vec2 tex_coord_i;
void main()
{
gl_Position = projection * modelview * vec4(pos, 1);
color_i = color;
tex_coord_i = tex_coord;
}

BIN
res/drawable/texture.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@ -3,7 +3,6 @@ package com.homelinux.holtrop.opengltest;
import android.opengl.GLSurfaceView;
import android.app.Activity;
import android.os.Bundle;
import android.content.res.AssetManager;
import android.view.WindowManager;
import android.view.Window;
@ -21,7 +20,7 @@ public class MainOpenGL extends Activity
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
mGLView = new MyGLSurfaceView(this, getAssets());
mGLView = new MyGLSurfaceView(this, getAssets(), getResources());
setContentView(mGLView);
}
}

View File

@ -3,19 +3,21 @@ package com.homelinux.holtrop.opengltest;
import android.opengl.GLSurfaceView;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.view.MotionEvent;
public class MyGLSurfaceView extends GLSurfaceView
{
MyRenderer m_renderer;
public MyGLSurfaceView(Context context, AssetManager am)
public MyGLSurfaceView(Context context, AssetManager am,
Resources resources)
{
super(context);
setEGLContextClientVersion(2);
m_renderer = new MyRenderer(am);
m_renderer = new MyRenderer(am, resources);
setRenderer(m_renderer);
}

View File

@ -10,6 +10,7 @@ import java.nio.IntBuffer;
import java.nio.ByteOrder;
import android.util.Log;
import android.content.res.AssetManager;
import android.content.res.Resources;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
@ -17,6 +18,9 @@ import java.io.IOException;
import android.opengl.Matrix;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.opengl.GLUtils;
public class MyRenderer implements GLSurfaceView.Renderer
{
@ -24,6 +28,7 @@ public class MyRenderer implements GLSurfaceView.Renderer
private final String DBGTAG = "JoshsOpenGL";
private FloatBuffer m_quad_attrib_buffer;
private AssetManager m_asset_manager;
private Resources m_resources;
private float m_proj_matrix[] = new float[16];
private float m_modelview[] = new float[16];
private int m_width;
@ -32,9 +37,10 @@ public class MyRenderer implements GLSurfaceView.Renderer
private float m_x = 0.0f;
private float m_y = 0.0f;
public MyRenderer(AssetManager am)
public MyRenderer(AssetManager am, Resources resources)
{
m_asset_manager = am;
m_resources = resources;
}
public void checkGLError(String glOperation)
@ -87,10 +93,10 @@ public class MyRenderer implements GLSurfaceView.Renderer
public void onSurfaceCreated(GL10 unused, EGLConfig config)
{
final float attribs[] = {
1, 1, 0, 1, 1, 1,
-1, 1, 0, 1, 0, 0,
-1, -1, 0, 0, 1, 0,
1, -1, 0, 0, 0, 1
1, 1, 0, 1, 1,
-1, 1, 0, 0, 1,
-1, -1, 0, 0, 0,
1, -1, 0, 1, 0,
};
checkGLError("onSurfaceCreated");
ByteBuffer bb = ByteBuffer.allocateDirect(attribs.length * 4);
@ -116,6 +122,24 @@ public class MyRenderer implements GLSurfaceView.Renderer
Log.e(DBGTAG, "Program log: " + info_log);
}
Bitmap texture = BitmapFactory.decodeResource(m_resources,
R.drawable.texture);
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
checkGLError("glGenTextures");
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
checkGLError("glBindTexture");
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
checkGLError("glTexParameterf");
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, texture, 0);
checkGLError("GLUtils.texImage2D");
GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
checkGLError("glGenerateMipmap");
texture.recycle();
GLES20.glClearColor(1.0f, 0.6f, 0.1f, 1.0f);
}
@ -133,21 +157,22 @@ public class MyRenderer implements GLSurfaceView.Renderer
GLES20.glGetUniformLocation(m_program, "modelview"),
1, false, m_modelview, 0);
int attr_pos = GLES20.glGetAttribLocation(m_program, "pos");
int attr_color = GLES20.glGetAttribLocation(m_program, "color");
int attr_tex_coord = GLES20.glGetAttribLocation(m_program, "tex_coord");
checkGLError("glGetAttribLocation");
GLES20.glEnableVertexAttribArray(attr_pos);
GLES20.glEnableVertexAttribArray(attr_color);
GLES20.glEnableVertexAttribArray(attr_tex_coord);
checkGLError("glEnableVertexAttribArray");
m_quad_attrib_buffer.position(0);
GLES20.glVertexAttribPointer(attr_pos, 3, GLES20.GL_FLOAT, false,
6 * 4, m_quad_attrib_buffer);
5 * 4, m_quad_attrib_buffer);
m_quad_attrib_buffer.position(3);
GLES20.glVertexAttribPointer(attr_color, 3, GLES20.GL_FLOAT, false,
6 * 4, m_quad_attrib_buffer);
GLES20.glVertexAttribPointer(attr_tex_coord, 2, GLES20.GL_FLOAT, false,
5 * 4, m_quad_attrib_buffer);
checkGLError("glVertexAttribPointer");
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
checkGLError("glDrawArrays");
GLES20.glDisableVertexAttribArray(attr_pos);
GLES20.glDisableVertexAttribArray(attr_color);
GLES20.glDisableVertexAttribArray(attr_tex_coord);
checkGLError("glDisableVertexAttribArray");
}