2016-08-01 2 views
0

enter image description here 장치에서안드로이드는 OpenGL 원 (구) 타원 ..

또한 첨부 된 스크린 샷, 이 유 pls는 ... 안드로이드 킷캣이 문제를 해결 나를 도울 수없는 투사/카메라처럼 장치에서 버전

public class Balloon 
{ 
    private final String vertexShaderCode = 
      // This matrix member variable provides a hook to manipulate 
      // the coordinates of the objects that use this vertex shader 
        "attribute vec4 vPosition;" + 
        "void main() {" + 
        "gl_PointSize = 5.0;     "+ 
        " 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; 
    private int mMVPMatrixHandle; 

    private float[] data = new float[126]; 

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

    private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex 

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

    /** 
    * Sets up the drawing object data for use in an OpenGL ES context. 
    */ 
    public Balloon() { 

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

    } 

    private void FillBalloon(float x1,float y1,float r1) 
    { 
     int i=0,j=0; 
     float angle = 0; 

     float twicePI = (float)2.0 * (float)3.1415926; 


     float angle_stepsize = 0.1f; 

     // go through all angles from 0 to 2 * PI radians 
     for(;angle < twicePI;angle = (angle + angle_stepsize)) 
     { 
      // calculate x, y from a vector with known length and angle 
      data[j++] = x1 + r1 * (float)Math.cos(angle); 
      data[j++] = y1 + r1 * (float)Math.sin(angle); 

     } 

     // initialize vertex byte buffer for shape coordinates 
     ByteBuffer bb = ByteBuffer.allocateDirect(
       // (number of coordinate values * 4 bytes per float) 
       j * 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(data); 
     // set the buffer to read the first coordinate 
     vertexBuffer.position(0); 

    } 


    /** 
    * Encapsulates the OpenGL ES instructions for drawing this shape. 
    * 
    */ 
    public void draw() { 
     // Add program to OpenGL environment 
     GLES20.glUseProgram(mProgram); 

     float posX = 0.0f,posY = 0.0f,radius =0.2f; 

     FillBalloon(posX, posY, radius); 

     // 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 balloon coordinate data 

     GLES20.glVertexAttribPointer(mPositionHandle, 2, GLES20.GL_FLOAT, false, 0, 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); 
     MyGLRenderer.checkGlError("mColorHandle"); 


     GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 63); 

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

또한 첨부 된 스크린 샷은 유 pls는 저 문제를 해결하는 데 도움이 ... 01,235 X 그 규모에 의해 좌표 그런 다음 드로우 시간을 곱

public void draw(float X_SCALE) { 
    ... 
    FillBaloon(posX, posY, radius, X_SCALE); 
    ... 
} 

을 : 16,float 타입을 추가 풍선 동그라미처럼 보이게하기 위해 안드로이드 킷캣 버전

답변

0

그릴 매개 변수로 (의는 X_SCALE를 부르 자) FillBaloon의 요소 (...) :

data[j++] = x1 + X_SCALE * r1 * (float)Math.cos(angle); 

그리고 당신의 렌더러 무승부로 스케일 계수를 설정 (X_SCALE) 전화 :

X_SCALE = (float)MyGLSurfaceView.getHeight()/(float)MyGLSurfaceView.getWidth(); 
myBaloon.draw(X_SCALE); 

onSurfaceCreated (...)에서 X_SCALE을 한 번 설정하고 서페이스가 onSurfaceChanged (...)에서 크기가 조정될 때마다 더 좋습니다.

마지막으로 한가지 : 당신이 명시 적으로 초기화에 "F"접미사를 사용하여 수레로 더블 캐스팅 방지 할 수 있습니다

float twicePI = 6.28318530717959f; 
+0

내 모습이 거꾸로 보입니다. 난 당신이 스케일링 요소로 종횡비의 반대를 사용하려는 생각합니다. 또는이 값을 사용하여 x가 아닌 y의 크기를 조정합니다. –

+0

레토 코라 디 (Reto Koradi), 리차드 클라센 (Richard Klaassen) ...Aspect on X에 대한 Aspect 또는 Y에 대한 Factor의 역함수는 잘 작동합니다 ... 당신은 ppl로 하여금 하루를 보냈습니다 ... 감사합니다. – murty

+0

@RetoKoradi 좋은 콜, 코드의 정의를 수정했습니다. –

1

이것은 예상 된 동작입니다. OpenGL의 기본 좌표계는 모든 축에서 [-1, 1]입니다. 즉, 가장 왼쪽에있는 점은 x=-1이고, 가장 오른쪽에있는 x=1이고 가장 위에있는 것은 y=1이고 가장 아래쪽에있는 것은 y=-1입니다. 이제 화면 비율에 따라 결과에 동일한 비율이 적용됩니다.

원하는대로 모양을 그리는 장면을 처리하는 것은 사용자의 책임입니다. 한 가지 방법은 버텍스 데이터를 생성 할 때 버텍스 데이터의 크기를 조절하는 것입니다. 그러나 모델 버텍스 데이터가 그대로 있어야하므로 버텍스 데이터를 스케일링하는 것이 좋습니다. 또 다른 방법은 단순히 사각형 표면 뷰를 작성하여 드로잉 표면을 제한하는 것이므로 최상의 접근 방식은 아닙니다. 또 다른 방법은 뷰포트를 사용하고 모양을 그리려는 직사각형을 설정하는 것입니다.이 직사각형은 전체 너비와 높이로 다시 설정해야합니다. 그리고 나서 행렬이 있습니다 ...

좌표계를 정의하려면 행렬을 사용하십시오. 2D의 경우 가장 좋은 방법은 보통 테두리 매개 변수 (왼쪽, 위쪽, 아래쪽, 오른쪽)를 사용하는 Ortho을 사용하는 것입니다. 그러면 원하는대로 설정할 수 있습니다. 예를 들어 당신이 뷰의 좌표 (left = 0, right = viewWidth, top = 0, bottom = viewHeight)를 사용할 수 있습니다 또는 제로 주위에 시스템을 정상화하고 1.0의 값이해야하는 국경 선택할 수 있습니다 : 당신이해야합니다 수평 정상화를 위해 (left = -1, right = 1, top = viewHeight/viewWidth, bottom = -viewHeight/viewWidth) 수직 (left = -viewWidth/viewHeight, right = viewWidth/viewHeight, top = 1.0, bottom = -1.0)입니다.