2011-03-15 3 views
0
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; 
    } 
} 

peut-on dire que l'image pourquoi ne tire pas ....Texture dessin en opengl dans Android

Répondre

2
  1. Vos indices semblent bizarre pour moi. Essayez de les supprimer et de dessiner des verticies (qui semblent être dans le bon ordre) avec glDrawArrays.
  2. Supprimer le test de profondeur, je ne suivais pas vraiment vos coordonnées z, mais ils pourraient ne pas réussir le test.
  3. (Facultatif) En général, vous devez bindTexture avant de dessiner un élément. Mais si c'est un code de programme complet qui devrait encore fonctionner, vous perdrez seulement la première image.

EDIT: Wow tenir, je viens de voir: gl.glVertexPointer (3, GL10.GL_FLOAT, 0, mTextuteBuffer);

vous devez fournir les coordonnées de texture à glTexCoordPointer

+0

grâce Yuriy Fro guide .... – user643144