2

I have a 2d bitmap that I would like to convert into a 3d cube(example like in minecraft: enter image description here)

I managed to rotate around images in 3d space using the "Camera" but I can't understand how to control it or how to create a cube out of it, anyone has ideas? Please notice i'm allowed only to use canvas and no OpenGL.

Edit: this is as close as I got: enter image description here

using this code:

        Matrix mMatrix = canvas.getMatrix();
        canvas.save();
        Camera camera=new Camera();
        camera.save();
        camera.rotateY(30);
        camera.getMatrix(mMatrix);
        mMatrix.preTranslate(-30, 0);
        mMatrix.postTranslate(30, 0);
        canvas.concat(mMatrix);
        canvas.drawBitmap(b, 150, 150, null);
        canvas.drawBitmap(b, 180, 180, null);
        camera.restore();
        canvas.restore();
        canvas.save();
SpoocyCrep
  • 604
  • 7
  • 23
  • Did you see this: http://stackoverflow.com/questions/12205462/3d-cube-using-canvas-need-a-little-improvement – mjstam Jan 20 '15 at 18:34
  • @mjstam Yes, this is where i took the idea of using camera, but I can't really understand how it works.. I read about it but i cant manage to rotate it the way i want it to.. also the guy didnt post all of his code, i dont know what angle is and width \width 2 – SpoocyCrep Jan 20 '15 at 18:39
  • @SpoocyCrep, can u please share the full code, as I am unable to create it – Reprator Oct 23 '17 at 05:33

1 Answers1

1

Okay, so because none helped me I looked for another way of doing it and thought of a workaround way that works, its not efficient and it might slow the program down while doing it, so think twice before using it.

The way I did it is like that:

First of all, I created a cube in paint (could be any 3D shape) enter image description here Then, I cut the cube sides and saved them separately. enter image description here

After loading those images all is left is the code:

    public Bitmap CreateACube(Bitmap b2D){
    Bitmap result=Bitmap.createBitmap(b2D);
    /*loading sides of cube and painting texture on them*/
    Bitmap top;
    Bitmap left;
    Bitmap front;
    top=BitmapFactory.decodeResource(getResources(), R.drawable.top);
    top=CubeCreator(top, b2D,"top");
    left=BitmapFactory.decodeResource(getResources(), R.drawable.left);
    left=CubeCreator(left, b2D,"left");
    front=BitmapFactory.decodeResource(getResources(), R.drawable.front);
    front=CubeCreator(front, b2D,"front");
    Bitmap merge;
    merge=overlay(top, left);//connecting all cube sides together into one bitmap
    merge=overlay(merge, front);
    result=Bitmap.createScaledBitmap(merge, merge.getWidth()*2, merge.getHeight()*2, false); //Scaling the result, you can remove if you don't want to.
    return result;
}
private Bitmap CubeCreator(Bitmap srcBmp,Bitmap b2D,String s){
/*gets cube side bitmap, the texture bitmap, and a string of the side name*/
      int width = srcBmp.getWidth();
        int height = srcBmp.getHeight();
        int width2=b2D.getWidth();
        int height2=b2D.getHeight();
        int rows1=0;
        int rows2=0;
        Bitmap dstBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        /*Running on every pixel in the cube side*/
        for (int row = 0; row < height; row++) {
            rows1=++rows1%height2; //running on every pixel on the texture and reseting it if reached the last pixel
            for (int col = 0; col < width; col++) {
                 rows2=++rows2%width2;
                int pixel = srcBmp.getPixel(col, row);
                int alpha = Color.alpha(pixel);
                int dstColor=b2D.getPixel(rows2, rows1);
                switch(s){//you can add more sides, I used 3
                case "front":{
                    float[] hsv = new float[3];
                    int color = dstColor;
                    Color.colorToHSV(color, hsv);
                    hsv[2] *= 0.8f; // giving brightness to certain sides to make it look more 3D
                    dstColor = Color.HSVToColor(hsv);
                break;
                }
                case "left":{
                    float[] hsv = new float[3];
                    int color = dstColor;
                    Color.colorToHSV(color, hsv);
                    hsv[2] *= 0.44f; 
                    dstColor = Color.HSVToColor(hsv);
                    break;
                }
                case "top":{
                    float[] hsv = new float[3];
                    int color = dstColor;
                    Color.colorToHSV(color, hsv);
                    hsv[2] *= 1.2f; 
                    dstColor = Color.HSVToColor(hsv);
                    break;
                }

                }
                int pixel2=srcBmp.getPixel(col, row);
                if(pixel2!= Color.TRANSPARENT)//checking if the current pixel of the side is not transparent
                dstBitmap.setPixel(col, row, dstColor);
                else{
                    dstBitmap.setPixel(col, row, pixel2);
                }
            }
        }

        return dstBitmap;
}
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
    /*connects two bitmaps together*/
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, 0, 0, null);
    return bmOverlay;
}

Summary of the code: I run on every pixel of the cube side (front, left, top) and while doing so I also run on every pixel in the texture bitmap, and color the side pixels with the pixels of the texture. After it I connect all colored sides together into one bitmap and return them.

Result: enter image description here

SpoocyCrep
  • 604
  • 7
  • 23