레이블이 Android Develop인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Android Develop인 게시물을 표시합니다. 모든 게시물 표시

2014년 4월 25일 금요일

Vertex Shader problem in Galaxy S Mali GPU


Google document 의 문제인가.
Galaxy S Mali GPU 의 문제인가.


http://developer.android.com/training/graphics/opengl/index.html


http://stackoverflow.com/questions/22384272/android-opengl-es-not-rasterizing-matrix-multiplication-switched




------- EDIT : SOLUTION FOUND -------
Okay, I found the error. The google document shows a "wrong" the multiplication of the matrices in the vertex shader:
uniform mat4 uMVPMatrix;
attribute vec4 vPosition;
void main() {
   gl_Position = uMVPMatrix * vPosition;
}
This doesn't seem to be a problem for my old Galaxy S1 but somehow the S3 (or the Mali GPU) is picky about this. I changed the order of multiplication to this:
uniform mat4 uMVPMatrix;
attribute vec4 vPosition;
void main() {
   gl_Position = vPosition * uMVPMatrix;
}
And it works (also on the S1). Still not sure why the S1 works fine with both versions but this solves the problem.




--------------------------------------------------------
Google document 는 문제가 없고,
Galaxy S Mali GPU 도 문제가 없다.


translate 까지 정확히 동작하려면 matrix 가 앞에오는 코드가 맞다.

uniform mat4 uMVPMatrix;
attribute vec4 vPosition;
void main() {
   gl_Position = uMVPMatrix * vPosition;
}



model matrix 에서 rotation, translation 까지 정상적으로 동작하게하려면,
 matrix 가 앞에오는 다음 코드가 맞다.


gl_Position = uMVPMatrix * vPosition;



viewport 설정.
view matrix 설정.
projection matrix 설정.
model matrix 설정.


gl_Position = vPosition * uMVPMatrix;

Samsung Galaxy note 8.0 SHW-M500W 에서 위 코드만 정상적으로 출력되고,  matrix 가 앞에 오는 맞는 코드는 출력되지 않는것처럼 보이는 현상은 projection matrix 가 잘못 설정되어 있거나, matrix 를 곱하는 순서가 잘못되었거나, 같은 matrix 를 입출력에 같이사용하거나 할때 나타난다.

matrix 가 뒤에 오는 위 코드를 사용하면 translate 가 되지 않는다.


model matrix 에서 rotation, translation 까지 정상적으로 동작하게하려면,
 matrix 가 앞에오는 다음 코드가 맞다.


gl_Position = uMVPMatrix * vPosition;



2013년 8월 21일 수요일

Applying Projection and Camera Views












import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

private GLSurfaceView mGLView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
       
        //setContentView(R.layout.activity_main);
       
       
       
        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity.      
        mGLView = new MyGLSurfaceView(this);
        setContentView( mGLView);
       
   
       
    }
   

    @Override
    protected void onPause() {
   
        super.onPause();
       
        // The following call pauses the rendering thread.
        // If your OpenGL application is memory intensive,
        // you should consider de-allocating objects that
        // consume significant memory here.
        mGLView.onPause();
    }
   
    @Override
    protected void onResume() {
   
        super.onResume();
       
        // The following call resumes a paused rendering thread.
        // If you de-allocated graphic objects for onPause()
        // this is a good place to re-allocate them.
        mGLView.onResume();
    }  


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
   
}




-------------------------------------------------------------------



import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.os.SystemClock;
import android.util.Log;

public class MyGLRenderer implements GLSurfaceView.Renderer {
private static final String TAG = "MyRenerer";
private Triangle mTriangle;
private Square mSquare;
private final float[] mMVPMatrix      = new float[16];
private final float[] mProjMatrix     = new float[16];
private final float[] mVMatrix        = new float[16];
private final float[] mRotationMatrix = new float[16];
// Declare as volatile because we are updating it from another thread
public volatile float mAngle;


@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Set the background frame color
GLES20.glClearColor( 1.0f, 0.0f, 0.0f, 1.0f);
// GLES20.glClearColor( 0.5f, 0.5f, 0.5f, 1.0f);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glDepthFunc(GLES20.GL_LEQUAL);

// set the camera position ( View matrix)
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -5, 0, 0, 0, 0, 1, 0);
mTriangle = new Triangle();
mSquare = new Square();
}
@Override
public void onDrawFrame(GL10 gl) {
// Redraw background color
GLES20.glClear( GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
// set the camera position ( View matrix)
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -5, 0, 0, 0, 0, 1, 0);
// Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);
// Combine the projection and camera view matices
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
// Create a rotation for the triangle
//long time = SystemClock.uptimeMillis() % 4000L;
//float angle = 0.090f * ((int) time);
// mAngle = angle;
Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);
// Combine the rotation matrix with the projection and camera view
        Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);
mTriangle.draw(mMVPMatrix);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {

// Adjust the viewport based on geometry changes,
// such as screen rotation
GLES20.glViewport( 0, 0, width, height);
float ratio = (float) width / height;
// float FIXED_RATIO = 2;
// this projection matrix is applied to object coordinates
// in the onDrawFrame() method
Matrix.frustumM( mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
// Matrix.frustumM( mProjMatrix, 0, -FIXED_RATIO, FIXED_RATIO, -FIXED_RATIO, FIXED_RATIO, 1, 10);
Log.e(TAG, "onSurfaceChanged");
}
    public static int loadShader(int type, String shaderCode){

        // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
        // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
        int shader = GLES20.glCreateShader(type);

        // add the source code to the shader and compile it
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);

        return shader;
    }

    
    public static void checkGlError(String glOperation) {
    int error;
    while((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
    Log.e(TAG, glOperation + ": glError " + error);
    throw new RuntimeException(glOperation + ": glError " + error);
    }
    }

}


-------------------------------------------------------------------



import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.MotionEvent;


public class MyGLSurfaceView extends GLSurfaceView {
private final MyGLRenderer mRenderer;

public MyGLSurfaceView(Context context) {

super(context);
// Create an OpenGL ES 2.0 context
setEGLContextClientVersion(2);
// Set the Renderer for drawing on the GLSurfaceView
        mRenderer = new MyGLRenderer();
setRenderer( mRenderer);
// Render the view only when there is a change in the drawing data
setRenderMode( GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
    private float mPreviousX;
    private float mPreviousY;
public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
switch( e.getAction()) {
case MotionEvent.ACTION_MOVE:
float dx = x - mPreviousX;
float dy = y - mPreviousY;
// reverse direction of rotation above the mid-line
if (y > getHeight() / 2) {
dx = dx * -1;
}
// reverse direction of rotation to left of the mid-line
if(x < getWidth() / 2) {
dy = dy * -1;
}
mRenderer.mAngle += (dx + dy)*TOUCH_SCALE_FACTOR; // 180.0F / 320
requestRender();
}
mPreviousX = x;
    mPreviousY = y;
    
    
    Log.e( "MyGLSurfaceView", "onTouchEvent");
return true;
}


}


-------------------------------------------------------------------



import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.opengl.GLES20;

public class Triangle {
    private final String vertexShaderCode =
   
    // This matrix member variable provides a hook to manipulate
    // the coordinates of objects that use this vertex shader.
    "uniform mat4 uMVPMatrix; " +    
            "attribute vec4 vPosition;" +
            "void main() {" +
            "  gl_Position = uMVPMatrix * vPosition;" +
            "}";

        private final String fragmentShaderCode =
            "precision mediump float;" +
            "uniform vec4 vColor;" +
            "void main() {" +
            "  gl_FragColor = vColor;" +
            "}";
private FloatBuffer vertexBuffer;
    private final int mProgram;
    private int mPositionHandle;
    private int mColorHandle;
    private int mMVPMatrixHandle;
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;

static float triangleCoords[] = {
0.0f,  0.622008459f, 0.0f,
  -0.5f, -0.311004243f, 0.0f,
0.5f, -0.311004243f, 0.0f
};
    private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
    private final int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };
public Triangle() {
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
triangleCoords.length * 4

);
// use the device hardware's native byte order
bb.order( ByteOrder.nativeOrder() );
// create a floating point buffer from the ByteBuffer
vertexBuffer = bb.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer.put(triangleCoords);
// set the buffer to read the first coordinate
vertexBuffer.position(0);

        // prepare shaders and OpenGL program
        int vertexShader   = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
        vertexShaderCode);
        int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
                                                      fragmentShaderCode);

        mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
        GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
        GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
        GLES20.glLinkProgram(mProgram);                  // create OpenGL program executables
}
    public void draw(float[] mvpMatrix) {
        // Add program to OpenGL environment
        GLES20.glUseProgram(mProgram);

        // get handle to vertex shader's vPosition member
        mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

        // Enable a handle to the triangle vertices
        GLES20.glEnableVertexAttribArray(mPositionHandle);

        // Prepare the triangle coordinate data
        GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
                                     GLES20.GL_FLOAT, false,
                                     vertexStride, vertexBuffer);

        // get handle to fragment shader's vColor member
        mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

        // Set color for drawing the triangle
        GLES20.glUniform4fv(mColorHandle, 1, color, 0);
        
        
        // get handle to shape's transformation matrix
        mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
        MyGLRenderer.checkGlError("glGetUniformLocation");
        
        // Apply the projection and view transformation
        GLES20.glUniformMatrix4fv(mMVPMatrixHandle,  1, false, mvpMatrix, 0);
        MyGLRenderer.checkGlError("glUniformMatrix4fv");
        
        
        
        

        // Draw the triangle
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

        // Disable vertex array
        GLES20.glDisableVertexAttribArray(mPositionHandle);
    }

}









2013년 8월 17일 토요일

Publisher Account 등록



Google Play 에서 앱을 판매하려면

Google Play Developer Console 에 등록해야 한다.


https://play.google.com/apps/publish/



등록비는 $25 이다.
Google Wallet 을 사용하여 결제한다.


유료앱을 판매하려면 Google Wallet Merchant Account 가 있어야 한다.


한국은 유료앱 판매가능국가 에 포함되어 있으므로 Google Wallet Merchant Account 를 만들 수 있다.


등록 과정에서 신용카드가 필요하다.
















2013년 8월 16일 금요일

Drawing Shapes











import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

private GLSurfaceView mGLView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);    
     
        //setContentView(R.layout.activity_main);
     
     
        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity.
     
        mGLView = new MyGLSurfaceView(this);
        setContentView( mGLView);      
     
    }
 

    @Override
    protected void onPause() {
   
        super.onPause();
     
        // The following call pauses the rendering thread.
        // If your OpenGL application is memory intensive,
        // you should consider de-allocating objects that
        // consume significant memory here.
        mGLView.onPause();
    }
 
    @Override
    protected void onResume() {
   
        super.onResume();
     
        // The following call resumes a paused rendering thread.
        // If you de-allocated graphic objects for onPause()
        // this is a good place to re-allocate them.
        mGLView.onResume();
    }  


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}


----------------------------------------------------------------------





import android.content.Context;
import android.opengl.GLSurfaceView;


public class MyGLSurfaceView extends GLSurfaceView {

public MyGLSurfaceView(Context context) {

super(context);


// Create an OpenGL ES 2.0 context
setEGLContextClientVersion(2);



// Set the Renderer for drawing on the GLSurfaceView
setRenderer( new MyRenderer());


// Render the view only when there is a change in the drawing data
setRenderMode( GLSurfaceView.RENDERMODE_WHEN_DIRTY);


}


}


----------------------------------------------------------------------




import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

public class MyRenderer implements GLSurfaceView.Renderer {


private Triangle mTriangle;


@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {

// Set the background frame color
GLES20.glClearColor( 1.0f, 0.0f, 0.0f, 1.0f);

mTriangle = new Triangle();

}

@Override
public void onDrawFrame(GL10 gl) {

// Redraw background color
GLES20.glClear( GLES20.GL_COLOR_BUFFER_BIT );


mTriangle.draw();

}


@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {

GLES20.glViewport( 0, 0, width, height);

}


    public static int loadShader(int type, String shaderCode){

        // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
        // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
        int shader = GLES20.glCreateShader(type);

        // add the source code to the shader and compile it
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);

        return shader;
    }


}


----------------------------------------------------------------------





import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.opengl.GLES20;

public class Triangle {

