2014-06-05 3 views
0

저는 OpenGL에서 초보자입니다. 저는 프리즘 (각면이 정삼각형입니다)을 OpenGL 라이브러리를 사용하는 안드로이드에서 만들고 프리즘을 성공적으로 회전시킬 수 있습니다. 하지만 내 requirment 프리즘의 각 얼굴에 세 가지 이미지를 넣어하는 것입니다 그리고 나는 이미지를 넣을 수 없습니다. 내가 이미지를 올리면 스케일링되고 모든면에 매핑됩니다.피라미드의 각면에 다른 질감을 부여합니다.

MyRenderer 클래스

public class MyRenderer implements Renderer { 

/** Cube instance */ 

/* Rotation values for all axis */ 
private float xrot;    //X Rotation (NEW) 
private float yrot;    //Y Rotation (NEW) 
private float zrot;    //Z Rotation (NEW) 

/** The Activity Context (NEW) */ 
private Context context; 
private Pyramid pyramid; 
/** 
* Instance the Cube object and set 
* the Activity Context handed over 
*/ 

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

    pyramid = new Pyramid(this.context); 

} 

/** 
* The Surface is created/init() 
*/ 
public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
    //Load the texture for the cube once during Surface creation 


    gl.glEnable(GL10.GL_TEXTURE_2D);   //Enable Texture Mapping (NEW) 
    gl.glShadeModel(GL10.GL_SMOOTH);   //Enable Smooth Shading 
    gl.glClearColor(1.0f, 1.0f, 1.0f, 0.5f); //Black Background 
    gl.glClearDepthf(1.0f);      //Depth Buffer Setup 
    gl.glEnable(GL10.GL_DEPTH_TEST);   //Enables Depth Testing 
    gl.glDepthFunc(GL10.GL_LEQUAL);    //The Type Of Depth Testing To Do 

    //Really Nice Perspective Calculations 
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 
} 

/** 
* Here we do our drawing 
*/ 

public void onDrawFrame(GL10 gl) { 

    //Clear Screen And Depth Buffer 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);  
    gl.glLoadIdentity();     //Reset The Current Modelview Matrix 

    //Drawing 
    gl.glTranslatef(0.0f, -1.0f, -5.0f);  //Move 5 units into the screen 
    gl.glScalef(1.0f, 1.0f, 1.0f);   //Scale the Cube to 80 percent, otherwise it would be too large for the screen 

    //Rotate around the axis based on the rotation matrix (rotation, x, y, z) 
    gl.glRotatef(yrot, 0.0f, 1.65f, 0.0f); //X 

    pyramid.draw(gl, context); 

    yrot += 1.0f; 

} 

/** 
* If the surface changes, reset the view 
*/ 

public void onSurfaceChanged(GL10 gl, int width, int height) { 
    if(height == 0) {      //Prevent A Divide By Zero By 
     height = 1;       //Making Height Equal One 
    } 

    gl.glViewport(0, 0, width, height);  //Reset The Current Viewport 
    gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix 
    gl.glLoadIdentity();     //Reset The Projection Matrix 

    //Calculate The Aspect Ratio Of The Window 
    GLU.gluPerspective(gl, 45.0f, (float)width/(float)height, 0.1f, 100.0f); 

    gl.glMatrixMode(GL10.GL_MODELVIEW);  //Select The Modelview Matrix 
    gl.glLoadIdentity();     //Reset The Modelview Matrix 
} 

}

MyPyramid 클래스

public class Pyramid { 

/** The buffer holding the vertices */ 
private FloatBuffer vertexBuffer; 
/** The buffer holding the color values */ 
private FloatBuffer colorBuffer; 

private ByteBuffer indexBuffer; 

private FloatBuffer textureBuffer; 

private int noOfFaces = 3; 

private int[] texturesID = new int[3]; 

private float PyramidVertices [] = { 

              0.0f, 1.65f, 0.0f, 
             -1.3f, 0.0f, 1.0f, 
              1.3f, 0.0f, 1.0f, 
              0.0f, 0.0f, -1.65f, 

}; 


private float textures[] = {   
      //Mapping coordinates for the vertices 
      0.0f, 1.65f, 
      0.0f, 1.65f, 
      -1.3f, 0.0f, 
      1.3f, 0.0f, 

    }; 



private float colors[] = { 
    1.0f, 0.0f, 0.0f, 1.0f, //Red 
    0.0f, 1.0f, 0.0f, 1.0f, //Green 
    0.0f, 0.0f, 1.0f, 1.0f, //Blue 
    1.0f, 0.0f, 0.0f, 1.0f, //Red 

}; 


private byte indices [] = { 0, 2, 1, 
      0, 2, 3, 
      0, 1, 3, 
     }; 

/** 
* The Pyramid constructor. 
* 
* Initiate the buffers. 
*/ 
public Pyramid(Context context) { 
    // 
    ByteBuffer byteBuf = ByteBuffer.allocateDirect(PyramidVertices.length * 4); 
    byteBuf.order(ByteOrder.nativeOrder()); 
    vertexBuffer = byteBuf.asFloatBuffer(); 
    vertexBuffer.put(PyramidVertices); 
    vertexBuffer.position(0); 

    byteBuf = ByteBuffer.allocateDirect(colors.length * 4); 
    byteBuf.order(ByteOrder.nativeOrder()); 
    colorBuffer = byteBuf.asFloatBuffer(); 
    colorBuffer.put(colors); 
    colorBuffer.position(0); 

    indexBuffer = ByteBuffer.allocateDirect(indices.length); 
    indexBuffer.put(indices); 
    indexBuffer.position(0); 

    byteBuf = ByteBuffer.allocateDirect(textures.length * 4); 
    byteBuf.order(ByteOrder.nativeOrder()); 
    textureBuffer = byteBuf.asFloatBuffer(); 
    textureBuffer.put(textures); 
    textureBuffer.position(0); 
} 

/** 
* The object own drawing function. 
* Called from the renderer to redraw this instance 
* with possible changes in values. 
* 
* @param gl - The GL Context 
*/ 


public void draw(GL10 gl, Context context) {  
    //Set the face rotation 
// gl.glFrontFace(GL10.GL_CW); 
    gl.glCullFace(GL10.GL_CCW); 

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    // gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer); 

     loadTexture(gl, context); 

     gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 

     gl.glEnable(GL10.GL_TEXTURE_2D); 

     // Enable the texture state 
     gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

     // Point to our buffers 
     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 

     gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 


     gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer); 

    //Disable the client state before leaving 
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 


} 

     public void loadTexture(GL10 gl, Context context) { 

      Bitmap bitmap; 

      gl.glGenTextures(3, texturesID, 0); // Generate texture-ID array for 6 IDs 

      gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesID[2]); 

      InputStream is = context.getResources().openRawResource(R.drawable.forward); 

      try { 
       //BitmapFactory is an Android graphics utility for images 

       bitmap = BitmapFactory.decodeStream(is); 

      } finally { 
       //Always clear and close 
       try { 
        is.close(); 
        is = null; 
       } catch (IOException e) { 
       } 
      } 


      // Generate OpenGL texture images 

     // 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); 

      // Build Texture from loaded bitmap for the currently-bind texture ID 

      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); 



     } 

과 어떻게 다른 줄 http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_3D.html http://nehe.gamedev.net/

에서 도움을했다 각 얼굴에 이미지가 있습니까?

답변

0

가이 일을 몇 가지 방법이 있지만 여기에 당신이 시도하는 간단한 두 가지이다 : 당신의 요구에

수정 객체

그것은 한 번에 몇 텍스처를 사용하여 선택하는 것은 매우 까다로운 얼굴 당,하지만 간단한 해결책은 피라미드를 몇 개의 개체로 나누는 것입니다. 그런 다음 각 개체에 대해 원하는대로 서로 다른 질감을 지정할 수 있습니다. 귀하의 요구에

당신은 Texture Atlas으로 알려진 기술을 사용할 수 있습니다에

수정 질감. 이 솔루션에서는 몇 가지 텍스처를 가져 와서 하나의 더 큰 비트 맵으로 결합 할 수 있습니다. 그런 다음이 비트 맵을 기본 텍스처로 사용합니다. 또한 정점 UVs을 수정해야하므로 삼각형 중 하나가 더 큰 텍스처의 일부를 사용하고 다른 삼각형은 텍스처의 다른 부분을 사용합니다. 이 기법을 사용하면 다른 삼각형이 텍스처로 완전히 다른 이미지를 사용하는 모양을 얻을 수 있습니다 (심지어 렌더링하는 동안 하나의 실제 이미지가 texture 임).

자세한 내용이 필요하면 알려주십시오.

+0

는 u는 나에게 객체에 내 피라미드를 분할하는 방법의 예를주세요 수 있습니까?. – Aalok

+0

4 개의 삼각형을 동시에 그리는 대신 피라미드를 수정하여 하나의 삼각형을 네 번 그릴 수 있으며 매번 다른 텍스처를 선택할 수 있습니다. 당신은 이미 가지고있는 버퍼에서이 모든 그림을 할 수 있습니다 : http://stackoverflow.com/questions/9431923/using-an-offset-with-vbos-in-opengl – kolenda

+0

나는 같은 접근법을 따랐고 대답을 발견했습니다. 내 솔루션을 붙여 넣습니다. – Aalok

0

응답 해 주셔서 감사합니다.

내 문제의 해결책을 찾았습니다. 나는 정삼각형을 만들고 올바른 회전 각을줌으로써 회전시킬 수있을뿐 아니라 각면에 다른 질감을 넣을 수 있습니다.

MyPyramid 클래스

public class PyramidNew { 

    int []texturesID = new int[3]; 

    Bitmap []bitmap = new Bitmap[3]; 

    private FloatBuffer textureBuffer; 

    private FloatBuffer vertexBuffer; // Buffer for vertex-array 

    private float[][] colors = { // Colors of the 6 faces 
     {1.0f, 0.5f, 0.0f, 1.0f}, // 0. orange 
     {1.0f, 0.0f, 1.0f, 1.0f}, // 1. violet 
     {0.0f, 1.0f, 0.0f, 1.0f}, // 2. green 
     {0.0f, 0.0f, 1.0f, 1.0f}, // 3. blue 
     {1.0f, 0.0f, 0.0f, 1.0f}, // 4. red 
     {1.0f, 1.0f, 0.0f, 1.0f} // 5. yellow 
    }; 

/* private float[] vertices = { // Vertices for the front face 
     -1.5f, 0.0f, 0.86f, // 0. left-bottom-front 
     1.5f, 0.0f, 0.86f, // 1. right-bottom-front 
     0.0f, 1.86f, 0.0f, // 2. left-top-front 
     // 3. right-top-front 
    };*/ 
    private float[] vertices = { // Vertices for the front face 
       -1.0f, 0.0f, 0.86f, // 0. left-bottom-front 
       1.0f, 0.0f, 0.86f, // 1. right-bottom-front 
       0.0f, 1.86f, 0.0f, // 2. left-top-front 
       // 3. right-top-front 
      }; 
    private float textures[] = {   
      //Mapping coordinates for the vertices 
      0.0f, 0.0f, 
      0.0f, 1.0f, 
      1.0f, 1.0f, 

    }; 

    // Constructor - Set up the buffers 
    public PyramidNew(Context context) { 
    // Setup vertex-array buffer. Vertices in float. An float has 4 bytes 
     System.out.println("calling Pyramid:::::::::"); 
     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 

    ByteBuffer byteBuf = ByteBuffer.allocateDirect(textures.length * 4); 
    byteBuf.order(ByteOrder.nativeOrder()); 
    textureBuffer = byteBuf.asFloatBuffer(); 
    textureBuffer.put(textures); 
    textureBuffer.position(0); 


    InputStream stream1 = context.getResources().openRawResource(R.drawable.splash_screen); 
    InputStream stream2 = context.getResources().openRawResource(R.drawable.bg); 
    InputStream stream3 = context.getResources().openRawResource(R.drawable.bg1); 


     try { 
      //BitmapFactory is an Android graphics utility for images 

      bitmap[0] = BitmapFactory.decodeStream(stream1); 
      bitmap[1] = BitmapFactory.decodeStream(stream2); 
      bitmap[2] = BitmapFactory.decodeStream(stream3); 

     } finally { 
      //Always clear and close 
      try { 
       stream1.close(); 
       stream2.close(); 
       stream3.close(); 

       stream1 = stream2 = stream3 = null; 
      } catch (IOException e) { 
      } 
     } 


    } 

    // Draw the color cube 
    public void draw(GL10 gl, Context context) { 
     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) 

     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 
     gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 


     //loading the first texture in first face 
     loadTexture(gl, context, 0); 
     gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3); 

     // rotating the face and puting the second texture in face 
     gl.glRotatef(120.0f, 0.0f, 1.0f, 0.0f); 
     //gl.glColor4f(colors[1][0], colors[1][1], colors[1][2], colors[1][3]); 
     loadTexture(gl, context, 1); 
     gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3); 

     // Back - Rotate another 120 degree about y-axis and then put different texture 
     gl.glRotatef(120.0f, 0.0f, 1.0f, 0.0f); 
     //gl.glColor4f(colors[2][0], colors[2][1], colors[2][2], colors[2][3]); 
     loadTexture(gl, context, 2); 
     gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3); 

     gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
     gl.glDisable(GL10.GL_CULL_FACE); 
    } 


    public void loadTexture(GL10 gl, Context context, int currentImage) { 

     // Bitmap []bitmap = new Bitmap[3]; 

      gl.glGenTextures(3, texturesID, 0); // Generate texture-ID array for 6 IDs 

      gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesID[currentImage]); 

      // Generate OpenGL texture images 

     // 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); 

      // Build Texture from loaded bitmap for the currently-bind texture ID 

      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap[currentImage], 0); 

      gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 

      gl.glEnable(GL10.GL_TEXTURE_2D); 

      // Enable the texture state 
      gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

      // Point to our buffers 
      gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 



     } 

은}

관련 문제