takumi12 - VAO (Vertex Array Object) MAIN 5.2

Publicado por Dakosmu, Mar 02, 2025, 10:16 PM

Tema anterior - Siguiente tema

Dakosmu

takumi12 - VAO (Vertex Array Object) MAIN 5.2
Los visitantes no pueden visualizar imágenes en los mensajes, por favor Regístrate o Inicia Sesión


principalmente he estado trabajando en lograr una mejor estabilidad en 5.2 sin tener que reconstruir todos los sistemas de renderizacion y actualización tanto como efectos, objetos, y texturas, he tenido un gran avance obteniendo calidad y estabilidad, pero aun no es suficiente, ya que el sistema de objetos no aprovecha al maximo el gpu, y hace sobre cargas del cpu y luego copiando al gpu, haciendo esto ineficiente, comencé a trabajar con VAO, y obtuve buenos resultados, ya que la carga del modelo y los vertices se hacen una sola vez al gpu, y luego solo necesitas llamarlo para renderizar, y/o para actualizar el desplazamiento en el mapa, el código por si solo hace la carga del VAO y las configuraciones de los verices, como la carga de los triangulos, las coordenadas de la textura, y el VBO, para renderizar se necesita un shader para controlar el pipelin, no tengo mucho conocimiento para construir los shaders ya que nunca habia tenido la necesidad de hacer uno, si quieren aportar con la comunidad y mejorar esto adelante, y pal que no, pues ahí esta la base...

RESULTADO SIN EL SHADER con el pipelin sin llamar:
Los visitantes no pueden visualizar imágenes en los mensajes, por favor Regístrate o Inicia Sesión

Los visitantes no pueden visualizar imágenes en los mensajes, por favor Regístrate o Inicia Sesión


typedef struct _Mesh_t
{
    bool            NoneBlendMesh;
    short           Texture;
    short           NumVertices;
    short           NumNormals;
    short           NumTexCoords;
    short           NumVertexColors;    //ver1.3
    short           NumTriangles;
    int             NumCommandBytes; //ver1.1
    Vertex_t*       Vertices;
    Normal_t*      Normals;
    TexCoord_t*    TexCoords;
    VertexColor_t*  VertexColors;    //ver1.3
    Triangle_t*    Triangles;
    unsigned char*  Commands; //ver1.1
    TextureScript*  m_csTScript;

    GLuint VAO, VBO_Vertices, VBO_Normals, VBO_TexCoords, VBO_Colors, EBO;

    _Mesh_t()
    {
        Vertices = NULL;
        Normals = NULL;
        Triangles = NULL;
        Commands = NULL;
        m_csTScript = NULL;

        TexCoords = NULL;
        VertexColors = NULL;

        Texture = -1;
        NumCommandBytes = 0;
        NoneBlendMesh = false;

        NumVertices = NumNormals = NumTexCoords =
            NumVertexColors = NumTriangles = 0;
    }
} Mesh_t;

void BMD::CreateVertexBuffer(int i, Mesh_t& mesh)
{
    auto vertices = RenderArrayVertices;
    auto colors = RenderArrayColors;
    auto textCoords = RenderArrayTexCoords;
    std::vector<unsigned short> indices;

    int target_vertex_index = -1;

    for (int j = 0; j < mesh.NumTriangles; j++)
    {
        Triangle_t* triangle = &mesh.Triangles[j];

        for (int k = 0; k < triangle->Polygon; k++)
        {
            int source_vertex_index = triangle->VertexIndex[k];

            target_vertex_index++;

            VectorCopy(VertexTransform[i][source_vertex_index], vertices[target_vertex_index]);

            TexCoord_t* textcoord = &mesh.TexCoords[triangle->TexCoordIndex[k]];
            textCoords[target_vertex_index][0] = 0.0;
            textCoords[target_vertex_index][1] = 0.0;
            colors[target_vertex_index][0] = 1.f;
            colors[target_vertex_index][1] = 1.f;
            colors[target_vertex_index][2] = 1.f;
            colors[target_vertex_index][3] = 1.f;
            indices.push_back(target_vertex_index);
        }
    }

    glGenVertexArrays(1, &mesh.VAO);
    glGenBuffers(1, &mesh.VBO_Vertices);
    glGenBuffers(1, &mesh.VBO_TexCoords);
    glGenBuffers(1, &mesh.EBO);

    glBindVertexArray(mesh.VAO);
    // V&eacute;rtices
    glBindBuffer(GL_ARRAY_BUFFER, mesh.VBO_Vertices);
    glBufferData(GL_ARRAY_BUFFER, target_vertex_index * sizeof(vec3_t), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vec3_t), (void*)0);
    glEnableVertexAttribArray(0);
    // Coordenadas de Textura
    glBindBuffer(GL_ARRAY_BUFFER, mesh.VBO_TexCoords);
    glBufferData(GL_ARRAY_BUFFER, target_vertex_index * sizeof(TexCoord_t), textCoords, GL_STATIC_DRAW);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TexCoord_t), (void*)0);
    glEnableVertexAttribArray(1);
    // &Iacute;ndices de los Tri&aacute;ngulos
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), indices.data(), GL_STATIC_DRAW);
    glBindVertexArray(0); // Desvinculamos el VAO
}

