OpenGL textures et cubes

Fermé
Moldave72 Messages postés 6 Date d'inscription jeudi 21 mars 2013 Statut Membre Dernière intervention 3 janvier 2014 - 4 déc. 2013 à 09:09
Bonjour, suite à la copie d'un code pour integrer du OPENGL en android pour faire des interfaces plus interessantes, je suis confronté a un problème qui illustre mon niveau de nooby sur le sujet :

J'ai une classe qui defini les cube, et une autre qui les créée, j'ai essayé de charger plusieurs textures différentes, mais tous mes objets, qu'ils s'appellent cube1 cube2 ou pierre paul jacques n'ont toujours qu'une seule et même texture, la derniere chargée, je comprends pas, j'ai l'impression d'avoir pourtant instancié des objets différents...

Je voudrai savoir ou intervenir alors pour pouvoir différencier les caractéristiques des objets de cette classe.
			  	                  GLCube cube0 = new GLCube();
	                  GLCube cube1 = new GLCube();
	                  GLCube cube2 = new GLCube();

// plus loin, je pense définir de nouveaux attributs correspondants a des textures differentes:

		cube1.loadTexture(gl, context , R.drawable.texture_one);
	        cube2.loadTexture(gl, context, R.drawable.texture_two);
  		cube0.loadTexture(gl, context, R.drawable.texture_zero);


( j'ai tiré cela d'un tuto et oui, comme dis plus haut, je ne suis pas très calé sur le sujet, je me sens comme un apprentit sorcier, mais j'ai vraiment envie de comprendre un peu tout ça, merci pour votre aide d'avance)

Ma classe cube, implémente une methode de loadTexture() :
package org.example.opengl;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;

class GLCube {
   private final IntBuffer mVertexBuffer;
   
   
   private final IntBuffer mTextureBuffer;

   
   public GLCube() {
      
      int one = 65536;
      int half = one / 2;
      int vertices[] = { 
            // FRONT
            -half, -half, half, half, -half, half,
            -half, half, half, half, half, half,
            // BACK
            -half, -half, -half, -half, half, -half,
            half, -half, -half, half, half, -half,
            // LEFT
            -half, -half, half, -half, half, half,
            -half, -half, -half, -half, half, -half,
            // RIGHT
            half, -half, -half, half, half, -half,
            half, -half, half, half, half, half,
            // TOP
            -half, half, half, half, half, half,
            -half, half, -half, half, half, -half,
            // BOTTOM
            -half, -half, half, -half, -half, -half,
            half, -half, half, half, -half, -half, };
      
      
      int texCoords[] = {
            // FRONT
            0, one, one, one, 0, 0, one, 0,
            // BACK
            one, one, one, 0, 0, one, 0, 0,
            // LEFT
            one, one, one, 0, 0, one, 0, 0,
            // RIGHT
            one, one, one, 0, 0, one, 0, 0,
            // TOP
            one, 0, 0, 0, one, one, 0, one,
            // BOTTOM
            0, 0, 0, one, one, 0, one, one, };
 
      ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
      vbb.order(ByteOrder.nativeOrder());
      mVertexBuffer = vbb.asIntBuffer();
      mVertexBuffer.put(vertices);
      mVertexBuffer.position(0);
      

      
      // ...
      ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
      tbb.order(ByteOrder.nativeOrder());
      mTextureBuffer = tbb.asIntBuffer();
      mTextureBuffer.put(texCoords);
      mTextureBuffer.position(0);
      
   }
   

   public void draw(GL10 gl) { 
      gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
      
      
      gl.glEnable(GL10.GL_TEXTURE_2D); // workaround bug 3623
      gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, mTextureBuffer);
      
      

      gl.glColor4f(1, 0, 1, 1);
      gl.glNormal3f(0, 0, 1);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
      gl.glNormal3f(0, 0, -1);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);

      gl.glColor4f(1, 1, 0, 1);
      gl.glNormal3f(-1, 0, 0);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
      gl.glNormal3f(1, 0, 0);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);

      gl.glColor4f(0, 1, 1, 1);
      gl.glNormal3f(0, 1, 0);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
      gl.glNormal3f(0, -1, 0);
      gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);
   }
   
   
   void loadTexture(GL10 gl, Context context, int resource) {
      Bitmap bmp = BitmapFactory.decodeResource(
            context.getResources(), resource);
      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
       gl.glTexParameterx(GL10.GL_TEXTURE_2D,
            GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
      bmp.recycle();
   }
   
}




