Skip to content

Commit

Permalink
fixed that annoying ass texture bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ZEArgos committed Jun 26, 2024
1 parent bd30a40 commit a0b0563
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Source/Assets/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.143
0.2.1
13 changes: 8 additions & 5 deletions Source/Modules/Renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ CreateRenderer(f32 swidth, f32 sheight, const char* caller)
glm_ortho(0.0f, swidth, sheight, 0.0f, 0.0f, 1000.0f, projection);

// Slide the projection matrix into the shader.
UseShader(GetNodeContents(basic_shader, Shader));
SetMat4(GetNodeContents(basic_shader, Shader), "projection", projection);
UseShader(*GetNodeContents(basic_shader, Shader));
SetMat4(*GetNodeContents(basic_shader, Shader), "projection", projection);

PrintSuccess("Successfully set up the projection matrix on shader '%s'.",
basic_shader->name);
Expand All @@ -66,17 +66,20 @@ void RenderWindowContent(Renderer* renderer)
// Get the basic shader, the one we use to render plain textures, and slot
// it as our current one.
u32 basic_shader =
GetNodeContents(GetRendererHead(renderer, Shader), Shader);
*GetNodeContents(GetRendererHead(renderer, Shader), Shader);
UseShader(basic_shader);

// Bind the "missing" texture to render as a placeholder.
BindTexture(&GetNodeContents(GetRendererHead(renderer, Texture), Texture));
Texture* missing_texture =
GetNodeContents(GetRendererHead(renderer, Texture), Texture);
BindTexture(missing_texture);

// Position transform the bound texture so it's within our viewport.
mat4 model = GLM_MAT4_IDENTITY_INIT;
glm_translate(model, (vec3){0.0f, 0.0f, 0.0f});
SetMat4(basic_shader, "model", model);

// Draw the texture.
glDrawArrays(GL_TRIANGLES, 0, 6);
// glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
9 changes: 6 additions & 3 deletions Source/Types/LinkedList.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "LinkedList.h"
#include <Logger.h>

Node* __CreateNode(NodeType type, const char* name, u32 shader, Texture texture)
Node* __CreateNode(NodeType type, const char* name, u32 shader,
Texture* texture)
{
Node* created_node = malloc(sizeof(struct Node));
created_node->next = NULL;
Expand Down Expand Up @@ -30,6 +31,8 @@ void DestroyLinkedList(LinkedList* list)
while (current_node != NULL)
{
Node* next_node = current_node->next;
if (current_node->type == texture)
KillTexture(current_node->contents.texture);
free(current_node);
current_node = next_node;
}
Expand All @@ -40,8 +43,8 @@ void DestroyLinkedList(LinkedList* list)
__BOOLEAN VerifyNodeContents(NodeType type, NodeContents* contents)
{
if (type == shader && contents->shader != 0) return true;
else if (type == texture && contents->texture.inner != 0 &&
contents->texture.vao != 0)
else if (type == texture && contents->texture->texture != 0 &&
contents->texture->vao != 0)
return true;

return false;
Expand Down
10 changes: 5 additions & 5 deletions Source/Types/LinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef union NodeContents
/**
* @brief A texture image and all the information associated with it.
*/
Texture texture;
Texture* texture;
} NodeContents;

typedef struct Node
Expand All @@ -68,10 +68,10 @@ typedef struct LinkedList
Node* last_node;
} LinkedList;

#define GetNodeContents(node, type) (*Get##type##Contents(node))
#define GetNodeContents(node, type) Get##type##Contents(node)
__INLINE Texture* GetTextureContents(Node* node)
{
return &node->contents.texture;
return node->contents.texture;
}
__INLINE u32* GetShaderContents(Node* node) { return &node->contents.shader; }

Expand All @@ -87,13 +87,13 @@ __INLINE u32* GetShaderContents(Node* node) { return &node->contents.shader; }
// #define GetTextureListHead(list) list->list_head_texture

#define CreateShaderNode(type, name) \
__CreateNode(type, name, LoadShader(name, __func__), TEXTURE_EMPTY_INIT)
__CreateNode(type, name, LoadShader(name, __func__), NULL)
#define CreateTextureNode(type, name, swidth, sheight) \
__CreateNode(type, name, 0, LoadTextureFromFile(name, swidth, sheight))

// stupid fucking solution
Node* __CreateNode(NodeType type, const char* name, u32 shader,
Texture texture);
Texture* texture);

