package edu.dhbw.andar.sample;
import java.io.ByteArrayOutputStream;
import android.app.AlertDialog;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import edu.dhbw.andar.ARObject;
import edu.dhbw.andar.pub.SimpleBox;
import edu.dhbw.andar.util.GraphicsUtil;
/**
* An example of an AR object being drawn on a marker.
* @author tobi
*
*/
public class CustomObject extends ARObject {
int[] texture;
private static final int TEX_SIZE=64;
int tex;
ByteArrayOutputStream baos;
byte[] b;
float texCoords1[] = {0f, 0f,20f,20f};
FloatBuffer texCoords;
public CustomObject(String name, String patternName,
double markerWidth, double[] markerCenter) {
super(name, patternName, markerWidth, markerCenter);
float mat_ambientf[] = {0f, 1.0f, 0f, 1.0f};
float mat_flashf[] = {0f, 1.0f, 0f, 1.0f};
float mat_diffusef[] = {0f, 1.0f, 0f, 1.0f};
float mat_flash_shinyf[] = {50.0f};
texCoords= GraphicsUtil.makeFloatBuffer(texCoords1);
mat_ambient = GraphicsUtil.makeFloatBuffer(mat_ambientf);
mat_flash = GraphicsUtil.makeFloatBuffer(mat_flashf);
mat_flash_shiny = GraphicsUtil.makeFloatBuffer(mat_flash_shinyf);
mat_diffuse = GraphicsUtil.makeFloatBuffer(mat_diffusef);
}
public CustomObject(String name, String patternName,
double markerWidth, double[] markerCenter, float[] customColor) {
super(name, patternName, markerWidth, markerCenter);
float mat_flash_shinyf[] = {50.0f};
mat_ambient = GraphicsUtil.makeFloatBuffer(customColor);
mat_flash = GraphicsUtil.makeFloatBuffer(customColor);
mat_flash_shiny = GraphicsUtil.makeFloatBuffer(mat_flash_shinyf);
mat_diffuse = GraphicsUtil.makeFloatBuffer(customColor);
}
/**
* Just a box, imported from the AndAR project.
*/
private SimpleBox box = new SimpleBox();
private FloatBuffer mat_flash;
private FloatBuffer mat_ambient;
private FloatBuffer mat_flash_shiny;
private FloatBuffer mat_diffuse;
/**
* Everything drawn here will be drawn directly onto the marker,
* as the corresponding translation matrix will already be applied.
*/
@Override
public final void draw(GL10 gl) {
super.draw(gl);
//draw cube
try
{
// gl.glColor4f(0, 1.0f, 0, 1.0f);
// gl.glTranslatef( 0.0f, 0.0f, 12.5f );
if (texture==null)
texture=new int[1];
gl.glGenTextures(1, texture, 0);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/Classic_scaled.png");
int pixels[]=new int[TEX_SIZE*TEX_SIZE];
bMap.getPixels(pixels, 0, TEX_SIZE, 0, 0, TEX_SIZE, TEX_SIZE);
int pix1[]=new int[TEX_SIZE*TEX_SIZE];
for(int i=0; i<TEX_SIZE; i++)
{
for(int j=0; j<TEX_SIZE; j++)
{
//correction of R and B
int pix=pixels[i*TEX_SIZE+j];
int pb=(pix>>16)&0xff;
int pr=(pix<<16)&0x00ff0000;
int px1=(pix&0xff00ff00) | pr | pb;
//correction of rows
pix1[(TEX_SIZE-i-1)*TEX_SIZE+j]=px1;
}
}
IntBuffer tbuf=IntBuffer.wrap(pix1);
tbuf.position(0);
tex = texture[0];
baos = new ByteArrayOutputStream();
bMap.compress(Bitmap.CompressFormat.PNG, 100, baos);
b = baos.toByteArray();
gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_LUMINANCE, TEX_SIZE, TEX_SIZE, 0, GL10.GL_LUMINANCE, GL10.GL_UNSIGNED_BYTE, tbuf); //I am loading the texture
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR,mat_flash);
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, mat_flash_shiny);
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, mat_diffuse);
gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, mat_ambient);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoords);
//draw the box
box.draw(gl);
}
catch(Exception e)
{
e.printStackTrace();
Log.d("error", "IOException e = " + e.toString());
}
}
@Override
public void init(GL10 gl) {
}
}
截图:http://blinksolution.com/110504 - 213626. - jpg
您可能需要调用gl.glEnable(GL_TEXTURE_2D)