et ma classe GLRenderer s'en sert pour définir les objets qu'elle fabrique a partir de cette classe ici :


package org.example.opengl;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.content.Context;

public class GLRenderer implements GLSurfaceView.Renderer {
	

	public static final String TAG = "GLRenderer";

	GLCube cube0 = new GLCube();
	  GLCube cube1 = new GLCube();
	  GLCube cube2 = new GLCube();
	 
	private long startTime;
	private long fpsStartTime;
	private long numFrames;


	public Context context;

	public GLRenderer(Context context) {
		this.context = context;

	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {



		boolean SEE_THRU = true;
		// ...
		if (SEE_THRU) {
		gl.glDisable(GL10.GL_DEPTH_TEST);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_ALPHA, GL10.GL_ONE);
		}
		
		
		startTime = System.currentTimeMillis();
		fpsStartTime = startTime;
		numFrames = 0;
		// Define the lighting

		float lightAmbient[] = new float[] { 0.2f, 0.2f, 0.2f, 1 };
		float lightDiffuse[] = new float[] { 1, 1, 1, 1 };
		float[] lightPos = new float[] { 0, -6, -10, 1 };
		gl.glEnable(GL10.GL_LIGHTING);
		gl.glEnable(GL10.GL_LIGHT0);
		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0);
		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0);
		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPos, 0);

		// What is the cube made of?

		float matAmbient[] = new float[] { 1, 1, 1, 1 };
		float matDiffuse[] = new float[] { 1, 1, 1, 1 };
		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT,   matAmbient, 0);
		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, matDiffuse, 0);

		// Enable textures
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glEnable(GL10.GL_TEXTURE_2D);



		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

		// Optional: disable dither to boost performance
		 gl.glDisable(GL10.GL_DITHER);
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
	
		// Define the view frustum
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		float ratio = (float) width / height;
		GLU.gluPerspective(gl, 40.0f, ratio, 1, 100f);
		GLU.gluLookAt(gl, 4
				, -9, 5
				, 0f, 0f,  0F, 0f,1f, -1f);
		cube1.loadTexture(gl, context , R.drawable.texture_one);
	        cube2.loadTexture(gl, context, R.drawable.texture_two);
  		cube0.loadTexture(gl, context, R.drawable.texture_zero);
	}
	public void onSurfaceChanged2(GL10 gl2, int width, int height) {
		
		// Define the view frustum
		gl2.glViewport(0, 0, width, height);
		gl2.glMatrixMode(GL10.GL_PROJECTION);
		gl2.glLoadIdentity();
		float ratio = (float) width / height;
		GLU.gluPerspective(gl2, 40.0f, ratio, 1, 100f);
		GLU.gluLookAt(gl2, 4
				, -9, 5
				, 0f, 0f,  0F, 0f,1f, -1f);
	    cube2.loadTexture(gl2, context, R.drawable.texture_two);

	}
	
	
	

	// To start with, we set the screen to black. We clear both the color and
	// depth buffers. 
	public void onDrawFrame(GL10 gl) {
		
		// Clear the screen to black
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		// Position model so we can see it
		
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	      long elapsed = System.currentTimeMillis() - startTime;
	      gl.glPushMatrix();
 	      gl.glRotatef(120, (float) 0.5, 1, 0);
	      gl.glRotatef(elapsed * (15f / 1000f), (float) 1, 0, 0);

	   // Draw the model
	      
 
	      for (int i=1; i<=15; i++){
	    	  gl.glPushMatrix();
		  gl.glTranslatef(3*(float)Math.cos((elapsed/800f)-i*(2*3.14159/15)), 0, 3*(float)Math.sin((elapsed/800f)-i*(2*3.14159/15)));
		      cube1.draw(gl);
		      gl.glPopMatrix();
		      }

	      for (int i=1; i<=8; i++){
		      gl.glPushMatrix();
		      gl.glTranslatef( 0,2*(float) (1.5*Math.cos((-elapsed/800f)-i*(2*3.14159/8))), (float) (1.5*Math.sin((-elapsed/800f)-i*(2*3.14159/8))));
		      cube2.draw(gl);
		      gl.glPopMatrix();
		      }
	      gl.glPopMatrix();
	      gl.glRotatef(elapsed * (20f / 160f),1,  (float) 0.5,  (float) 0.25);
	      cube0.draw(gl);
 

 			}
 


	}


	
}