{"id":23658622,"url":"https://github.com/oopb/opengl_ray_tracing","last_synced_at":"2025-11-28T04:30:40.180Z","repository":{"id":267919592,"uuid":"902378102","full_name":"oopb/opengl_ray_tracing","owner":"oopb","description":"本项目为北航BUAA 2024学年秋季学期 《计算机图形学》（孟宪海）课程大作业，所选实现方式为OpenGL（c++中的freeglut库），实现效果为光线追踪、镜面反射、自然软阴影、纹理映射等。","archived":false,"fork":false,"pushed_at":"2024-12-26T13:51:30.000Z","size":643,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-26T14:30:01.357Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oopb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-12T12:58:57.000Z","updated_at":"2024-12-26T13:51:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"630500b7-3a73-4860-ad86-485927d9f0be","html_url":"https://github.com/oopb/opengl_ray_tracing","commit_stats":null,"previous_names":["oopb/opengl_ray_tracing"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oopb%2Fopengl_ray_tracing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oopb%2Fopengl_ray_tracing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oopb%2Fopengl_ray_tracing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oopb%2Fopengl_ray_tracing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oopb","download_url":"https://codeload.github.com/oopb/opengl_ray_tracing/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239644122,"owners_count":19673577,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-29T00:48:29.795Z","updated_at":"2025-11-28T04:30:34.539Z","avatar_url":"https://github.com/oopb.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OpenGL Ray-Tracing\n\n\u003e This is a simple ray-tracing program that uses OpenGL to render the scene. The program is written in C++ and uses the\n\u003e OpenGL library for rendering. The program uses the ray-tracing algorithm to render the scene. The program uses the\n\u003e Phong reflection model to calculate the color of the objects in the scene.\n\n## Table of Contents\n\n- [OpenGL Ray-Tracing](#opengl-ray-tracing)\n    - [Table of Contents](#table-of-contents)\n    - [Installation](#installation)\n    - [Usage](#usage)\n    - [Details](#details)\n        - [Ray-Tracing Algorithm](#ray-tracing-algorithm)\n        - [Phong Reflection Model](#phong-reflection-model)\n        - [Natural Soft Shadows](#natural-soft-shadows)\n        - [Reflection](#reflection)\n    - [Acknowledgements](#acknowledgements)\n\n## Installation\n\nThis program requires the OpenGL library (`freeglut`) to run. Required libraries are included in the\nrepository [releases](https://github.com/oopb/opengl_ray_tracing/releases)\n(please unzip mingw.zip to the current directory \"./mingw64\").\n\nTo install the program, you need to clone the repository and compile the program using the following commands:\n\n```bash\ngit clone git@github.com:oopb/opengl_ray_tracing.git\ncd opengl_ray_tracing\n```\n\nTo compile the program, you need to run the following command:\n\n```bash\n.\\mingw64\\bin\\g++.exe -o ray_tracing ray_tracing.cpp -lfreeglut -lopengl32 -lglu32\n```\n\n## Usage\n\nTo run the program, you need to run the following command:\n\n```bash\n./ray_tracing\n```\n\nOr you can also double-click on the `ray_tracing.exe` file to run the program.\n\nAfter running the program (and wait for a few seconds), you should see a window with the rendered scene.\n\n![effect.png](effect.png)\n\n## Details\n\n### Ray-Tracing Algorithm\n\nThe ray-tracing algorithm is a rendering algorithm that uses the ray-tracing technique to render the scene. The\nray-tracing algorithm works by tracing the path of the light rays in the scene. The algorithm works by casting a ray\nfrom the camera through each pixel in the image plane. The ray is then tested for intersection with the objects in the\nscene. If the ray intersects with an object, the color of the object is calculated using the Phong reflection model.\n\n### Phong Reflection Model\n\nThe Phong reflection model is a lighting model that is used to calculate the color of the objects in the scene. The\nPhong reflection model works by calculating the color of the object based on the ambient, diffuse, and specular\ncomponents of the lighting. The ambient component is the color of the object when there is no light. The diffuse\ncomponent is the color of the object when the light is shining directly on the object. The specular component is\nthe color of the object when the light is shining directly on the object and is reflected off the object.\n\nThe Phong reflection model is used to calculate the color of the objects in the scene. The color of the object is\ncalculated using the following formula:\n\n```cpp\ncolor = ambient + diffuse + specular\n```\n\nThe ambient component is calculated using the following formula:\n\n```cpp\nambient = ka * Ia\n```\n\nThe diffuse component is calculated using the following formula:\n\n```cpp\ndiffuse = kd * Id * max(0, dot(N, L))\n```\n\nThe specular component is calculated using the following formula:\n\n```cpp\nspecular = ks * Is * pow(max(0, dot(R, V)), shininess)\n```\n\nWhere:\n\n- `ka` is the ambient reflection coefficient\n- `Ia` is the ambient light intensity\n- `kd` is the diffuse reflection coefficient\n- `Id` is the diffuse light intensity\n- `N` is the normal vector of the object\n- `L` is the light vector\n- `ks` is the specular reflection coefficient\n- `Is` is the specular light intensity\n- `R` is the reflection vector\n- `V` is the view vector\n- `shininess` is the shininess coefficient\n- `dot` is the dot product\n- `max` is the maximum function\n- `pow` is the power function\n\n### Natural Soft Shadows\n\nNatural soft shadows are created by simulating the light rays that are blocked by the objects in the scene. The soft\nshadows are created by casting multiple rays from the light source to the object. The rays are then tested for\nintersection with the objects in the scene. If the ray intersects with an object, the color of the object is calculated\nusing the Phong reflection model. The color of the object is then multiplied by the transparency of the object to create\nthe soft shadows.\n\nHere's how to achieve soft shadows:\n\n- **Light source sampling**: simulating an area light source by generating multiple random sample points around the\n  light source. Each sample point represents a subarea of the light source.\n\n- **Shadow Ray Casting**: Cast Shadow Rays from the hit point to each light source sample point to check if these rays\n  are blocked by other objects in the scene.\n\n- **Calculate the occlusion ratio**: count the number of the sample points of the light source which is occluded, and\n  calculate the occlusion ratio (shadow factor) . The higher the occlusion ratio, the deeper the shadow.\n\n- **Applying the shadow factor**: The Shadow Factor is applied to the lighting calculation to achieve a soft shadow\n  effect.\n\n### Reflection\n\nReflection is the process of simulating the light rays that are reflected off the objects in the scene. The reflection\nis calculated using the reflection vector and the view vector. The reflection vector is the vector that is reflected off\nthe object. The reflection vector is calculated using the following formula:\n\n```cpp\nR = 2 * dot(N, L) * N - L\n```\n\nWhere:\n\n- `R` is the reflection vector\n- `N` is the normal vector of the object\n- `L` is the light vector\n- `dot` is the dot product\n- `2` is the scalar\n- `N` is the normal vector of the object\n\nThe reflection vector is then used to calculate the color of the object using the Phong reflection model. The color of\nthe object is then multiplied by the reflection coefficient to create the reflection effect.\n\n## Acknowledgements\n\n- [OpenGL](https://www.opengl.org/)\n- [FreeGLUT](http://freeglut.sourceforge.net/)\n- [GLU](https://www.opengl.org/resources/libraries/glut/spec3/node80.html)\n- [Phong Reflection Model](https://en.wikipedia.org/wiki/Phong_reflection_model)\n- [Ray-Tracing](https://en.wikipedia.org/wiki/Ray_tracing_(graphics))\n- [Ray-Tracing in One Weekend](https://raytracing.github.io/books/RayTracingInOneWeekend.html)\n- [stb_image](https://github.com/nothings/stb/blob/master/stb_image.h)\n\n## Source Code\n\n- [GitHub Repository](https://github.com/oopb/opengl_ray_tracing)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foopb%2Fopengl_ray_tracing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foopb%2Fopengl_ray_tracing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foopb%2Fopengl_ray_tracing/lists"}