本文介绍了如何为opengles 2.0实现glOrtho?是否具有glOrtho规范中的tx,ty,tz值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从opengles文档 http:实现我自己的glOtho函数: //www.khronos.org/opengles/documentation/opengles1_0/html/glOrtho.html
在我的顶点着色器中修改投影矩阵.由于我看到简单的三角形顶点不正确,当前它无法正常工作.能否请您检查下面的代码,看看我做错了什么.

Im trying to implement my own glOtho function from the opengles docs http://www.khronos.org/opengles/documentation/opengles1_0/html/glOrtho.html
to modify a Projection matrix in my vertex shader. It's currently not working properly as I see my simple triangles vertices out of place. Can you please check the code below and see if i'm doing things wrong.

我尝试将tx,ty和tz设置为0,这似乎使其能够正确呈现.任何想法为什么会这样吗?

I've tried setting tx,ty and tz to 0 and that seems to make it render properly. Any ideas why would this be so?

void ES2Renderer::_applyOrtho(float left, float right,float bottom, float top,float near, float far) const{ 

        float a = 2.0f / (right - left);
        float b = 2.0f / (top - bottom);
        float c = -2.0f / (far - near);

        float tx = - (right + left)/(right - left);
        float ty = - (top + bottom)/(top - bottom);
        float tz = - (far + near)/(far - near);

        float ortho[16] = {
            a, 0, 0, tx,
            0, b, 0, ty,
            0, 0, c, tz,
            0, 0, 0, 1
        };


        GLint projectionUniform = glGetUniformLocation(_shaderProgram, "Projection");
        glUniformMatrix4fv(projectionUniform, 1, 0, &ortho[0]);

}

void ES2Renderer::_renderScene()const{
    GLfloat vVertices[] = {
        0.0f,  5.0f, 0.0f,  
        -5.0f, -5.0f, 0.0f,
        5.0f, -5.0f,  0.0f};

    GLuint positionAttribute = glGetAttribLocation(_shaderProgram, "Position");

    glEnableVertexAttribArray(positionAttribute);


    glVertexAttribPointer(positionAttribute, 3, GL_FLOAT, GL_FALSE, 0, vVertices);  
    glDrawArrays(GL_TRIANGLES, 0, 3);       


    glDisableVertexAttribArray(positionAttribute);

}

着色器

attribute vec4 Position;
uniform mat4 Projection;
void main(void){
    gl_Position = Projection*Position;
}

解决方案从下面的Nicols答案中,我对矩阵进行了修改,使其看起来正确呈现

SolutionFrom Nicols answer below I modifed my matrix as so and it seemed render properly

float ortho[16] = {
    a, 0, 0, 0,
    0, b, 0, 0,
    0, 0, c, 0,
    tx, ty, tz, 1
};

重要提示您不能使用GL_TRUE为glUniform的转置参数,如下所示. opengles不支持.它必须是GL_FALSE

Important noteYou cannot use GL_TRUE for transpose argument for glUniform as below. opengles does not support it. it must be GL_FALSE

glUniformMatrix4fv(projectionUniform, 1, GL_TRUE, &ortho[0]);

来自 http://www.khronos.org/opengles /sdk/docs/man/xhtml/glUniform.xml

转置指定在将值加载到统一变量中时是否转置矩阵.必须为GL_FALSE.

transposeSpecifies whether to transpose the matrix as the values are loaded into the uniform variable. Must be GL_FALSE.

推荐答案

OpenGL中的矩阵是主要列.除非您将GL_TRUE作为第三个参数传递给glUniformMatrix4fv,否则矩阵将相对于您想要的有效地转置.

Matrices in OpenGL are column major. Unless you pass GL_TRUE as the third parameter to glUniformMatrix4fv, the matrix will effectively be transposed relative to what you would intend.

这篇关于如何为opengles 2.0实现glOrtho?是否具有glOrtho规范中的tx,ty,tz值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 01:41