    private final String vertexShaderCode =
            "attribute vec4 vPosition;" +
            "void main() {" +
            "  gl_Position = vPosition;" +
            "}";

        private final String fragmentShaderCode =
            "precision mediump float;" +
            "uniform vec4 vColor;" +
            "void main() {" +
            "  gl_FragColor = vColor;" +
            "}";



private FloatBuffer vertexBuffer;
    private final int mProgram;
    private int mPositionHandle;
    private int mColorHandle;

// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;

static float triangleCoords[] = {
0.0f,  0.622008459f, 0.0f,
  -0.5f, -0.311004243f, 0.0f,
0.5f, -0.311004243f, 0.0f
};

    private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
    private final int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex



float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };

public Triangle() {



// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(


// (number of coordinate values * 4 bytes per float)
triangleCoords.length * 4

);


// use the device hardware's native byte order
bb.order( ByteOrder.nativeOrder() );

// create a floating point buffer from the ByteBuffer
vertexBuffer = bb.asFloatBuffer();

// add the coordinates to the FloatBuffer
vertexBuffer.put(triangleCoords);

// set the buffer to read the first coordinate
vertexBuffer.position(0);


        // prepare shaders and OpenGL program
        int vertexShader = MyRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
                                                   vertexShaderCode);
        int fragmentShader = MyRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
                                                     fragmentShaderCode);

        mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
        GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
        GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
        GLES20.glLinkProgram(mProgram);                  // create OpenGL program executables


}


    public void draw() {
        // Add program to OpenGL environment
        GLES20.glUseProgram(mProgram);

        // get handle to vertex shader's vPosition member
        mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

        // Enable a handle to the triangle vertices
        GLES20.glEnableVertexAttribArray(mPositionHandle);

        // Prepare the triangle coordinate data
        GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
                                     GLES20.GL_FLOAT, false,
                                     vertexStride, vertexBuffer);

        // get handle to fragment shader's vColor member
        mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

        // Set color for drawing the triangle
        GLES20.glUniform4fv(mColorHandle, 1, color, 0);

        // Draw the triangle
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

        // Disable vertex array
        GLES20.glDisableVertexAttribArray(mPositionHandle);
    }


}











Hello OpenGL











GLSurfaceView 를 상속받아 View 를 만드는 일.


GLSurfaceView.Renderer 를 상속받아 renderer 를 만드는 일.






--------------------------------------------------------------



import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

private GLSurfaceView mGLView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);       
        
        //setContentView(R.layout.activity_main);
        
        
        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity.
        
        mGLView = new MyGLSurfaceView(this);
        setContentView( mGLView);        
        
    }
    

    @Override
    protected void onPause() {
   
        super.onPause();
        
        // The following call pauses the rendering thread.
        // If your OpenGL application is memory intensive,
        // you should consider de-allocating objects that
        // consume significant memory here.
        mGLView.onPause();
    }
    
    @Override
    protected void onResume() {
   
        super.onResume();
        
        // The following call resumes a paused rendering thread.
        // If you de-allocated graphic objects for onPause()
        // this is a good place to re-allocate them.
        mGLView.onResume();
    }    


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}




--------------------------------------------------------------






import android.content.Context;
import android.opengl.GLSurfaceView;


public class MyGLSurfaceView extends GLSurfaceView {

public MyGLSurfaceView(Context context) {

super(context);
// Create an OpenGL ES 2.0 context
setEGLContextClientVersion(2);

// Set the Renderer for drawing on the GLSurfaceView
setRenderer( new MyRenderer());
// Render the view only when there is a change in the drawing data
setRenderMode( GLSurfaceView.RENDERMODE_WHEN_DIRTY);

}


}


--------------------------------------------------------------



import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

public class MyRenderer implements GLSurfaceView.Renderer {

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Set the background frame color
GLES20.glClearColor( 1.0f, 0.0f, 0.0f, 1.0f);
}
@Override
public void onDrawFrame(GL10 gl) {
// Redraw background color
GLES20.glClear( GLES20.GL_COLOR_BUFFER_BIT );
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {

GLES20.glViewport( 0, 0, width, height);
}


}



















Eclipse hangs at the Android SDK Content Loader

eclipse -clean