Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add simple texture rendering shader and module #111

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cgogn/rendering/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ set(src_list
"${CMAKE_CURRENT_LIST_DIR}/shaders/shader_flat_color_per_face.h"
"${CMAKE_CURRENT_LIST_DIR}/shaders/shader_flat_color_per_face.cpp"

"${CMAKE_CURRENT_LIST_DIR}/shaders/shader_flat_texture.h"
"${CMAKE_CURRENT_LIST_DIR}/shaders/shader_flat_texture.cpp"

"${CMAKE_CURRENT_LIST_DIR}/shaders/shader_phong.h"
"${CMAKE_CURRENT_LIST_DIR}/shaders/shader_phong.cpp"
"${CMAKE_CURRENT_LIST_DIR}/shaders/shader_phong_color_per_vertex.h"
Expand Down
79 changes: 65 additions & 14 deletions cgogn/rendering/apps/simple_surface_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@

#include <cgogn/core/ui_modules/mesh_provider.h>
#include <cgogn/geometry/ui_modules/surface_differential_properties.h>

#include <cgogn/rendering/ui_modules/surface_render.h>
#include <cgogn/rendering/ui_modules/surface_tex_render.h>




#define DEFAULT_MESH_PATH CGOGN_STR(CGOGN_DATA_PATH) "/meshes/"

Expand All @@ -54,11 +59,15 @@ using Mesh = cgogn::CMap2;
template <typename T>
using Attribute = typename cgogn::mesh_traits<Mesh>::Attribute<T>;
using Vertex = typename cgogn::mesh_traits<Mesh>::Vertex;
using Edge = typename cgogn::mesh_traits<Mesh>::Edge;
using Face = typename cgogn::mesh_traits<Mesh>::Face;

using Vec3 = cgogn::geometry::Vec3;
using Vec2 = cgogn::geometry::Vec2;
using Scalar = cgogn::geometry::Scalar;



int main(int argc, char** argv)
{
std::string filename;
Expand All @@ -75,17 +84,16 @@ int main(int argc, char** argv)

cgogn::ui::MeshProvider<Mesh> mp(app);
cgogn::ui::SurfaceRender<Mesh> sr(app);
cgogn::ui::SurfaceTexRender<Mesh> str(app);
cgogn::ui::SurfaceDifferentialProperties<Mesh> sdp(app);

app.init_modules();

cgogn::ui::View* v1 = app.current_view();
v1->link_module(&mp);
v1->link_module(&sr);
v1->link_module(&str);

// cgogn::ui::View* v2 = app.add_view();
// v2->link_module(&mp);
// v2->link_module(&sr);

if (filename.length() > 0)
{
Expand All @@ -97,25 +105,68 @@ int main(int argc, char** argv)
}

std::shared_ptr<Attribute<Vec3>> vertex_position = cgogn::get_attribute<Vec3, Vertex>(*m, "position");
std::shared_ptr<Attribute<Vec3>> vertex_normal = cgogn::add_attribute<Vec3, Vertex>(*m, "normal");
std::shared_ptr<Attribute<Vec3>> vertex_normal = cgogn::add_attribute<Vec3, Vertex>(*m, "normal");

using Vec4 = cgogn::geometry::Vec4;

// // attrinut de couleur sur l'arête
// std::shared_ptr<Attribute<Vec3>> edge_color = cgogn::add_attribute<Vec3, Edge>(*m, "edge_color");
// std::shared_ptr<Attribute<Vec3>> face_color = cgogn::add_attribute<Vec3, Face>(*m, "face_color");


// plan diagonal de la BB
// std::pair<Vec3, Vec3> bb = mp.meshes_bb();
// Vec3 N = (bb.second - bb.first).normalized();
// Vec4 plane(N[0],N[1],N[2], - N.dot((bb.first+ bb.second)/Scalar(2)));
// plane.topRows<3>() = bb.second - bb.first;

// //parcours de arêtes rouge si coupee par le plan, inon bleu
// cgogn::foreach_cell(*m, [&](Edge e) -> bool {
// if (cgogn::is_incident_to_boundary(*m,e))
// {
// cgogn::value<Vec3>(*m, edge_color, e) = Vec3(0,1,0);
// std::vector<Face> incF = incident_faces(*m, e);
// for(auto f: incF)
// cgogn::value<Vec3>(*m, face_color, f) = Vec3(1,0,1);
// }
// else
// {
// std::vector<Vertex> iv = incident_vertices(*m, e);
// const Vec3& A = cgogn::value<Vec3>(*m, vertex_position, iv[0]);
// const Vec3& B = cgogn::value<Vec3>(*m, vertex_position, iv[1]);
// bool Sa = std::signbit(plane.dot(Vec4(A[0],A[1],A[2],1)));
// bool Sb = std::signbit(plane.dot(Vec4(B[0],B[1],B[2],1)));
// if (Sa != Sb)
// cgogn::value<Vec3>(*m, edge_color, e) = Vec3(1,0,0);
// else
// cgogn::value<Vec3>(*m, edge_color, e) = Vec3(0,0,1);
// }
// return true;
// });

std::shared_ptr<Attribute<Vec3>> face_color = cgogn::add_attribute<Vec3, Face>(*m, "color");
std::shared_ptr<Attribute<Scalar>> face_weight = cgogn::add_attribute<Scalar, Face>(*m, "weight");

cgogn::foreach_cell(*m, [&](Face f) -> bool {
Vec3 c(0, 0, 0);
c[rand() % 3] = 1;
cgogn::value<Vec3>(*m, face_color, f) = c;
cgogn::value<Scalar>(*m, face_weight, f) = double(rand()) / RAND_MAX;
return true;
});

mp.set_mesh_bb_vertex_position(*m, vertex_position);

sdp.compute_normal(*m, vertex_position.get(), vertex_normal.get());

sr.set_vertex_position(*v1, *m, vertex_position);
sr.set_vertex_normal(*v1, *m, vertex_normal);

str.set_vertex_position(*v1, *m, vertex_position);
std::shared_ptr<Attribute<Vec2>> vertex_tc = cgogn::add_attribute<Vec2, Vertex>(*m, "tc");

std::pair<Vec3,Vec3> bb = mp.meshes_bb();
Vec3 bbw = bb.second - bb.first;

cgogn::foreach_cell(*m, [&](Vertex v) -> bool {
Vec2 P = cgogn::value<Vec3>(*m, vertex_position, v).topRows<2>();
Vec2 TC{(P.x() - bb.first.x())/ bbw.x(), (P.y() - bb.first.y())/ bbw.y()};
cgogn::value<Vec2>(*m, vertex_tc, v) = TC;
// std::cout<< TC.transpose() << std::endl;
return true;
});
str.set_vertex_texcoord(*v1, *m, vertex_tc);
str.chekered_texture();
}

return app.launch();
Expand Down
91 changes: 91 additions & 0 deletions cgogn/rendering/shaders/shader_flat_texture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* Copyright (C), IGG Group, ICube, University of Strasbourg, France *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.unistra.fr/ *
* Contact information: [email protected] *
* *
*******************************************************************************/

#define CGOGN_RENDER_SHADERS_FLAT_CPP_

#include <iostream>

#include <cgogn/rendering/shaders/shader_flat_texture.h>

namespace cgogn
{

namespace rendering
{

ShaderFlatTexture* ShaderFlatTexture::instance_ = nullptr;

ShaderFlatTexture::ShaderFlatTexture()
{
const char* vertex_shader_source = R"(
#version 330
uniform mat4 projection_matrix;
uniform mat4 model_view_matrix;

in vec3 vertex_position;
in vec2 vertex_tc;

out vec3 position;
out vec2 tc;
void main()
{
vec4 position4 = model_view_matrix * vec4(vertex_position, 1.0);
position = position4.xyz;
tc = vertex_tc;
gl_Position = projection_matrix * position4;
}
)";

const char* fragment_shader_source = R"(
#version 330

uniform sampler2D texture_img_unit;
uniform vec3 light_position;
uniform bool draw_param;

in vec3 position;
in vec2 tc;

out vec3 frag_out;

void main()
{
vec3 N = normalize(cross(dFdx(position), dFdy(position)));
vec3 L = normalize(light_position - position);
float lambert = 0.25 + 0.75 * max(0.0,dot(N, L));
frag_out = (draw_param) ? vec3(tc,0) : texture(texture_img_unit,vec2(tc.x,1.0-tc.y)).rgb*lambert;
}
)";

load2_bind(vertex_shader_source, fragment_shader_source, "vertex_position", "vertex_tc");
get_uniforms("texture_img_unit","light_position", "draw_param");
}

void ShaderParamFlatTexture::set_uniforms()
{
shader_->set_uniforms_values(texture_->bind(0), light_position_, draw_param_);
}

} // namespace rendering

} // namespace cgogn
64 changes: 64 additions & 0 deletions cgogn/rendering/shaders/shader_flat_texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* Copyright (C), IGG Group, ICube, University of Strasbourg, France *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.unistra.fr/ *
* Contact information: [email protected] *
* *
*******************************************************************************/

