Hazel Game Engine (067) Texture Sheet Preliminary Study

If there are errors in the code, terminology, etc. in the text, please correct me

foreword

  • Why do Texture Sheet Atlas

    Since OpenGL has only 32 texture slots for one rendering

    If the number of textures rendered at one time exceeds 32, it needs to be rendered several times (need to unbind and then bind) which will cause some trouble

    So you need a texture Sheet

  • material website

    https://kenney.nl/

A Preliminary Study of Texture Sets

Analyze a texture set

Please add a picture description

The size of the texture is 2560*1664

The texture coordinates of the brown grass pot framed by the red frame are deduced from the above picture

  1. The texture coordinates of the lower left corner are, {128*2/ 2560 , 128 * 3 / 1664}

  2. The texture coordinates of the lower right corner are, {128*(2**+1**)/2560, 128*3/1664}

  3. The texture coordinates of the upper right corner are, {128*(2**+1**)/2560 , 128 * (3**+1**) / 1664}

  4. The texture coordinates of the upper left corner are, {128*2/ 2560 , 128 * (3**+1**) / 1664}

Code: Rendering a small portion of a texture set

void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref<Texture2D>& texture, float tilingFactor, const glm::vec4& tintColor)
{
    
    
    HZ_PROFILE_FUNCTION();
	
    // 对应上图红框框住的棕色草丛罐的 x y 值
    // 测试纹理集代码
    constexpr float x = 2, y = 3;
    constexpr float sheetWidth = 2560.0f, sheetHeight = 1664.0f;
    constexpr float spriteWidth = 128.0f, spriteHeight = 128.0f;

    constexpr size_t quadVertexCount = 4;
    /
    // 由前分析推导的定位纹理坐标
    constexpr glm::vec2 textureCoords[] = {
    
    
        {
    
     x * spriteWidth / sheetWidth, y * spriteHeight / sheetHeight},
        {
    
     (x + 1) * spriteWidth / sheetWidth, y * spriteHeight / sheetHeight},
        {
    
     (x + 1) * spriteWidth / sheetWidth, (y + 1) * spriteHeight / sheetHeight},
        {
    
     x * spriteWidth / sheetWidth, (y + 1) * spriteHeight / sheetHeight},
    };

    if (s_Data.QuadIndexCount >= Renderer2DData::MaxIndices) {
    
    
        FlushAndReset();
    }

    constexpr glm::vec4 color = {
    
     1.0f, 1.0f, 1.0f, 1.0f };

    float textureIndex = 0.0f;
    for (uint32_t i = 1; i < s_Data.TextureSlotIndex; i++)
    {
    
    
        // 当前纹理,如果已经存储在纹理槽,就直接读取
        if (*s_Data.TextureSlots[i].get() == *texture.get()) {
    
    
            textureIndex = (float)i;
            break;
        }
    }
    if (textureIndex == 0.0f) {
    
    
        textureIndex = (float)s_Data.TextureSlotIndex;
        //
        // 将加载的纹理集放到纹理数组中
        s_Data.TextureSlots[s_Data.TextureSlotIndex] = texture;
        s_Data.TextureSlotIndex++;// 记得++
    }
    // 设置transform
    glm::mat4 tranform = glm::translate(glm::mat4(1.0f), position) *
        glm::scale(glm::mat4(1.0f), {
    
     size.x, size.y, 1.0f });

    // quad的左下角为起点
    s_Data.QuadVertexBufferPtr->Position = tranform * s_Data.QuadVertexPosition[0];
    s_Data.QuadVertexBufferPtr->Color = color;
    s_Data.QuadVertexBufferPtr->TexCoord = textureCoords[0];
    s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
    s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
    s_Data.QuadVertexBufferPtr++;

    s_Data.QuadVertexBufferPtr->Position = tranform * s_Data.QuadVertexPosition[1];
    s_Data.QuadVertexBufferPtr->Color = color;
    s_Data.QuadVertexBufferPtr->TexCoord = textureCoords[1];
    s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
    s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
    s_Data.QuadVertexBufferPtr++;

    s_Data.QuadVertexBufferPtr->Position = tranform * s_Data.QuadVertexPosition[2];
    s_Data.QuadVertexBufferPtr->Color = color;
    s_Data.QuadVertexBufferPtr->TexCoord = textureCoords[2];
    s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
    s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
    s_Data.QuadVertexBufferPtr++;

    s_Data.QuadVertexBufferPtr->Position = tranform * s_Data.QuadVertexPosition[3];
    s_Data.QuadVertexBufferPtr->Color = color;
    s_Data.QuadVertexBufferPtr->TexCoord = textureCoords[3];
    s_Data.QuadVertexBufferPtr->TexIndex = textureIndex;
    s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor;
    s_Data.QuadVertexBufferPtr++;

    s_Data.QuadIndexCount += 6;// 每一个quad用6个索引

    s_Data.Stats.QuadCount++;
}

Effect

The corresponding loaded texture atlas

Guess you like

Origin blog.csdn.net/qq_34060370/article/details/132013311