Skip to content

Commit

Permalink
task05
Browse files Browse the repository at this point in the history
  • Loading branch information
nobuyuki83 committed May 19, 2024
1 parent 33c7b88 commit 3323bc2
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ add_subdirectory(task01)
add_subdirectory(task02)
add_subdirectory(task03)
add_subdirectory(task04)
# add_subdirectory(task05)
add_subdirectory(task05)
# add_subdirectory(task06)
# add_subdirectory(task07)
# add_subdirectory(task08)
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ Topics:
|(2)<br>Apr. 15| **Parametric curves / surfaces** <br/>**Rasterization in 2D**, Digital Differential Analyzer | [task01](task01) | [[5]](http://nobuyuki-umetani.com/acg2024s/rasterization_2d.pdf) [[6]](http://nobuyuki-umetani.com/acg2024s/barycentric_coordinates.pdf) |
|(3)<br>Apr. 22| **Parametric representation**<br/>Bézier curve, polynominal | [task02](task02) | [[7] ](http://nobuyuki-umetani.com/acg2024s/parametric_curve.pdf) [[8]](http://nobuyuki-umetani.com/acg2024s/polynominal.pdf) |
|(5)<br>May 7| **Coordinate transformation**<br>Affine, homography transformation | [task03](task03) | [[9]](http://nobuyuki-umetani.com/acg2024s/transformation.pdf) [[10]](http://nobuyuki-umetani.com/acg2024s/transformation_homogeneous_2d.pdf) [[11]](http://nobuyuki-umetani.com/acg2024s/transformation_homogeneous_3d.pdf) |
|(4)<br>May 13| **Graphics pipeline**<br>depth buffer method, shading, shadow, anti aliasing | [task04](task04) | [[12]](http://nobuyuki-umetani.com/acg2024s/rasterization_3d.pdf)[[13]](http://nobuyuki-umetani.com/acg2024s/graphics_pipeline.pdf)[[14]](http://nobuyuki-umetani.com/acg2024s/shading.pdf) |
|(6)<br>May 20| **Ray Casting 1**<br/>spatial data structure | task05 | |
|(4)<br>May 13| **Graphics pipeline**<br>depth buffer method, shading, shadow, anti aliasing | [task04](task04) | [[12]](http://nobuyuki-umetani.com/acg2024s/rasterization_3d.pdf) [[13]](http://nobuyuki-umetani.com/acg2024s/graphics_pipeline.pdf) |
|(6)<br>May 20| **Ray Casting 1**<br/>spatial data structure | [task05](task05) | [[14]](http://nobuyuki-umetani.com/acg2024s/shading.pdf) [[15]](http://nobuyuki-umetani.com/acg2024s/rasterization_subpixel.pdf) [[16]](http://nobuyuki-umetani.com/acg2024s/implicit_modeling.pdf) |
|(7)<br>May 27| **Ray Casting 2**<br>Rendering equation, Monte Carlo integration | task06 | |
|(8)<br>June 3| **Character animation**<br> Linear blend skinning | task07 | |
|(9)<br>June 10| Guest lecture by Dr. Rex West | | |
Expand Down Expand Up @@ -82,7 +82,7 @@ Look at the following document.
| [task02](task02) | **Rasterization of parametric curves**<br> Quadratic Bézier curve, root of polynominal | <img src="task02/preview.png" width=100px> |
| [task03](task03) | **Perspectively-correct texture mapping**<br>rasterization of triangle, barycentric coordinate | <img src="task03/preview.png" width=100px> |
| [task04](task04) | **Vertex shader practice** <br>Rendering pipeline, mirror reflection, OpenGL | <img src="task04/preview.png" width=100px> |
| task05 | **Fragment shader practice**<br>Ray marching method, CSG modeling, implicit modeling | <img src="task05/preview.png" width=100px> |
| [task05](task05) | **Fragment shader practice**<br>Ray marching method, CSG modeling, implicit modeling | <img src="task05/preview.png" width=100px> |
| task06 | **Monte Carlo integration**<br>Multiple importance sampling, path tracing, rendering equation | <img src="task06/preview.png" width=100px> |
| task07 | TBD | |
| task08 | **Skeletal Character Animation**<br>Linear blend skinning, articulated rigid body | <img src="task08/preview.png" width=100px> |
Expand Down Expand Up @@ -113,6 +113,8 @@ Look at the following document.
- [[12] 3D Rasterization](http://nobuyuki-umetani.com/acg2024s/rasterization_3d.pdf)
- [[13] Graphics Pipeline](http://nobuyuki-umetani.com/acg2024s/graphics_pipeline.pdf)
- [[14] Shading](http://nobuyuki-umetani.com/acg2024s/shading.pdf)
- [[15] Subpixel Effect](http://nobuyuki-umetani.com/acg2024s/rasterization_subpixel.pdf)
- [[16] Implicit Modeling](http://nobuyuki-umetani.com/acg2024s/implicit_modeling.pdf)



Expand Down
54 changes: 54 additions & 0 deletions task05/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
cmake_minimum_required(VERSION 3.12)

#############################
# set C++ detail
enable_language(CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

#############################

# set project name
project(task05)

# define a macro to get absolute path in C++
add_definitions(-DSOURCE_DIR="${PROJECT_SOURCE_DIR}")

#############################
# specifying libraries to use

# use opengl
find_package(OpenGL REQUIRED)

# use glfw
set(CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../external/glfwlib)
find_package(glfw3 REQUIRED)

########################
# include, build, & link

include_directories(${PROJECT_NAME}
${CMAKE_CURRENT_SOURCE_DIR}/../external/delfem2/include
${CMAKE_CURRENT_SOURCE_DIR}/../external/glad3/include
)

add_executable(${PROJECT_NAME}
# main_ans.cpp
main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../external/glad3/include/glad/glad.h
${CMAKE_CURRENT_SOURCE_DIR}/../external/glad3/src/glad.c
)

# https://stackoverflow.com/questions/33678965/need-to-link-cmake-project-to-dl-library
target_link_libraries(${PROJECT_NAME}
# stdc++fs # uncomment here if <filesystem> cannot be included
OpenGL::GL
glfw
${CMAKE_DL_LIBS})

#############################
# showing status of libraries

GET_TARGET_PROPERTY(GLFW_INCLUDE_DIR
glfw INTERFACE_INCLUDE_DIRECTORIES)
message(STATUS "task02 glfw include directory: ${GLFW_INCLUDE_DIR}")
Binary file added task05/Csg_tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions task05/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Task05: Fragment Shader Practice (Implicit Modeling, Sphere Tracing)

![preview](preview.png)

**Deadline: May 23rd (Thu) at 15:00pm**

----

## Before Doing Assignment

If you have not done the [task01](../task01), [task02](../task02) do it first to set up the C++ development environment.

Follow [this document](../doc/submit.md) to submit the assignment, In a nutshell, before doing the assignment,
- make sure you synchronized the `main ` branch of your local repository to that of remote repository.
- make sure you created branch `task05` from `main` branch.
- make sure you are currently in the `task05` branch (use `git branch -a` command).

Additionally, you need to install `glfw` library.
Follow [this document](../doc/setup_glfw.md) to install glfw.

Now you are ready to go!

---

## Problem 1

1. Build the code using cmake
2. Run the code
3. Take a screenshot image (looks like image at the top)
4. Save the screenshot image overwriting `task05/problem1.png`

![problem1](problem1.png)


The current code render a cylinder defined by a signed distance function using the sphere tracing technique.

Your following tasks are to perform CSG operation (union, intersection, difference) as the following image.

![csg](Csg_tree.png)
Figure 1. Let's design an object using CSG modeling.

## Problem 2

Write some code (about 5 ~ 10 lines) around `line #40` in `shader.frag` to define the signed distance function resulting from the CSG operation in Figure 1.
(The resulting object should be colored in green).

Save the screenshot image overwriting `task05/problem2.png`

![problem2](problem2.png)

## Problem 3

Write some code (about 5 ~ 10 lines) around `line #47` in `shader.frag`
to paint the object such that it looks similar to the coloring in Figure 1.

Save the screenshot image overwriting `task05/problem2.png`

![problem3](problem3.png)


## After Doing the Assignment

After modify the code, push the code and submit a pull request.
Make sure your pull request only contains the files you edited.
62 changes: 62 additions & 0 deletions task05/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <filesystem>
// #include <experimental/filesystem> // uncomment here if the <filesystem> cannot be included above
#include <cstdlib>
#include <glad/glad.h>
#define GL_SILENCE_DEPRECATION
#include <GLFW/glfw3.h>
//
#include "../src/util_opengl.h"

int main() {
if (!glfwInit()) { exit(EXIT_FAILURE); }
// set OpenGL's version (note: ver. 2.1 is very old, but I chose because it's simple)
::glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
::glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
GLFWwindow *window = ::glfwCreateWindow(500, 500, "task05", nullptr, nullptr);
if (!window) { // exit if failed to create window
::glfwTerminate();
exit(EXIT_FAILURE);
}
::glfwMakeContextCurrent(window); // working on this window below
//
if (!gladLoadGL()) { // glad: load all OpenGL function pointers
printf("Something went wrong in loading OpenGL functions!\n");
exit(-1);
}

int shaderProgram;
{
const auto vrt_path = std::filesystem::path(SOURCE_DIR) / "shader.vert";
const auto frg_path = std::filesystem::path(SOURCE_DIR) / "shader.frag";
std::string vrt = acg::load_file_as_string(vrt_path.string().c_str()); // read source code of vertex shader program
std::string frg = acg::load_file_as_string(frg_path.string().c_str()); // read source code of fragment shader program
shaderProgram = acg::create_shader_program(vrt, frg); // compile the shader on GPU
}

glDisable(GL_MULTISAMPLE);
const GLint iloc = glGetUniformLocation(shaderProgram, "time"); // location of variable in the shader program

::glClearColor(1, 1, 1, 1); // set the color to fill the frame buffer when glClear is called.
::glEnable(GL_DEPTH_TEST);
while (!::glfwWindowShouldClose(window)) {
::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const auto time = static_cast<float>(glfwGetTime());
glUniform1f(iloc,time);
::glMatrixMode(GL_PROJECTION);
::glLoadIdentity(); // identity transformation
::glMatrixMode(GL_MODELVIEW);
::glLoadIdentity(); // identity transformation
::glUseProgram(shaderProgram); // use the shader program from here
::glBegin(GL_QUADS); // draw a rectangle that cover the entire screen
::glVertex2d(-1,-1);
::glVertex2d(+1,-1);
::glVertex2d(+1,+1);
::glVertex2d(-1,+1);
::glEnd();
::glfwSwapBuffers(window);
::glfwPollEvents();
}
::glfwDestroyWindow(window);
::glfwTerminate();
exit(EXIT_SUCCESS);
}
Binary file added task05/problem1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added task05/problem2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added task05/problem3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions task05/shader.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#version 120

// see the GLSL 1.2 specification:
// https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.1.20.pdf

// signed distance function of a cylinder the axis is aligned to z-direction
// code from: https://iquilezles.org/articles/distfunctions/
float sdCappedCylinder( vec3 p, float h, float r )
{
vec2 d = abs(vec2(length(p.xz),p.y)) - vec2(r,h);
return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}

// signed distance function of an axis aligned box.
// code from: https://iquilezles.org/articles/distfunctions/
float sdBox( vec3 p, vec3 b )
{
vec3 q = abs(p) - b;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
}

// signed distance function of a sphere
// code from: https://iquilezles.org/articles/distfunctions/
float sdSphere( vec3 p, float s )
{
return length(p)-s;
}

// here is the parameter use to draw the objects
float len_cylinder = 0.8; // length of the cylinder
float rad_cylinder = 0.45; // radius of the cylinder
float rad_sphere = 0.8; // radius of the sphere
float box_size = 0.6; // size of box

/// singed distance function at the position `pos`
float SDF(vec3 pos)
{
float d0 = sdCappedCylinder(pos, len_cylinder, rad_cylinder);
// write some code to combine the signed distance fields above to design the object described in the README.md
return d0; // comment out and define new distance
}

/// RGB color at the position `pos`
vec3 SDF_color(vec3 pos)
{
// write some code below to return color (RGB from 0 to 1) to paint the object describe in README.md
return vec3(0., 1., 0.); // comment out and define new color
}

uniform float time; // current time given from CPU

void main()
{
// camera position
vec3 cam_pos = normalize( vec3(sin(time),cos(time),0.5*sin(time)) );

// local frame defined on the cameera
vec3 frame_z = cam_pos;
vec3 frame_x = normalize(cross(vec3(0,0,1),frame_z));
vec3 frame_y = cross(frame_z,frame_x);

// gl_FragCoord: the coordinate of the pixel
// left-bottom is (0,0), right-top is (W,H)
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_FragCoord.xhtml
vec2 scr_xy = gl_FragCoord.xy / vec2(500,500) * 2.0 - vec2(1,1); // canonical screen position [-1,+1] x [-1,+1]
vec3 src = frame_x * scr_xy.x + frame_y * scr_xy.y + frame_z * 1; // source of ray from pixel
vec3 dir = -frame_z; // direction of ray (looking at the origin)

vec3 pos_cur = src; // the current ray position
for(int itr=0;itr<60;++itr){
float s0 = SDF(pos_cur);
if( s0 < 0.0 ){ // ray starting from inside the object
gl_FragColor = vec4(1, 0, 0, 1); // paint red
return;
}
if( s0 < 1.0e-3 ){ // the ray hit the implicit surfacee
float eps = 1.0e-3;
float sx = SDF(pos_cur+vec3(eps,0,0))-s0; // finite difference x-direction
float sy = SDF(pos_cur+vec3(0,eps,0))-s0; // finite difference x-direction
float sz = SDF(pos_cur+vec3(0,0,eps))-s0; // finite difference y-direction
vec3 nrm = normalize(vec3(sx,sy,sz)); // normal direction
float coeff = -dot(nrm, dir); // Lambersian reflection. The light is at the camera position.
vec3 color = SDF_color(pos_cur);
gl_FragColor = vec4(coeff*color, 1);
return;
}
pos_cur += s0 * dir; // advance ray
}
gl_FragColor = vec4(0.9, 0.9, 1.0, 1); // ray doesn't hit the object
}
10 changes: 10 additions & 0 deletions task05/shader.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#version 120

// see the GLSL 1.2 specification:
// https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.1.20.pdf

void main()
{
// draw the primitive without transformation
gl_Position = gl_Vertex; // following code do nothing (input == output)
}

0 comments on commit 3323bc2

Please sign in to comment.