在opengl的纹理图画在机器人

时间:2011-03-15 08:28:04

标签: android opengl-es textures draw

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_BUFFER_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;
  }
}

任何人都可以说出为什么图像没有绘制......

1 个答案:

答案 0 :(得分:2)

  1. 你的指数对我来说很奇怪。尝试删除它们并使用glDrawArrays绘制verticies(这似乎是正确的顺序)。
  2. 删除深度测试,我并没有真正跟踪您的z坐标,但他们可能无法通过测试。
  3. (可选)一般来说,在绘制元素之前应该使用bindTexture。但如果它仍然是完整的程序代码,那么你将只会失去第一帧。
  4. 编辑: 哇,我刚刚看到: gl.glVertexPointer(3,GL10.GL_FLOAT,0,mTextuteBuffer);

    你应该为glTexCoordPointer提供纹理坐标