Skip to content
Snippets Groups Projects
Commit e879a174 authored by Marie AUDOUARD's avatar Marie AUDOUARD
Browse files

Jolis sapins

parent fa7b5bab
No related branches found
No related tags found
No related merge requests found
Showing
with 382398 additions and 2 deletions
This diff is collapsed.
This diff is collapsed.
projet-code/scenes_inf443/base/assets/pine.png

1.68 MiB

This diff is collapsed.
projet-code/scenes_inf443/base/assets/trunk.png

2.16 MiB

#version 300 es // Compatible with OpenGL ES
precision mediump float;
// Fragment shader - this code is executed for every pixel/fragment that belongs to a displayed shape
//
// Compute the color using Phong illumination (ambient, diffuse, specular)
// There is 3 possible input colors:
// - fragment_data.color: the per-vertex color defined in the mesh
// - material.color: the uniform color (constant for the whole shape)
// - image_texture: color coming from the texture image
// The color considered is the product of: fragment_data.color x material.color x image_texture
// The alpha (/transparent) channel is obtained as the product of: material.alpha x image_texture.a
//
// Inputs coming from the vertex shader
in struct fragment_data
{
vec3 position; // position in the world space
vec3 normal; // normal in the world space
vec3 color; // current color on the fragment
vec2 uv; // current uv-texture on the fragment
} fragment;
// Output of the fragment shader - output color
layout(location=0) out vec4 FragColor;
// Uniform values that must be send from the C++ code
// ***************************************************** //
uniform sampler2D image_texture; // Texture image identifiant
uniform mat4 view; // View matrix (rigid transform) of the camera - to compute the camera position
uniform vec3 light; // position of the light
// Coefficients of phong illumination model
struct phong_structure {
float ambient;
float diffuse;
float specular;
float specular_exponent;
};
// Settings for texture display
struct texture_settings_structure {
bool use_texture; // Switch the use of texture on/off
bool texture_inverse_v; // Reverse the texture in the v component (1-v)
bool two_sided; // Display a two-sided illuminated surface (doesn't work on Mac)
};
// Material of the mesh (using a Phong model)
struct material_structure
{
vec3 color; // Uniform color of the object
float alpha; // alpha coefficient
phong_structure phong; // Phong coefficients
texture_settings_structure texture_settings; // Additional settings for the texture
};
uniform material_structure material;
void main()
{
// Compute the position of the center of the camera
mat3 O = transpose(mat3(view)); // get the orientation matrix
vec3 last_col = vec3(view*vec4(0.0, 0.0, 0.0, 1.0)); // get the last column
vec3 camera_position = -O*last_col;
// Renormalize normal
vec3 N = normalize(fragment.normal);
// Inverse the normal if it is viewed from its back (two-sided surface)
// (note: gl_FrontFacing doesn't work on Mac)
if (material.texture_settings.two_sided && gl_FrontFacing == false) {
N = -N;
}
// Phong coefficient (diffuse, specular)
// *************************************** //
// Unit direction toward the light
vec3 L = normalize(light-fragment.position);
// Diffuse coefficient
float diffuse_component = max(dot(N,L),0.0);
// Specular coefficient
float specular_component = 0.0;
if(diffuse_component>0.0){
vec3 R = reflect(-L,N); // symetric of light-direction with respect to the normal
vec3 V = normalize(camera_position-fragment.position);
specular_component = pow( max(dot(R,V),0.0), material.phong.specular_exponent );
}
// Texture
// *************************************** //
// Current uv coordinates
vec2 uv_image = vec2(fragment.uv.x, fragment.uv.y);
if(material.texture_settings.texture_inverse_v) {
uv_image.y = 1.0-uv_image.y;
}
// Get the current texture color
vec4 color_image_texture = texture(image_texture, uv_image);
if(material.texture_settings.use_texture == false) {
color_image_texture=vec4(1.0,1.0,1.0,1.0);
}
// Fully discard the pixel if the alpha value is less than a given threshold.
if(color_image_texture.a < 0.5){
discard;
}
// Compute Shading
// *************************************** //
// Compute the base color of the object based on: vertex color, uniform color, and texture
vec3 color_object = fragment.color * material.color * color_image_texture.rgb;
// Compute the final shaded color using Phong model
float Ka = material.phong.ambient;
float Kd = material.phong.diffuse;
float Ks = material.phong.specular;
vec3 color_shading = (Ka + Kd * diffuse_component) * color_object + Ks * specular_component * vec3(1.0, 1.0, 1.0);
// Output color, with the alpha component
FragColor = vec4(color_shading, material.alpha * color_image_texture.a);
}
\ No newline at end of file
#version 300 es // Compatible with OpenGL ES
precision mediump float;
// Vertex shader - this code is executed for every vertex of the shape
// Inputs coming from VBOs
layout (location = 0) in vec3 vertex_position; // vertex position in local space (x,y,z)
layout (location = 1) in vec3 vertex_normal; // vertex normal in local space (nx,ny,nz)
layout (location = 2) in vec3 vertex_color; // vertex color (r,g,b)
layout (location = 3) in vec2 vertex_uv; // vertex uv-texture (u,v)
// Output variables sent to the fragment shader
out struct fragment_data
{
vec3 position; // vertex position in world space
vec3 normal; // normal position in world space
vec3 color; // vertex color
vec2 uv; // vertex uv
} fragment;
// Uniform variables expected to receive from the C++ program
uniform mat4 model; // Model affine transform matrix associated to the current shape
uniform mat4 view; // View matrix (rigid transform) of the camera
uniform mat4 projection; // Projection (perspective or orthogonal) matrix of the camera
uniform mat4 modelNormal; // Model without scaling used for the normal. modelNormal = transpose(inverse(model))
void main()
{
// The position of the vertex in the world space
vec4 position = model * vec4(vertex_position, 1.0);
// The normal of the vertex in the world space
vec4 normal = modelNormal * vec4(vertex_normal, 0.0);
// The projected position of the vertex in the normalized device coordinates:
vec4 position_projected = projection * view * position;
// Fill the parameters sent to the fragment shader
fragment.position = position.xyz;
fragment.normal = normal.xyz;
fragment.color = vertex_color;
fragment.uv = vertex_uv;
// gl_Position is a built-in variable which is the expected output of the vertex shader
gl_Position = position_projected; // gl_Position is the projected vertex position (in normalized device coordinates)
}
#include "sapin.hpp"
#include "environment.hpp"
void sapin::initialize_sapin()
{
cgp::mesh_drawable trunk;
cgp::mesh_drawable branches;
cgp::mesh_drawable foliage;
trunk.initialize_data_on_gpu(mesh_load_file_obj(project::path + "assets/trunk.obj"));
trunk.texture.load_and_initialize_texture_2d_on_gpu(project::path + "assets/trunk.png");
branches.initialize_data_on_gpu(mesh_load_file_obj(project::path + "assets/branches.obj"));
branches.material.color = { 0.45f, 0.41f, 0.34f }; // no textures on branches
foliage.initialize_data_on_gpu(mesh_load_file_obj(project::path + "assets/foliage.obj"));
foliage.texture.load_and_initialize_texture_2d_on_gpu(project::path + "assets/pine.png");
foliage.shader.load(project::path + "shaders/mesh_transparency/vert.glsl", project::path + "shaders/mesh_transparency/frag.glsl"); // set the shader handling transparency for the foliage
foliage.material.phong = { 0.4f, 0.6f, 0, 1 }; // remove specular effect for the billboard
//scale the model
trunk.model.scaling = 0.5f;
branches.model.scaling = 0.5f;
foliage.model.scaling = 0.5f;
sapin.add(trunk, "Trunk");
sapin.add(branches, "Branches", "Trunk");
sapin.add(foliage, "Foliage", "Trunk");
}
\ No newline at end of file
#pragma once
#include "cgp/cgp.hpp"
using namespace cgp;
struct sapin {
hierarchy_mesh_drawable sapin;
void initialize_sapin();
};
\ No newline at end of file
......@@ -7,6 +7,7 @@
#include "bat.hpp"
#include "terrain.hpp"
#include "projectile.hpp"
#include "sapin.hpp"
using namespace cgp;
int num_trees = 200;
......@@ -70,6 +71,7 @@ void scene_structure::initialize()
cone.material.color = { 0.0f,0.8f,0.0f };
cone.material.phong.specular = 0.0f;
mesh tree_mesh = create_tree();
tree.initialize_data_on_gpu(tree_mesh);
......@@ -79,6 +81,7 @@ void scene_structure::initialize()
tree_position = generate_positions_on_terrain(num_trees, terrain_length-1);
grass_position = generate_positions_on_terrain(num_grass, terrain_length-1);
mesh quadrangle_mesh;
quadrangle_mesh.position = { {0,0,0},{1, 0, 0}, {1,0,1}, {0, 0,1} };
......@@ -97,6 +100,7 @@ void scene_structure::initialize()
bouncing.initialize(10); //10 balls
bat1.initialize_bat();
projectiles.initialize();
sapin1.initialize_sapin();
}
void scene_structure::initialize_mvt()
......@@ -157,8 +161,12 @@ void scene_structure::display_frame()
draw_wireframe(terrain, environment);
for (int i = 0; i < num_trees; i++) {
tree.model.translation = tree_position[i];
draw(tree, environment);
//tree.model.translation = tree_position[i];
//draw(tree, environment);
sapin1.sapin["Trunk"].transform_local.translation = tree_position[i];
sapin1.sapin["Trunk"].transform_local.rotation = rotation_transform::from_axis_angle({ 1,0,0 }, Pi / 2);
sapin1.sapin.update_local_to_global_coordinates();
draw(sapin1.sapin, environment);
}
vec3 p = display_mvt();
//display_bird(p);
......
......@@ -9,6 +9,7 @@
#include "chain.hpp"
#include "bat.hpp"
#include "projectile.hpp"
#include "sapin.hpp"
// This definitions allow to use the structures: mesh, mesh_drawable, etc. without mentionning explicitly cgp::
......@@ -73,6 +74,7 @@ struct scene_structure : cgp::scene_inputs_generic {
chain chain1;
bat bat1;
projectile projectiles;
sapin sapin1;
// ****************************** //
// Functions
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment