2012-09-13 12 views
0

두 개의 삼각형을 직사각형으로 함께 그려서 전체 텍스처를 그려 보려고합니다. 단지 1.0 좌표 일 것이라고 생각합니다. 작은 퇴색 된 텍스처가 많은 1 삼각형을 얻습니다. 임 매우 새로운 OpenGL과 일종의 이러한 전화로 무슨 일이 있었는지 잃었습니다. 잘못된 좌표 또는 잘못된 순서로 그리는 것처럼 누군가 내 QUAD 클래스에서 분명히 잘못된 것을 볼 수 있습니까?텍스처 맵핑 된 사각형 그리기

public class MyRenderer implements GLSurfaceView.Renderer { 
     private Quad myquad; 
     private Context context; 
     int texture[] = new int[1]; 
     private int width,height; 
     int x,y; 
     private long startframe,timediff,sleeptime,frameskip; 
     private final static int MAX_FPS = 25; 
     private final static int MAX_FRAME_SKIPS = 5; 
     private final static int FRAME_PERIOD = 1000/MAX_FPS; 
     private static Direction move_direction=Direction.NONE; 


     public MyRenderer (Context context){ 
      this.myquad = new Quad(); 
      this.context = context; 
     } 
     public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
      myquad.loadGLTexture(gl, this.context); 

      //turn on alpha blending 
      gl.glEnable(GL10.GL_BLEND); 
      gl.glBlendFunc(gl.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 

      gl.glEnable(GL10.GL_TEXTURE_2D); 
      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
      gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 
      gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); 
      gl.glDisable(GL10.GL_DEPTH_TEST); 

      x=y=0; 
     } 

     public void onSurfaceChanged(GL10 gl, int w, int h) { 
      width=w; 
      height=h; 
      gl.glViewport(0, 0, w, h); 
      gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix   
      gl.glLoadIdentity(); 
      gl.glOrthof(0.0f, 320, 0.0f, 480, 1.0f, -1.0f);   
     } 

     public void onDrawFrame(GL10 gl) { 
      startframe=SystemClock.elapsedRealtime(); 
      // define the color we want to be displayed as the "clipping wall" 
      gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
      // clear the color buffer to show the ClearColor we called above... 
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
      // gl.glLoadIdentity(); 
      switch (move_direction) 
      { 
      case UP: 
       gl.glTranslatef(0, 1, 0); 
       y=y+1; 
       break; 
      case DOWN: 
       gl.glTranslatef(0, -1, 0); 
       y= y-1; 
       break; 
      case LEFT: 
       gl.glTranslatef(-1, 0, 0); 
       x=x-1; 
       break; 
      case RIGHT: 
       gl.glTranslatef(1, 0, 0); 
       x=x+1; 
       break; 
      case NONE: 
       //gl.glTranslatef(0, 0, 0); 
       break; 
      } 
      myquad.Draw(gl); 

class Quad { 
    private FloatBuffer vertices; 
    private int[] textures = new int[1]; 
    private ShortBuffer indexbuffer; 
    //private float vertices[] = { 
    //  50.0f, 50.0f, // 0. 
    //   100.0f, 50.0f, // 1. 
    //  100.0f, 100.0f, // 2. 
    //   50.0f, 100.0f // 3. 
    //}; 

    private float texture[] = {   
      0.0f, 0.0f, //0 
      1.0f, 0.0f, //1 
      1.0f, 1.0f,  //2 
      1.0f, 0.0f  //3 
    }; 

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



       public Quad(){ 
       try 
      { 
        float[] coords={50.0f, 50.0f, //0 
        300.0f,50.0f, //1 
        300.0f, 300.0f, //2 
        300.0f, 50.0f}; //3 

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

      ByteBuffer verticesbuffer =  ByteBuffer.allocateDirect(coords.length*4); 
      verticesbuffer.order(ByteOrder.nativeOrder()); 
      vertices = verticesbuffer.asFloatBuffer(); 
      vertices.put(coords); 
      //vertices.position(0); 
      vertices.flip(); 

     ByteBuffer idb = ByteBuffer.allocateDirect(indices.length * 2); 
     idb.order(ByteOrder.nativeOrder()); 
     indexbuffer = idb.asShortBuffer(); 
     indexbuffer.put(indices); 
     //indexbuffer.position(0); 
     indexbuffer.flip(); 
     }catch(Exception e) 
     { 
      Log.d("buffers","not working",e); 
     } 
    } 

    public void loadGLTexture(GL10 gl, Context context) { 
      //Get the texture from the Android resource directory 
      //Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.herowalkback); 
     try 
     { 
     Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.herowalkback); 

      if (bitmap == null) 
      { 
       Log.i ("info", "bitmap could not be decoded"); 
      } 
      //Generate one texture pointer... 
      gl.glGenTextures(1, textures, 0); 
      //...and bind it to our array 
      gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); 

      //Create Nearest Filtered Texture 
      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_NEAREST); 

      gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE); 

      //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap 
      GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0,bitmap, 0); 

      //Clean up 
      bitmap.recycle(); 
     }catch(Exception e) 
     { 
      Log.d("load texture","not working"); 
     } 
     } 

    public void Draw(GL10 gl) 
    { 
     // set the color for the triangle 
     gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f); 

     //Enable the vertex and texture state 
     vertices.position(0); 
     gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertices); 
     vertices.position(2); 
     gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, vertices); 


     // Draw the vertices as triangle strip 
     gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indexbuffer); 

    } 
} 


public class test extends Activity { 
    private GLSurfaceView glsurface; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     glsurface = new GLSurfaceView(this); 
     glsurface.setRenderer(new MyRenderer(this)); 
     setContentView(glsurface); 
     glsurface.setOnKeyListener(watchkeys); 
     glsurface.setFocusable(true); 
     glsurface.setFocusableInTouchMode(true); 


    } 

답변

0

잘못된 것은 아닙니다. 이것은 텍스처 매핑이라고 불리우므로 이러한 유형의 상황에 익숙해 져야합니다.

예를 들어 0.0과 1.0을 사용하여 삼각형을 수평으로 (x 좌표로) 매핑하는 것을 기억하십시오. 관련된 두 개의 삼각형을 사용하여 그에 따라 좌표를 나눌 필요가 있습니다. 따라서 0.0, 0.5, 1.0입니다. 그리고 Y 축에 대해서도 마찬가지입니다.

블렌더와 같은 모델링 도구를 사용하여 개체를 텍스처 맵핑하는 것이 이상적이므로 x를 삼각형 x 번호로 나누는 것에 대해 걱정할 필요가 없습니다.

+0

감사합니다. 왜 그것은 단지 하나의 삼각형을 그릴 것인가 아니면 둘 다 그리는 것인가? 나는 단지 잘못된 텍스쳐 때문에 오직 1을 보았을 뿐인가? – Oscuro

+0

코드에 많은 문제가 있으며 3 가지 이상의 큰 문제가 있습니다. 시작하기 위해 안드로이드 예제를 수정하지 않는 이유는 무엇입니까? 그리고 나서 더 많은 도움을 얻기 위해 stackoverflow로 돌아 가라. 이 정확한 문제에 대해 안드로이드 개발자 페이지에 코드가 완전히 작동하면 코드를 복구하는 데 시간이 낭비됩니다. 희망이 도움이됩니다. – Pixelapp