en el render mesh:

glBindVertexArray(m->VAO);
glBindBuffer(GL_ARRAY_BUFFER, m->VBO_Vertices);
glBufferSubData(GL_ARRAY_BUFFER, 0, target_vertex_index * sizeof(vec3_t), vertices);

glBindBuffer(GL_ARRAY_BUFFER, m->VBO_TexCoords);
glBufferSubData(GL_ARRAY_BUFFER, 1, target_vertex_index * sizeof(TexCoord_t), textCoords);

glDrawElements(GL_TRIANGLES, target_vertex_index, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(0);

shader.vs
#version 330 core

layout (location = 0) in vec3 aPos;   // Posici&oacute;n del v&eacute;rtice
layout (location = 1) in vec2 aTexCoord; // Coordenadas de textura

out vec2 TexCoord; // Enviamos las coordenadas al fragment shader

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main() {
    gl_Position = projection * view * model * vec4(aPos, 1.0);
    TexCoord = aTexCoord; // Pasamos la coordenada de textura al fragment shader
}

shader.fs
#version 330 core

in vec2 TexCoord; // Recibe la coordenada de textura del vertex shader
out vec4 FragColor; // Color final del fragmento

uniform sampler2D texture1; // La textura que vamos a usar

void main() {
    FragColor = texture(texture1, TexCoord); // Aplicamos la textura
}

me estan preguntando donde debe ser llamado, se utiliza al final del ciclo for donde termina la creacion del mesh, donde ya carg&oacute; los vertices, en el Open2
Los visitantes no pueden visualizar imágenes en los mensajes, por favor Regístrate o Inicia Sesión


suerte y exito en sus proyectos
creditos: takumi12
Bon Dia

Dakosmu

[CShaderGL.h]
Code: [Select]

#pragma once

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> // Necesario para glm::value_ptr

class CShaderGL
{
public:
    CShaderGL();
    virtual ~CShaderGL();

    void Init();
    void RenderShader();
    bool CheckedShader();
    GLuint GetShaderId();

    bool readshader(const char* filename, std::string& shader_text);
    GLuint run_shader(const char* shader_text, GLenum type);

    // Funciones para establecer uniforms
    void setBool(const char* name, bool value) const;
    void setInt(const char* name, int value) const;
    void setFloat(const char* name, float value) const;
    void setVec2(const char* name, float x, float y) const;
    void setVec3(const char* name, float x, float y, float z) const;
    void setVec4(const char* name, float x, float y, float z, float w) const;
    void setMat4(const char* name, glm::mat4& matrix) const;

    static CShaderGL* Instance();
private:
    GLuint shader_id;
};

#define gShaderGL (CShaderGL::Instance())

[CShaderGL.cpp]
Code: [Select]

#include "stdafx.h"
#include "CShaderGL.h"

#include "Utilities/Log/muConsoleDebug.h"

CShaderGL::CShaderGL()
{
    shader_id = 0;
}

CShaderGL::~CShaderGL()
{
}

void CShaderGL::Init()
{
    std::string vertex_shader;

    if (!readshader("Shaders\\shader.vs", vertex_shader))
    {
        return;
    }

    std::string frgmen_shader;

    if (!readshader("Shaders\\shader.fs", frgmen_shader))
    {
        return;
    }

    GLuint shader_vertex = run_shader(vertex_shader.data(), GL_VERTEX_SHADER);

    GLuint shader_frgmen = run_shader(frgmen_shader.data(), GL_FRAGMENT_SHADER);

    shader_id = glCreateProgram();
    glAttachShader(shader_id, shader_vertex);
    glAttachShader(shader_id, shader_frgmen);
    glLinkProgram(shader_id);

    int success;
    glGetProgramiv(shader_id, GL_LINK_STATUS, &success);

    if (!success)
    {
        char infoLog[512];
        glGetProgramInfoLog(shader_id, 512, NULL, infoLog);
        g_ConsoleDebug->Write(5, "Error al enlazar el Shader Program:");
        g_ConsoleDebug->Write(5, infoLog);
    }

    // Eliminar los shaders compilados
    glDeleteShader(shader_vertex);
    glDeleteShader(shader_frgmen);
}

void CShaderGL::RenderShader()
{
    if (this->CheckedShader())
    {
        glUseProgram(shader_id);
    }
}

bool CShaderGL::CheckedShader()
{
    return (shader_id != 0);
}

GLuint CShaderGL::GetShaderId()
{
    return shader_id;
}

bool CShaderGL::readshader(const char* filename, std::string& shader_text)
{
    FILE* compressedFile = fopen(filename, "rb");

    if (compressedFile)
    {
        fseek(compressedFile, 0, SEEK_END);
        long fileSize = ftell(compressedFile);
        fseek(compressedFile, 0, SEEK_SET);

        shader_text.resize(fileSize, 0);
        fread(shader_text.data(), 1, fileSize, compressedFile);
        fclose(compressedFile);

        return true;
    }

    return false;
}

GLuint CShaderGL::run_shader(const char* shader_text, GLenum type)
{
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, &shader_text, NULL);
    glCompileShader(shader);

    // Verificar errores de compilación
    int success;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &success);

    if (!success)
    {
        char infoLog[512];
        glGetShaderInfoLog(shader, 512, NULL, infoLog);
        g_ConsoleDebug->Write(5, "Error al compilar shader:");
        g_ConsoleDebug->Write(5, infoLog);
    }

    return shader;
}

