Skip to content

Commit

Permalink
instanced point drawing, 3D shading, worldspace point sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
wkjarosz committed Dec 26, 2023
1 parent 7e71ba6 commit 0b8e233
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 113 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ if(pcg32_ADDED)
target_include_directories(pcg32 INTERFACE "${pcg32_SOURCE_DIR}")
endif()

CPMAddPackage("gh:wkjarosz/galois#2bf22e85952c8612832838a26baf68ad93beff7a")
CPMAddPackage("gh:wkjarosz/galois#6105d219a6f26777fc02a1649c2e1d56f6e8e51e")
if(galois_ADDED)
message(STATUS "galois++ library added")
target_include_directories(galois++ INTERFACE "${galois_SOURCE_DIR}/include")
Expand Down
1 change: 0 additions & 1 deletion assets/shaders/grid.frag
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
uniform float alpha;
uniform ivec2 size;
in vec4 v_position;
in vec2 v_texcoord;

// The MIT License
Expand Down
29 changes: 29 additions & 0 deletions assets/shaders/point_instance.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
uniform mat4 mvp;
uniform mat3 rotation;
uniform vec3 color;
uniform float point_size;
in vec2 v_texcoord;

// taken from https://gist.github.com/romainguy/6415cbf511233c063a1abe691ad4883b
vec3 Irradiance_SphericalHarmonics(const vec3 n)
{
// Uniform array of 4 vec3 for SH environment lighting
// Computed from "Ditch River" (http://www.hdrlabs.com/sibl/archive.html)
const vec3 sh0 = vec3(0.754554516862612, 0.748542953903366, 0.790921515418539);
const mat3 sh1 = mat3(vec3(-0.188884931542396, -0.277402551592231, -0.377844212327557),
vec3(0.308152705331738, 0.366796330467391, 0.466698181299906),
vec3(-0.083856548007422, 0.092533500963210, 0.322764661032516));
return max(sh0 + sh1 * n, 0.0);
}

void main()
{
float alpha = 1.0;
float radius2 = dot(v_texcoord, v_texcoord);
if (radius2 > 1.0)
discard;
vec3 n = vec3(v_texcoord, sqrt(1.0 - radius2));
vec3 sh = Irradiance_SphericalHarmonics(n);
sh = mix(mix(sh, vec3(dot(sh, vec3(1.0 / 3.0))), 0.5), vec3(1.0), 0.25);
fo_FragColor = vec4(sh * color * alpha, alpha);
}
14 changes: 14 additions & 0 deletions assets/shaders/point_instance.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
uniform mat4 mvp;
uniform mat4 smash;
uniform mat3 rotation;
uniform float point_size;
in vec3 vertices;
in vec3 center;
out vec2 v_texcoord;

void main()
{
gl_Position =
mvp * (vec4(rotation * (point_size * vertices), 1.0) + vec4((smash * vec4(center - 0.5, 1.0)).xyz, 0.0));
v_texcoord = 2.0 * vertices.xy;
}
15 changes: 0 additions & 15 deletions assets/shaders/points.frag

This file was deleted.

9 changes: 0 additions & 9 deletions assets/shaders/points.vert

This file was deleted.

22 changes: 11 additions & 11 deletions include/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ enum CameraType

struct CameraParameters
{
Arcball arcball;
float persp_factor = 0.0f;
float zoom = 1.0f, view_angle = 30.0f;
float dnear = 0.05f, dfar = 1000.0f;
float3 eye = float3{0.0f, 0.0f, 2.0f};
float3 center = float3{0.0f, 0.0f, 0.0f};
float3 up = float3{0.0f, 1.0f, 0.0f};
CameraType camera_type = CAMERA_CURRENT;
Arcball arcball;
float persp_factor = 0.0f;
float zoom = 1.0f, view_angle = 30.0f;
float dnear = 0.05f, dfar = 1000.0f;
static constexpr float3 eye = float3{0.0f, 0.0f, 2.0f};
static constexpr float3 center = float3{0.0f, 0.0f, 0.0f};
static constexpr float3 up = float3{0.0f, 1.0f, 0.0f};
CameraType camera_type = CAMERA_CURRENT;

float4x4 matrix(float window_aspect) const;
};
Expand Down Expand Up @@ -114,7 +114,7 @@ class SampleViewer
void populate_point_subset();
void draw_text(const int2 &pos, const std::string &text, const float4 &col, ImFont *font = nullptr,
int align = TextAlign_RIGHT | TextAlign_BOTTOM) const;
void draw_points(const float4x4 &mvp, const float3 &color);
void draw_points(const float4x4 &mvp, const float4x4 &smash, const float3 &color);
void draw_trigrid(Shader *shader, const float4x4 &mvp, float alpha, const int2x3 &count);
void draw_2D_points_and_grid(const float4x4 &mvp, int2 dims, int plotIndex);
int2 get_draw_range() const;
Expand All @@ -126,7 +126,7 @@ class SampleViewer
int m_num_dimensions = 3;
int3 m_dimension{0, 1, 2};
Array2d<float> m_points, m_subset_points;
vector<float3> m_3d_points;
vector<float3> m_3d_points, m_2d_points;
int m_target_point_count = 256, m_point_count = 256;
int m_subset_count = 0;

Expand All @@ -142,7 +142,7 @@ class SampleViewer
bool m_show_1d_projections = false, m_show_point_nums = false, m_show_point_coords = false,
m_show_coarse_grid = false, m_show_fine_grid = false, m_show_custom_grid = false, m_show_bbox = false;

Shader *m_point_shader = nullptr, *m_2d_point_shader = nullptr, *m_grid_shader = nullptr;
Shader *m_3d_point_shader = nullptr, *m_2d_point_shader = nullptr, *m_grid_shader = nullptr;

int2 m_viewport_pos, m_viewport_pos_GL, m_viewport_size;
float m_animate_start_time = 0.0f;
Expand Down
8 changes: 6 additions & 2 deletions include/arcball.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,14 @@ struct Arcball
return true;
}

Quatf quat() const
{
return linalg::qmul(m_incr, m_quat);
}

Mat44f matrix() const
{
auto m = linalg::qmat(linalg::qmul(m_incr, m_quat));
return {{m.x, 0.f}, {m.y, 0.f}, {m.z, 0.f}, {0.f, 0.f, 0.f, 1.f}};
return linalg::rotation_matrix(quat());
}

private:
Expand Down
7 changes: 7 additions & 0 deletions include/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ class Shader
set_buffer(name, get_type<T>(), 2, shape, vecs.data());
}

template <typename T, int M>
void set_buffer(const std::string &name, const std::vector<linalg::vec<T, M>> &vecs, size_t offset, size_t count)
{
size_t shape[3] = {count, M, 1};
set_buffer(name, get_type<T>(), 2, shape, &vecs[offset]);
}

template <typename T>
void set_buffer(const std::string &name, const std::vector<T> &data)
{
Expand Down
Loading

0 comments on commit 0b8e233

Please sign in to comment.