Untitled

 avatar
unknown
plain_text
5 months ago
3.8 kB
6
Indexable
#1

#include <glad/glad.h>
#include <stb_image.h>
#include <iostream>

// Функция за зареждане на текстура
GLuint init_texture(const char* texture_path) {
    GLuint textureID;
    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);

    // Настройки за текстура
    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_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    // Зареждане на изображение
    int width, height, nrChannels;
    unsigned char* data = stbi_load(texture_path, &width, &height, &nrChannels, 0);
    if (data) {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);
    } else {
        std::cerr << "Неуспешно зареждане на текстурата: " << texture_path << std::endl;
    }
    stbi_image_free(data);

    return textureID;
}

// Извикване на функцията с нов път към текстура
GLuint texture = init_texture("path_to_new_texture.jpg");



#2
void processInput(GLFWwindow* window) {
    // Промяна на позицията на светлината
    if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
        lightPos.y += 0.1f;
    if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
        lightPos.y -= 0.1f;
    if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
        lightPos.x -= 0.1f;
    if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
        lightPos.x += 0.1f;

    // Промяна на цвета на светлината
    if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
        lightColor = vec3(1.0f, 0.0f, 0.0f); // Червено
    if (glfwGetKey(window, GLFW_KEY_G) == GLFW_PRESS)
        lightColor = vec3(0.0f, 1.0f, 0.0f); // Зелено
    if (glfwGetKey(window, GLFW_KEY_B) == GLFW_PRESS)
        lightColor = vec3(0.0f, 0.0f, 1.0f); // Синьо
}

#Fragment Shader:

#version 460 core
out vec4 FragColor;

in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;

uniform vec3 lightPos;
uniform vec3 viewPos;
uniform vec3 lightColor;
uniform vec3 objectColor;

uniform sampler2D texture1;

void main() {
    // Амбиентна компонента
    float ambientStrength = 0.1;
    vec3 ambient = ambientStrength * lightColor;

    // Диффузна компонента
    vec3 norm = normalize(Normal);
    vec3 lightDir = normalize(lightPos - FragPos);
    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = diff * lightColor;

    // Спекуларна компонента
    float specularStrength = 0.5;
    vec3 viewDir = normalize(viewPos - FragPos);
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
    vec3 specular = specularStrength * spec * lightColor;

    // Комбиниране на компонентите
    vec3 result = (ambient + diffuse + specular) * vec3(texture(texture1, TexCoords));
    FragColor = vec4(result, 1.0);
}


#Vertex Shader:

#version 460 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;

out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoords;

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

void main() {
    FragPos = vec3(model * vec4(aPos, 1.0));
    Normal = mat3(transpose(inverse(model))) * aNormal;
    TexCoords = aTexCoords;
    gl_Position = projection * view * vec4(FragPos, 1.0);
}
Editor is loading...
Leave a Comment