//??!!!! brother why am i take both type and node?? type is stored in node?? fix
LinkedList* CreateLinkedList(NodeType type, Node* head);
Expand Down
51 changes: 31 additions & 20 deletions Source/Types/Texture.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@
#include <cglm/cglm.h>
#include <stbi/stb_image.h>

Texture LoadTextureFromFile(const char* name, f32 swidth, f32 sheight)
#define __SQUARE_VAO(width, height) \
{ \
width, 0.0f, 0.0f, 1.0f, 1.0f, /* top right */ \
width, height, 0.0f, 1.0f, 0.0f, /* bottom right */ \
0.0f, height, 0.0f, 0.0f, 0.0f, /* bottom left */ \
0.0f, 0.0f, 0.0f, 0.0f, 1.0f /* top left */ \
};
#define __SQUARE_INDICES {0, 1, 3, 1, 2, 3};

Texture* LoadTextureFromFile(const char* name, f32 swidth, f32 sheight)
{
u32 texture_identifier;
glGenTextures(1, &texture_identifier);
glBindTexture(GL_TEXTURE_2D, texture_identifier);
Texture* texture = malloc(sizeof(struct Texture));
texture->name = name;

glGenTextures(1, &texture->texture);
glBindTexture(GL_TEXTURE_2D, texture->texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Expand All @@ -34,26 +45,27 @@ Texture LoadTextureFromFile(const char* name, f32 swidth, f32 sheight)

stbi_image_free(data);

f32 vao_width = (swidth / image_width) * 4,
vao_height = (sheight / image_height) * 4;
float vertices[] = {
// positions // texture coords
0.0f, vao_height, 0.0f, 0.0f, 1.0f, vao_width, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
vao_height, 0.0f, 0.0f, 1.0f, vao_width, vao_height, 0.0f, 1.0f,
1.0f, vao_width, 0.0f, 0.0f, 1.0f, 0.0f};

u32 VBO, VAO;
glGenVertexArrays(1, &VAO);
texture->width = (swidth / image_width) * 4;
texture->height = (sheight / image_height) * 4;
f32 vertices[] = __SQUARE_VAO(texture->width, texture->height);
u32 indices[] = __SQUARE_INDICES;

u32 VBO, EBO;
glGenVertexArrays(1, &texture->vao);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);

// bind the Vertex Array Object first, then bind and set vertex buffer(s),
// and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindVertexArray(texture->vao);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
GL_STATIC_DRAW);

// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), NULL);
glEnableVertexAttribArray(0);
Expand All @@ -62,8 +74,7 @@ Texture LoadTextureFromFile(const char* name, f32 swidth, f32 sheight)
(void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);

return (struct Texture){texture_identifier, VAO, vao_width, vao_height,
name};
return texture;
}

TextureInstance RegisterFullTexture(Texture* from, f32 brightness, f32 rotation,
Expand Down
16 changes: 11 additions & 5 deletions Source/Types/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,33 @@
#define _RENAI_TEXTURE_

#include <Declarations.h>
#include <Logger.h>

typedef struct Texture
{
u32 inner, vao;
f32 width, height;
u16 width, height;
u32 texture, vao;
const char* name;
} Texture;

typedef struct TextureInstance
{
Texture* inherits;
f32 brightness, rotation;
// add a scale option here
u8 scale;
f32 x, y;
u8 z;
} TextureInstance;

#define TEXTURE_EMPTY_INIT \
(struct Texture) { 0, 0, 0.0f, 0.0f, "" }

Texture LoadTextureFromFile(const char* name, f32 swidth, f32 sheight);
Texture* LoadTextureFromFile(const char* name, f32 swidth, f32 sheight);
__INLINE void KillTexture(Texture* texture)
{
PrintWarning("Freed the texture '%s'.", texture->name);
free(texture);
}

#define RegisterTexture(from, x, y, z) \
RegisterFullTexture(from, 1.0f, 0.0f, x, y, z)
Expand All @@ -43,7 +49,7 @@ TextureInstance RegisterFullTexture(Texture* from, f32 brightness, f32 rotation,
__INLINE void BindTexture(Texture* texture)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture->inner);
glBindTexture(GL_TEXTURE_2D, texture->texture);
glBindVertexArray(texture->vao);
}

Expand Down

0 comments on commit a0b0563

Please sign in to comment.