Skip to content

Commit

Permalink
use checkboard as default texture if model has texture coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
LiangliangNan committed Dec 27, 2024
1 parent e423ce4 commit ec4d62c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
43 changes: 39 additions & 4 deletions easy3d/renderer/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
********************************************************************/

#include <easy3d/renderer/state.h>
#include <easy3d/renderer/texture_manager.h>
#include <easy3d/renderer/opengl_error.h>
#include <easy3d/util/logging.h>
#include <easy3d/util/resource.h>


namespace easy3d {
Expand Down Expand Up @@ -106,9 +109,16 @@ namespace easy3d {
coloring_method_ = TEXTURED;
property_location_ = texcoord_location;
property_name_ = texcoord_name;
texture_ = texture;
texture_repeat_ = repeat;
texture_fractional_repeat_ = repeat_fraction;
if (texture) {
texture_ = texture;
texture_repeat_ = repeat;
texture_fractional_repeat_ = repeat_fraction;
}
else { // create a default color texture
texture_ = create_color_texture();
texture_repeat_ = 5.0f;
texture_fractional_repeat_ = 0.0f;
}
}


Expand All @@ -117,7 +127,10 @@ namespace easy3d {
coloring_method_ = SCALAR_FIELD;
property_location_ = scalar_location;
property_name_ = scalar_name;
texture_ = texture;
if (texture)
texture_ = texture;
else // create a default scalar texture
texture_ = create_scalar_texture();
texture_repeat_ = 1.0f;
texture_fractional_repeat_ = 0.0f;
clamp_lower_ = clamp_lower;
Expand All @@ -129,6 +142,28 @@ namespace easy3d {
coloring_method_ = method;
property_location_ = location;
property_name_ = name;

if (method == TEXTURED)
texture_ = create_color_texture();
else if (method == SCALAR_FIELD)
texture_ = create_scalar_texture();
}


Texture* State::create_color_texture() {
const std::string texture_file = resource::directory() + "/textures/checkerboard.png";
auto tex = TextureManager::request(texture_file, Texture::REPEAT);
LOG_IF_FIRST_N(3, !tex, ERROR) << "failed to create texture from " << texture_file << ". " << COUNTER;
return tex;
}


Texture* State::create_scalar_texture() {
const std::string texture_file = resource::directory() + "/colormaps/default.png";
auto tex = TextureManager::request(texture_file);
LOG_IF_FIRST_N(3, !tex, ERROR) << "failed to create texture from " << texture_file << ". " << COUNTER;
easy3d_debug_log_gl_error
return tex;
}

}
18 changes: 15 additions & 3 deletions easy3d/renderer/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ namespace easy3d {
* Constructs a scheme for textured rendering.
* @param texcoord_location The location of texture coordinate.
* @param texcoord_name The name of the texture coordinate property.
* @param texture The pointer to the texture.
* @param texture The pointer to the texture. If nullptr, a default texture will be created from
* "textures/checkerboard.png". Client code is expected to provide a meaningful texture at a later stage.
* @param repeat The repeat factor of the texture. Default value is 1.0.
* @param repeat_fraction The fractional repeat factor of the texture. Default value is 0.0.
* @note
*/
void set_texture_coloring(Location texcoord_location, const std::string &texcoord_name,
const Texture *texture = nullptr, float repeat = 1.0f,
Expand All @@ -131,7 +133,8 @@ namespace easy3d {
* Constructs a scheme for rendering scalar fields.
* @param scalar_location The location of the scalar field.
* @param scalar_name The name of the scalar field.
* @param texture The pointer to the texture.
* @param texture The pointer to the texture. Default is nullptr, a scalar default texture will be created
* from "/colormaps/default.png". Client code can provide a more suitable texture at a later stage.
* @param clamp_lower The percentage of values to be clamped at the lower side of the range. Default is 5%.
* @param clamp_upper The percentage of values to be clamped at the upper side of the range. Default is 5%.
*/
Expand Down Expand Up @@ -256,6 +259,15 @@ namespace easy3d {
void set_highlight_range(const std::pair<int, int> &range) { highlight_range_ = range; }
const std::pair<int, int> &highlight_range() const { return highlight_range_; }

protected:
// Create a default texture.
// Default texture file: "/textures/checkerboard.png";
Texture* create_color_texture();

// Create a default texture for rendering scalar fields.
// Default texture file: "/colormaps/default.png";
Texture* create_scalar_texture();

protected:
bool visible_;
bool selected_;
Expand All @@ -275,7 +287,7 @@ namespace easy3d {
const Texture *texture_;
// How many times do you want to repeat the texture?
float texture_repeat_;
// Control at a finer level: 100 fractional repeat == repeat.
// Control at a finer level: 100 fractional repeat == 1 repeat.
float texture_fractional_repeat_;

bool ssao_enabled_;
Expand Down
11 changes: 0 additions & 11 deletions tests/visualization_scalar_field/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,6 @@ int test_scalar_field(int duration) {

drawable->set_scalar_coloring(State::VERTEX, "v:elevation", nullptr, 0.0f, 0.0f);

// Create texture for coloring the scalar field.
const std::string texture_file = resource::directory() + "/colormaps/rainbow.png";
Texture *texture = TextureManager::request(texture_file);
if (!texture) {
LOG(ERROR) << "failed to create texture.";
return EXIT_FAILURE;
}

// Use the texture
drawable->set_texture(texture);

viewer.set_usage("testing scalar field...");

Timer<>::single_shot(duration, (Viewer*)&viewer, &Viewer::exit);
Expand Down
19 changes: 9 additions & 10 deletions tutorials/Tutorial_303_ScalarField/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,15 @@ int main(int argc, char **argv) {

drawable->set_scalar_coloring(State::VERTEX, "v:elevation", nullptr, 0.0f, 0.0f);

// Create texture for coloring the scalar field.
const std::string texture_file = resource::directory() + "/colormaps/rainbow.png";
Texture *texture = TextureManager::request(texture_file);
if (!texture) {
LOG(ERROR) << "failed to create texture.";
return EXIT_FAILURE;
}

// Use the texture
drawable->set_texture(texture);
// A default scalar texture is automatically created.
// However, you can choose a more suitable one using the code below:
// const std::string texture_file = resource::directory() + "/colormaps/rainbow.png";
// Texture *texture = TextureManager::request(texture_file);
// if (!texture) {
// LOG(ERROR) << "failed to create texture.";
// return EXIT_FAILURE;
// }
// drawable->set_texture(texture); // Use the texture

// run the viewer
return viewer.run();
Expand Down

0 comments on commit ec4d62c

Please sign in to comment.