// Funciones para establecer uniforms
void CShaderGL::setBool(const char* name, bool value) const
{
    glUniform1i(glGetUniformLocation(shader_id, name), (int)value);
}

void CShaderGL::setInt(const char* name, int value) const
{
    glUniform1i(glGetUniformLocation(shader_id, name), value);
}

void CShaderGL::setFloat(const char* name, float value) const
{
    glUniform1f(glGetUniformLocation(shader_id, name), value);
}

void CShaderGL::setVec2(const char* name, float x, float y) const
{
    glUniform2f(glGetUniformLocation(shader_id, name), x, y);
}

void CShaderGL::setVec3(const char* name, float x, float y, float z) const
{
    glUniform3f(glGetUniformLocation(shader_id, name), x, y, z);
}

void CShaderGL::setVec4(const char* name, float x, float y, float z, float w) const
{
    glUniform4f(glGetUniformLocation(shader_id, name), x, y, z, w);
}

void CShaderGL::setMat4(const char* name, glm::mat4& matrix) const
{
    glUniformMatrix4fv(glGetUniformLocation(shader_id, name), 1, GL_FALSE, glm::value_ptr(matrix));
}

CShaderGL* CShaderGL::Instance()
{
    static CShaderGL sInstance;
    return &sInstance;
}

[new function render in zzzbmd.cpp]
Code: [Select]

