Flip Y coordinates on textures on UIShader
This commit is contained in:
		@@ -66,7 +66,7 @@ void UICanvas::addQuad(
 | 
			
		||||
  const struct Color color,
 | 
			
		||||
  const std::shared_ptr<Texture> text
 | 
			
		||||
) {
 | 
			
		||||
  float_t fTexture = -1;
 | 
			
		||||
  float_t fTexture;
 | 
			
		||||
  if(text == nullptr) {
 | 
			
		||||
    fTexture = -1;
 | 
			
		||||
  } else {
 | 
			
		||||
 
 | 
			
		||||
@@ -6,5 +6,6 @@
 | 
			
		||||
# Sources
 | 
			
		||||
target_sources(${DAWN_TARGET_NAME}
 | 
			
		||||
  PRIVATE
 | 
			
		||||
    IShader.cpp
 | 
			
		||||
    IShaderStage.cpp
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										46
									
								
								src/dawn/display/shader/IShader.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								src/dawn/display/shader/IShader.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
// Copyright (c) 2023 Dominic Masters
 | 
			
		||||
// 
 | 
			
		||||
// This software is released under the MIT License.
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#include "display/shader/Shader.hpp"
 | 
			
		||||
#include "assert/assert.hpp"
 | 
			
		||||
#include "display/Color.hpp"
 | 
			
		||||
#include "display/Texture.hpp"
 | 
			
		||||
 | 
			
		||||
using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
size_t shaderParameterTypeGetSize(const enum ShaderParameterType type) {
 | 
			
		||||
  switch(type) {
 | 
			
		||||
    case ShaderParameterType::VEC2:
 | 
			
		||||
      return sizeof(glm::vec2);
 | 
			
		||||
 | 
			
		||||
    case ShaderParameterType::VEC3:
 | 
			
		||||
      return sizeof(glm::vec3);
 | 
			
		||||
 | 
			
		||||
    case ShaderParameterType::VEC4:
 | 
			
		||||
      return sizeof(glm::vec4);
 | 
			
		||||
 | 
			
		||||
    case ShaderParameterType::MAT3:
 | 
			
		||||
      return sizeof(glm::mat3);
 | 
			
		||||
 | 
			
		||||
    case ShaderParameterType::MAT4:
 | 
			
		||||
      return sizeof(glm::mat4);
 | 
			
		||||
 | 
			
		||||
    case ShaderParameterType::COLOR:
 | 
			
		||||
      return sizeof(struct Color);
 | 
			
		||||
 | 
			
		||||
    case ShaderParameterType::FLOAT:
 | 
			
		||||
      return sizeof(float);
 | 
			
		||||
 | 
			
		||||
    case ShaderParameterType::INT:
 | 
			
		||||
      return sizeof(int32_t);
 | 
			
		||||
 | 
			
		||||
    case ShaderParameterType::TEXTURE:
 | 
			
		||||
      return sizeof(shadertexturebinding_t);
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
      assertUnreachable("Unknown ShaderParameterType");
 | 
			
		||||
      return 0;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -75,4 +75,12 @@ namespace Dawn {
 | 
			
		||||
        
 | 
			
		||||
      }
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Returns the size of the ShaderParameterType.
 | 
			
		||||
 * 
 | 
			
		||||
 * @param type The type to get the size of.
 | 
			
		||||
 * @return Size of the type.
 | 
			
		||||
 */
 | 
			
		||||
size_t shaderParameterTypeGetSize(const enum Dawn::ShaderParameterType type);
 | 
			
		||||
@@ -11,7 +11,7 @@ void UILabel::getSelfQuads(const glm::vec2 t, UICanvas &ctx) {
 | 
			
		||||
  std::vector<struct UIShaderQuad> quads;
 | 
			
		||||
  if(this->texture == nullptr) return;
 | 
			
		||||
 | 
			
		||||
  const std::wstring text = L"He";
 | 
			
		||||
  const std::wstring text = L"Hello World";
 | 
			
		||||
  glm::vec2 position = t;
 | 
			
		||||
  glm::vec4 quad;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -22,7 +22,7 @@ using namespace Dawn;
 | 
			
		||||
std::shared_ptr<TrueTypeTexture> texture;
 | 
			
		||||
 | 
			
		||||
void Dawn::helloWorldScene(Scene &s) {
 | 
			
		||||
  texture = s.getGame()->assetManager.get<TrueTypeTexture>("ysabeau_regular", 16);
 | 
			
		||||
  texture = s.getGame()->assetManager.get<TrueTypeTexture>("ysabeau_regular", 32);
 | 
			
		||||
 | 
			
		||||
  while(!s.getGame()->assetManager.isLoaded("ysabeau_regular")) {
 | 
			
		||||
    s.getGame()->assetManager.update();
 | 
			
		||||
@@ -54,9 +54,14 @@ void Dawn::helloWorldScene(Scene &s) {
 | 
			
		||||
 | 
			
		||||
  // auto rect = std::make_shared<UIRectangle>();
 | 
			
		||||
  // rect->position = { -32, -32 };
 | 
			
		||||
  // rect->size = { 8, 8 };
 | 
			
		||||
  // rect->color = COLOR_MAGENTA;
 | 
			
		||||
  // uiCanvas->components.push_back(rect);
 | 
			
		||||
 | 
			
		||||
  // auto rect2 = std::make_shared<UIRectangle>();
 | 
			
		||||
  // rect2->color = COLOR_BLUE;
 | 
			
		||||
  // uiCanvas->components.push_back(rect2);
 | 
			
		||||
 | 
			
		||||
  auto label = std::make_shared<UILabel>();
 | 
			
		||||
  label->setFont(texture);
 | 
			
		||||
  uiCanvas->components.push_back(label);
 | 
			
		||||
 
 | 
			
		||||
@@ -60,24 +60,36 @@ namespace Dawn {
 | 
			
		||||
        getParameters(dummy, this->parameters);
 | 
			
		||||
 | 
			
		||||
        // Update offsets.
 | 
			
		||||
        std::for_each(
 | 
			
		||||
          this->parameters.begin(),
 | 
			
		||||
          this->parameters.end(),
 | 
			
		||||
          [&](struct ShaderParameter ¶m) {
 | 
			
		||||
            param.offset -= (size_t)(&dummy);
 | 
			
		||||
        auto itParams = this->parameters.begin();
 | 
			
		||||
        while(itParams != this->parameters.end()) {
 | 
			
		||||
          struct ShaderParameter ¶m = *itParams;
 | 
			
		||||
          param.offset -= (size_t)(&dummy);
 | 
			
		||||
 | 
			
		||||
            // Check for non-aligned OpenGL structures.
 | 
			
		||||
            if(param.offset % sizeof(glm::vec4) != 0) {
 | 
			
		||||
              assertUnreachable(
 | 
			
		||||
                "%s%s%s",
 | 
			
		||||
                "Non-aligned OpenGL structure detected on param ",
 | 
			
		||||
                param.name.c_str(),
 | 
			
		||||
                "!\nEnsure you have padded correctly."
 | 
			
		||||
              );
 | 
			
		||||
            }
 | 
			
		||||
          // Check for non-aligned OpenGL structures.
 | 
			
		||||
          if(param.offset % sizeof(glm::vec4) != 0) {
 | 
			
		||||
            assertUnreachable(
 | 
			
		||||
              "%s%s%s",
 | 
			
		||||
              "Non-aligned OpenGL structure detected on param ",
 | 
			
		||||
              param.name.c_str(),
 | 
			
		||||
              "!\nEnsure you have padded correctly."
 | 
			
		||||
            );
 | 
			
		||||
          }
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
          if(
 | 
			
		||||
            itParams == (this->parameters.end() - 1) &&
 | 
			
		||||
            count > 1 &&
 | 
			
		||||
            (sizeof(T) % sizeof(glm::vec4)) != 0
 | 
			
		||||
          ) {
 | 
			
		||||
            assertUnreachable(
 | 
			
		||||
              "%s%s%s",
 | 
			
		||||
              "Non-aligned OpenGL structure detected on last element in array structure on param ",
 | 
			
		||||
              param.name.c_str(),
 | 
			
		||||
              "!\nEnsure you have padded correctly."
 | 
			
		||||
            );
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          ++itParams;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
@@ -52,22 +52,22 @@ void UIShader::getStages(
 | 
			
		||||
            "pos.x = quad.quad.x;\n"
 | 
			
		||||
            "pos.y = quad.quad.y;\n"
 | 
			
		||||
            "coord.x = quad.uv.x;\n"
 | 
			
		||||
            "coord.y = quad.uv.y;\n"
 | 
			
		||||
            "coord.y = quad.uv.w;\n"
 | 
			
		||||
          "} else if(vertexIndex == 1) {\n"
 | 
			
		||||
            "pos.x = quad.quad.z;\n"
 | 
			
		||||
            "pos.y = quad.quad.y;\n"
 | 
			
		||||
            "coord.x = quad.uv.z;\n"
 | 
			
		||||
            "coord.y = quad.uv.y;\n"
 | 
			
		||||
            "coord.y = quad.uv.w;\n"
 | 
			
		||||
          "} else if(vertexIndex == 2) {\n"
 | 
			
		||||
            "pos.y = quad.quad.w;\n"
 | 
			
		||||
            "pos.x = quad.quad.x;\n"
 | 
			
		||||
            "coord.x = quad.uv.x;\n"
 | 
			
		||||
            "coord.y = quad.uv.w;\n"
 | 
			
		||||
            "coord.y = quad.uv.y;\n"
 | 
			
		||||
          "} else if(vertexIndex == 3) {\n"
 | 
			
		||||
            "pos.x = quad.quad.z;\n"
 | 
			
		||||
            "pos.y = quad.quad.w;\n"
 | 
			
		||||
            "coord.x = quad.uv.z;\n"
 | 
			
		||||
            "coord.y = quad.uv.w;\n"
 | 
			
		||||
            "coord.y = quad.uv.y;\n"
 | 
			
		||||
          "}\n"
 | 
			
		||||
          "pos.z = 0;\n"
 | 
			
		||||
          "pos.w = 1;\n"
 | 
			
		||||
@@ -88,27 +88,30 @@ void UIShader::getStages(
 | 
			
		||||
        "out vec4 o_Color;\n" 
 | 
			
		||||
        "void main() {\n"
 | 
			
		||||
          "vec4 texColor = vec4(1, 1, 1, 1);\n"
 | 
			
		||||
          // "switch(int(v_TextureIndex)) {\n"
 | 
			
		||||
          //   "case 0:\n"
 | 
			
		||||
          //     "texColor = texture(u_Texture[0], v_TextCoord);\n"
 | 
			
		||||
          //     "break;\n"
 | 
			
		||||
          //   "case 1:\n"
 | 
			
		||||
          //     "texColor = texture(u_Texture[1], v_TextCoord);\n"
 | 
			
		||||
          //     "break;\n"
 | 
			
		||||
          //   "case 2:\n"
 | 
			
		||||
          //     "texColor = texture(u_Texture[2], v_TextCoord);\n"
 | 
			
		||||
          //     "break;\n"
 | 
			
		||||
          //   "case 3:\n"
 | 
			
		||||
          //     "texColor = texture(u_Texture[3], v_TextCoord);\n"
 | 
			
		||||
          //     "break;\n"
 | 
			
		||||
          //   "case 4:\n"
 | 
			
		||||
          //     "texColor = texture(u_Texture[4], v_TextCoord);\n"
 | 
			
		||||
          //     "break;\n"
 | 
			
		||||
          //   "case 5:\n"
 | 
			
		||||
          //     "texColor = texture(u_Texture[5], v_TextCoord);\n"
 | 
			
		||||
          //     "break;\n"
 | 
			
		||||
          // "}\n"
 | 
			
		||||
          "texColor = texture(u_Texture[0], v_TextCoord);\n"
 | 
			
		||||
          "int vTextInd = int(round(v_TextureIndex));\n"
 | 
			
		||||
          "switch(vTextInd) {\n"
 | 
			
		||||
            "case -1:\n"
 | 
			
		||||
              "texColor = vec4(1, 1, 1, 1);\n"
 | 
			
		||||
              "break;\n"
 | 
			
		||||
            "case 0:\n"
 | 
			
		||||
              "texColor = texture(u_Texture[0], v_TextCoord);\n"
 | 
			
		||||
              "break;\n"
 | 
			
		||||
            "case 1:\n"
 | 
			
		||||
              "texColor = texture(u_Texture[1], v_TextCoord);\n"
 | 
			
		||||
              "break;\n"
 | 
			
		||||
            "case 2:\n"
 | 
			
		||||
              "texColor = texture(u_Texture[2], v_TextCoord);\n"
 | 
			
		||||
              "break;\n"
 | 
			
		||||
            "case 3:\n"
 | 
			
		||||
              "texColor = texture(u_Texture[3], v_TextCoord);\n"
 | 
			
		||||
              "break;\n"
 | 
			
		||||
            "case 4:\n"
 | 
			
		||||
              "texColor = texture(u_Texture[4], v_TextCoord);\n"
 | 
			
		||||
              "break;\n"
 | 
			
		||||
            "case 5:\n"
 | 
			
		||||
              "texColor = texture(u_Texture[5], v_TextCoord);\n"
 | 
			
		||||
              "break;\n"
 | 
			
		||||
          "}\n"
 | 
			
		||||
          "o_Color = texColor * v_Color;\n"
 | 
			
		||||
        "}\n"
 | 
			
		||||
      );
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,7 @@ namespace Dawn {
 | 
			
		||||
    glm::vec4 uv;
 | 
			
		||||
    struct Color color;
 | 
			
		||||
    float_t texture;
 | 
			
		||||
    glm::vec3 padding;
 | 
			
		||||
  };
 | 
			
		||||
  
 | 
			
		||||
  struct UIShaderData {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user