Commit 223eccb2 authored by Nicolas Van der Noot's avatar Nicolas Van der Noot

mbsyscopy updated

parent 81d6c897
......@@ -144,6 +144,7 @@ set(TRIAL_PATHS_GLFW3
set(TRIAL_PATHS_LIB_OPEN_GL
C:/Program\ Files\ \(x86\)/Windows\ Kits/8.1/Lib/winv6.3/um/x64
C:/Program\ Files\ \(x86\)/Windows\ Kits/8.1/Lib/winv6.3/um/x86
C:/Program\ Files\ \(x86\)/Windows\ Kits/8.0/Lib/win8/um/x64
)
# possible paths for: 'glfw3.lib'
......
......@@ -9,6 +9,7 @@
#include "mbs_load_xml.h"
#include "mbs_project_interface.h"
#include "nrfct.h"
#include "mbs_sensor.h"
//#define DEBUG_PRINT
......
......@@ -12,6 +12,12 @@ namespace OpenGLMbs{
/// limit x in the [min;max] range
inline int limit_range(int x, int min, int max) { return (x <= min) ? min : ((x >= max) ? max : x); }
// redefine round as not available in math.h prior to C99
double my_round(double x)
{
return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
}
/*! \brief constructor
*
* \param[in] anim_filename name of the .anim file (with its path)
......@@ -237,7 +243,7 @@ void AnimReader::IndexTime(double target_t, int &i0, int &i1)
}
// initial guess
guess_i = round(((target_t - init_t) / diff_t) * t_size);
guess_i = my_round(((target_t - init_t) / diff_t) * t_size);
// bound guess
guess_i = limit_range(guess_i, 0, t_size-1);
......
......@@ -4,6 +4,7 @@
#include "MbsWorld3D.hh"
#include "user_realtime.h"
#include "cmake_open_gl_config.h"
#include "shader_multisample.hh"
#include <iostream>
#include <string>
......@@ -155,6 +156,7 @@ void init_open_gl(Realtime_visu *visu, MbsData *mbs_data, int nb_models, int *nb
OpenGlIn *open_gl_in;
#elif (!defined OPEN_GL_PROCESS_WIN)
OpenGLMbs::MbsWorld3D *world_3d;
int shader_flag, multi_samp;
#endif
// safety
......@@ -222,8 +224,19 @@ void init_open_gl(Realtime_visu *visu, MbsData *mbs_data, int nb_models, int *nb
opengl_win_send_ok_pipe();
#else
// get the shader flag and the mutlisampling option
if (nb_models > 0)
{
OpenGLMbs::shader_multisample(mbs_file[0], shader_flag, multi_samp);
}
else
{
shader_flag = LIGHTS_NO_SPEC_SHADER;
multi_samp = 4;
}
// create world
world_3d = new OpenGLMbs::MbsWorld3D(1024, 768);
world_3d = new OpenGLMbs::MbsWorld3D(1024, 768, shader_flag, multi_samp);
// load each model
for(int i=0; i<nb_models; i++)
......@@ -810,6 +823,7 @@ void opengl_thread_unix_run_visu(OpenGlIn* open_gl_in)
int nb_models;
int flag_state, flag_continue, flag_viewpoint;
int nb_viewpoint;
int shader_flag, multi_samp;
int *nb_q;
double **q_vec;
......@@ -821,15 +835,26 @@ void opengl_thread_unix_run_visu(OpenGlIn* open_gl_in)
return; // safety, should never happen
}
// number of .mbs models to load
nb_models = open_gl_in->nb_models;
// get the shader flag and the mutlisampling option
if (nb_models > 0)
{
OpenGLMbs::shader_multisample(open_gl_in->mbs_file[0], shader_flag, multi_samp);
}
else
{
shader_flag = LIGHTS_NO_SPEC_SHADER;
multi_samp = 4;
}
// create world
world_3d = new OpenGLMbs::MbsWorld3D(1024, 768);
world_3d = new OpenGLMbs::MbsWorld3D(1024, 768, shader_flag, multi_samp);
// save world adress
open_gl_in->visu->visu_class = world_3d;
// number of .mbs models to load
nb_models = open_gl_in->nb_models;
// load each model
for(int i=0; i<nb_models; i++)
{
......
......@@ -9,6 +9,7 @@
#include "cmake_config.h"
#include "cmake_open_gl_config.h"
#include "open_gl_c_int.h"
#include "shader_multisample.hh"
#include <sys/types.h>
#include <sys/stat.h>
......@@ -37,6 +38,7 @@ int main(int argc, char const *argv[])
size_t res;
int nb_models, in_count, nb_viewpoint;
int flag_state, flag_viewpoint, flag_continue;
int shader_flag, multi_samp;
bool run_open_gl;
std::string pipe_start_name;
......@@ -60,9 +62,6 @@ int main(int argc, char const *argv[])
OpenGLMbs::MbsWorld3D *world_3d;
// create world
world_3d = new OpenGLMbs::MbsWorld3D(1024, 768);
// named pipes: get names
pipe_start_name = argv[1];
......@@ -96,12 +95,26 @@ int main(int argc, char const *argv[])
exit(EXIT_FAILURE);
}
in_count = 4; ///< first possible index for mbs
// number of .mbs models to load
nb_models = string_to_int(argv[2]);
// load each model
in_count = 4; ///< first possible index
// get the shader flag and the mutlisampling option
if (nb_models > 0)
{
OpenGLMbs::shader_multisample(argv[in_count], shader_flag, multi_samp);
}
else
{
shader_flag = LIGHTS_NO_SPEC_SHADER;
multi_samp = 4;
}
// create world
world_3d = new OpenGLMbs::MbsWorld3D(1024, 768, shader_flag, multi_samp);
// load each model
for(int i=0; i<nb_models; i++)
{
world_3d->AddMbs(argv[in_count]);
......
......@@ -9,6 +9,7 @@
#include "cmake_config.h"
#include "cmake_open_gl_config.h"
#include "open_gl_c_int.h"
#include "shader_multisample.hh"
#include <iostream>
#include <stdio.h>
......@@ -44,6 +45,7 @@ int main(int argc, char const *argv[])
int nb_models;
int flag_state, flag_viewpoint;
bool run_open_gl;
int shader_flag, multi_samp;
int *nb_q;
double **q_vec;
......@@ -60,20 +62,38 @@ int main(int argc, char const *argv[])
exit(EXIT_FAILURE);
}
// create world
world_3d = new OpenGLMbs::MbsWorld3D(1024, 768);
// number of .mbs models to load
opengl_win_wait_pipe();
nb_models = opengl_win_pipe_to_int();
opengl_win_send_continue_pipe();
// load each model
for(int i=0; i<nb_models; i++)
if (nb_models <= 0)
{
opengl_win_wait_pipe();
world_3d->AddMbs(pipe_msg);
opengl_win_send_continue_pipe();
shader_flag = LIGHTS_NO_SPEC_SHADER;
multi_samp = 4;
// create world
world_3d = new OpenGLMbs::MbsWorld3D(1024, 768, shader_flag, multi_samp);
}
else
{
// load each model
for(int i=0; i<nb_models; i++)
{
opengl_win_wait_pipe();
if (!i)
{
// get the shader flag and the mutlisampling option
OpenGLMbs::shader_multisample(pipe_msg, shader_flag, multi_samp);
// create world
world_3d = new OpenGLMbs::MbsWorld3D(1024, 768, shader_flag, multi_samp);
}
world_3d->AddMbs(pipe_msg);
opengl_win_send_continue_pipe();
}
}
// save number of joints for each model
......
#ifdef OPEN_GL
#include "shader_multisample.hh"
#include <libxml/parser.h>
#include <iostream>
#include <string.h>
#include "MbsWorld3D.hh"
namespace OpenGLMbs{
/*! \brief get the integer value associated with a node
*
* \param[in] node node with the integer to extract
* \param[in] doc parsed xml document
* \return requested integer value, 0 if not found
*/
int int_node(xmlNodePtr node, xmlDocPtr doc)
{
int result;
xmlChar* elem_val = NULL;
// safety
if (node == NULL)
{
std::cout << "shader_multisample warning : cannot read integer from node" << std::endl;
return 0.;
}
elem_val = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
result = (int) round(atof((const char*) elem_val));
xmlFree(elem_val);
return result;
}
/*! \brief get the first node with a given name
*
* \param[in] node first node to check, then checking its brothers (i.e. nodes at the same level)
* \param[in] node_name name of the node to find
* \return XML node pointer (succeed) or NULL (not found)
*/
xmlNodePtr first_node(xmlNodePtr node, const char* node_name)
{
while (node != NULL)
{
if(!strcmp((const char*)node->name, node_name))
{
return node;
}
node = node->next;
}
// node not found
return NULL;
}
/*! \brief get the first node with the correct parent name and own name
*
* \param[in] node first parent node to check, then checking its brothers (i.e. nodes at the same level)
* \param[in] name_1 name of the parent of the node to find
* \param[in] name_2 name of the node to find
* \return XML node pointer (succeed) or NULL (not found)
*/
xmlNodePtr first_node_level_2(xmlNodePtr node, const char* name_1, const char* name_2)
{
node = first_node(node, name_1);
return (node == NULL) ? NULL : first_node(node->children, name_2);
}
/*! \brief extract the shader flag and the multisampling option from .mbs
*
* \param[in] mbs_filename name of the .mbs file (with path)
* \param[out] shader_flag flag to choose the type of shader
* \param[out] multi_samp number of samples to use for multisampling
*/
void shader_multisample(const char* mbs_filename, int &shader_flag, int &multi_samp)
{
xmlDocPtr doc;
xmlNodePtr cur_node, root, x3D_n;
xmlChar* elem_val = NULL;
// xml document opening
doc = xmlParseFile(mbs_filename);
if (doc == NULL)
{
std::cout << "shader_multisample warning: invalid XML document" << std::endl;
exit(EXIT_FAILURE);
}
// getting the root
root = xmlDocGetRootElement(doc);
if (root == NULL)
{
std::cout << "shader_multisample warning: empty XML document" << std::endl;
xmlFreeDoc(doc);
exit(EXIT_FAILURE);
}
// check root name
if (strcmp((const char*)root->name, "mbsDescR7"))
{
std::cout << "shader_multisample warning : xml is not a mbsDescR7" << std::endl;
xmlFreeDoc(doc);
exit(EXIT_FAILURE);
}
// getting x3D
x3D_n = first_node(root->children, "x3D");
if (x3D_n == NULL)
{
std::cout << "shader_multisample warning : x3D not found !" << std::endl;
xmlFreeDoc(doc);
exit(EXIT_FAILURE);
}
// shader
cur_node = first_node_level_2(x3D_n->children, "opengl", "lightAndShadowMode");
if (cur_node != NULL)
{
elem_val = xmlNodeListGetString(doc, cur_node->xmlChildrenNode, 1);
if(!strcmp((const char*) elem_val, "NO_LIGHT_SHADER"))
{
shader_flag = NO_LIGHT_SHADER;
}
else if(!strcmp((const char*) elem_val, "LIGHTS_NO_SPEC_SHADER"))
{
shader_flag = LIGHTS_NO_SPEC_SHADER;
}
else if(!strcmp((const char*) elem_val, "LIGHTS_SHADER"))
{
shader_flag = LIGHTS_SHADER;
}
else if(!strcmp((const char*) elem_val, "SHADOW_MAPPING_SHADER"))
{
shader_flag = SHADOW_MAPPING_SHADER;
}
else
{
std::cout << "shader_multisample error : unknown shader flag : " << (const char*) elem_val << " !" << std::endl;
exit(EXIT_FAILURE);
}
}
else // lightAndShadowMode not present -> default value
{
std::cout << ".mbs file reading warning: 'lightAndShadowMode' not found, default field (LIGHTS_NO_SPEC_SHADER) chosen." << std::endl;
shader_flag = LIGHTS_NO_SPEC_SHADER;
}
// multisampling
cur_node = first_node_level_2(x3D_n->children, "opengl", "glfwSample");
if (cur_node != NULL)
{
multi_samp = int_node(cur_node, doc);
}
else // glfwSample not present -> default value
{
std::cout << ".mbs file reading warning: 'glfwSample' not found, default value (4) chosen." << std::endl;
multi_samp = 4;
}
// free the memory allocate to the xml parser
xmlFreeDoc(doc);
}
}
#endif
/*!
* \author Nicolas Van der Noot
* \file shader_multisample.hh
* \brief extract shader flag and multisampling from .mbs
*/
#ifndef _SHADER_MULTISAMPLIG_HH_
#define _SHADER_MULTISAMPLIG_HH_
namespace OpenGLMbs{
// function prototype
void shader_multisample(const char* mbs_filename, int &shader_flag, int &multi_samp);
}
#endif
......@@ -51,6 +51,7 @@ class MbsRead
void ListNodes(xmlNodePtr parent_node, const char* node_name, std::vector<xmlNodePtr> &node_list);
int IntNode(xmlNodePtr node, xmlDocPtr doc);
float FloatNode(xmlNodePtr node, xmlDocPtr doc);
float FloatAttr(xmlAttr* attr, xmlDocPtr doc);
float FloatChild(xmlNodePtr parent_node, xmlDocPtr doc, const char *child_name);
......
/*!
* \author Nicolas Van der Noot
* \file temp_mbs.cc
* \brief temporary: set the missing fields in the MBS file
*/
#ifdef OPEN_GL
#include "MbsWorld3D.hh"
#include "MbsLight.hh"
#include <stdlib.h>
#include <iostream>
namespace OpenGLMbs{
/*! \brief choose the shader_flag (temporary function, should be replaced by .mbs encoding)
*
* \param[out] shader_flag flag indicating the shader to choose
*
* Here are the possibilities
* NO_LIGHT_SHADER : no light is computed, basic color is applied
* LIGHTS_SHADER : lights are applied, but they generate no shadow
* SHADOW_MAPPING_SHADER : lights are applied, and they generate shadow (through shadow mapping)
*/
void temp_shader_config(int &shader_flag)
{
shader_flag = LIGHTS_SHADER;
}
/*! \brief fill the shapes missing parameters (temporary function, should be replaced by .mbs encoding)
*
* \param[in] shape_id ID of the current shape
* \param[out] shiny_mat material shininess
* \param[out] specular_mat specular material vector
*/
void temp_shape_config(
int shape_id,
float &shiny_mat,
float specular_mat[3])
{
shiny_mat = 250.f;
specular_mat[0] = 1.0f;
specular_mat[1] = 1.0f;
specular_mat[2] = 1.0f;
}
/*! \brief fill the lights missing parameters (temporary function, should be replaced by .mbs encoding)
*
* \param[in] light_id ID of the current light
* \param[in] light_type type of the current light
* \param[out] color light color
* \param[out] ambient ambient light component
* \param[out] depth_x_width depth texture resolution along the X axis (in pixels)
* \param[out] depth_y_width depth texture resolution along the Y axis (in pixels)
* \param[out] shadow_bias shadow bias (to avoid shadow acne) [m]
* \param[out] depth_near near depth texture camera
* \param[out] depth_far far depth texture camera
* \param[out] z_up_vec Z up direction for the shadow texture camera
* \param[out] depth_FoV_rad field of view (for point and spot lights) [rad]
* \param[out] depth_scale scale of the parallel view (for directional light)
* \param[out] shadow_dir_vec direction of the shadow view (for point lights)
* \param[out] rel_point_vec rlative position of the reference point (for directional lights)
*/
void temp_light_config(
int light_id,
int light_type,
float color[3],
float &ambient,
int &depth_x_width,
int &depth_y_width,
float &shadow_bias,
float &depth_near,
float &depth_far,
float z_up_vec[3],
float &depth_FoV_rad,
float &depth_scale,
float shadow_dir_vec[3],
float rel_point_vec[3])
{
// color
color[0] = 1.0f;
color[1] = 1.0f;
color[2] = 1.0f;
ambient = 0.05f;
// depth texture size
depth_x_width = 1024;
depth_y_width = 1024;
// bias
shadow_bias = 0.0f;
// Z up for camera
z_up_vec[0] = 0.0;
z_up_vec[1] = 0.0;
z_up_vec[2] = 1.0;
switch (light_type)
{
case POINT_LIGHT :
// near, far
depth_near = 0.1f;
depth_far = 100.0f;
// FoV angle
depth_FoV_rad = 1.0f;
// directional view
shadow_dir_vec[0] = 0.0;
shadow_dir_vec[1] = -1.0;
shadow_dir_vec[2] = 0.0;
break;
case SPOT_LIGHT :
// near, far
depth_near = 0.1f;
depth_far = 100.0f;
// FoV angle
depth_FoV_rad = 1.0f;
break;
case DIR_LIGHT :
// near, far
depth_near = -10.0f;
depth_far = 20.0f;
// scale
depth_scale = 0.0015f;
// relative point position
rel_point_vec[0] = 0.0;
rel_point_vec[1] = 0.0;
rel_point_vec[2] = 0.0;
break;
default:
std::cout << "temp_shadow error: unknown light type (" << light_type << ") !" << std::endl;
exit(EXIT_FAILURE);
}
}
}
#endif
// GLSL version
#version 330 core
// maximal number of lights
#define MAX_LIGHTS 10
// actual number of lights
uniform int nbLights;
// light strucutre
uniform struct ShaderLight
{
vec4 pos;
vec3 color;
vec3 attenuation;
float ambientCoef;
// cone related
float coneCosAngle;
vec3 coneDir;
} sh_lights[MAX_LIGHTS];
// transparency
uniform float transparency;
// fragment shader inputs from the vertex shader
in vec3 in_Normal_frag;
in vec3 in_Vertex_frag;
in vec3 in_Color_frag;
// fragment shader output
out vec4 finalColor;
// compute the color due to one light
vec3 light_compute(ShaderLight cur_light, vec3 vertex_color, vec3 normal, vec3 vertex_pos)
{
float attenuation;
vec3 cur_light_dir;
if(cur_light.pos.w == 0.0)
{
//directional light
cur_light_dir = cur_light.pos.xyz;
attenuation = 1.0; //no attenuation for directional lights
}
else
{
//point light
cur_light_dir = normalize(cur_light.pos.xyz - vertex_pos);
attenuation = 1.0 / (cur_light.attenuation.x + cur_light.attenuation.z * pow(length(cur_light.pos.xyz - vertex_pos), 2));
//cone restrictions
if(dot(-cur_light_dir, cur_light.coneDir) < cur_light.coneCosAngle)
{
attenuation = 0.0;
}
}
//ambient
vec3 ambient = cur_light.ambientCoef * vertex_color.rgb * cur_light.color;
//diffuse
float diffuseCoefficient = max(0.0, dot(normal, cur_light_dir));
vec3 diffuse = diffuseCoefficient * vertex_color.rgb * cur_light.color;
// linear color (color before gamma correction)
return ambient + attenuation * diffuse;
}
// main function
void main()
{
// normal in world space
vec3 normal = normalize(in_Normal_frag);
// color
vec4 vertex_color = vec4(in_Color_frag, transparency);
// sum of all the colors due to each light
vec3 color_sum = vec3(0.0);
// loop on all the light
for(int i = 0; i<nbLights; i++)
{
color_sum += light_compute(sh_lights[i], vertex_color.rgb, normal, in_Vertex_frag);
}
//final color (after gamma correction)
vec3 gamma = vec3(1.0 / 2.2);
finalColor = vec4(pow(color_sum, gamma), vertex_color.a);
}
......@@ -599,9 +599,14 @@ void MbsShape3D::LightsInit()
// get attributes locations
transID = glGetUniformLocation(shader.ProgramID(), "transparency");
nbLightsID = glGetUniformLocation(shader.ProgramID(), "nbLights" );
cameraID = glGetUniformLocation(shader.ProgramID(), "camPos" );
shininessID = glGetUniformLocation(shader.ProgramID(), "shiny" );
specularID = glGetUniformLocation(shader.ProgramID(), "specColor" );
if (shader_flag != LIGHTS_NO_SPEC_SHADER)
{
cameraID = glGetUniformLocation(shader.ProgramID(), "camPos" );
shininessID = glGetUniformLocation(shader.ProgramID(), "shiny" );
specularID = glGetUniformLocation(shader.ProgramID(), "specColor");
}
// loop on all the lights
for(unsigned int i=0; i<sh_lights.size(); i++)
......@@ -653,8 +658,12 @@ void MbsShape3D::LightsInit()
// lights constants
glUniform1i( nbLightsID , sh_lights.size() );
glUniform1f( shininessID , shiny_mat );
glUniform3fv( specularID , 1, &(specular_mat[0]) );
if (shader_flag != LIGHTS_NO_SPEC_SHADER)