void BMD::RenderVertexBuffer(int i, Mesh_t* m, int vertex_index, vec3_t* vertices, vec2_t* textCoords)
{
    GLuint shader_id = gShaderGL->GetShaderId();

    glm::mat4 model = glm::mat4(1.0f);
    glm::mat4 view = glm::lookAt(glm::vec3(-CameraPosition[0], -CameraPosition[1], -CameraPosition[2]),
                                  glm::vec3(0.0f, 0.0f, 0.0f),
                                  glm::vec3(0.0f, 1.0f, 0.0f));
    glm::mat4 projection = glm::perspective(glm::radians(CameraFOV), (float)WindowWidth / (float)WindowHeight, CameraViewNear, CameraViewFar * 1.4f);

    // Enviar la matriz de proyección
    glUseProgram(shader_id);

    glUniformMatrix4fv(glGetUniformLocation(shader_id, "projection"), 1, GL_FALSE, glm::value_ptr(projection));

    // Enviar la matriz de vista
    glUniformMatrix4fv(glGetUniformLocation(shader_id, "view"), 1, GL_FALSE, glm::value_ptr(view));

    // Enviar la matriz de modelo
    glUniformMatrix4fv(glGetUniformLocation(shader_id, "model"), 1, GL_FALSE, glm::value_ptr(model));

    glBindVertexArray(m->VAO);
    glDrawElements(GL_TRIANGLES, vertex_index, GL_UNSIGNED_SHORT, 0);
    glBindVertexArray(0);
    glUseProgram(0);
}

CitarThe problem remains how to load the shader and correctly position the transformation values for the camera, view, and model:
[@kayito] [@Kapocha33]
Bon Dia

Dakosmu

Integración de CShaderGL en tu Proyecto

1. Crear Archivos para CShaderGL

Primero, necesitas crear dos archivos para `CShaderGL`: `CShaderGL.h` y `CShaderGL.cpp`.

CShaderGL.h

Crea un archivo llamado `CShaderGL.h` y agrega el siguiente contenido:

#pragma once

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> // Necesario para glm::value_ptr

class CShaderGL
{
public:
    CShaderGL();
    virtual ~CShaderGL();

    void Init();
    void RenderShader();
    bool CheckedShader();
    GLuint GetShaderId();

    bool readshader(const char* filename, std::string& shader_text);
    GLuint run_shader(const char* shader_text, GLenum type);

    // Funciones para establecer uniforms
    void setBool(const char* name, bool value) const;
    void setInt(const char* name, int value) const;
    void setFloat(const char* name, float value) const;
    void setVec2(const char* name, float x, float y) const;
    void setVec3(const char* name, float x, float y, float z) const;
    void setVec4(const char* name, float x, float y, float z, float w) const;
    void setMat4(const char* name, glm::mat4& matrix) const;

    static CShaderGL* Instance();
private:
    GLuint shader_id;
};

#define gShaderGL (CShaderGL::Instance())

CShaderGL.cpp

Crea un archivo llamado `CShaderGL.cpp` y agrega el siguiente contenido:

#include "CShaderGL.h"
#include "Utilities/Log/muConsoleDebug.h" // Asegúrate de incluir el encabezado correcto para el logging

CShaderGL::CShaderGL()
{
    shader_id = 0;
}

CShaderGL::~CShaderGL()
{
}

void CShaderGL::Init()
{
    std::string vertex_shader;
    if (!readshader("Shaders\\shader.vs", vertex_shader))
    {
        return;
    }

    std::string frgmen_shader;
    if (!readshader("Shaders\\shader.fs", frgmen_shader))
    {
        return;
    }

    GLuint shader_vertex = run_shader(vertex_shader.data(), GL_VERTEX_SHADER);
    GLuint shader_frgmen = run_shader(frgmen_shader.data(), GL_FRAGMENT_SHADER);

    shader_id = glCreateProgram();
    glAttachShader(shader_id, shader_vertex);
    glAttachShader(shader_id, shader_frgmen);
    glLinkProgram(shader_id);

    int success;
    glGetProgramiv(shader_id, GL_LINK_STATUS, &success);
    if (!success)
    {
        char infoLog[512];
        glGetProgramInfoLog(shader_id, 512, NULL, infoLog);
        g_ConsoleDebug->Write(5, "Error al enlazar el Shader Program:");
        g_ConsoleDebug->Write(5, infoLog);
    }

    // Eliminar los shaders compilados
    glDeleteShader(shader_vertex);
    glDeleteShader(shader_frgmen);
}

void CShaderGL::RenderShader()
{
    if (this->CheckedShader())
    {
        glUseProgram(shader_id);
    }
}

bool CShaderGL::CheckedShader()
{
    return (shader_id != 0);
}

GLuint CShaderGL::GetShaderId()
{
    return shader_id;
}

bool CShaderGL::readshader(const char* filename, std::string& shader_text)
{
    FILE* compressedFile = fopen(filename, "rb");
    if (compressedFile)
    {
        fseek(compressedFile, 0, SEEK_END);
        long fileSize = ftell(compressedFile);
        fseek(compressedFile, 0, SEEK_SET);

        shader_text.resize(fileSize, 0);
        fread(shader_text.data(), 1, fileSize, compressedFile);
        fclose(compressedFile);
        return true;
    }
    return false;
}

GLuint CShaderGL::run_shader(const char* shader_text, GLenum type)
{
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, &shader_text, NULL);
    glCompileShader(shader);

    // Verificar errores de compilación
    int success;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        char infoLog[512];
        glGetShaderInfoLog(shader, 512, NULL, infoLog);
        g_ConsoleDebug->Write(5, "Error al compilar shader:");
        g_ConsoleDebug->Write(5, infoLog);
    }

    return shader;
}

// Funciones para establecer uniforms
void CShaderGL::setBool(const char* name, bool value) const
{
    glUniform1i(glGetUniformLocation(shader_id, name), (int)value);
}

void CShaderGL::setInt(const char* name, int value) const
{
    glUniform1i(glGetUniformLocation(shader_id, name), value);
}

void CShaderGL::setFloat(const char* name, float value) const
{
    glUniform1f(glGetUniformLocation(shader_id, name), value);
}

void CShaderGL::setVec2(const char* name, float x, float y) const
{
    glUniform2f(glGetUniformLocation(shader_id, name), x, y);
}

void CShaderGL::setVec3(const char* name, float x, float y, float z) const
{
    glUniform3f(glGetUniformLocation(shader_id, name), x, y, z);
}

void CShaderGL::setVec4(const char* name, float x, float y, float z, float w) const
{
    glUniform4f(glGetUniformLocation(shader_id, name), x, y, z, w);
}

void CShaderGL::setMat4(const char* name, glm::mat4& matrix) const
{
    glUniformMatrix4fv(glGetUniformLocation(shader_id, name), 1, GL_FALSE, glm::value_ptr(matrix));
}

CShaderGL* CShaderGL::Instance()
{
    static CShaderGL sInstance;
    return &sInstance;
}

2. Integrar en Main.cpp

Ahora, en tu archivo `Main.cpp`, necesitas incluir el encabezado de `CShaderGL` y llamarlo desde la función `EntryProc()`.

Modificaciones en Main.cpp

Agrega la inclusión del encabezado al inicio de `Main.cpp`:

#include "CShaderGL.h" // Agrega esta línea

Luego, dentro de la función `EntryProc()`, inicializa el shader:

extern "C" _declspec(dllexport) void EntryProc() // OK
{
    CreateThread(0, 0, (LPTHREAD_START_ROUTINE)StartAddress, 0, 0, 0);

    // ... (el resto del código)

    // Inicializa el shader
    gShaderGL->Init();

    // ... (el resto del código)
}

3. Archivos de Shader

Asegúrate de tener los archivos de shader (`shader.vs` y `shader.fs`) en la carpeta `Shaders` de tu proyecto. Estos archivos deben contener el código del vertex shader y el fragment shader, respectivamente.

4. Compilar y Probar




Bon Dia

Dakosmu



Los visitantes no pueden visualizar imágenes en los mensajes, por favor Regístrate o Inicia Sesión
Descripción: takumi12 - VAO (Vertex Array Object) SHADER MAIN 5.2

Los visitantes no pueden visualizar imágenes en los mensajes, por favor Regístrate o Inicia Sesión
Descripción: takumi12 - VAO (Vertex Array Object) SHADER MAIN 5.2

Los visitantes no pueden visualizar imágenes en los mensajes, por favor Regístrate o Inicia Sesión
Descripción: takumi12 - VAO (Vertex Array Object) SHADER MAIN 5.2


CShaderGL.h
Code: [Select]

#pragma once

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> // Necesario para glm::value_ptr

class CShaderGL
{
public:
    CShaderGL();
    virtual ~CShaderGL();

    void Init();
    void RenderShader();
    bool CheckedShader();
    GLuint GetShaderId();

    bool readshader(const char* filename, std::string& shader_text);
    GLuint run_shader(const char* shader_text, GLenum type);

    // Funciones para establecer uniforms
    void setBool(const char* name, bool value) const;
    void setInt(const char* name, int value) const;
    void setFloat(const char* name, float value) const;
    void setVec2(const char* name, float x, float y) const;
    void setVec3(const char* name, float x, float y, float z) const;
    void setVec4(const char* name, float x, float y, float z, float w) const;
    void setMat4(const char* name, glm::mat4& matrix) const;

    static CShaderGL* Instance();
private:
    GLuint shader_id;
};

#define gShaderGL (CShaderGL::Instance())

CShaderGL.cpp
Code: [Select]

#include "stdafx.h"
#include "CShaderGL.h"

#include "Utilities/Log/muConsoleDebug.h"

CShaderGL::CShaderGL()
{
    shader_id = 0;
}

CShaderGL::~CShaderGL()
{
}

void CShaderGL::Init()
{
    std::string vertex_shader;

    if (!readshader("Shaders\\shader.vs", vertex_shader))
    {
        return;
    }

    std::string frgmen_shader;

    if (!readshader("Shaders\\shader.fs", frgmen_shader))
    {
        return;
    }

    GLuint shader_vertex = run_shader(vertex_shader.data(), GL_VERTEX_SHADER);

    GLuint shader_frgmen = run_shader(frgmen_shader.data(), GL_FRAGMENT_SHADER);

    shader_id = glCreateProgram();
    glAttachShader(shader_id, shader_vertex);
    glAttachShader(shader_id, shader_frgmen);
    glLinkProgram(shader_id);

    int success;
    glGetProgramiv(shader_id, GL_LINK_STATUS, &success);

    if (!success)
    {
        char infoLog[512];
        glGetProgramInfoLog(shader_id, 512, NULL, infoLog);
        g_ConsoleDebug->Write(5, "Error al enlazar el Shader Program:");
        g_ConsoleDebug->Write(5, infoLog);
    }

    // Eliminar los shaders compilados
    glDeleteShader(shader_vertex);
    glDeleteShader(shader_frgmen);
}

void CShaderGL::RenderShader()
{
    if (this->CheckedShader())
    {
        glUseProgram(shader_id);
    }
}

bool CShaderGL::CheckedShader()
{
    return (shader_id != 0);
}

GLuint CShaderGL::GetShaderId()
{
    return shader_id;
}

bool CShaderGL::readshader(const char* filename, std::string& shader_text)
{
    FILE* compressedFile = fopen(filename, "rb");

    if (compressedFile)
    {
        fseek(compressedFile, 0, SEEK_END);
        long fileSize = ftell(compressedFile);
        fseek(compressedFile, 0, SEEK_SET);

        shader_text.resize(fileSize, 0);
        fread(shader_text.data(), 1, fileSize, compressedFile);
        fclose(compressedFile);

        return true;
    }

    return false;
}

GLuint CShaderGL::run_shader(const char* shader_text, GLenum type)
{
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, &shader_text, NULL);
    glCompileShader(shader);

    // Verificar errores de compilación
    int success;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &success);

    if (!success)
    {
        char infoLog[512];
        glGetShaderInfoLog(shader, 512, NULL, infoLog);
        g_ConsoleDebug->Write(5, "Error al compilar shader:");
        g_ConsoleDebug->Write(5, infoLog);
    }

    return shader;
}

// Funciones para establecer uniforms
void CShaderGL::setBool(const char* name, bool value) const
{
    glUniform1i(glGetUniformLocation(shader_id, name), (int)value);
}

void CShaderGL::setInt(const char* name, int value) const
{
    glUniform1i(glGetUniformLocation(shader_id, name), value);
}

void CShaderGL::setFloat(const char* name, float value) const
{
    glUniform1f(glGetUniformLocation(shader_id, name), value);
}

void CShaderGL::setVec2(const char* name, float x, float y) const
{
    glUniform2f(glGetUniformLocation(shader_id, name), x, y);
}

void CShaderGL::setVec3(const char* name, float x, float y, float z) const
{
    glUniform3f(glGetUniformLocation(shader_id, name), x, y, z);
}

void CShaderGL::setVec4(const char* name, float x, float y, float z, float w) const
{
    glUniform4f(glGetUniformLocation(shader_id, name), x, y, z, w);
}

void CShaderGL::setMat4(const char* name, glm::mat4& matrix) const
{
    glUniformMatrix4fv(glGetUniformLocation(shader_id, name), 1, GL_FALSE, glm::value_ptr(matrix));
}

