3DFileViewer: Add texture menu

This allows setting different texture wrap modes
and setting different texture coordinate scale factors.
This commit is contained in:
Stephan Unverwerth 2021-08-11 23:43:28 +02:00 committed by Andreas Kling
parent b9523e15df
commit 75bc7be622
Notes: sideshowbarker 2024-07-18 07:04:52 +09:00
3 changed files with 103 additions and 6 deletions

View file

@ -29,7 +29,7 @@ Mesh::Mesh(Vector<Vertex> vertices, Vector<TexCoord> tex_coords, Vector<Triangle
{
}
void Mesh::draw()
void Mesh::draw(float uv_scale)
{
// Light direction
const FloatVector3 light_direction(1.f, 1.f, 1.f);
@ -76,7 +76,7 @@ void Mesh::draw()
m_vertex_list.at(m_triangle_list[i].a).z);
if (is_textured())
glTexCoord2f(m_tex_coords.at(m_triangle_list[i].tex_coord_index0).u, 1.0f - m_tex_coords.at(m_triangle_list[i].tex_coord_index0).v);
glTexCoord2f(m_tex_coords.at(m_triangle_list[i].tex_coord_index0).u * uv_scale, (1.0f - m_tex_coords.at(m_triangle_list[i].tex_coord_index0).v) * uv_scale);
// Vertex 2
glVertex3f(
@ -85,7 +85,7 @@ void Mesh::draw()
m_vertex_list.at(m_triangle_list[i].b).z);
if (is_textured())
glTexCoord2f(m_tex_coords.at(m_triangle_list[i].tex_coord_index1).u, 1.0f - m_tex_coords.at(m_triangle_list[i].tex_coord_index1).v);
glTexCoord2f(m_tex_coords.at(m_triangle_list[i].tex_coord_index1).u * uv_scale, (1.0f - m_tex_coords.at(m_triangle_list[i].tex_coord_index1).v) * uv_scale);
// Vertex 3
glVertex3f(
@ -94,7 +94,7 @@ void Mesh::draw()
m_vertex_list.at(m_triangle_list[i].c).z);
if (is_textured())
glTexCoord2f(m_tex_coords.at(m_triangle_list[i].tex_coord_index2).u, 1.0f - m_tex_coords.at(m_triangle_list[i].tex_coord_index2).v);
glTexCoord2f(m_tex_coords.at(m_triangle_list[i].tex_coord_index2).u * uv_scale, (1.0f - m_tex_coords.at(m_triangle_list[i].tex_coord_index2).v) * uv_scale);
glEnd();
}

View file

@ -22,7 +22,7 @@ public:
size_t triangle_count() const { return m_triangle_list.size(); }
void draw();
void draw(float uv_scale);
bool is_textured() const { return m_tex_coords.size() > 0; }

View file

