마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 opencv 큐브에 텍스쳐 가 안됩니다....

0 추천
큐브에텍스쳐하는것이 안되는데 조언부탁드립니다... 코드는 댓글로 남기겠습니다........
익명사용자 님이 2014년 5월 6일 질문
public class CustomActivity extends AndARActivity
{

    /*
    CustomObject someObject;
    ARToolkit artoolkit;
    */
    private Context context;
    private ARObject someObject;
    private ARToolkit artoolkit;
    @Override
    public void onCreate(Bundle savedInstanceState) {
       
        super.onCreate(savedInstanceState);
        CustomRenderer renderer = new CustomRenderer(context);//optional, may be set to null
        //super.setNonARRenderer(renderer);//or might be omited
        setNonARRenderer(renderer);
        try {

            artoolkit = getArtoolkit();

           
            someObject = new CustomObject1("test","marker_at16.patt",80.0,new double[]{0,0});
            artoolkit.registerARObject(someObject);
           
           
        } catch (AndARException ex){
            //handle the exception, that means: show the user what happened
            System.out.println("");
        }       
        startPreview();
    }

    /**
     * Inform the user about exceptions that occurred in background threads.
     * This exception is rather severe and can not be recovered from.
     * TODO Inform the user and shut down the application.
     */
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        Log.e("AndAR EXCEPTION", ex.getMessage());
        finish();
    }
   
   
   
}
public class CustomObject1 extends ARObject {
   
    private MultiTexturedCube m_Cube = null;
   
    private Context m_Context = null;

   
    public CustomObject1(String name, String patternName,
            double markerWidth, double[] markerCenter) {
        super(name, patternName, markerWidth, markerCenter);
        float   mat_ambientf[]     = {0f, 1.0f, 0f, 1.0f};
        float   mat_flashf[]       = {0f, 1.0f, 0f, 1.0f};
        float   mat_diffusef[]       = {0f, 1.0f, 0f, 1.0f};
        float   mat_flash_shinyf[] = {50.0f};

        mat_ambient = GraphicsUtil.makeFloatBuffer(mat_ambientf);
        mat_flash = GraphicsUtil.makeFloatBuffer(mat_flashf);
        mat_flash_shiny = GraphicsUtil.makeFloatBuffer(mat_flash_shinyf);
        mat_diffuse = GraphicsUtil.makeFloatBuffer(mat_diffusef);
       
    }
    public CustomObject1(String name, String patternName,
            double markerWidth, double[] markerCenter, float[] customColor) {
        super(name, patternName, markerWidth, markerCenter);
        float   mat_flash_shinyf[] = {50.0f};

        mat_ambient = GraphicsUtil.makeFloatBuffer(customColor);
        mat_flash = GraphicsUtil.makeFloatBuffer(customColor);
        mat_flash_shiny = GraphicsUtil.makeFloatBuffer(mat_flash_shinyf);
        mat_diffuse = GraphicsUtil.makeFloatBuffer(customColor);
       
    }
   
    /**
     * Just a box, imported from the AndAR project.
     */
    //private SimpleBox box = new SimpleBox();
    private FloatBuffer mat_flash;
    private FloatBuffer mat_ambient;
    private FloatBuffer mat_flash_shiny;
    private FloatBuffer mat_diffuse;
   
    /**
     * Everything drawn here will be drawn directly onto the marker,
     * as the corresponding translation matrix will already be applied.
     */
    @Override
    public final void draw(GL10 gl) {
        super.draw(gl);
       
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR,mat_flash);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, mat_flash_shiny);   
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, mat_diffuse);   
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, mat_ambient);

/*        //draw cube
        gl.glColor4f(0f, 1.0f, 0, 1.0f);
        gl.glTranslatef( 0.0f, 0.0f, 12.5f );
        
        //draw the box
        box.draw(gl);*/
       
        gl.glTranslatef( 0.0f, 0.0f, 12.5f );
        m_Cube.draw(gl);
       
    }
    @Override
    public void init(GL10 gl) {
       
    }
}
public class CustomRenderer implements OpenGLRenderer {
   
    private Context context;
    private MultiTexturedCube cube;
   
   
    public CustomRenderer(Context context){
        this.context = context;

        Bitmap []bitmap = new Bitmap[6];

        bitmap[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.m1);

        bitmap[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.m2);

        bitmap[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.m3);

        bitmap[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.m4);

        bitmap[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.m5);

        bitmap[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.m6);

        cube = new MultiTexturedCube(bitmap);

    }
   