CShaderGL* CShaderGL::Instance()
{
    static CShaderGL sInstance;
    return &sInstance;
}

new funtion render in zzzbmd.cpp
Code: [Select]

void BMD::RenderVertexBuffer(int i, Mesh_t* m, int vertex_index, vec3_t* vertices, vec2_t* textCoords)
{
    GLuint shader_id = gShaderGL->GetShaderId();

    glm::mat4 model = glm::mat4(1.0f);
    glm::mat4 view = glm::lookAt(glm::vec3(-CameraPosition[0], -CameraPosition[1], -CameraPosition[2]),
                                  glm::vec3(0.0f, 0.0f, 0.0f),
                                  glm::vec3(0.0f, 1.0f, 0.0f));
    glm::mat4 projection = glm::perspective(glm::radians(CameraFOV), (float)WindowWidth / (float)WindowHeight, CameraViewNear, CameraViewFar * 1.4f);

    // Enviar la matriz de proyección
    glUseProgram(shader_id);

    glUniformMatrix4fv(glGetUniformLocation(shader_id, "projection"), 1, GL_FALSE, glm::value_ptr(projection));

    // Enviar la matriz de vista
    glUniformMatrix4fv(glGetUniformLocation(shader_id, "view"), 1, GL_FALSE, glm::value_ptr(view));

    // Enviar la matriz de modelo
    glUniformMatrix4fv(glGetUniformLocation(shader_id, "model"), 1, GL_FALSE, glm::value_ptr(model));

    glBindVertexArray(m->VAO);
    glDrawElements(GL_TRIANGLES, vertex_index, GL_UNSIGNED_SHORT, 0);
    glBindVertexArray(0);
    glUseProgram(0);
}

The problem remains how to load the shader and correctly position the transformation values for the camera, view, and model:
@[kayito] @[Kapocha33]
Bon Dia

Dakosmu


it will look like this


Los visitantes no pueden visualizar imágenes en los mensajes, por favor Regístrate o Inicia Sesión

void BMD::RenderVertexBuffer(int i, Mesh_t* m, int vertex_index, vec3_t* vertices, vec2_t* textCoords)
{
   GLuint shader_id = gShaderGL->GetShaderId();
   glUseProgram(shader_id);
    glm::mat4 model;
    for (int j = 0; j < m->NumVertices; j++)
   {
        Vertex_t* v = &m->Vertices[j];

        float* vp = VertexTransform[j];
        VectorTransform(v->Position, BoneMatrix[v->Node], vp);
       VectorRotate(v->Position, BoneMatrix[v->Node], vp);
      vp[0] = vp[0] * BoneScale + BoneMatrix[v->Node][0][3];
      vp[1] = vp[1] * BoneScale + BoneMatrix[v->Node][1][3];
      vp[2] = vp[2] * BoneScale + BoneMatrix[v->Node][2][3];
        model = glm::translate(glm::mat4(1.0f), vp);
               
    }

   glm::mat4 view = glm::lookAt(glm::vec3(-CameraPosition[0], -CameraPosition[1], -CameraPosition[2]), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
   glm::mat4 projection = glm::perspective(glm::radians(CameraFOV), (float)WindowWidth / (float)WindowHeight, CameraViewNear, CameraViewFar * 1.4f);

   glUniformMatrix4fv(glGetUniformLocation(shader_id, "projection"), 1, GL_FALSE, glm::value_ptr(projection));

   // Enviar la matriz de vista
   glUniformMatrix4fv(glGetUniformLocation(shader_id, "view"), 1, GL_FALSE, glm::value_ptr(view));

   // Enviar la matriz de modelo
   glUniformMatrix4fv(glGetUniformLocation(shader_id, "model"), 1, GL_FALSE, glm::value_ptr(model));


   glBindVertexArray(m->VAO);
   glDrawElements(GL_TRIANGLES, vertex_index, GL_UNSIGNED_SHORT, 0);
   glBindVertexArray(0);
   glUseProgram(0);
}

Creditos
Smudevelop
Bon Dia

Dakosmu

model + view = Transform vertex->potision with bonematrix >> VectorTransform(v->Position, BoneMatrix[v->Node], vp);

Créditos
Takumi12
Bon Dia

🡱 🡳