2012-02-18 5 views
3

저는 image.png로 텍스처링하려고하는 사각형을 그리는 GlSurfaceView를 가지고 있습니다. 두 개의 서로 다른 png 파일을 동일한 드로어 블 폴더에서 재생했습니다. 언제 내가 잘 참조로드 하나를하지만 만약 내가 R.image.png 다른 전환, 내 sqaure 그냥 흰색으로 변합니다. 누구든지 도울 수 있습니까? 아래는 내 사각형 클래스이고 내 glView입니다. 광장일부 이미지가로드되지 않습니다 - 안드로이드 openGL 텍스처

:

package com.eliddell.AR; 
    import java.io.InputStream; 
    import java.nio.ByteBuffer; 
    import java.nio.ByteOrder; 
    import java.nio.FloatBuffer; 
    import javax.microedition.khronos.opengles.GL10; 
    import android.content.Context; 
    import android.graphics.Bitmap; 
    import android.graphics.BitmapFactory; 
    import android.opengl.GLUtils; 

    public class Square { 

     private FloatBuffer vertexBuffer; // buffer holding the vertices 

     private float vertices[] = { 
     -1.0f, -1.0f, 0.0f,  // V1 - bottom left 
     -1.0f, 1.0f, 0.0f,  // V2 - top left 
     1.0f, -1.0f, 0.0f,  // V3 - bottom right 
     1.0f, 1.0f, 0.0f   // V4 - top right 

}; 

private FloatBuffer textureBuffer; // buffer holding the texture coordinates 
private float texture[] = { 
     // Mapping coordinates for the vertices 
     0.0f, 1.0f,  // top left  (V2) 
     0.0f, 0.0f,  // bottom left (V1) 
     1.0f, 1.0f,  // top right (V4) 
     1.0f, 0.0f  // bottom right (V3) 

}; 


public Square() { 

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4); 
    byteBuffer.order(ByteOrder.nativeOrder()); 
    vertexBuffer = byteBuffer.asFloatBuffer(); 
    vertexBuffer.put(vertices); 
    vertexBuffer.position(0); 

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

} 
/** The texture pointer */ 
private int[] textures = new int[1]; 

public void loadGLTexture(GL10 gl, Context context) { 
    // loading texture 
    InputStream is =          context.getResources().openRawResource(R.drawable.android); 
    Bitmap bitmap = BitmapFactory.decodeStream(is); 
    //Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.android); 

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

    // Use 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(); 
} 
public void draw(GL10 gl) { 
    // bind the previously generated texture 
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); 

    // Point to our buffers 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

    // Set the face rotation 
    gl.glFrontFace(GL10.GL_CW); 

    // Point to our vertex buffer 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 

    // Draw the vertices as triangle strip 
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length/3); 

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

} 

    } 

내 GLSurfaceView 및 렌더링은 :

Propably 당신의 질감이 dosent
package com.eliddell.AR; 

    import javax.microedition.khronos.egl.EGLConfig; 
    import javax.microedition.khronos.opengles.GL10; 
    import android.opengl.GLU; 
    import android.content.Context; 
    import android.graphics.PixelFormat; 
    import android.hardware.Camera; 
    import android.opengl.GLSurfaceView; 
    import android.opengl.GLSurfaceView.Renderer; 
    import android.view.SurfaceHolder; 

    public class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback, Renderer { 

private Context context; 
private Square square; // the triangle to be drawn 


public GLLayer(Context context) { 
    super(context); 
    this.context = context; 
    this.square = new Square(); 

    // settings for translucent glView 
    this.setEGLConfigChooser(8, 8, 8, 8, 16, 0); 
    this.getHolder().setFormat(PixelFormat.TRANSLUCENT); 

    // set render to inline 
    this.setRenderer(this); 

} 

@Override 
public void onDrawFrame(GL10 gl) { 
    // clear Screen and Depth Buffer 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

    // Reset the Modelview Matrix 
    gl.glLoadIdentity(); 

    // Drawing 
    gl.glTranslatef(0.0f, 0.0f, -5.0f);  // move 5 units INTO the screen 
              // is the same as moving the camera 5 units away 
    square.draw(gl);      // Draw the triangle 

} 

@Override 
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 


} 

@Override 
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) { 
    // Load the texture for the square 
    square.loadGLTexture(gl, this.context); 

    gl.glEnable(GL10.GL_TEXTURE_2D);   //Enable Texture Mapping (NEW) 
    gl.glShadeModel(GL10.GL_SMOOTH);   //Enable Smooth Shading 
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //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); 

} 

@Override 
public void onPreviewFrame(byte[] data, Camera camera) { 
    // TODO Auto-generated method stub 

} 

    } 

답변

2

2 차원의 힘이있다. OpenGL은 32X32, 64X64, 128X128 ..... 및 36X36, 48X48 등의 크기 (2의 거듭) 크기의 텍스처 만로드 할 수 있습니다.

이미지 크기를 변경하십시오.

+0

ruh roh .. 너도 알다시피,이 말이 맞다. 내 계획은 웹에서 이미지로 정사각형을 만드는 것이 었습니다. 이것이 문제입니까? 그것은 가능한가? – erik

+0

글쎄,이 경우 (만약 내가 이해가) 웹에서 이미지가 냄비 크기가 있는지 확인하고 만약 당신이 비트 맵 크기를 조정하는 방법을 보려면 이것을 참조하십시오 OpenGL에로드하기 전에 비트 맵 크기를 조정해야합니다 fisrt 필요 Logged : //thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/ – SteveL

+0

좋아, 이제 텍스쳐로 사용할 수있는 포토샵 cs5에서 png 파일을 만들려고합니다. 72dpi로 256x256 크기의 이미지를 만들고 웹용으로 png-24로 저장했지만 작동하지 않습니다. OpenGL 텍스처 용 PNG 이미지를 만드는 과정에서 누락 된 특별한 단계가 있습니까? – erik