[ADDED] smoke and some small changes
This commit is contained in:
4495
res/House.obj
Normal file
4495
res/House.obj
Normal file
File diff suppressed because it is too large
Load Diff
BIN
res/Texture.png
Normal file
BIN
res/Texture.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 970 B |
@@ -12,7 +12,7 @@ namespace entities
|
||||
class Camera
|
||||
{
|
||||
private:
|
||||
const float SPEED = 0.02f;
|
||||
const float SPEED = 0.52f;
|
||||
|
||||
glm::vec3 position;
|
||||
glm::vec3 rotation;
|
||||
|
||||
20
src/main.cpp
20
src/main.cpp
@@ -46,12 +46,19 @@ int main(void)
|
||||
});
|
||||
|
||||
|
||||
models::RawModel raw_model = render_engine::LoadObjModel("res/Tree.obj");
|
||||
models::ModelTexture texture = { render_engine::loader::LoadTexture("res/TreeTexture.png") };
|
||||
models::RawModel raw_model = render_engine::LoadObjModel("res/House.obj");
|
||||
models::ModelTexture texture = { render_engine::loader::LoadTexture("res/Texture.png") };
|
||||
texture.shine_damper = 10;
|
||||
texture.reflectivity = 1;
|
||||
texture.reflectivity = 0;
|
||||
models::TexturedModel model = { raw_model, texture };
|
||||
entities::Entity entity(model, glm::vec3(0, -25, -50), glm::vec3(0, 0, 0), 1);
|
||||
|
||||
std::vector<entities::Entity> entities;
|
||||
int z = 0;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
entities.push_back(entities::Entity(model, glm::vec3(0, -50, -50 - z), glm::vec3(0, 90, 0), 20));
|
||||
z += (raw_model.model_size.x * 20);
|
||||
}
|
||||
|
||||
entities::Light light(glm::vec3(0, 0, -30), glm::vec3(1, 1, 1));
|
||||
|
||||
@@ -66,16 +73,19 @@ int main(void)
|
||||
{
|
||||
// Update
|
||||
const double delta = UpdateDelta();
|
||||
entity.IncreaseRotation(glm::vec3(0, 1, 0));
|
||||
camera.Move(window);
|
||||
|
||||
// Render
|
||||
render_engine::renderer::Prepare();
|
||||
shader.Start();
|
||||
shader.LoadSkyColor(render_engine::renderer::SKY_COLOR);
|
||||
shader.LoadLight(light);
|
||||
shader.LoadViewMatrix(camera);
|
||||
|
||||
for (entities::Entity& entity : entities)
|
||||
{
|
||||
render_engine::renderer::Render(entity, shader);
|
||||
}
|
||||
|
||||
// Finish up
|
||||
shader.Stop();
|
||||
|
||||
@@ -15,6 +15,7 @@ namespace models
|
||||
{
|
||||
GLuint vao_id;
|
||||
int vertex_count;
|
||||
glm::vec3 model_size;
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#include <GL/glew.h>
|
||||
#include <glm/vec3.hpp>
|
||||
#include "../stb_image.h"
|
||||
#include "loader.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace render_engine
|
||||
{
|
||||
namespace loader
|
||||
@@ -9,6 +12,7 @@ namespace render_engine
|
||||
static GLuint CreateVao();
|
||||
static void StoreDataInAttributeList(int attribute_number, int coordinate_size, std::vector<float>& data);
|
||||
static void BindIndicesBuffer(std::vector<unsigned int>& indices);
|
||||
static glm::vec3 GetSizeModel(std::vector<float>& positions);
|
||||
|
||||
static std::vector<GLuint> vaos;
|
||||
static std::vector<GLuint> vbos;
|
||||
@@ -19,13 +23,16 @@ namespace render_engine
|
||||
*/
|
||||
models::RawModel LoadToVAO(std::vector<float>& positions, std::vector<float>& texture_coords, std::vector<float>& normals, std::vector<unsigned int>& indices)
|
||||
{
|
||||
GLuint vao_id = CreateVao();
|
||||
const GLuint vao_id = CreateVao();
|
||||
BindIndicesBuffer(indices);
|
||||
StoreDataInAttributeList(0, 3, positions);
|
||||
StoreDataInAttributeList(1, 2, texture_coords);
|
||||
StoreDataInAttributeList(2, 3, normals);
|
||||
glBindVertexArray(0);
|
||||
return { vao_id, static_cast<int>(indices.size()) };
|
||||
|
||||
const glm::vec3 model_size = GetSizeModel(positions);
|
||||
|
||||
return { vao_id, static_cast<int>(indices.size()), model_size };
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -41,6 +48,12 @@ namespace render_engine
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glBindTexture(GL_TEXTURE_2D, texture_id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imgData);
|
||||
|
||||
// Set mipmapping with a constant LOD
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -0.4f);
|
||||
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
@@ -114,5 +127,67 @@ namespace render_engine
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_id);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * indices.size(), &indices[0], GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
static glm::vec3 GetSizeModel(std::vector<float>& positions)
|
||||
{
|
||||
float minX = 100;
|
||||
float maxX = -100;
|
||||
|
||||
float minY = 100;
|
||||
float maxY = -100;
|
||||
|
||||
float minZ = 100;
|
||||
float maxZ = -100;
|
||||
|
||||
for (int i = 0; i < positions.size(); ++i)
|
||||
{
|
||||
const int index = i % 3;
|
||||
const float value = positions[i];
|
||||
|
||||
switch (index)
|
||||
{
|
||||
case 0: // x
|
||||
{
|
||||
if (value < minX)
|
||||
{
|
||||
minX = value;
|
||||
} else if (value > maxX)
|
||||
{
|
||||
maxX = value;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 1: // y
|
||||
{
|
||||
if (value < minY)
|
||||
{
|
||||
minY = value;
|
||||
}
|
||||
else if (value > maxY)
|
||||
{
|
||||
maxY = value;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 2: // z
|
||||
{
|
||||
if (value < minZ)
|
||||
{
|
||||
minZ = value;
|
||||
}
|
||||
else if (value > maxZ)
|
||||
{
|
||||
maxZ = value;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const float sizeX = maxX - minX;
|
||||
const float sizeY = maxY - minY;
|
||||
const float sizeZ = maxZ - minZ;
|
||||
return { sizeX, sizeY, sizeZ };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace render_engine
|
||||
{
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glClearColor(0.3f, 0.4f, 0.6f, 1.0f);
|
||||
glClearColor(SKY_COLOR.r, SKY_COLOR.g, SKY_COLOR.b, 1.0f);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -7,6 +7,8 @@ namespace render_engine
|
||||
{
|
||||
namespace renderer
|
||||
{
|
||||
const glm::vec3 SKY_COLOR = { 0.3f, 0.4f, 0.6f };
|
||||
|
||||
/*
|
||||
Call this function when starting the program
|
||||
*/
|
||||
|
||||
@@ -20,19 +20,25 @@ namespace shaders
|
||||
out vec3 surface_normal;
|
||||
out vec3 to_light_vector;
|
||||
out vec3 to_camera_vector;
|
||||
out float visibility;
|
||||
|
||||
uniform mat4 model_matrix;
|
||||
uniform mat4 projection_matrix;
|
||||
uniform mat4 view_matrix;
|
||||
uniform vec3 light_position;
|
||||
|
||||
const float density = 0.0017;
|
||||
const float gradient = 4;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
// Calculate the real position of the vertex (after rotation and scaling)
|
||||
vec4 world_position = model_matrix * vec4(position, 1.0);
|
||||
|
||||
vec4 position_rel_to_cam = view_matrix * world_position;
|
||||
|
||||
// Tell OpenGL where to render the vertex
|
||||
gl_Position = projection_matrix * view_matrix * world_position;
|
||||
gl_Position = projection_matrix * position_rel_to_cam;
|
||||
|
||||
// Pass the textureCoords directly to the fragment shader
|
||||
pass_texture_coords = texture_coords;
|
||||
@@ -40,6 +46,11 @@ namespace shaders
|
||||
surface_normal = (model_matrix * vec4(normal, 0.0)).xyz;
|
||||
to_light_vector = light_position - world_position.xyz;
|
||||
to_camera_vector = (inverse(view_matrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz - world_position.xyz;
|
||||
|
||||
// Calculate the density/visibility of the vertex with the fog
|
||||
float distance = length(position_rel_to_cam.xyz);
|
||||
visibility = exp(-pow((distance * density), gradient));
|
||||
visibility = clamp(visibility, 0.0, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
@@ -55,6 +66,7 @@ namespace shaders
|
||||
in vec3 surface_normal;
|
||||
in vec3 to_light_vector;
|
||||
in vec3 to_camera_vector;
|
||||
in float visibility;
|
||||
|
||||
// Final color of the pixel
|
||||
out vec4 out_color;
|
||||
@@ -65,6 +77,7 @@ namespace shaders
|
||||
uniform vec3 light_color;
|
||||
uniform float shine_damper;
|
||||
uniform float reflectivity;
|
||||
uniform vec3 sky_color;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
@@ -86,6 +99,7 @@ namespace shaders
|
||||
vec3 specular = damped_specular * reflectivity * light_color;
|
||||
|
||||
out_color = vec4(diffuse, 1.0) * texture(texture_sampler, pass_texture_coords) + vec4(specular, 1.0);
|
||||
out_color = mix(vec4(sky_color, 1.0), out_color, visibility);
|
||||
}
|
||||
)";
|
||||
|
||||
@@ -122,6 +136,11 @@ namespace shaders
|
||||
LoadFloat(location_reflectivity, reflectivity);
|
||||
}
|
||||
|
||||
void EntityShader::LoadSkyColor(glm::vec3 sky_color) const
|
||||
{
|
||||
LoadVector(location_sky_color, sky_color);
|
||||
}
|
||||
|
||||
void EntityShader::SetAttributes() const
|
||||
{
|
||||
// Load the position VBO and textureCoords VBO from the VAO into the shader "in" variables
|
||||
@@ -140,5 +159,6 @@ namespace shaders
|
||||
location_light_color = GetUniformLocation("light_color");
|
||||
location_shine_damper = GetUniformLocation("shine_damper");
|
||||
location_reflectivity = GetUniformLocation("reflectivity");
|
||||
location_sky_color = GetUniformLocation("sky_color");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace shaders
|
||||
GLuint location_light_color;
|
||||
GLuint location_shine_damper;
|
||||
GLuint location_reflectivity;
|
||||
GLuint location_sky_color;
|
||||
|
||||
public:
|
||||
EntityShader();
|
||||
@@ -32,6 +33,8 @@ namespace shaders
|
||||
void LoadLight(entities::Light& light) const;
|
||||
void LoadShineVariables(float shine_damper, float reflectivity) const;
|
||||
|
||||
void LoadSkyColor(glm::vec3 sky_color) const;
|
||||
|
||||
protected:
|
||||
void SetAttributes() const override;
|
||||
void GetAllUniformLocations() override;
|
||||
|
||||
Reference in New Issue
Block a user