Now have a material and highlight map.

This commit is contained in:
Zed A. Shaw 2026-08-21 15:37:02 -04:00
parent 19a91bab18
commit fb2c871419
8 changed files with 159 additions and 110 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 457 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

View file

@ -0,0 +1,46 @@
#version 330 core
struct Material {
sampler2D diffuse;
sampler2D specular;
float shininess;
};
struct Light {
vec3 position;
vec3 diffuse;
vec3 specular;
vec3 ambient;
};
out vec4 FragColor;
in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;
uniform vec3 viewPos;
uniform Light light;
uniform Material material;
void main()
{
vec3 mat_tex = vec3(texture(material.diffuse, TexCoords));
vec3 spec_tex = vec3(texture(material.specular, TexCoords));
vec3 ambient = light.ambient * mat_tex;
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(light.position - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light.diffuse * diff * mat_tex;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = light.specular * spec * spec_tex;
vec3 result = ambient + diffuse + specular;
FragColor = vec4(result, 1.0);
}

View file

@ -0,0 +1,16 @@
#version 330 core
out vec4 FragColor;
struct Light {
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform Light light;
void main()
{
FragColor = vec4(light.diffuse, 1.0); // set all 4 vector values to 1.0
}

View file

@ -0,0 +1,20 @@
#version 330 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()
{
gl_Position = projection * view * model * vec4(aPos, 1.0f);
FragPos = vec3(model * vec4(aPos, 1.0));
Normal = mat3(transpose(inverse(model))) * aNormal;
TexCoords = aTexCoords;
}

View file

@ -2,45 +2,46 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
float vertices[] = { float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // positions // normals // texture coords
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0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0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
}; };

View file

@ -12,12 +12,10 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include "data.hpp" #include "data.hpp"
#include "physics.hpp"
#include "model.hpp" #include "model.hpp"
#include "camera.hpp" #include "camera.hpp"
void framebuffer_size_callback(GLFWwindow *window, int width, int height); void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void processInput(GLFWwindow *window);
const unsigned int SCR_WIDTH = 800; const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600; const unsigned int SCR_HEIGHT = 600;
@ -25,7 +23,6 @@ const unsigned int SCR_HEIGHT = 600;
struct Config { struct Config {
Shader shader; Shader shader;
Shader lightShader; Shader lightShader;
Model model;
Light light{ Light light{
.position={1.2f, 1.0f, 2.0f}, .position={1.2f, 1.0f, 2.0f},
@ -34,11 +31,19 @@ struct Config {
.specular={1.0f, 1.0f, 1.0f}, .specular={1.0f, 1.0f, 1.0f},
}; };
glm::vec3 boxPos{2.0f, 0.0f, 0.0f}; glm::vec3 cubePos{2.0f, 0.0f, 0.0f};
Camera camera{.movementSpeed=2.0f};
Material cubeMaterial{
.ambient = {1.0f, 0.5f, 0.31f},
.shininess = 32.0f,
.diffuseMap = 0,
.specularMap = 0,
};
unsigned int cubeVAO = 0; unsigned int cubeVAO = 0;
unsigned int lightCubeVAO = 0; unsigned int lightCubeVAO = 0;
unsigned int VBO = 0; unsigned int VBO = 0;
Camera camera{.movementSpeed=2.0f};
float deltaTime = 0.0f; float deltaTime = 0.0f;
float lastFrame = 0.0f; float lastFrame = 0.0f;
@ -89,20 +94,12 @@ GLFWwindow* create_window() {
} }
void processInput(GLFWwindow *window, BoxTest &box, Camera& camera) { void processInput(GLFWwindow *window, Camera& camera) {
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
} }
if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
b2Vec2 force(0.0f, 0.5f);
for(size_t i = 0; i < BODY_COUNT; i++) {
box.bodies[i]->ApplyForceToCenter(force, true);
box.bodies[i]->ApplyTorque(0.1f, true);
}
}
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
camera.forward(); camera.forward();
} }
@ -159,9 +156,8 @@ Config setup() {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
Config config{ Config config{
.shader={"shaders/08-vert.glsl", "shaders/08-frag.glsl"}, .shader={"shaders/11-vert.glsl", "shaders/11-frag.glsl"},
.lightShader={"shaders/08-vert.glsl", "shaders/08-lightsource.frag.glsl"}, .lightShader={"shaders/11-vert.glsl", "shaders/11-lightsource.frag.glsl"},
.model={"assets/popcorn_model_a.glb"}
}; };
unsigned int VBO = 0; unsigned int VBO = 0;
@ -178,59 +174,36 @@ Config setup() {
glBindVertexArray(cubeVAO); glBindVertexArray(cubeVAO);
// position attribute // position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float)));
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
// light vao configure, this is the light source // light vao configure, this is the light source
glGenVertexArrays(1, &lightCubeVAO); glGenVertexArrays(1, &lightCubeVAO);
glBindVertexArray(lightCubeVAO); glBindVertexArray(lightCubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
config.lightCubeVAO = lightCubeVAO; config.lightCubeVAO = lightCubeVAO;
config.cubeVAO = cubeVAO; config.cubeVAO = cubeVAO;
config.VBO = VBO; config.VBO = VBO;
return config; config.cubeMaterial.diffuseMap = load_texture("resources/textures/container2.png");
} config.cubeMaterial.specularMap = load_texture("resources/textures/container2_specular.png");
void draw_popcorn(Config& config, BoxTest& box, size_t box_i, glm::mat4& projection, glm::mat4& view)
{
config.shader.use(); config.shader.use();
config.shader.setInt("material.diffuse", 0);
config.shader.setInt("material.specular", 1);
config.shader.setMat4("view", view); return config;
config.shader.setMat4("projection", projection);
config.shader.setVec3("viewPos", config.camera.pos);
config.shader.applyLight(config.light);
glm::vec3 objectColor = {1.0, 0.94, 0.78};
config.shader.applyMaterial({
.ambient = objectColor,
.diffuse = objectColor,
.specular = objectColor,
.shininess = 100000.0f});
float time = glfwGetTime();
// get position from box
b2Vec2 position = box.bodies[box_i]->GetPosition();
float angle = box.bodies[box_i]->GetAngle();
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(position.x, position.y, 0.0f));
model = glm::rotate(model, angle / 5.0f, glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, glm::radians(time * 10.0f), glm::vec3(0.0f, 1.0f, 1.0f));
config.shader.setMat4("model", model);
config.model.Draw(config.shader);
} }
void draw_cube(Config& config, glm::mat4& projection, glm::mat4& view) { void draw_cube(Config& config, glm::mat4& projection, glm::mat4& view) {
@ -241,15 +214,19 @@ void draw_cube(Config& config, glm::mat4& projection, glm::mat4& view) {
config.shader.setMat4("view", view); config.shader.setMat4("view", view);
config.shader.setMat4("projection", projection); config.shader.setMat4("projection", projection);
config.shader.setVec3("viewPos", config.camera.pos); config.shader.setVec3("viewPos", config.camera.pos);
config.shader.applyMaterial({
.ambient = {1.0f, 0.5f, 0.31f}, config.shader.applyMaterial(config.cubeMaterial);
.diffuse = {1.0f, 0.5f, 0.31f},
.specular = config.light.specular,
.shininess = 32.0f});
glm::mat4 model = glm::mat4(1.0f); glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, config.boxPos); model = glm::translate(model, config.cubePos);
config.shader.setMat4("model", model); config.shader.setMat4("model", model);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, config.cubeMaterial.diffuseMap);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, config.cubeMaterial.specularMap);
glBindVertexArray(config.cubeVAO); glBindVertexArray(config.cubeVAO);
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
} }
@ -270,15 +247,12 @@ void draw_light(Config& config, glm::mat4& projection, glm::mat4& view) {
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
} }
void render(GLFWwindow* window, Config& config) {
void render(GLFWwindow* window, Config& config, BoxTest& box, b2World& world) {
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
config.shader.use(); config.shader.use();
Box2d_step_world(world);
// time is used for fake 3d rotation // time is used for fake 3d rotation
float time = glfwGetTime(); float time = glfwGetTime();
@ -286,15 +260,13 @@ void render(GLFWwindow* window, Config& config, BoxTest& box, b2World& world) {
glm::mat4 view = config.camera.lookAt(); glm::mat4 view = config.camera.lookAt();
glm::mat4 projection = glm::mat4(1.0f); glm::mat4 projection = glm::mat4(1.0f);
// fov, aspect, near plane, far plane // fov, aspect, near plane, far plane
projection = glm::perspective(glm::radians(config.camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); projection = glm::perspective(glm::radians(config.camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
// update light color // update light color
draw_cube(config, projection, view); draw_cube(config, projection, view);
// the 0 is for which popcorn, so later when we have N for-loop
draw_popcorn(config, box, 0, projection, view);
draw_light(config, projection, view); draw_light(config, projection, view);
glfwSwapBuffers(window); glfwSwapBuffers(window);
@ -311,18 +283,14 @@ void cleanup(Config& config) {
int main() { int main() {
init_glfw(); init_glfw();
b2Vec2 gravity(0.0f, -10.0f);
b2World world(gravity);
BoxTest box = Box2d_setup(world);
auto window = create_window(); auto window = create_window();
auto config = setup(); auto config = setup();
glfwSetWindowUserPointer(window, &config.camera); glfwSetWindowUserPointer(window, &config.camera);
while(!glfwWindowShouldClose(window)) { while(!glfwWindowShouldClose(window)) {
processInput(window, box, config.camera); processInput(window, config.camera);
render(window, config, box, world); render(window, config);
config.updateDelta(); config.updateDelta();
} }

View file

@ -7,9 +7,9 @@
struct Material { struct Material {
glm::vec3 ambient{0.1f,0.1f,0.1f}; glm::vec3 ambient{0.1f,0.1f,0.1f};
glm::vec3 diffuse{0.5f,0.5f,0.5f};
glm::vec3 specular{0.8f, 0.8f, 0.8f};
float shininess{32.0f}; float shininess{32.0f};
unsigned int diffuseMap = 0;
unsigned int specularMap = 0;
}; };
struct Light { struct Light {
@ -39,8 +39,6 @@ public:
void applyMaterial(const Material& material) { void applyMaterial(const Material& material) {
setVec3("material.ambient", material.ambient); setVec3("material.ambient", material.ambient);
setVec3("material.diffuse", material.diffuse);
setVec3("material.specular", material.specular);
setFloat("material.shininess", material.shininess); setFloat("material.shininess", material.shininess);
} }