首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将glDrawElements (x,y,z)设置为相对于视图端口。安卓

如何将glDrawElements (x,y,z)设置为相对于视图端口。安卓
EN

Stack Overflow用户
提问于 2012-05-21 14:30:04
回答 1查看 685关注 0票数 0

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

我用这个示例来画glDrawElements的形状。看起来是这样的:

代码语言:javascript
复制
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};

抽签结果是这样的:

代码语言:javascript
复制
// 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

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

代码语言:javascript
复制
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)中绘制2D图像。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-21 21:00:17

如果你只是画一个纹理,我建议放弃透视投影,只做一个简单的正交矩阵。

将gluPerspective替换为类似的

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

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

代码语言:javascript
复制
float w = screen_width;
float h = screen_height;
float[] vertices = new float[]{
                0, 0,
                w, 0,
                0, h,
                w, h,
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10687216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档