Android - Charging multiple textures OpenGL-ES Android

 
Vista:
sin imagen de perfil

Charging multiple textures OpenGL-ES Android

Publicado por Sara (1 intervención) el 28/08/2015 10:17:57
I am making an application for Android with OpenGL-Es following Nehe's tutorial. I have a cube with a texture and my problem is that I want to change it by pressing a button (I have 2 textures in the folder "raw"). I think this is due to my variable imagenes in which I save the route of my images (R.raw.imagen and R.raw.imagen2 on MainActivity.java) only charge the image once at the start of the application, so even if I change the variable later in my function onClick() the texture remains the same.

What I tried to do is making a switch in the class TextureCube.java looking my variable imagenes in the function where I load the texture so it should charge the first image at the start of the application and then if I press the button change it to the other image because of the code onClick().

The image never change because I printed the variable imagenes. I don't know what I am doing wrong.

MainActivity:

1
2
3
4
5
6
7
8
9
10
11
12
b.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                   if (get_imagenes() == R.raw.imagen) {
                       imagenes = R.raw.imagen2;
                       b.setText("image2");
                   } else if (get_imagenes() == R.raw.imagen2) {
                       imagenes = R.raw.imagen;
                       b.setText("image1");
                   }
               }
           });

TextureCube:
1
2
3
4
5
6
7
8
9
     // Construct an input stream to texture image
                    switch (main.imagenes) {
                        case R.raw.imagen:
                          is = context.getResources().openRawResource(R.raw.imagen);
                            break;
                        case R.raw.imagen2:
                          is = context.getResources().openRawResource(R.raw.imagen2);
                            break;
                    }

I left the rest of the code of the application here.Here is my code of the MainActivity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
public class MainActivity extends AppCompatActivity {
        private GLSurfaceView glView;
        private TextureCube cube;
        int imagenes = R.raw.imagen;
        Button b;
        Bitmap bitmap;
 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
 
            //GLSurfaceView
            glView = new MyGLSurfaceView(this);
            cube = new TextureCube();
            setContentView(glView);
            createButtons();
        }
 
        public void createButtons() {
            //ButtonB
            LinearLayout ll = new LinearLayout(this);
            b = new Button(this);
            b.setText("Change Texture");
            ll.addView(b);
            ll.setGravity(Gravity.BOTTOM | Gravity.CENTER);
            this.addContentView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
 
           b.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                   if (get_imagenes() == R.raw.imagen) {
                       imagenes = R.raw.imagen2;
                       b.setText("image2");
                   } else if (get_imagenes() == R.raw.imagen2) {
                       imagenes = R.raw.imagen;
                       b.setText("image1");
                   }
               }
           });
        }
 
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
 
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
 
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
 
        // Call back when the activity is going into the background
        @Override
        protected void onPause() {
            super.onPause();
            glView.onPause();
        }
 
        // Call back after onPause()
        @Override
        protected void onResume() {
            super.onResume();
            glView.onResume();
        }
 
        public int get_imagenes() {
            return imagenes;
        }
    }

And here TextureCube:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
public class TextureCube {
        private FloatBuffer vertexBuffer; //Buffer for vertex-array
        private FloatBuffer texBuffer;    //Buffer for texture-coords-array (NEW)
        private MainActivity main = new MainActivity();
 
        private float[] vertices = { //Vertices for a face
                -1.0f, -1.0f, 0.0f,  //left-bottom-front
                1.0f, -1.0f, 0.0f,  //right-bottom-front
                -1.0f, 1.0f, 0.0f,  //left-top-front
                1.0f, 1.0f, 0.0f   //right-top-front
        };
 
        float[] texCoords = { // Texture coords
                0.0f, 1.0f,  //left-bottom
                1.0f, 1.0f,  //right-bottom
                0.0f, 0.0f,  //left-top
                1.0f, 0.0f   //right-top (
        };
        int[] textureIDs = new int[1]; //new
 
 
        public TextureCube() {
            // Setup vertex-array buffer
            ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
            vbb.order(ByteOrder.nativeOrder()); // Use native byte order
            vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
            vertexBuffer.put(vertices);         // Copy data into buffer
            vertexBuffer.position(0);           // Rewind
 
            // Setup texture-array buffer
            ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
            tbb.order(ByteOrder.nativeOrder());
            texBuffer = tbb.asFloatBuffer();
            texBuffer.put(texCoords);
            texBuffer.position(0);
        }
 
        // Draw the cube
        public void draw(GL10 gl) {
            gl.glFrontFace(GL10.GL_CCW);    // Front face in counter-clockwise orientation
            gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
            gl.glCullFace(GL10.GL_BACK);    // Cull the back face (don't display)
 
            //Enable vertex and texture client
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  // Enable texture-coords-array (NEW)
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer); // Define texture-coords buffer (NEW)
 
            //Draw all the faces
            //Front
            gl.glPushMatrix();
            gl.glTranslatef(0.0f, 0.0f, 1.0f);
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
            gl.glPopMatrix();
 
            //Left
            gl.glPushMatrix();
            gl.glRotatef(270.0f, 0.0f, 1.0f, 0.0f);
            gl.glTranslatef(0.0f, 0.0f, 1.0f);
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
            gl.glPopMatrix();
 
            //Back
            gl.glPushMatrix();
            gl.glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
            gl.glTranslatef(0.0f, 0.0f, 1.0f);
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
            gl.glPopMatrix();
 
            //Right
            gl.glPushMatrix();
            gl.glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
            gl.glTranslatef(0.0f, 0.0f, 1.0f);
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
            gl.glPopMatrix();
 
            //Top
            gl.glPushMatrix();
            gl.glRotatef(270.0f, 1.0f, 0.0f, 0.0f);
            gl.glTranslatef(0.0f, 0.0f, 1.0f);
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
            gl.glPopMatrix();
 
            //Bottom
            gl.glPushMatrix();
            gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
            gl.glTranslatef(0.0f, 0.0f, 1.0f);
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
            gl.glPopMatrix();
 
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);  // Disable texture-coords-array (NEW)
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glDisable(GL10.GL_CULL_FACE);
        }
 
        // Load an image into GL texture
        public void loadTexture(GL10 gl, Context context) {
 
            gl.glGenTextures(1, textureIDs, 0); // Generate texture-ID array new
 
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]);   // Bind to texture ID
 
            // Set up texture filters
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
 
 
            InputStream is = new InputStream() {
                @Override
                public int read() throws IOException {
                    return 0;
                }
            };
            // Construct an input stream to texture image
            switch (main.imagenes) {
                case R.raw.imagen:
                  is = context.getResources().openRawResource(R.raw.imagen);
                    break;
                case R.raw.imagen2:
                  is = context.getResources().openRawResource(R.raw.imagen2);
                    break;
            }
 
            Log.d("prueba","imagenes"+main.imagenes);
 
            Bitmap bitmap;
            try {
                // Read and decode input as bitmap
                    bitmap = BitmapFactory.decodeStream(is);
            } finally {
                try {
                        is.close();
                } catch (IOException e) {
                }
            }
 
            // Build Texture from loaded bitmap for the currently-bind texture ID
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
            bitmap.recycle();
        }
    }
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder