Skip to content

Commit

Permalink
feat: use image for point colors
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasnijs committed Aug 2, 2024
1 parent 56d4674 commit 9d4590c
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 84 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "libs/imgui"]
path = libs/imgui
url = [email protected]:ocornut/imgui.git
[submodule "libs/stb"]
path = libs/stb
url = https://github.com/nothings/stb
13 changes: 7 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ mkdir -p build
SOURCE="src/main.cpp"
NAME="boids.html"
VERSION="00"
CONFIG="-DSHOW_UI=0"
CONFIG="-DNO_CONTROL_PANEL"


# files
SET_FRONTEND_TEMPLATE="--shell-file site/index.html"
FILES="${FILES} --preload-file src/view/shaders/boids.vertex.glsl"
FILES="${FILES} --preload-file src/view/shaders/boids.fragment.glsl"
FILES="${FILES} --preload-file res/texture.jpeg"

# includes and libs
INCS="${INCS} -I libs/base -I libs/glad -I libs/imgui"
LIBS="-lm -lGL -lpthread -lglfw -L libs/imgui/ -limgui"
INCS="-I libs/glad -I libs/imgui -I libs/base -I libs/stb"
LIBS="-L ./libs/archives -lm -lGL -lpthread -lglfw -limgui -lstb"

# flags
EMFLAGS="-s USE_GLFW=3 -s FULL_ES3=1 -s USE_WEBGL2=1 -DPLATFORM_WEB"
EMFLAGS="-s USE_GLFW=3 -s FULL_ES3=1 -s USE_WEBGL2=1 -DPLATFORM_WEB -s MAXIMUM_MEMORY=1GB -s ALLOW_MEMORY_GROWTH"
CPPFLAGS="${CPPFLAGS} -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=2 -DVERSION=\"${VERSION}\""
CFLAGS="${CFLAGS} -g -Wall -Wno-deprecated-declarations -Wno-write-strings -Wno-writable-strings -Wno-unused-function -O3 ${INCS} ${CPPFLAGS}"

Expand All @@ -28,7 +29,7 @@ CC="em++"

# building
echo "[build.sh]# building $NAME"
echo ${CC} $CFLAGS ${EMFLAGS} ${FILES} -o "build/$NAME" $SOURCE $LIBS $SET_FRONTEND_TEMPLATE
${CC} $CFLAGS ${EMFLAGS} ${FILES} -o "build/$NAME" $SOURCE $LIBS $SET_FRONTEND_TEMPLATE
echo ${CC} $CFLAGS ${EMFLAGS} ${FILES} ${CONFIG} -o "build/$NAME" $SOURCE $LIBS $SET_FRONTEND_TEMPLATE
${CC} $CFLAGS ${EMFLAGS} ${FILES} ${CONFIG} -o "build/$NAME" $SOURCE $LIBS $SET_FRONTEND_TEMPLATE
echo "[build.sh]# finished building"

1 change: 1 addition & 0 deletions libs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
archives/
65 changes: 65 additions & 0 deletions libs/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/sh

ARCHIVE_DIR="archives"
mkdir -p $ARCHIVE_DIR

IMGUI_DIR="imgui"
IMGUI_CXX=emcc
IMGUI_CXXFLAGS="-Wall -I. -Ibackends"
IMGUI_SOURCES=("imgui.cpp" "imgui_draw.cpp" "imgui_widgets.cpp" "imgui_tables.cpp" "backends/imgui_impl_glfw.cpp" "backends/imgui_impl_opengl3.cpp")

STB_DIR="stb"
STB_CXX=emcc
STB_CXXFLAGS="-Wall -I."
STB_SOURCES=("stb_image.cpp")


build() {
local DIR=$1
local CXX=$2
local CXXFLAGS=$3
shift 3
local SOURCES=("$@")

cd $DIR || exit 1

OBJECTS=()
LIBRARY="lib${DIR}.a"

echo "building $LIBRARY... "

for src in "${SOURCES[@]}"; do
OBJECTS+=("${src%.cpp}.o")
done

for src in "${SOURCES[@]}"; do
$CXX $CXXFLAGS -c $src -o "${src%.cpp}.o"
done


ar rcs "../$ARCHIVE_DIR/$LIBRARY" "${OBJECTS[@]}"

rm -f "${OBJECTS[@]}"

cd .. || exit 1
}

