0

我是 OpenGL 的新手,我只想知道如何将 glDrawElements (x,y,z) 设置为相对于 Android 中的视口?

我一直在使用这个示例来使用 glDrawElements 绘制我的形状。它看起来像这样:

float textureCoordinates[] = {
                0.0f, 1.0f, //
                1.0f, 1.0f, //
                0.0f, 0.0f, //
                1.0f, 0.0f, //
        };

        short[] indices = new short[]{0, 1, 2, 1, 3, 2};

        float[] vertices = new float[]{
                -1f, -1f, 0.0f,
                1f, -1f, 0.0f,
                -1f, 1f, 0.0f,
                1f, 1f, 0.0f};

平局看起来像这样:

// Counter-clockwise winding.
        gl.glFrontFace(GL10.GL_CCW);
        // Enable face culling.
        gl.glEnable(GL10.GL_CULL_FACE);
        // What faces to remove with the face culling.
        gl.glCullFace(GL10.GL_BACK);
        // Enabled the vertices buffer for writing and to be used during
        // rendering.
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        // Specifies the location and data format of an array of vertex
        // coordinates to use when rendering.
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVerticesBuffer);
        // Set flat color
        gl.glColor4f(mRGBA[0], mRGBA[1], mRGBA[2], mRGBA[3]);
        // Smooth color
        if (mColorBuffer != null) {
            // Enable the color array buffer to be used during rendering.
            gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
            gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
        }

        // New part...
        if (mShouldLoadTexture) {
            loadGLTexture(gl);
            mShouldLoadTexture = false;
        }
        if (mTextureId != -1 && mTextureBuffer != null) {
            gl.glEnable(GL10.GL_TEXTURE_2D);
            // Enable the texture state
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

            // Point to our buffers
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
            gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
        }
        // ... end new part.

        gl.glTranslatef(x, y, z);
        gl.glRotatef(rx, 1, 0, 0);
        gl.glRotatef(ry, 0, 1, 0);
        gl.glRotatef(rz, 0, 0, 1);

        // Point out the where the color buffer is.
        gl.glDrawElements(GL10.GL_TRIANGLES, mNumOfIndices,
                GL10.GL_UNSIGNED_SHORT, mIndicesBuffer);
        // Disable the vertices buffer.
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

        // New part...
        if (mTextureId != -1 && mTextureBuffer != null) {
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        }
        // ... end new part.

        // Disable face culling.
        gl.glDisable(GL10.GL_CULL_FACE);

在我的情况下 x = y = z = 0

这就是我在表面上所做的改变:

public void onSurfaceChanged(GL10 gl, int width, int height) {
        // Sets the current view port to the new size.
        gl.glViewport(0, 0, width, height);
        // Select the projection matrix
        gl.glMatrixMode(GL10.GL_PROJECTION);
        // Reset the projection matrix
        gl.glLoadIdentity();
        // Calculate the aspect ratio of the window
        GLU.gluPerspective(gl, 45, (float) width / (float) height, -1f, 1f);
        // Select the modelview matrix
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        // Reset the modelview matrix
        gl.glLoadIdentity();
    }

这会在屏幕中间绘制我的纹理(就像在示例中一样)但是我如何使用我的视口控制它,并设置相对于我的视口的 x、y、z?

编辑:我想做的只是设置我的屏幕尺寸,并在给定的坐标(x,y,z)中绘制二维图像。

4

1 回答 1

0

如果您只是在绘制纹理,我建议您放弃透视投影并只做一个简单的正交矩阵。

用类似的东西替换 gluPerspective

glOrtho(0, screen_width, 0, screen_height, -1, 1);

然后您可以在像素位置绘制顶点,即全屏纹理:

float w = screen_width;
float h = screen_height;
float[] vertices = new float[]{
                0, 0,
                w, 0,
                0, h,
                w, h,
}
于 2012-05-21T21:00:17.037 回答