Can now load a glb model with textures embedded in the model. Further cleanup coming.
This commit is contained in:
parent
5b5e8a9fb0
commit
c6a7c42a4e
12 changed files with 323 additions and 194 deletions
|
|
@ -25,6 +25,7 @@ const float AMBIENT_LEVEL = 0.25f;
|
|||
struct Config {
|
||||
Shader shader;
|
||||
Shader lightShader;
|
||||
std::vector<Model> models;
|
||||
|
||||
Lighting light{
|
||||
.directional={
|
||||
|
|
@ -86,9 +87,6 @@ struct Config {
|
|||
.specularMap = 0,
|
||||
};
|
||||
|
||||
unsigned int cubeVAO = 0;
|
||||
unsigned int lightCubeVAO = 0;
|
||||
unsigned int VBO = 0;
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
|
||||
|
|
@ -208,95 +206,27 @@ Config setup() {
|
|||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
Config config{
|
||||
.shader={"shaders/13-vert.glsl", "shaders/13-frag.glsl"},
|
||||
.lightShader={"shaders/13-vert.glsl", "shaders/13-lightsource.frag.glsl"},
|
||||
.shader={"shaders/14-vert.glsl", "shaders/14-frag.glsl"},
|
||||
.lightShader={"shaders/14-vert.glsl", "shaders/14-lightsource.frag.glsl"},
|
||||
.models={
|
||||
{"assets/popcorn_model_a.glb"}
|
||||
},
|
||||
};
|
||||
|
||||
unsigned int VBO = 0;
|
||||
unsigned int lightCubeVAO = 0;
|
||||
unsigned int cubeVAO = 0;
|
||||
|
||||
// cube vao configure, this is the light target
|
||||
glGenVertexArrays(1, &cubeVAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
glBindVertexArray(cubeVAO);
|
||||
|
||||
// position attribute
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3 * sizeof(float)));
|
||||
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
|
||||
glGenVertexArrays(1, &lightCubeVAO);
|
||||
glBindVertexArray(lightCubeVAO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
config.lightCubeVAO = lightCubeVAO;
|
||||
config.cubeVAO = cubeVAO;
|
||||
config.VBO = VBO;
|
||||
|
||||
config.cubeMaterial.diffuseMap = load_texture("resources/textures/container2.png");
|
||||
config.cubeMaterial.specularMap = load_texture("resources/textures/container2_specular.png");
|
||||
|
||||
config.shader.use();
|
||||
config.shader.setInt("material.diffuse", 0);
|
||||
config.shader.setInt("material.specular", 1);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
void draw_cube(size_t pos, Config& config, glm::mat4& projection, glm::mat4& view) {
|
||||
config.shader.setMat4("view", view);
|
||||
config.shader.setMat4("projection", projection);
|
||||
config.shader.setVec3("viewPos", config.camera.pos);
|
||||
|
||||
void draw_popcorn(Config& config, size_t model_i, size_t model_pos, glm::mat4& projection, glm::mat4& view)
|
||||
{
|
||||
glm::vec3 objectColor = {1.0, 0.94, 0.78};
|
||||
config.shader.applyMaterial(config.cubeMaterial);
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, cube_positions[pos]);
|
||||
|
||||
float angle = glfwGetTime() * (float)pos;
|
||||
model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||
model = glm::translate(model, popcorn_positions[model_pos]);
|
||||
|
||||
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);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
|
||||
void draw_light(Config& config, Light& light, glm::mat4& projection, glm::mat4& view) {
|
||||
config.lightShader.use();
|
||||
|
||||
config.lightShader.setVec3("diffuse", light.diffuse);
|
||||
config.lightShader.setMat4("projection", projection);
|
||||
config.lightShader.setMat4("view", view);
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, light.position);
|
||||
model = glm::scale(model, glm::vec3(0.2f));
|
||||
config.lightShader.setMat4("model", model);
|
||||
|
||||
glBindVertexArray(config.lightCubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
config.models[model_i].Draw(config.shader);
|
||||
}
|
||||
|
||||
void render(GLFWwindow* window, Config& config) {
|
||||
|
|
@ -317,16 +247,16 @@ void render(GLFWwindow* window, Config& config) {
|
|||
projection = glm::perspective(glm::radians(config.camera.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
|
||||
config.shader.use();
|
||||
config.shader.setMat4("view", view);
|
||||
config.shader.setMat4("projection", projection);
|
||||
config.shader.setVec3("viewPos", config.camera.pos);
|
||||
|
||||
config.light.spot.position = config.camera.pos;
|
||||
config.light.spot.direction = config.camera.front;
|
||||
config.shader.applyLighting(config.light);
|
||||
|
||||
for(size_t i = 0; i < CUBE_COUNT; i++) {
|
||||
draw_cube(i, config, projection, view);
|
||||
}
|
||||
|
||||
for(auto& light : config.light.positioned) {
|
||||
draw_light(config, light, projection, view);
|
||||
draw_popcorn(config, 0, i, projection, view);
|
||||
}
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
|
|
@ -334,9 +264,6 @@ void render(GLFWwindow* window, Config& config) {
|
|||
}
|
||||
|
||||
void cleanup(Config& config) {
|
||||
glDeleteVertexArrays(1, &config.lightCubeVAO);
|
||||
glDeleteBuffers(1, &config.lightCubeVAO);
|
||||
glDeleteBuffers(1, &config.VBO);
|
||||
config.shader.cleanup();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue