-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33c7b88
commit 3323bc2
Showing
11 changed files
with
286 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Task05: Fragment Shader Practice (Implicit Modeling, Sphere Tracing) | ||
|
||
 | ||
|
||
**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` | ||
|
||
 | ||
|
||
|
||
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. | ||
|
||
 | ||
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` | ||
|
||
 | ||
|
||
## 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` | ||
|
||
 | ||
|
||
|
||
## 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |