java - OpenGL形状绘图绕y轴反转?

标签 java android opengl

这很尴尬,

我正在尝试遵循 google 中的简单指南使用 openGL 进行绘制。现在,我稍微修改了那里的代码,看看发生了什么以及发生了什么,这立即对我造成了“错误”。该代码确实工作正常并且显示良好 - 但并不完全是我期望的显示效果。

我试图用以下坐标绘制一个三角形 (0, 0.75, 0) (-0.5, 0, 0) (0, 0, 0)。这应该是一个具有垂直长边和短水平边的三角形。对角线应该从左下角到“右上”。

如上所述:我看到一个三角形:但是第二个坐标似乎绕 y 轴“翻转”:负 x 值真的“向右”吗?

渲染器代码:

public class MyGLRenderer implements GLSurfaceView.Renderer {
    private Triangle mTriangle;
    // mMVPMatrix is an abbreviation for "Model View Projection Matrix"
    private final float[] mMVPMatrix = new float[16];
    private final float[] mProjectionMatrix = new float[16];
    private final float[] mViewMatrix = new float[16];    

    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        // Set the background frame color
        GLES20.glClearColor(0f, 0f, 0f, 1.0f);
        // initialize a triangle
        float triangleCoords[] = {
                0.0f,  0.75f, 0.0f, // top
                -0.5f, 0f, 0.0f, // bottom left
                0.0f, 0.0f, 0.0f  // bottom right
        };
        mTriangle = new Triangle(triangleCoords);
    }

    public void onDrawFrame(GL10 unused) {
        // Redraw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

        // Set the camera position (View matrix)
        Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

        // Calculate the projection and view transformation
        Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);

        // Draw shape
        mTriangle.draw(mMVPMatrix);
    }


    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 coordinates
        // in the onDrawFrame() method
        Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 2.99f, 7);
    }

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

为了完整起见,Triangle 类:

public class Triangle {

    private FloatBuffer vertexBuffer;
    // number of coordinates per vertex in this array
    private static final int COORDS_PER_VERTEX = 3;
    private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex

    private int vertexCount;


    // Set color with red, green, blue and alpha (opacity) values
    private static final float colorDefault[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };

    public Triangle(float[] sizes) {

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

        int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
                vertexShaderCode);
        int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
                fragmentShaderCode);

        // create empty OpenGL ES Program
        mProgram = GLES20.glCreateProgram();

        // add the vertex shader to program
        GLES20.glAttachShader(mProgram, vertexShader);

        // add the fragment shader to program
        GLES20.glAttachShader(mProgram, fragmentShader);

        // creates OpenGL ES program executables
        GLES20.glLinkProgram(mProgram);
    }




    private final int mProgram;
    private final String vertexShaderCode =
            "uniform mat4 uMVPMatrix;" +
            "attribute vec4 vSizes;" +
                    "void main() {" +
                    "  gl_Position = uMVPMatrix * vSizes;" +
                    "}";

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




    public void draw(float[] mvpMatrix) {
        int mSizesHandle;
        int mColorHandle;
        int mMVPMatrixHandle;
        // Add program to OpenGL ES environment
        GLES20.glUseProgram(mProgram);

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

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

        // Prepare the triangle coordinate data
        GLES20.glVertexAttribPointer(mSizesHandle, 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, colorDefault, 0);


        // get handle to shape's transformation matrix
        mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

        // Pass the projection and view transformation to the shader
        GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);


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

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

}

最佳答案

根据你的矩阵代码,看起来你正在构建一个从 <0,0,0> 观察 <-3,0,0> 的 View 矩阵,并且考虑到你的三角形平放在 x-y 平面上,实际上你能看到任何东西都是相当奇迹的。因此,您需要修改构建观看矩阵的方式。我还建议您按顶点为三角形着色,以便更容易知道变换后三角形的顶点最终在哪里。

关于java - OpenGL形状绘图绕y轴反转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32334415/

相关文章:

Java泛型在集合及其子类型中扩展和 super 实现

java - Android RecyclerView 无法呈现 SurfaceView

c++ - glBufferData 带有一个顶点数组,每个顶点有 3 个 float (x、y 和 z)

opengl - 如何使用 GPU 在 Google Compute Engine 上运行 Gazebo?

c++ - OpenGL 的 GLM 库没有幅度函数吗?

java - 如何使用 XML 输入变量以根据 XSD 进行验证

java - 如何在 Moshi 中将 PolymorphicJsonAdapterFactory 与接口(interface)一起使用?

java - 帮助使用正则表达式

android - Lollipop Activity 共享元素过渡与淡入淡出

java - 如何在谷歌地图上的 InfoWindow 上实现 onclicklistener