Skip to content

Commit 217fe74

Browse files
committed
changed color, added screenshots
1 parent 03493f2 commit 217fe74

7 files changed

+111
-39
lines changed

Diff for: Program1.cpp

+48-18
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,18 @@ namespace Program1 {
3838
static vector3 wallLimit4(25.0, 0.0, 0.0);
3939

4040
// colors
41-
static vector3 grey(0.75, 0.75, 0.75);
42-
static vector3 lightRed(0.75, 0.1, 0.1);
41+
static vector3 floorColor(0.0, 0.325, 0.612); // Darker Blue
42+
static vector3 wallColor(0.686, 0.796, 1.0); // Pastel Blue
43+
static vector3 border(0.0, 0.0, 0.0);
44+
static GLfloat borderWidth = 1.0;
45+
46+
47+
// background Color
48+
static GLclampf red = 0.8;
49+
static GLclampf green = 0.8;
50+
static GLclampf blue = 0.8;
51+
static GLclampf alpha = 1.0; // Fully opaque
52+
4353

4454
// lighting position
4555
static vector3 lightPosition(25, 25, 100.0);
@@ -50,12 +60,6 @@ namespace Program1 {
5060
static float reflect_coef = 0.4;
5161
static float local_coef = 0.6;
5262

53-
// initialize
54-
static void initialize() {
55-
// set background color
56-
glClearColor(0.5, 0.7, 0.5, 0.0);
57-
}
58-
5963
// calculate local color
6064
// local color is intensity * base color
6165
static vector3 localColor(vector3 intersect, vector3 baseColor, vector3 normal) {
@@ -102,8 +106,8 @@ namespace Program1 {
102106
vector3 reflect = incidentRay.reflect(floorNormal);
103107

104108
//Calculate local color // the surface color of the local object
105-
vector3 floorLocalColor = localColor(intersect, grey, floorNormal);
106-
vector3 wallLocalColor = localColor(intersect, lightRed, wallNormal);
109+
vector3 floorLocalColor = localColor(intersect, floorColor, floorNormal);
110+
vector3 wallLocalColor = localColor(intersect, wallColor, wallNormal);
107111

108112
vector3 colorToBeMixed = outputColor;
109113

@@ -150,13 +154,27 @@ namespace Program1 {
150154

151155
}
152156
}
157+
158+
// Draw black border around the wall
159+
glColor3f(border.x, border.y, border.z);
160+
glLineWidth(borderWidth); // Set line width for the border
161+
162+
// Draw the border as a line loop on the wall border
163+
glBegin(GL_LINE_LOOP);
164+
glVertex3i(-25, -1, 0); // Bottom left corner
165+
glVertex3i(25, -1, 0); // Bottom right corner
166+
glVertex3i(25, 50, 0); // Top right corner
167+
glVertex3i(-25, 50, 0); // Top left corner
168+
glEnd();
153169
}
154170

155171
// draw the floor - reflective surface
156172
static void drawFloor() {
157-
for (int i = -75; i < 75; i++) {
158-
for (int j = -75; j < 75; j++) {
173+
float gridSize = 0.5;
174+
for (float i = -75.0; i < 75.0; i = i + gridSize) {
175+
for (float j = -75.0; j < 75.0; j = j + gridSize) {
159176

177+
160178
vector3 intersect = vector3(i, 0, j);
161179
vector3 color = recursive_ray_tracing_algorithm(intersect, 0); // local color of the floor
162180

@@ -165,10 +183,10 @@ namespace Program1 {
165183

166184
// Floor _ use the small rectangles method
167185
glBegin(GL_POLYGON);
168-
glVertex3i(i, 0, j);
169-
glVertex3i(i + 1, 0, j);
170-
glVertex3i(i + 1, 0, j + 1);
171-
glVertex3i(i, 0, j + 1);
186+
glVertex3f(i, 0.0, j);
187+
glVertex3f(i + gridSize, 0.0, j);
188+
glVertex3f(i + gridSize, 0.0, j + gridSize);
189+
glVertex3f(i, 0.0, j + gridSize);
172190
glEnd();
173191
}
174192
}
@@ -211,18 +229,30 @@ namespace Program1 {
211229

212230
}
213231

232+
// initialize
233+
static void initialize() {
234+
// set background color
235+
glClearColor(red, green, blue, alpha);
236+
// Enable multisampling for anti-aliasing
237+
glEnable(GL_LINE_SMOOTH); //enable line anti-aliasing
238+
glEnable(GL_POINT_SMOOTH);
239+
glEnable(GL_SMOOTH);
240+
241+
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); //set the quality of anti-aliasing
242+
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); //set the quality of anti-aliasing
243+
}
214244

215245

216246
// main program
217247

218248
static int main(int argc, char** argv)
219249
{
220250
glutInit(&argc, argv);
221-
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
251+
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);
222252
glutInitWindowSize(500, 500);
223253
glutInitWindowPosition(100, 100);
224254

225-
int windowHandle = glutCreateWindow("TME 4 _ Q1");
255+
int windowHandle = glutCreateWindow("Ray Tracing Algorithm");
226256
glutSetWindow(windowHandle);
227257

228258
glutDisplayFunc(display);

Diff for: Program2.cpp

+49-18
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,16 @@ namespace Program2 {
3636
static vector3 wallLimit4(25.0, 0.0, 0.0);
3737

3838
// colors
39-
static vector3 grey(0.75, 0.75, 0.75);
40-
static vector3 lightRed(0.75, 0.1, 0.1);
39+
static vector3 floorColor(0.65, 0.85, 0.95); // Darker Blue
40+
static vector3 wallColor(0.686, 0.796, 1.0); // Pastel Blue
41+
static vector3 border(0.0, 0.0, 0.0);
42+
static GLfloat borderWidth = 1.0;
43+
44+
// background Color
45+
static GLclampf red = 0.8;
46+
static GLclampf green = 0.8;
47+
static GLclampf blue = 0.8;
48+
static GLclampf alpha = 1.0; // Fully opaque
4149

4250
// lighting position
4351
static vector3 lightPosition(25, 25, 100.0);
@@ -48,11 +56,6 @@ namespace Program2 {
4856
static float reflect_coef = 0.4;
4957
static float local_coef = 0.6;
5058

51-
// initialize
52-
static void initialize() {
53-
// set background color
54-
glClearColor(0.5, 0.7, 0.5, 0.0);
55-
}
5659

5760
// calculate local color
5861
// local color is intensity * base color
@@ -102,8 +105,8 @@ namespace Program2 {
102105
vector3 sm = wall.intersect(intersect, intersect.add(reflect));
103106

104107
//Calculate the surface color of the local object
105-
vector3 floorLocalColor = localColor(intersect, grey, floorNormal);
106-
vector3 wallLocalColor = localColor(intersect, lightRed, wallNormal);
108+
vector3 floorLocalColor = localColor(intersect, floorColor, floorNormal);
109+
vector3 wallLocalColor = localColor(intersect, wallColor, wallNormal);
107110

108111
vector3 colorToBeMixed = outputColor;
109112

@@ -122,7 +125,7 @@ namespace Program2 {
122125

123126
// if it is the shadow part on the floor,
124127
else if ((st.x >= wallLimit1.x) && (st.x <= wallLimit4.x) && (st.y >= wallLimit1.y) && (st.y <= wallLimit2.y)) {
125-
outputColor = grey.scalar(ambient_coef); // shadow color (ambient color)
128+
outputColor = floorColor.scalar(ambient_coef); // shadow color (ambient color)
126129
}
127130

128131
// else it is on the floor without the shawdow part
@@ -159,12 +162,26 @@ namespace Program2 {
159162

160163
}
161164
}
165+
166+
// Draw black border around the wall
167+
glColor3f(border.x, border.y, border.z);
168+
glLineWidth(borderWidth); // Set line width for the border
169+
170+
// Draw the border as a line loop on the wall border
171+
glBegin(GL_LINE_LOOP);
172+
glVertex3i(-25, -1, 0); // Bottom left corner
173+
glVertex3i(25, -1, 0); // Bottom right corner
174+
glVertex3i(25, 50, 0); // Top right corner
175+
glVertex3i(-25, 50, 0); // Top left corner
176+
glEnd();
162177
}
163178

164179
// draw the floor - reflective surface
165180
static void drawFloor() {
166-
for (int i = -75; i < 75; i++) {
167-
for (int j = -75; j < 75; j++) {
181+
float gridSize = 0.5;
182+
for (float i = -75.0; i < 75.0; i = i + gridSize) {
183+
for (float j = -75.0; j < 75.0; j = j + gridSize) {
184+
168185

169186
vector3 intersect = vector3(i, 0, j);
170187
vector3 color = recursive_ray_tracing_algorithm(intersect, 0); // local color of the floor
@@ -174,15 +191,29 @@ namespace Program2 {
174191

175192
// Floor _ use the small rectangles method
176193
glBegin(GL_POLYGON);
177-
glVertex3i(i, 0, j);
178-
glVertex3i(i + 1, 0, j);
179-
glVertex3i(i + 1, 0, j + 1);
180-
glVertex3i(i, 0, j + 1);
194+
glVertex3f(i, 0.0, j);
195+
glVertex3f(i + gridSize, 0.0, j);
196+
glVertex3f(i + gridSize, 0.0, j + gridSize);
197+
glVertex3f(i, 0.0, j + gridSize);
181198
glEnd();
182199
}
183200
}
184201
}
185202

203+
// initialize
204+
static void initialize() {
205+
// set background color
206+
glClearColor(red, green, blue, alpha);
207+
// Enable multisampling for anti-aliasing
208+
glEnable(GL_LINE_SMOOTH); //enable line anti-aliasing
209+
glEnable(GL_POINT_SMOOTH);
210+
glEnable(GL_SMOOTH);
211+
212+
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); //set the quality of anti-aliasing
213+
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); //set the quality of anti-aliasing
214+
}
215+
216+
186217
// renders the scene
187218
static void render() {
188219

@@ -227,11 +258,11 @@ namespace Program2 {
227258
static int main(int argc, char** argv)
228259
{
229260
glutInit(&argc, argv);
230-
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
261+
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);
231262
glutInitWindowSize(500, 500);
232263
glutInitWindowPosition(100, 100);
233264

234-
int windowHandle = glutCreateWindow("TME 4 _ Q2");
265+
int windowHandle = glutCreateWindow("Ray Tracing Algorithm With Shadow");
235266
glutSetWindow(windowHandle);
236267

237268
glutDisplayFunc(display);

Diff for: README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ Ensure you have Visual Studio installed. If not, download it from [Visual Studio
2929
This project requires the OpenGL library or `glut.h`. The easiest way to set it up in Visual Studio is as follows:
3030

3131
1. Navigate to the directory where you cloned the project.
32-
2. Open the `recursive-ray-tracing-program-in-openGL.sln` file in Visual Studio.
32+
2. Open the `Ray_Tracing.sln` file in Visual Studio.
3333
3. Click on `Project` in the menu bar.
34-
4. Select `Manage NuGet Packages`.
34+
4. Select `Manage NuGet Packages`. (If you don't see it, close the VS window, then resume from step 2.)
3535
5. Click on the "Browse" tab.
3636
6. Search for "freeglut".
37-
7. Select "nupengl.core" (which includes glut.h) and click `Install`.
37+
7. Select "nupengl.core" (which includes glut.h) and click `Install`. (If already installed, then uninstall then install again).
3838

3939
## Running the Project
4040

@@ -45,3 +45,12 @@ This project requires the OpenGL library or `glut.h`. The easiest way to set it
4545
## Purpose
4646

4747
The primary goal of this project is to showcase the recursive ray tracing algorithm. Rays are cast from the source to the scene, determining if they hit walls (non-reflective surfaces) or floors (reflective surfaces). The algorithm either terminates upon hitting a wall, returning the wall's local color, or recursively invokes itself upon hitting the floor, calculating the mixed color. An additional feature tests intersections to determine if they can see the light source, introducing shadows where necessary.
48+
49+
## Screenshots
50+
### Program 1:
51+
52+
![Ray Tracing](assets/non_shadow.png "Ray Tracing without shadow")
53+
<br> <br> <br>
54+
### Program 2:
55+
56+
![Ray Tracing](assets/non_shadow.png "Ray Tracing without shadow")

Diff for: Ray_Tracing.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
</ItemGroup>
147147
<ItemGroup>
148148
<None Include="packages.config" />
149+
<None Include="README.md" />
149150
</ItemGroup>
150151
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
151152
<ImportGroup Label="ExtensionTargets">

Diff for: Ray_Tracing.vcxproj.filters

+1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@
4141
</ItemGroup>
4242
<ItemGroup>
4343
<None Include="packages.config" />
44+
<None Include="README.md" />
4445
</ItemGroup>
4546
</Project>

Diff for: assets/non_shadow.png

68.7 KB
Loading

Diff for: assets/shadow.png

92.3 KB
Loading

0 commit comments

Comments
 (0)