开发者

Texture drawing in opengl in android

开发者 https://www.devze.com 2023-02-17 08:53 出处:网络
package com.opengl; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer;
package com.opengl;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.Bitmap;
import android.opengl.GLUtils;

public class Square {
  // Our vertices.
  short[] indices = new short[] { 0, 1, 2, 1, 3, 2 };

  float[] vertices = new float[] { -0.5f, -0.5f,
    0.0f, 0.5f,
    -0.5f, 0.0f,
    -0.5f, 0.5f,
    0.0f, 0.5f,
    0.5f, 0.0f 
  };

  float mTextureCoordinates[] = { 0.0f, 1.0f, //
    1.0f, 1.0f, //
    0.0f, 0.0f, //
    1.0f, 0.0f, //
  };

  // Our vertex buffer.
  private FloatBuffer vertexBuffer;

  // Our index buffer.
  private ShortBuffer indexBuffer;

  private FloatBuffer mTextuteBuffer;

  private int mTextureId;

  private Bitmap mBitmap;

  public Square() {
    // a float is 4 bytes, therefore we multiply the number if 
    // vertices with 4.
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    // short is 2 bytes, therefore we multiply the number if 
    // vertices with 2.
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    indexBuffer = ibb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);

    // Int has 2 bytes 
    ByteBuffer tbb = ByteBuffer.allocateDirect(mTextureCoordinates.length * 4);
    tbb.order(ByteOrder.nativeOrder());
    mTextuteBuffer = tbb.asFloatBuffer();
    mTextuteBuffer.put(mTextureCoordinates);
    mTextuteBuffer.position(0);
  }

  public void draw(GL10 gl, Bitmap bitmap ) {

    mBitmap = bitmap;

    gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f);
    // Counter-clockwise winding.
    gl.glFrontFace(GL10.GL_CCW);
    // Enable face culling.
    gl.glEnable(GL10.GL_CULL_FACE);
    // What faces to remove with the face culling.
    gl.glCullFace(GL10.GL_BACK);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glDrawElements(GL10.GL_TRIANGLE_FAN, indices.length, 
      GL10.GL_UNSIGNED_SHORT, indexBuffer);



    loadGLTexture(gl);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

    gl.glDisable(GL10.GL_CULL_FACE);
  }


  private void loadGLTexture(GL10 gl) { // New function
    // Generate one texture pointer...
    int[] mTexture = new int[1];
    gl.glGenTextures(1, mTexture, 0);
    mTextureId = mTexture[0];

    // ...and bind it to our array
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);

    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mTextuteBuffer);
    //gl.glDrawElements(GL10.GL_TRIANGLE_FAN, indices.length, 
    //GL10.GL_UNSIGNED_SHORT, indexBuffer);

    // Create Nearest Filtered Texture
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
      GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
      GL10.GL_LINEAR);

    // Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
      GL10.GL_CLAMP_TO_EDGE);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
      GL10.GL_REPEAT);

    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glDisable(GL10.GL_TEXTURE_2D);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);
  }

  public void loadBitmap(Bitmap bitmap) {
    mBitmap = bitmap;
  }
}
package com.opengl;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class OpenGL extends Activity {

  @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

      GLSurfaceView view = new GLSurfaceView(this);
      OpenGLRenderer rendrer = new OpenGLRenderer();
      rendrer.loadBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.jay));

      view.setRenderer(rendrer);
      setContentView(view);
  }
}

package com.opengl;


import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.graphics.Bitmap;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;

public class OpenGLRenderer implements Renderer {
  private Square square;
  private SmoothColoredSquare smooth;
  private float scale;
  private Bitmap mBitmap;
  public OpenGLRenderer() {
    // Initialize our square. 
    square = new Square();
    square.loadBitmap(mBitmap);
    smooth = new SmoothColoredSquare();
    scale = 0;
  }

  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // Set the background color to black ( rgba ).
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    // Enable Smooth Shading, default not really needed.
    gl.glShadeModel(GL10.GL_SMOOTH);
    // Depth buffer setup.
    gl.glClearDepthf(1.0f);
    // Enables depth testing.
    gl.glEnable(GL10.GL_DEPTH_TEST);
    // The type of depth testing to do.
    gl.glDepthFunc(GL10.GL_LEQUAL);
    // Really nice perspective calculations.
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
  }

  public void onDrawFrame(GL10 gl) {
    // Clears the screen and depth buffer.
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFF开发者_C百科ER_BIT);
    // Replace the current matrix with the identity matrix
    gl.glLoadIdentity();
    gl.glTranslatef(0, 0, -5);
    //gl.glScalef(0.1f, scale, 0);

    float mid = (SmoothColoredSquare.mVertices[0] + SmoothColoredSquare.mVertices[3])/2;
    //gl.glTranslatef(0, mid, 0);       

    //smooth.draw(gl, mBitmap);
    square.draw(gl, mBitmap);
    scale = (float) (scale + 0.01);
  }


  public void onSurfaceChanged(GL10 gl, int width, int height) {
    // Sets the current view port to the new size.
    gl.glViewport(0, 0, width, height);
    // Select the projection matrix
    gl.glMatrixMode(GL10.GL_PROJECTION);
    // Reset the projection matrix
    gl.glLoadIdentity();
    // Calculate the aspect ratio of the window
    GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
      100.0f);
    // Select the modelview matrix
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    // Reset the modelview matrix
    gl.glLoadIdentity();
  }
  public void loadBitmap(Bitmap bitmap) {
    mBitmap = bitmap;
  }
}

can any one tell that why image is not drawing....


  1. Your indices look weird for me. Try to remove them and draw verticies(that seems to be in correct order) with glDrawArrays.
  2. Remove Depth test, I was not really tracking your z coordinates but they may not pass the test.
  3. (Optional) in general you should bindTexture before drawing an element. But if it is full program code that still should work, you will lose only first frame.

EDIT: Wow hold on, I just saw: gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mTextuteBuffer);

you should provide texture coordinates to glTexCoordPointer

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号