#ifndef CGOGN_RENDERING_SHADERS_FLAT_TEXTURE_H_
#define CGOGN_RENDERING_SHADERS_FLAT_TEXTURE_H_

#include <cgogn/rendering/cgogn_rendering_export.h>
#include <cgogn/rendering/shader_program.h>
#include <cgogn/rendering/texture.h>
namespace cgogn
{

namespace rendering
{
DECLARE_SHADER_CLASS(FlatTexture, false, CGOGN_STR(FlatTexture))

class CGOGN_RENDERING_EXPORT ShaderParamFlatTexture : public ShaderParam
{
void set_uniforms() override;

enum VBOName : uint32
{
VERTEX_POSITION = 0,
VERTEX_TC
};

public:
GLVec3 light_position_;
std::shared_ptr<Texture2D> texture_;
bool draw_param_;

using ShaderType = ShaderFlatTexture;

ShaderParamFlatTexture(ShaderType* sh)
: ShaderParam(sh), light_position_(1000, 10000, 100000), texture_(nullptr), draw_param_(false)
{
}
};

} // namespace rendering

} // namespace cgogn

#endif // CGOGN_RENDERING_SHADERS_FLAT_TEXTURE_H__
1 change: 0 additions & 1 deletion cgogn/rendering/shaders/shader_obj_flat_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ ShaderObjFlatTexture::ShaderObjFlatTexture()
#version 330
uniform mat4 projection_matrix;
uniform mat4 model_view_matrix;
uniform mat3 normal_matrix;

uniform usamplerBuffer position_ind;
uniform usamplerBuffer tex_coord_ind;
Expand Down
Loading