case "$1" in
all)
build $IMGUI_DIR $IMGUI_CXX "$IMGUI_CXXFLAGS" "${IMGUI_SOURCES[@]}"

echo '#define STB_IMAGE_IMPLEMENTATION' > "$STB_DIR/stb_image.cpp"
echo '#include "stb_image.h"' >> "$STB_DIR/stb_image.cpp"
build $STB_DIR $STB_CXX "$STB_CXXFLAGS" "${STB_SOURCES[@]}"
rm -f "$STB_DIR/stb_image.cpp"
;;
clean)
echo "cleaning..."
rm -rf $ARCHIVE_DIR
;;
*)
echo "Usage: $0 {all|clean}"
exit 1
;;
esac

48 changes: 0 additions & 48 deletions libs/buildlibs.sh

This file was deleted.

2 changes: 1 addition & 1 deletion libs/imgui
Submodule imgui updated 60 files
+2 −2 backends/imgui_impl_allegro5.cpp
+1 −0 backends/imgui_impl_allegro5.h
+1 −0 backends/imgui_impl_android.h
+1 −0 backends/imgui_impl_dx10.h
+1 −0 backends/imgui_impl_dx11.h
+2 −0 backends/imgui_impl_dx12.h
+1 −0 backends/imgui_impl_dx9.h
+58 −18 backends/imgui_impl_glfw.cpp
+7 −2 backends/imgui_impl_glfw.h
+1 −0 backends/imgui_impl_glut.h
+2 −0 backends/imgui_impl_metal.h
+3 −0 backends/imgui_impl_opengl2.cpp
+1 −0 backends/imgui_impl_opengl2.h
+4 −0 backends/imgui_impl_opengl3.cpp
+1 −1 backends/imgui_impl_opengl3.h
+2 −0 backends/imgui_impl_osx.h
+3 −2 backends/imgui_impl_osx.mm
+19 −5 backends/imgui_impl_sdl2.cpp
+1 −0 backends/imgui_impl_sdl2.h
+88 −65 backends/imgui_impl_sdl3.cpp
+3 −2 backends/imgui_impl_sdl3.h
+1 −0 backends/imgui_impl_sdlrenderer2.h
+26 −3 backends/imgui_impl_sdlrenderer3.cpp
+1 −0 backends/imgui_impl_sdlrenderer3.h
+6 −2 backends/imgui_impl_vulkan.cpp
+2 −2 backends/imgui_impl_vulkan.h
+1 −0 backends/imgui_impl_wgpu.h
+3 −1 backends/imgui_impl_win32.cpp
+1 −0 backends/imgui_impl_win32.h
+19 −19 docs/BACKENDS.md
+230 −2 docs/CHANGELOG.txt
+6 −39 docs/EXAMPLES.md
+1 −1 docs/FAQ.md
+3 −3 docs/README.md
+1 −1 examples/example_android_opengl3/android/app/src/main/AndroidManifest.xml
+5 −0 examples/example_glfw_opengl2/main.cpp
+6 −1 examples/example_glfw_opengl3/main.cpp
+6 −1 examples/example_glfw_vulkan/main.cpp
+14 −1 examples/example_glfw_wgpu/CMakeLists.txt
+6 −1 examples/example_glfw_wgpu/main.cpp
+5 −0 examples/example_sdl2_directx11/main.cpp
+5 −0 examples/example_sdl2_opengl2/main.cpp
+5 −0 examples/example_sdl2_opengl3/main.cpp
+5 −0 examples/example_sdl2_sdlrenderer2/main.cpp
+6 −1 examples/example_sdl2_vulkan/main.cpp
+3 −3 examples/example_sdl3_opengl3/README.md
+6 −4 examples/example_sdl3_opengl3/main.cpp
+8 −3 examples/example_sdl3_sdlrenderer3/main.cpp
+5 −0 examples/example_win32_opengl3/main.cpp
+20 −0 examples/imgui_examples.sln
+4 −3 examples/libs/emscripten/emscripten_mainloop_stub.h
+9 −4 imconfig.h
+346 −173 imgui.cpp
+292 −60 imgui.h
+1,828 −280 imgui_demo.cpp
+4 −1 imgui_draw.cpp
+180 −81 imgui_internal.h
+29 −26 imgui_tables.cpp
+1,322 −190 imgui_widgets.cpp
+1 −1 misc/freetype/imgui_freetype.cpp
1 change: 1 addition & 0 deletions libs/stb
Submodule stb added at f75e8d
Binary file added res/texture.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 3 additions & 7 deletions src/boids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ update_boid(BoidsApplication *app, Boid *b) {

internal void
update_boids(BoidsApplication *app) {
// TODO(Elias): use a swap buffer
// TODO(Elias): make this parallel
#pragma omp for parallel
for (int32 i = 0; i < app->n; ++i) {
update_boid(app, &(app->bs[i]));
}
Expand All @@ -144,21 +141,20 @@ init_boid(Boid *b, Param *p) {
b->pos.y = rand() % window_height;
b->vel.x = (rand() % 100 - 50) / 50.0f;
b->vel.y = (rand() % 100 - 50) / 50.0f;
printf("b->vel.x: %f, b->vel.y: %f\n", b->vel.x, b->vel.y);
}

internal void
init_boids_app(BoidsApplication *app) {
Param *p = &app->p;
app->n = 800;
app->n = 2000;
p->r = 50;
p->theta_max = 3.14 / 4;
p->theta_max = 3.14 / 5;
p->c = 0.01;
p->s_r = 10;
p->s = 0.1;
p->a = 0.1;
p->max_vel = 10.0;
p->size = 1.0f;
p->size = 2.0f;

app->bs = (Boid *)calloc(MAX_BOIDS, sizeof(Boid));
for (int32 i = 0; i < MAX_BOIDS; ++i) {
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <imgui.h>
#include <backends/imgui_impl_glfw.h>
#include <backends/imgui_impl_opengl3.h>
#include <stb_image.h>

#define ENABLE_ASSERT 1
#define ENABLE_DEBUGLOG 1
Expand All @@ -26,6 +27,7 @@
#include "boids.cpp"

#include "view/shaders.cpp"
#include "view/textures.cpp"
#include "view/rendering.cpp"

// TODO(Elias): !!!! Let boids be influenced by music !!!!
Expand Down Expand Up @@ -62,7 +64,7 @@ frame() {
Process *p = &PROCESS;
float64 start_time = glfwGetTime();

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glfwPollEvents();
Expand All @@ -71,9 +73,7 @@ frame() {
update_boids(&p->boids_app);
render(&p->gpu, &p->boids_app);

#if SHOW_UI
imgui_frame(p);
#endif

glfwSwapBuffers(p->ctx.window);

Expand Down
14 changes: 9 additions & 5 deletions src/view/imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ imgui_frame(Process *p) {
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();

ImGui::SetNextWindowSize(ImVec2(500, 300));
ImGui::Begin("Controls", NULL, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
ImGui::Text("Process Information");
uint32 margin = 10;
ImGui::SetNextWindowSize(ImVec2(300, 50));
ImGui::SetNextWindowPos(ImVec2(window_width - 300 - margin,
window_height - 50 - margin));
ImGui::Begin("Process Information", NULL, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
ImGui::Text("Frame Time: %.3f ms | FPS: %.1f",
p->ctx.frame_time * 1000.0f, p->ctx.fps);
ImGui::Separator();
ImGui::Text("Boids controls");
ImGui::End();
#ifndef NO_CONTROL_PANEL
ImGui::Begin("Controls", NULL, ImGuiWindowFlags_NoCollapse);
ImGui::SliderInt("Number of Boids", &p->boids_app.n, 0, MAX_BOIDS);
ImGui::SliderFloat("Cohesion", &p->boids_app.p.c, 0.0f, 1.0f);
ImGui::SliderFloat("Separation", &p->boids_app.p.s, 0.0f, 1.0f);
Expand All @@ -37,6 +40,7 @@ imgui_frame(Process *p) {
ImGui::SliderFloat("Max Velocity", &p->boids_app.p.max_vel, 1.0f, 20.0f);
ImGui::SliderFloat("Point Size", &p->boids_app.p.size, 1.0f, 10.0f);
ImGui::End();
#endif

ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
Expand Down
28 changes: 18 additions & 10 deletions src/view/rendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ struct gpu_context_t {
uint32 boids_program;
uint32 boids_vao_id;
uint32 boids_vbo_id;
Texture tex;
};

internal bool
Expand All @@ -16,28 +17,34 @@ gpu_init(GpuContext *gpu) {
print_error("Failed to load boids shader program");
return false;
}

if (!load_texture("res/texture.jpeg", &gpu->tex)) {
print_error("failed to load texture");
return false;
}

glUseProgram(gpu->boids_program);
glUniform1i(glGetUniformLocation(gpu->boids_program, "tex"), 0);

glGenVertexArrays(1, &gpu->boids_vao_id);
glGenBuffers(1, &gpu->boids_vbo_id);

return is_success;
}

internal void
gpu_die(GpuContext *gpu) {
texture_die(&gpu->tex);
glDeleteBuffers(1, &gpu->boids_vbo_id);
glDeleteVertexArrays(1, &gpu->boids_vao_id);
glprogram_die(gpu->boids_program);
}

internal void
push_data_to_gpu() {
}

internal void
render(GpuContext *gpu, BoidsApplication *app) {
GLint u_window_width = glGetUniformLocation(gpu->boids_program,
"u_window_width");
GLint u_window_height = glGetUniformLocation(gpu->boids_program,
"u_window_height");
GLint u_point_size = glGetUniformLocation(gpu->boids_program,
"u_point_size");
GLint u_window_width = glGetUniformLocation(gpu->boids_program, "u_window_width" );
GLint u_window_height = glGetUniformLocation(gpu->boids_program, "u_window_height");
GLint u_point_size = glGetUniformLocation(gpu->boids_program, "u_point_size" );

glBindVertexArray(gpu->boids_vao_id);
glBindBuffer(GL_ARRAY_BUFFER, gpu->boids_vbo_id);
Expand All @@ -50,6 +57,7 @@ render(GpuContext *gpu, BoidsApplication *app) {
glEnableVertexAttribArray(1);

glUseProgram(gpu->boids_program);
use_texture(&gpu->tex, 0);
glUniform1f(u_window_width, window_width);
glUniform1f(u_window_height, window_height);
glUniform1f(u_point_size, app->p.size);
Expand Down
6 changes: 5 additions & 1 deletion src/view/shaders/boids.fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
precision mediump float;

in vec2 v_vel;
in vec2 v_uv;

out vec4 out_color;

uniform sampler2D tex;

void main() {
float hue = (atan(v_vel.y, v_vel.x) + 3.14159265359) / (2.0 * 3.14159265359);
float r = abs(hue * 6.0 - 3.0) - 1.0;
float g = 2.0 - abs(hue * 6.0 - 2.0);
float b = 2.0 - abs(hue * 6.0 - 4.0);

out_color = vec4(r, g, b, 1.0);
out_color = mix(texture(tex, v_uv), vec4(r, g, b, 1.0), 0.0);
}
8 changes: 5 additions & 3 deletions src/view/shaders/boids.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ uniform float u_window_height;
uniform float u_point_size;

out vec2 v_vel;
out vec2 v_uv;

void main() {
gl_PointSize = u_point_size;
float x = (a_position.x / u_window_width) * 2.0 - 1.0;
float y = (a_position.y / u_window_height) * 2.0 - 1.0;
gl_Position = vec4(x, -y, 0.0, 1.0);
float x = (a_position.x / u_window_width) * 2.0 - 1.0;
float y = (a_position.y / u_window_height) * 2.0 - 1.0;
gl_Position = vec4(x, -y, 0.0, 1.0);
v_uv = a_position / vec2(u_window_width, u_window_height);
v_vel = a_vel;
}
Loading

0 comments on commit 9d4590c

Please sign in to comment.