@ -39,6 +39,9 @@ public:
void toggle_rotate_z() { m_rotate_z = !m_rotate_z; }
void set_rotation_speed(float speed) { m_rotation_speed = speed; }
void set_stat_label(RefPtr<GUI::Label> l) { m_stats = l; };
void set_wrap_s_mode(GLint mode) { m_wrap_s_mode = mode; }
void set_wrap_t_mode(GLint mode) { m_wrap_t_mode = mode; }
void set_texture_scale(float scale) { m_texture_scale = scale; }
void toggle_show_frame_rate()
{
@ -97,6 +100,9 @@ private:
int m_cycles = 0;
int m_accumulated_time = 0;
RefPtr<GUI::Label> m_stats;
GLint m_wrap_s_mode = GL_REPEAT;
GLint m_wrap_t_mode = GL_REPEAT;
float m_texture_scale = 1.0f;
};
void GLContextWidget::paint_event(GUI::PaintEvent& event)
@ -141,8 +147,11 @@ void GLContextWidget::timer_event(Core::TimerEvent&)
glRotatef(m_angle_y, 0, 1, 0);
glRotatef(m_angle_z, 0, 0, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_wrap_s_mode);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, m_wrap_t_mode);
if (!m_mesh.is_null())
m_mesh->draw();
m_mesh->draw(m_texture_scale);
m_context->present();
@ -319,6 +328,94 @@ int main(int argc, char** argv)
view_menu.add_action(*show_frame_rate_action);
auto& texture_menu = window->add_menu("&Texture");
auto& wrap_u_menu = texture_menu.add_submenu("Wrap &S");
GUI::ActionGroup wrap_s_actions;
wrap_s_actions.set_exclusive(true);
auto wrap_u_repeat_action = GUI::Action::create_checkable("&Repeat", [&widget](auto&) {
widget.set_wrap_s_mode(GL_REPEAT);
});
auto wrap_u_mirrored_repeat_action = GUI::Action::create_checkable("&Mirrored Repeat", [&widget](auto&) {
widget.set_wrap_s_mode(GL_MIRRORED_REPEAT);
});
auto wrap_u_clamp_action = GUI::Action::create_checkable("&Clamp", [&widget](auto&) {
widget.set_wrap_s_mode(GL_CLAMP);
});
wrap_s_actions.add_action(*wrap_u_repeat_action);
wrap_s_actions.add_action(*wrap_u_mirrored_repeat_action);
wrap_s_actions.add_action(*wrap_u_clamp_action);
wrap_u_menu.add_action(*wrap_u_repeat_action);
wrap_u_menu.add_action(*wrap_u_mirrored_repeat_action);
wrap_u_menu.add_action(*wrap_u_clamp_action);
wrap_u_repeat_action->set_checked(true);
auto& wrap_t_menu = texture_menu.add_submenu("Wrap &T");
GUI::ActionGroup wrap_t_actions;
wrap_t_actions.set_exclusive(true);
auto wrap_t_repeat_action = GUI::Action::create_checkable("&Repeat", [&widget](auto&) {
widget.set_wrap_t_mode(GL_REPEAT);
});
auto wrap_t_mirrored_repeat_action = GUI::Action::create_checkable("&Mirrored Repeat", [&widget](auto&) {
widget.set_wrap_t_mode(GL_MIRRORED_REPEAT);
});
auto wrap_t_clamp_action = GUI::Action::create_checkable("&Clamp", [&widget](auto&) {
widget.set_wrap_t_mode(GL_CLAMP);
});
wrap_t_actions.add_action(*wrap_t_repeat_action);
wrap_t_actions.add_action(*wrap_t_mirrored_repeat_action);
wrap_t_actions.add_action(*wrap_t_clamp_action);
wrap_t_menu.add_action(*wrap_t_repeat_action);
wrap_t_menu.add_action(*wrap_t_mirrored_repeat_action);
wrap_t_menu.add_action(*wrap_t_clamp_action);
wrap_t_repeat_action->set_checked(true);
auto& texture_scale_menu = texture_menu.add_submenu("S&cale");
GUI::ActionGroup texture_scale_actions;
texture_scale_actions.set_exclusive(true);
auto texture_scale_025_action = GUI::Action::create_checkable("0.25x", [&widget](auto&) {
widget.set_texture_scale(0.25f);
});
auto texture_scale_05_action = GUI::Action::create_checkable("0.5x", [&widget](auto&) {
widget.set_texture_scale(0.5f);
});
auto texture_scale_1_action = GUI::Action::create_checkable("1x", [&widget](auto&) {
widget.set_texture_scale(1);
});
auto texture_scale_2_action = GUI::Action::create_checkable("2x", [&widget](auto&) {
widget.set_texture_scale(2);
});
auto texture_scale_4_action = GUI::Action::create_checkable("4x", [&widget](auto&) {
widget.set_texture_scale(4);
});
texture_scale_actions.add_action(*texture_scale_025_action);
texture_scale_actions.add_action(*texture_scale_05_action);
texture_scale_actions.add_action(*texture_scale_1_action);
texture_scale_actions.add_action(*texture_scale_2_action);
texture_scale_actions.add_action(*texture_scale_4_action);
texture_scale_menu.add_action(*texture_scale_025_action);
texture_scale_menu.add_action(*texture_scale_05_action);
texture_scale_menu.add_action(*texture_scale_1_action);
texture_scale_menu.add_action(*texture_scale_2_action);
texture_scale_menu.add_action(*texture_scale_4_action);
texture_scale_1_action->set_checked(true);
auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("3D File Viewer", app_icon, window));