Flip Y coordinates on textures on UIShader
This commit is contained in:
		@@ -66,7 +66,7 @@ void UICanvas::addQuad(
 | 
				
			|||||||
  const struct Color color,
 | 
					  const struct Color color,
 | 
				
			||||||
  const std::shared_ptr<Texture> text
 | 
					  const std::shared_ptr<Texture> text
 | 
				
			||||||
) {
 | 
					) {
 | 
				
			||||||
  float_t fTexture = -1;
 | 
					  float_t fTexture;
 | 
				
			||||||
  if(text == nullptr) {
 | 
					  if(text == nullptr) {
 | 
				
			||||||
    fTexture = -1;
 | 
					    fTexture = -1;
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,5 +6,6 @@
 | 
				
			|||||||
# Sources
 | 
					# Sources
 | 
				
			||||||
target_sources(${DAWN_TARGET_NAME}
 | 
					target_sources(${DAWN_TARGET_NAME}
 | 
				
			||||||
  PRIVATE
 | 
					  PRIVATE
 | 
				
			||||||
 | 
					    IShader.cpp
 | 
				
			||||||
    IShaderStage.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;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -76,3 +76,11 @@ 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;
 | 
					  std::vector<struct UIShaderQuad> quads;
 | 
				
			||||||
  if(this->texture == nullptr) return;
 | 
					  if(this->texture == nullptr) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const std::wstring text = L"He";
 | 
					  const std::wstring text = L"Hello World";
 | 
				
			||||||
  glm::vec2 position = t;
 | 
					  glm::vec2 position = t;
 | 
				
			||||||
  glm::vec4 quad;
 | 
					  glm::vec4 quad;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,7 +22,7 @@ using namespace Dawn;
 | 
				
			|||||||
std::shared_ptr<TrueTypeTexture> texture;
 | 
					std::shared_ptr<TrueTypeTexture> texture;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Dawn::helloWorldScene(Scene &s) {
 | 
					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")) {
 | 
					  while(!s.getGame()->assetManager.isLoaded("ysabeau_regular")) {
 | 
				
			||||||
    s.getGame()->assetManager.update();
 | 
					    s.getGame()->assetManager.update();
 | 
				
			||||||
@@ -54,9 +54,14 @@ void Dawn::helloWorldScene(Scene &s) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  // auto rect = std::make_shared<UIRectangle>();
 | 
					  // auto rect = std::make_shared<UIRectangle>();
 | 
				
			||||||
  // rect->position = { -32, -32 };
 | 
					  // rect->position = { -32, -32 };
 | 
				
			||||||
 | 
					  // rect->size = { 8, 8 };
 | 
				
			||||||
  // rect->color = COLOR_MAGENTA;
 | 
					  // rect->color = COLOR_MAGENTA;
 | 
				
			||||||
  // uiCanvas->components.push_back(rect);
 | 
					  // 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>();
 | 
					  auto label = std::make_shared<UILabel>();
 | 
				
			||||||
  label->setFont(texture);
 | 
					  label->setFont(texture);
 | 
				
			||||||
  uiCanvas->components.push_back(label);
 | 
					  uiCanvas->components.push_back(label);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -60,10 +60,9 @@ namespace Dawn {
 | 
				
			|||||||
        getParameters(dummy, this->parameters);
 | 
					        getParameters(dummy, this->parameters);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Update offsets.
 | 
					        // Update offsets.
 | 
				
			||||||
        std::for_each(
 | 
					        auto itParams = this->parameters.begin();
 | 
				
			||||||
          this->parameters.begin(),
 | 
					        while(itParams != this->parameters.end()) {
 | 
				
			||||||
          this->parameters.end(),
 | 
					          struct ShaderParameter ¶m = *itParams;
 | 
				
			||||||
          [&](struct ShaderParameter ¶m) {
 | 
					 | 
				
			||||||
          param.offset -= (size_t)(&dummy);
 | 
					          param.offset -= (size_t)(&dummy);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          // Check for non-aligned OpenGL structures.
 | 
					          // Check for non-aligned OpenGL structures.
 | 
				
			||||||
@@ -75,9 +74,22 @@ namespace Dawn {
 | 
				
			|||||||
              "!\nEnsure you have padded correctly."
 | 
					              "!\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.x = quad.quad.x;\n"
 | 
				
			||||||
            "pos.y = quad.quad.y;\n"
 | 
					            "pos.y = quad.quad.y;\n"
 | 
				
			||||||
            "coord.x = quad.uv.x;\n"
 | 
					            "coord.x = quad.uv.x;\n"
 | 
				
			||||||
            "coord.y = quad.uv.y;\n"
 | 
					            "coord.y = quad.uv.w;\n"
 | 
				
			||||||
          "} else if(vertexIndex == 1) {\n"
 | 
					          "} else if(vertexIndex == 1) {\n"
 | 
				
			||||||
            "pos.x = quad.quad.z;\n"
 | 
					            "pos.x = quad.quad.z;\n"
 | 
				
			||||||
            "pos.y = quad.quad.y;\n"
 | 
					            "pos.y = quad.quad.y;\n"
 | 
				
			||||||
            "coord.x = quad.uv.z;\n"
 | 
					            "coord.x = quad.uv.z;\n"
 | 
				
			||||||
            "coord.y = quad.uv.y;\n"
 | 
					            "coord.y = quad.uv.w;\n"
 | 
				
			||||||
          "} else if(vertexIndex == 2) {\n"
 | 
					          "} else if(vertexIndex == 2) {\n"
 | 
				
			||||||
            "pos.y = quad.quad.w;\n"
 | 
					            "pos.y = quad.quad.w;\n"
 | 
				
			||||||
            "pos.x = quad.quad.x;\n"
 | 
					            "pos.x = quad.quad.x;\n"
 | 
				
			||||||
            "coord.x = quad.uv.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"
 | 
					          "} else if(vertexIndex == 3) {\n"
 | 
				
			||||||
            "pos.x = quad.quad.z;\n"
 | 
					            "pos.x = quad.quad.z;\n"
 | 
				
			||||||
            "pos.y = quad.quad.w;\n"
 | 
					            "pos.y = quad.quad.w;\n"
 | 
				
			||||||
            "coord.x = quad.uv.z;\n"
 | 
					            "coord.x = quad.uv.z;\n"
 | 
				
			||||||
            "coord.y = quad.uv.w;\n"
 | 
					            "coord.y = quad.uv.y;\n"
 | 
				
			||||||
          "}\n"
 | 
					          "}\n"
 | 
				
			||||||
          "pos.z = 0;\n"
 | 
					          "pos.z = 0;\n"
 | 
				
			||||||
          "pos.w = 1;\n"
 | 
					          "pos.w = 1;\n"
 | 
				
			||||||
@@ -88,27 +88,30 @@ void UIShader::getStages(
 | 
				
			|||||||
        "out vec4 o_Color;\n" 
 | 
					        "out vec4 o_Color;\n" 
 | 
				
			||||||
        "void main() {\n"
 | 
					        "void main() {\n"
 | 
				
			||||||
          "vec4 texColor = vec4(1, 1, 1, 1);\n"
 | 
					          "vec4 texColor = vec4(1, 1, 1, 1);\n"
 | 
				
			||||||
          // "switch(int(v_TextureIndex)) {\n"
 | 
					          "int vTextInd = int(round(v_TextureIndex));\n"
 | 
				
			||||||
          //   "case 0:\n"
 | 
					          "switch(vTextInd) {\n"
 | 
				
			||||||
          //     "texColor = texture(u_Texture[0], v_TextCoord);\n"
 | 
					            "case -1:\n"
 | 
				
			||||||
          //     "break;\n"
 | 
					              "texColor = vec4(1, 1, 1, 1);\n"
 | 
				
			||||||
          //   "case 1:\n"
 | 
					              "break;\n"
 | 
				
			||||||
          //     "texColor = texture(u_Texture[1], v_TextCoord);\n"
 | 
					            "case 0:\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"
 | 
					              "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"
 | 
					          "o_Color = texColor * v_Color;\n"
 | 
				
			||||||
        "}\n"
 | 
					        "}\n"
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,6 +15,7 @@ namespace Dawn {
 | 
				
			|||||||
    glm::vec4 uv;
 | 
					    glm::vec4 uv;
 | 
				
			||||||
    struct Color color;
 | 
					    struct Color color;
 | 
				
			||||||
    float_t texture;
 | 
					    float_t texture;
 | 
				
			||||||
 | 
					    glm::vec3 padding;
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  struct UIShaderData {
 | 
					  struct UIShaderData {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user