    public void onSurfaceCreated(GL10 gl, EGLConfig config){

       
        gl.glEnable(GL10.GL_BLEND);
          gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
          gl.glClearColor(0,0,0,0);
          gl.glDisable(GL10.GL_BLEND);


   
        // Set the background color to black ( rgba ).

   

                // Enable Smooth Shading, default not really needed.

                gl.glShadeModel(GL10.GL_SMOOTH);

                // Depth buffer setup.

                gl.glClearDepthf(1.0f);

                // Enables depth testing.

                gl.glEnable(GL10.GL_DEPTH_TEST);

                // The type of depth testing to do.

                gl.glDepthFunc(GL10.GL_LEQUAL);

                // Really nice perspective calculations.

                gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

    }
   
    /**
     * Light definitions
     */   
   
    private float[] ambientlight1 = {.3f, .3f, .3f, 1f};
    private float[] diffuselight1 = {.7f, .7f, .7f, 1f};
    private float[] specularlight1 = {0.6f, 0.6f, 0.6f, 1f};
    private float[] lightposition1 = {20.0f,-40.0f,100.0f,1f};
   
    private FloatBuffer lightPositionBuffer1 =  GraphicsUtil.makeFloatBuffer(lightposition1);
    private FloatBuffer specularLightBuffer1 = GraphicsUtil.makeFloatBuffer(specularlight1);
    private FloatBuffer diffuseLightBuffer1 = GraphicsUtil.makeFloatBuffer(diffuselight1);
    private FloatBuffer ambientLightBuffer1 = GraphicsUtil.makeFloatBuffer(ambientlight1);
   
    /**
     * Do non Augmented Reality stuff here. Will be called once after all AR objects have
     * been drawn. The transformation matrices may have to be reset.
     */
    public final void draw(GL10 gl) {
    }


    /**
     * Directly called before each object is drawn. Used to setup lighting and
     * other OpenGL specific things.
     */
    public final void setupEnv(GL10 gl) {
        gl.glEnable(GL10.GL_LIGHTING);
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, ambientLightBuffer1);
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, diffuseLightBuffer1);
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_SPECULAR, specularLightBuffer1);
        gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, lightPositionBuffer1);
        gl.glEnable(GL10.GL_LIGHT1);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glDisable(GL10.GL_TEXTURE_2D);
        initGL(gl);
    }
   
    /**
     * Called once when the OpenGL Surface was created.
     */
    public final void initGL(GL10 gl) {
        gl.glDisable(GL10.GL_COLOR_MATERIAL);
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glDisable(GL10.GL_COLOR_MATERIAL);
        gl.glEnable(GL10.GL_LIGHTING);
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_NORMALIZE);
    }
}
public class MultiTexturedCube {
   
    // 육면체를 구성하는 6개의 면을 정점(24개)으로 표현한다

        float [] vertices = {

             // 앞면

                -1.0f, -1.0f, 1.0f, // 왼쪽 아래 정점

                1.0f, -1.0f, 1.0f,  // 오른쪽 아래

                -1.0f, 1.0f, 1.0f,  // 왼쪽 위

                1.0f, 1.0f, 1.0f,   // 오른쪽 위

                // 오른쪽 면

                1.0f, -1.0f, 1.0f,  // 왼쪽 아래

                1.0f, -1.0f, -1.0f, // 오른쪽 아래         

                1.0f, 1.0f, 1.0f,   // 왼쪽 위

                1.0f, 1.0f, -1.0f,  // 오른쪽 위

                // 뒷면

                1.0f, -1.0f, -1.0f,

                -1.0f, -1.0f, -1.0f,            

                1.0f, 1.0f, -1.0f,

                -1.0f, 1.0f, -1.0f,

                // 왼쪽면

                -1.0f, -1.0f, -1.0f,

                -1.0f, -1.0f, 1.0f,         

                -1.0f, 1.0f, -1.0f,

                -1.0f, 1.0f, 1.0f,

                // 아래쪽 면

                -1.0f, -1.0f, -1.0f,

                1.0f, -1.0f, -1.0f,         

                -1.0f, -1.0f, 1.0f,

                1.0f, -1.0f, 1.0f,

                // 위쪽면

                -1.0f, 1.0f, 1.0f,

                1.0f, 1.0f, 1.0f,           

                -1.0f, 1.0f, -1.0f,

                1.0f, 1.0f, -1.0f,

        };

       

        // 36개의 정점을 이용하여 12개의 3각형을 구성한다

