如何正确转换int使其在glsl 4.5中浮动?

时间:2019-04-16 03:24:39

标签: opengl glsl

我将所有顶点数据打包为一个int并将其传递到着色器中。在渲染阶段,我需要将int类型转换为float类型。

  1. 这样的转换是否会遭受性能损失?

  2. glsl4.5是否支持'&'运算符?

  3. 如何正确投射

代码:

layout(location = 0) in highp int vPackage;
//extract

float int2_to_float(int pos)
{
    return pos==3?-1.:float(pos);
}

vec4 vPosition = vec4(
    float( vPackage>>27),        
    float((vPackage>>22)&0x1F),
    float((vPackage>>17)&0x1F),
    1.
    );

vec3 vNormal   = vec3(
    int2_to_float((vPackage>>15)&0x03),
    int2_to_float((vPackage>>13)&0x03),
    int2_to_float((vPackage>>11)&0x03)
    );

vec2 vTexCoord = vec2(
    float((vPackage>> 6)&0x1F),
    float((vPackage>> 1)&0x1F)
    );

网格生成器代码:

 void CmeshBuilder::addFace(Smesh* mesh, int& id, int&& face,int (chunkX)[3],int& index)
{
    for (int i = 0; i < 4; ++i)
    {
        //positon xyz
        (mesh->pvertStore)[index]  = (((total_facesi[face][i][0] + (chunkX[0])) & 0x1F) << 27);
        (mesh->pvertStore)[index] |= (((total_facesi[face][i][1] + (chunkX[1])) & 0x1F) << 22);
        (mesh->pvertStore)[index] |= (((total_facesi[face][i][2] + (chunkX[2])) & 0x1F) << 17);

        //normal xyzs
        (mesh->pvertStore)[index] |= ((total_facesi[face][i][3] & 0x03) << 15);
        (mesh->pvertStore)[index] |= ((total_facesi[face][i][4] & 0x03) << 13);
        (mesh->pvertStore)[index] |= ((total_facesi[face][i][5] & 0x03) << 11);

        //texture xy
        (mesh->pvertStore)[index] |= (((coord_datai[(i << 1) + (blockTid[id][face] << 3)]) & 0x1F) << 6);
        (mesh->pvertStore)[index] |= (((coord_datai[(i << 1) + (blockTid[id][face] << 3) + 1]) & 0x1F) << 1);
        //int a = ((coord_datai[(i << 1) + (blockTid[id][face] << 3)]) & 0x1F);
        //shade
        ++index;
    }
}

vao属性格式:

glGenVertexArrays(1, &this->vao);
this->bindVAO();
glVertexAttribFormat(0, 1, GL_INT, GL_FALSE, 0);
glVertexAttribBinding(0, 0);
glEnableVertexAttribArray(0);
this->unbindVAO();

vbo绑定:

glBindVertexBuffer(0, this->VBO[0], 0, sizeof(int));

0 个答案:

没有答案