java - 在 Android 中绘制 gles20 圆时的随机线条

标签 java android opengl-es-2.0

我刚开始学习 Android 版 OpenGL,但在画圆时遇到了一个奇怪的问题。它的一些顶点粘在左边和顶壁上,使得线条有点随机地从圆圈中出来。每次我重新启动应用程序时,它们都有不同的位置。

绘制圆的 DrawScreen 类:

public class DrawScreen implements GLSurfaceView.Renderer {

Ball ball;

public float mAngle;

private int mProgram;
private int maPositionHandle;

private final String vertexShaderCode = 
        // This matrix member variable provides a hook to manipulate
        // the coordinates of the objects that use this vertex shader
        "uniform mat4 uMVPMatrix;   \n" +

        "attribute vec4 vPosition;  \n" +
        "void main(){               \n" +

        // the matrix must be included as a modifier of gl_Position
        " gl_Position = uMVPMatrix * vPosition; \n" +

        "}  \n";

private final String fragmentShaderCode = 
        "precision mediump float;  \n" +
                "void main(){              \n" +
                " gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n" +
                "}                         \n";

private int muMVPMatrixHandle;
private float[] mMVPMatrix = new float[16];
private float[] mVMatrix = new float[16];
private float[] mProjMatrix = new float[16];


public void onSurfaceCreated(GL10 unused, EGLConfig config) {

    ball = new Ball();

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

    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    ball.initShapes(240, 360, 50);

    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = 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);                  // creates OpenGL program executables

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

}

public void onDrawFrame(GL10 unused) {

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

    // Add program to OpenGL environment
    GLES20.glUseProgram(mProgram);

    // Prepare the circle data
    GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 0, ball.ballVB);
    GLES20.glEnableVertexAttribArray(maPositionHandle);

    // Apply a ModelView Projection transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
    GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);

    // Draw the circle
    GLES20.glDrawArrays(GLES20.GL_LINE_LOOP, 0, (int) (ball.getNumSeg() * 3));

}

public void onSurfaceChanged(GL10 unused, int width, int height) {
    GLES20.glViewport(0, 0, width, height);

    float ratio = (float) width / height;

    // this projection matrix is applied to object coodinates
    // in the onDrawFrame() method
    Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);

    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);


}

private 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;

}

创建圆的 Ball 类:

public class Ball {

public FloatBuffer ballVB;

private float cx, cy, r;

float numSegments = 360;

public void initShapes(float tx, float ty, float tr){   

    cx = (tx / 240.f) - 1.f;
    cy = (ty / 360.f) - 1.f;
    r =  (tr / 240.f);

    float ballCoords[] = new float[(int) (numSegments * 3)];

    double theta = (2 * 3.1415926 / numSegments); 
    float c = (float) Math.cos(theta);//precalculate the sine and cosine
    float s = (float) Math.sin(theta);
    float t;

    float x = r;//we start at angle = 0 
    float y = 0; 

    for(int i = 0; i < (numSegments * 3); i = i + 3 ) {

        ballCoords[i] = (x + cx);
        ballCoords[i + 1] = (y + cy);
        ballCoords[i + 2] = (0);

        //apply the rotation matrix
        t = x;
        x = c * x - s * y;
        y = s * t + c * y;

    }

    // initialize vertex Buffer for triangle  
    ByteBuffer vbb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            ballCoords.length * 4); 
    vbb.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
    ballVB = vbb.asFloatBuffer();  // create a floating point buffer from the ByteBuffer
    ballVB.put(ballCoords);    // add the coordinates to the FloatBuffer
    ballVB.position(0);            // set the buffer to read the first coordinate

}

public float getNumSeg(){

    return numSegments;

}

我已经在互联网上搜索了几个小时,但没有找到任何东西。希望你们能帮助我。

最佳答案

GLES20.glDrawArrays(GLES20.GL_LINE_LOOP, 0, (int) (ball.getNumSeg() * 3));

我对此表示怀疑,是指单个顶点吗?

glDrawArrays 的最后一个参数是要绘制的顶点 的数量,而不是 float 。您可能应该从 glDrawArrays 中删除 * 3 乘数。

您的额外线条可能来自绘制垃圾数据,因为您绘制的顶点数是实际分配的顶点数的 3 倍。

关于java - 在 Android 中绘制 gles20 圆时的随机线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10250006/

相关文章:

ios - OpenGL ES 2.0/iOS : Skewed drawing of RGBA4 texture

Java 到 Ruby (Base64/SHA1) 公共(public)摘要生成

java - 如何在 Applozic 中通过用户 ID 获取联系人的显示名称?

java - 为 android 自定义选项卡标题中的文本

android - ListView 在添加到 SQLiteDatabase 时不刷新

ios - OpenCV:旋转/平移向量到 OpenGL 模型 View 矩阵

ios - 在基于 cocos2d 的 iOS 游戏中绘制自定义形状

java - Oracle + dbunit 获取 AmbiguousTableNameException

Java 哈希表问题

java - Android java无法删除空元素