        short [] indices = {

           //정점배열의 정점 인덱스를 이용하여 각 면마다 2개의 3각형(CCW)을 구성한다

                0,1,3, 0,3,2,           //앞면을 구성하는 2개의 3각형

                4,5,7, 4,7,6,           //오른쪽면

                8,9,11, 8,11,10,        //...

                12,13,15, 12,15,14,     

                16,17,19, 16,19,18,     

                20,21,23, 20,23,22,

        };   




        // 정점배열에 선언된 정점의 위치에 텍스쳐 좌표를 배정한다. 해당 정점의 위치에 매핑할 텍스쳐 좌료를 선언하면 된다.

        // 인덱스 배열은 참고할 필요가 없고 정점배열의 정점 순서에 따라서 텍스쳐의 위치를 결정하는 것이 관건이다.

        private float [] textures = {

        //6개의 면에 매핑될 텍스쳐 좌표 24개를  선언한다

            0.0f, 1.0f,

                1.0f, 1.0f,

                0.0f, 0.0f,

                1.0f, 0.0f,

                

                0.0f, 1.0f,

                1.0f, 1.0f,

                0.0f, 0.0f,

                1.0f, 0.0f,

                

                0.0f, 1.0f,

                1.0f, 1.0f,

                0.0f, 0.0f,

                1.0f, 0.0f,

                

                0.0f, 1.0f,

                1.0f, 1.0f,

                0.0f, 0.0f,

                1.0f, 0.0f,

                

                0.0f, 1.0f,

                1.0f, 1.0f,

                0.0f, 0.0f,

                1.0f, 0.0f,

                

                0.0f, 1.0f,

                1.0f, 1.0f,

                0.0f, 0.0f,

                1.0f, 0.0f,

        };

       

        // Our vertex buffer.

        private FloatBuffer vertexBuffer;




        // Our index buffer.

        private ShortBuffer indexBuffer;

       

        // Our UV texture buffer.

        private FloatBuffer textureBuffer;




        // 다수개의 텍스쳐가 필요하므로 텍스쳐 아이디를 저장할 배열이 필요함

        private int [] textureIds;

       

        // The bitmap we want to load as a texture.

        private Bitmap [] bitmap;

       

        public MultiTexturedCube(Bitmap[] bitmap) {

            // a float is 4 bytes, therefore we multiply the number if

            // vertices with 4.

            ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);

            vbb.order(ByteOrder.nativeOrder());

            vertexBuffer = vbb.asFloatBuffer();

            vertexBuffer.put(vertices);

            vertexBuffer.position(0);

           

            // short is 2 bytes, therefore we multiply the number if

            // vertices with 2.

            ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);

            ibb.order(ByteOrder.nativeOrder());

            indexBuffer = ibb.asShortBuffer();

            indexBuffer.put(indices);

            indexBuffer.position(0);

           

            ByteBuffer tbb = ByteBuffer.allocateDirect(vertices.length * 4);

            tbb.order(ByteOrder.nativeOrder());

            textureBuffer = tbb.asFloatBuffer();

            textureBuffer.put(textures);

            textureBuffer.position(0);

           

            this.bitmap = bitmap;

        }

       

        public void draw(GL10 gl) {

            // Counter-clockwise winding.

            gl.glFrontFace(GL10.GL_CCW);

            // Enable face culling.

            gl.glEnable(GL10.GL_CULL_FACE);

            // What faces to remove with the face culling.

            gl.glCullFace(GL10.GL_BACK);

           

            // Enabled the vertices buffer for writing and to be used during

            // rendering.

            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

            // Specifies the location and data format of an array of vertex

            // coordinates to use when rendering.

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

           

            // 텍스쳐 관련 내용

            gl.glEnable(GL10.GL_TEXTURE_2D);

            // Enable the texture state

            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);




            if(textureIds==null) loadGLTexture(gl);

           

            // Point to our buffers

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

            /*

            gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,

                    GL10.GL_UNSIGNED_SHORT, indexBuffer);

            */

                    // 6개의 면을 구분하여 여러번 그릴 때마다 텍스쳐가 그려진다

            for(int i=0; i<6; ++i)

            {

                gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIds[i]);   //use texture of ith face

                indexBuffer.position(6*i);                          //select ith face

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

                

                //draw 2 triangles making up this face

                gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indexBuffer);

            }

            // Disable the vertices buffer.

            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

            gl.glDisable(GL10.GL_TEXTURE_2D);

            // Disable face culling.

            gl.glDisable(GL10.GL_CULL_FACE);

        }

       

        private void loadGLTexture(GL10 gl) {

            // Generate one texture pointer...

            textureIds = new int[6];

            gl.glGenTextures(6, textureIds, 0); // 텍스쳐 아이디 6개 생성, 배열에 저장함, offset 0

           

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

        }


}

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...