Actually, I have an Android 1.5 app with the GLSurfaceView class that shows a simple square polygon on the screen.
I want to learn how to add new functionality, the functionality of moving a square that touches a finger. I mean, when the user touches the square and moves the finger, the square must stick to the finger until the finger releases the screen.
Any tutorials / code examples / help would be appreciated.
My code is:
public class MySurfaceView extends GLSurfaceView implements Renderer {
private Context context;
private Square square;
private float xrot;
private float yrot;
private float zrot;
private float xspeed;
private float yspeed;
private float z = -1.15f;
private float oldX;
private float oldY;
private final float TOUCH_SCALE = 0.2f;
private MatrixGrabber mg = new MatrixGrabber();
private boolean firstTimeDone=false;
public MySurfaceView(Context context, Bitmap image) {
super(context);
this.context = context;
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
this.setRenderer(this);
this.requestFocus();
this.setFocusableInTouchMode(true);
square = new Square(image);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glClearColor(0,0,0,0);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
square.loadGLTexture(gl, this.context);
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, z);
gl.glScalef(0.8f, 0.8f, 0.8f);
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);
square.draw(gl);
xrot += xspeed;
yrot += yspeed;
if (!firstTimeDone)
{
mg.getCurrentProjection(gl);
mg.getCurrentModelView(gl);
float [] modelMatrix = new float[16];
float [] projMatrix = new float[16];
modelMatrix=mg.mModelView;
projMatrix=mg.mProjection;
int [] mView = new int[4];
mView[0] = 0;
mView[1] = 0;
mView[2] = 800;
mView[3] = 480;
float [] outputCoords = new float[3];
GLU.gluProject(-1.0f, -1.0f, z, modelMatrix, 0, projMatrix, 0, mView, 0, outputCoords, 0);
int i=0;
System.out.print(i);
}
}
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();
GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction())
{
case MotionEvent.ACTION_MOVE:
float dx = x - oldX;
float dy = y - oldY;
xrot += dy * TOUCH_SCALE;
yrot += dx * TOUCH_SCALE;
break;
}
oldX = x;
oldY = y;
return true;
}
public void zoomIn(){
z=z+0.2f;
if (z>-1.0f)
z=-1.0f;
}
public void zoomOut(){
z=z-0.2f;
if (z<-20.0f)
z=-20.0f;
}
public void rotateL(){
zrot=zrot+3.0f;
}
public void rotateR(){
zrot=zrot-3.0f;
}
public void reset()
{
xrot=0;
yrot=0;
zrot=0;
xspeed=0;
yspeed=0;
z = -5.0f;
}
}
This is my square class:
public class Square {
private FloatBuffer vertexBuffer;
private FloatBuffer textureBuffer;
private int[] textures = new int[3];
private Bitmap image;
private float vertices[] =
{
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f
};
private float texture[] =
{
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
};
public Square(Bitmap image) {
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
this.image=image;
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CCW);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
public void loadGLTexture(GL10 gl, Context context) {
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
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);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
InputStream is=null;
Bitmap bitmap =image;
int newW=bitmap.getWidth();
int newH=bitmap.getHeight();
float fact;
if (newH>256 || newW>256)
{
if (newH>256)
{
fact=(float)255/(float)newH;
newH=(int)(newH*fact);
newW=(int)(newW*fact);
}
if (newW>256)
{
fact=(float)255/(float)newW;
newH=(int)(newH*fact);
newW=(int)(newW*fact);
}
bitmap=Bitmap.createScaledBitmap(bitmap, newW, newH, true);
}
int nextPot=256;
int h = bitmap.getHeight();
int w = bitmap.getWidth();
int offx=(nextPot-w)/2;
int offy=(nextPot-h)/2;
Bitmap bitmap2 = Bitmap.createBitmap(nextPot, nextPot, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(bitmap2);
comboImage.drawBitmap(bitmap, offx, offy, null);
comboImage.save();
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap2, 0);
if(gl instanceof GL11) {
gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap2, 0);
} else {
buildMipmap(gl, bitmap2);
}
bitmap.recycle();
bitmap2.recycle();
}
private void buildMipmap(GL10 gl, Bitmap bitmap) {
int level = 0;
int height = bitmap.getHeight();
int width = bitmap.getWidth();
while(height >= 1 || width >= 1) {
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, level, bitmap, 0);
if(height == 1 || width == 1) {
break;
}
level++;
height /= 2;
width /= 2;
Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, width, height, true);
bitmap.recycle();
bitmap = bitmap2;
}
}
}