Skip to content

Commit 84535dd

Browse files
committed
Merge pull request pkivolowitz#3 from pkivolowitz/Perry's-Branch
Perry s branch
2 parents 2c9ef5f + 7a93006 commit 84535dd

6 files changed

+101
-39
lines changed

disc.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,13 @@ void Disc::Draw(bool draw_normals)
225225
}
226226
else
227227
{
228+
GLint winding;
229+
glGetIntegerv(GL_FRONT_FACE , &winding);
230+
if (is_fan)
231+
glFrontFace(GL_CCW);
228232
glBindVertexArray(this->vertex_array_handle);
229233
glDrawElements(this->is_fan ? GL_TRIANGLE_FAN : GL_TRIANGLES, this->data.indices.size(), GL_UNSIGNED_INT, nullptr);
234+
glFrontFace(winding);
230235
}
231236
glBindVertexArray(0);
232237
this->GLReturnedError("Disc::Draw() - exiting");

ilcontainer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ bool ILContainer::Initialize(const char * file_name)
1919
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
2020
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
2121
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
22-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
22+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
2323
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), this->width = ilGetInteger(IL_IMAGE_WIDTH), this->height = ilGetInteger(IL_IMAGE_HEIGHT), 0, this->format = ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, this->data = ilGetData());
24+
glGenerateTextureMipmap(this->il_texture_handle);
2425
return true;
2526
}
2627

main_pk.cpp

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ vector<string> instructions = {
3535
"'f' - toggle full screen",
3636
" fails on Intel GPU",
3737
"'i' - toggle instructions",
38-
"'l' and 'L' - line width",
38+
"'+' and '-' - field of view",
3939
"'p' - toggle pause",
40+
"'w' - toggle wireframe",
4041
"'z' and 'Z' - zoom",
4142
"'x' and ESC - exit"
4243
};
@@ -74,13 +75,13 @@ void DisplayInstructions(int w , int h)
7475
#define DISPLAY_MODE (GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE)
7576
#endif // USE_STEREO
7677

77-
const int NUMBER_OF_OBJECTS = 32;
78+
const int NUMBER_OF_OBJECTS = 128;
7879
vector<Instance> instances;
7980
freetype::font_data our_font;
8081
FrameBufferObject fbo;
8182

8283
Disc disc1(64, pi<float>() * 1.5f, 0.25f, 0.125f);
83-
Disc disc2(64, pi<float>() * 2.0f , 0.25f , 0.0f);
84+
Disc disc2(64, pi<float>() * 2.0f , 1.0f , 0.0f);
8485
Disc disc3(128, pi<float>() * 2.0f , 1.0f , 0.0f);
8586
Cylinder cylinder1(32, 4, pi<float>() * 2.0f, 1.0f, 1.0f);
8687
Cylinder cylinder2(4 , 2 , pi<float>() * 2.0f , 1.0f , 0.5f);
@@ -265,32 +266,55 @@ void KeyboardFunc(unsigned char c, int x, int y)
265266

266267
case 'x':
267268
case 27:
269+
glutLeaveFullScreen();
268270
glutLeaveMainLoop();
269271
return;
270272
}
271273
}
272274

273275
void DrawScene(Window * window)
274276
{
277+
glClearColor(0, 0, 0, 0);
278+
glClear(GL_COLOR_BUFFER_BIT);
279+
280+
vec3 ambient = vec3(0.1f , 0.1f , 0.1f);
281+
vec3 specular = vec3(1.0f , 1.0f , 1.0f);
275282
phong_shader.GLReturnedError("DrawScene() - entering");
276283
#ifdef MOVE
277-
mat4 m = rotate(mat4() , radians(window->LocalTime() * 30.0f) , vec3(0.0f , 1.0f , 0.2f));
278-
m = translate(m, vec3(0.0f, 11.5f * cos(window->LocalTime() * 0.5f) + 2.0f, 11.5f * sin(window->LocalTime() * 0.5f) + 2.0f));
284+
mat4 m = rotate(mat4() , radians(window->LocalTime() * 60.0f) , vec3(0.0f , 1.0f , 0.2f));
285+
m = translate(m, vec3(11.5f * sin(window->LocalTime() * 1.0f - 0.5) + 2.0f, 11.5f * cos(window->LocalTime() * 1.0f) + 2.0f, 11.5f * sin(window->LocalTime() * 1.0f) + 2.0f));
279286
#else
280287
mat4 m;
281288
#endif // MOVE
289+
mat4 projection_matrix = perspective(radians(window->fovy) , window->aspect , window->near_distance , window->far_distance);
290+
glViewport(0 , 0 , window->size.x , window->size.y);
282291

283-
mat4 view_matrix = lookAt(vec3(m * vec4(eye, 1.0f)), cop, up);
292+
vec3 e = vec3(m * vec4(eye , 1.0f));
293+
mat4 view_matrix = lookAt(e, cop, up);
284294
mat4 model_matrix;
285-
mat4 projection_matrix = perspective(radians(window->fovy), window->aspect, window->near_distance, window->far_distance);
286295

296+
// Skybox can be made here - translate by the inverse of the eye
297+
model_matrix = translate(model_matrix , e);
298+
model_matrix = scale(model_matrix , vec3(10.0f , 10.0f , 10.0f));
299+
300+
phong_shader.Use(model_matrix , view_matrix , projection_matrix);
301+
phong_shader.SelectSubroutine(PhongShader::PHONG_WITH_TEXTURE);
302+
phong_shader.EnableTexture(textures[4] , 0);
303+
phong_shader.SetMaterial(vec3(1.0f, 1.0f, 1.0f) , vec3() , 32.0f , ambient);
304+
phong_shader.SetLightPosition(vec3(0.0f , 0.0f , 1000.0f));
305+
phong_shader.SetOpacity(1.0f);
306+
glEnable(GL_BLEND);
307+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
308+
cube.Draw(false);
309+
phong_shader.UnUse();
310+
glDisable(GL_TEXTURE_2D);
311+
glClear(GL_DEPTH_BUFFER_BIT);
312+
313+
model_matrix = mat4();
287314
vec3 z_axis = vec3(0.0f, 0.0f, 1.0f);
288315
vec3 y_axis = vec3(0.0f, 1.0f, 0.0f);
289-
vec3 ambient = vec3(0.1f, 0.1f, 0.1f);
290-
vec3 specular = vec3(1.0f, 1.0f, 1.0f);
291316
float c_offset = radians(45.0f);
292317

293-
glViewport(0, 0, window->size.x, window->size.y);
294318

295319
const int count_of_shapes = 4;
296320

@@ -312,11 +336,12 @@ void DrawScene(Window * window)
312336
disc1.Draw(false);
313337
break;
314338
case 1:
315-
disc3.Draw(false);
339+
disc2.Draw(false);
316340
break;
317341
case 2:
342+
glEnable(GL_TEXTURE_2D);
318343
phong_shader.SelectSubroutine(PhongShader::PHONG_WITH_TEXTURE);
319-
phong_shader.EnableTexture(textures[2], 0);
344+
phong_shader.EnableTexture(textures[i % textures.size() - 1], 0);
320345
plane2.Draw(false);
321346
glDisable(GL_TEXTURE_2D);
322347
break;
@@ -327,11 +352,13 @@ void DrawScene(Window * window)
327352
phong_shader.UnUse();
328353
}
329354

355+
glDisable(GL_BLEND);
330356
model_matrix = mat4();
331357
mat4 mz = model_matrix;
332358
model_matrix = scale(model_matrix, vec3(0.5f, 0.5f, 16.0f));
333359

334360
phong_shader.Use(model_matrix, view_matrix, projection_matrix);
361+
phong_shader.SelectSubroutine(PhongShader::BASIC_PHONG);
335362
phong_shader.SetMaterial(vec3(0.0f, 0.0f, 0.8f), specular, 128.0f, ambient);
336363
phong_shader.SetLightPosition(vec3(0.0f, 1000.0f, 0.0f));
337364
cylinder1.Draw(false);
@@ -340,6 +367,7 @@ void DrawScene(Window * window)
340367
model_matrix = rotate(mz, radians(90.0f), y_axis);
341368
model_matrix = scale(model_matrix, vec3(0.5f, 0.5f, 16.0f));
342369
phong_shader.Use(model_matrix, view_matrix, projection_matrix);
370+
phong_shader.SelectSubroutine(PhongShader::BASIC_PHONG);
343371
phong_shader.SetMaterial(vec3(1.0f, 0.0f, 0.0f), specular, 128.0f, ambient);
344372
phong_shader.SetLightPosition(vec3(0.0f, 1000.0f, 0.0f));
345373
cylinder1.Draw(false);
@@ -348,12 +376,13 @@ void DrawScene(Window * window)
348376
model_matrix = rotate(mz, radians(-90.0f), vec3(1.0f, 0.0f, 0.0f));
349377
model_matrix = scale(model_matrix, vec3(0.5f, 0.5f, 16.0f));
350378
phong_shader.Use(model_matrix, view_matrix, projection_matrix);
379+
phong_shader.SelectSubroutine(PhongShader::BASIC_PHONG);
351380
phong_shader.SetMaterial(vec3(0.0f, 1.0f, 0.0f), specular, 128.0f, ambient);
352381
phong_shader.SetLightPosition(vec3(0.0f, 1000.0f, 0.0f));
353382
cylinder1.Draw(false);
354383
phong_shader.UnUse();
355384

356-
cylinder1.UpdateValues(TestUpdate, window->LocalTime(), nullptr);
385+
//cylinder1.UpdateValues(TestUpdate, window->LocalTime(), nullptr);
357386
}
358387

359388
void DisplayCube()
@@ -406,7 +435,7 @@ void DisplayDisc()
406435
vec4 crimson(0.6f , 0.0f , 0.0f , 1.0f);
407436
vec3 ambient = vec3(0.0f , 0.0f , 0.0f);
408437
vec3 specular = vec3(0.0f , 0.0f , 0.3f);
409-
vec3 diffuse = vec3(0.0f , 0.0f , 0.9f);
438+
vec3 diffuse = vec3(0.9f , 0.9f , 0.9f);
410439

411440
glClearColor(crimson.r , crimson.g , crimson.b , crimson.a);
412441
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
@@ -422,20 +451,21 @@ void DisplayDisc()
422451
phong_shader.Use(model_matrix , view_matrix , projection_matrix);
423452
phong_shader.SetMaterial(diffuse , specular , 128.0f , ambient);
424453
phong_shader.SetLightPosition(vec3(0.0f , 0.0f , 1000.0f));
425-
phong_shader.SelectSubroutine(PhongShader::PHONG_WITH_TEXTURE);
426-
phong_shader.EnableTexture(textures[2] , 0);
427-
disc3.Draw(false);
454+
phong_shader.SelectSubroutine(PhongShader::BASIC_PHONG);
455+
//phong_shader.SelectSubroutine(PhongShader::PHONG_WITH_TEXTURE);
456+
//phong_shader.EnableTexture(textures[2] , 0);
457+
disc2.Draw(false);
428458
phong_shader.UnUse();
429459

430460
if (window->draw_normals)
431461
{
432462
constant_shader.Use(model_matrix , view_matrix , projection_matrix);
433463
constant_shader.SetMaterial(diffuse , specular , 32.0f , vec3(1.0f , 1.0f , 1.0f));
434-
disc3.Draw(true);
464+
disc2.Draw(true);
435465
constant_shader.UnUse();
436466
}
437467
glutSwapBuffers();
438-
disc3.UpdateValues(TestUpdateDisc , window->LocalTime(), nullptr);
468+
//disc3.UpdateValues(TestUpdateDisc , window->LocalTime(), nullptr);
439469
}
440470

441471
void DisplayPlane()
@@ -547,21 +577,43 @@ void DisplayPlane()
547577

548578
void DisplayGrid()
549579
{
550-
//cout << "dg\n";
580+
static int texture_index = 0;
581+
static int opacity_direction = 0;
551582

552583
Window * window = Window::FindCurrentWindow(windows);
553584
if (window->handle == BAD_GL_VALUE)
554585
return;
555586

587+
float fade_controller = float(fmod(double(window->LocalTime()), 10.0));
588+
float opacity = 1.0f;
589+
590+
if (fade_controller < 1.0f)
591+
{
592+
593+
opacity = smoothstep(0.0f , 1.0f , fade_controller);
594+
if (opacity_direction < 0)
595+
texture_index = (texture_index + 1) % textures.size();
596+
opacity_direction = 1;
597+
}
598+
else if (fade_controller >= 9.0f)
599+
{
600+
opacity = smoothstep(0.0f , 1.0f , 10.0f - fade_controller);
601+
opacity_direction = -1;
602+
}
603+
604+
//cout << "Fade Controller: " << fade_controller << " opacity: " << opacity << endl;
605+
556606
glViewport(0 , 0 , window->size.x , window->size.y);
557-
vec4 crimson(0.6f , 0.0f , 0.0f , 1.0f);
607+
vec4 crimson(0.6f , 0.0f , 0.0f , 0.0f);
558608
vec3 ambient = vec3(0.0f , 0.0f , 0.0f);
559609
vec3 specular = vec3(1.0f , 1.0f , 1.0f);
560610
vec3 diffuse = vec3(0.0f , 0.0f , 0.8f);
561611

562612
glClearColor(crimson.r , crimson.g , crimson.b , crimson.a);
563613
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
564614
glEnable(GL_DEPTH_TEST);
615+
glEnable(GL_BLEND);
616+
glBlendFunc(GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA);
565617

566618
vector<Constellation::PositionData> & pd = gc.GetPositionData();
567619

@@ -600,7 +652,8 @@ void DisplayGrid()
600652
phong_shader.SetMaterial(diffuse , specular , 64.0f , ambient);
601653
phong_shader.SetLightPosition(vec3(0.0f , 0.0f , 1000.0f));
602654
phong_shader.SelectSubroutine(PhongShader::PHONG_WITH_TEXTURE);
603-
phong_shader.EnableTexture(textures[1] , 0);
655+
phong_shader.EnableTexture(textures[texture_index] , 0);
656+
phong_shader.SetOpacity(opacity);
604657
plane2.Draw(false);
605658
phong_shader.UnUse();
606659
if (window->draw_normals)
@@ -613,6 +666,7 @@ void DisplayGrid()
613666
// Animate the rotation of the objects within the grid.
614667
(*iter).outward_direction_vector = vec3(r * vec4((*iter).outward_direction_vector, 1.0f));
615668
}
669+
glDisable(GL_BLEND);
616670
glutSwapBuffers();
617671
}
618672

@@ -704,7 +758,7 @@ bool InitializeTextures()
704758
texture_file_names.push_back("c2.jpg");
705759
texture_file_names.push_back("c3.jpg");
706760
texture_file_names.push_back("c4.jpg");
707-
761+
texture_file_names.push_back("carthage-logo-main.png");
708762
textures.resize(texture_file_names.size());
709763
for (size_t i = 0; i < texture_file_names.size(); i++)
710764
{
@@ -751,12 +805,12 @@ int main(int argc, char * argv[])
751805
gc.Initialize(525);
752806

753807
// This vector is used to initialize all the window objects.
754-
//windows.push_back(Window("Basic Shape Viewer" , nullptr , nullptr , nullptr , nullptr , ivec2(512 , 512) , 50.0f , 1.0f , 100.0f));
808+
windows.push_back(Window("Basic Shape Viewer" , nullptr , nullptr , nullptr , nullptr , ivec2(512 , 512) , 50.0f , 1.0f , 100.0f));
755809
//windows.push_back(Window("Cylinder" , DisplayCylinder , nullptr , nullptr , nullptr , ivec2(512 , 512) , 50.0f , 1.0f , 100.0f));
756810
windows.push_back(Window("Plane" , DisplayPlane , nullptr , nullptr , nullptr , ivec2(512 , 512) , 50.0f , 1.0f , 100.0f));
757811
//windows.push_back(Window("Disc" , DisplayDisc , nullptr , nullptr , nullptr , ivec2(512 , 512) , 50.0f , 1.0f , 100.0f));
758812
//windows.push_back(Window("Cube", DisplayCube, nullptr, nullptr, nullptr, ivec2(512, 512), 50.0f, 1.0f, 100.0f));
759-
//windows.push_back(Window("Grid" , DisplayGrid , nullptr , nullptr , nullptr , ivec2(512 , 512) , 50.0f , 1.0f , 400.0f));
813+
windows.push_back(Window("Grid" , DisplayGrid , nullptr , nullptr , nullptr , ivec2(512 , 512) , 50.0f , 1.0f , 400.0f));
760814
Window::InitializeWindows(windows , DisplayFunc , KeyboardFunc , CloseFunc, ReshapeFunc , IdleFunc);
761815

762816
windows[0].SetWindowTitle("NEW TITLE");

per-fragment-phong.fs.glsl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ in VS_OUT
2323
uniform vec3 diffuse_albedo;
2424
uniform vec3 specular_albedo;
2525
uniform float specular_power;
26+
uniform float opacity = 1.0;
2627
uniform vec3 ambient;
2728

2829
uniform sampler2D base_texture;
@@ -35,7 +36,7 @@ vec3 light_position = vec3(0.0, 0.0, 100.0);
3536
subroutine(color_t)
3637
vec4 Constant()
3738
{
38-
return vec4(ambient, 1.0);
39+
return vec4(ambient, opacity);
3940
}
4041

4142
subroutine(color_t)
@@ -56,19 +57,14 @@ vec4 PerPixelLighting()
5657
vec3 diffuse = max(dot(s, n), 0.0) * diffuse2;
5758
vec3 specular = pow(max(dot(r, v), 0.0), specular_power) * specular_albedo;
5859

59-
return vec4(ambient + diffuse + specular, 1.0);
60-
}
61-
62-
subroutine(color_t)
63-
vec4 PPLWithTextureAndVignette()
64-
{
65-
return texture(base_texture, fs_in.T);
60+
return vec4(ambient + diffuse + specular, opacity);
6661
}
6762

6863
subroutine(color_t)
6964
vec4 PPLWithTexture()
7065
{
71-
vec3 diffuse2 = vec3(texture(base_texture, fs_in.T));
66+
vec4 t = texture(base_texture, fs_in.T);
67+
vec3 diffuse2 = vec3(t);
7268
vec3 N2 = fs_in.N;
7369

7470
if (!gl_FrontFacing)
@@ -82,8 +78,8 @@ vec4 PPLWithTexture()
8278
vec3 diffuse = max(dot(s, n), 0.0) * diffuse2;
8379
vec3 specular = pow(max(dot(r, v), 0.0), specular_power) * specular_albedo;
8480

85-
return vec4(ambient + diffuse + specular, 1.0);
86-
//vec4(fs_in.T.t, fs_in.T.t, fs_in.T.t, 0); //
81+
return vec4(vec3(ambient + diffuse + specular), t.a * opacity);
82+
//vec4(fs_in.T.t, fs_in.T.t, fs_in.T.t, 0); //
8783
}
8884

8985
subroutine(color_t)

phong_shader.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ void PhongShader::SetGlobalTime(float global_time)
6969
glUniform1f(uniforms.global_time , global_time);
7070
}
7171

72+
void PhongShader::SetOpacity(float opacity)
73+
{
74+
assert(this->is_used == true);
75+
glUniform1f(uniforms.opacity , opacity);
76+
}
7277
void PhongShader::CustomSetup()
7378
{
7479
Shader::Use();
@@ -79,6 +84,7 @@ void PhongShader::CustomSetup()
7984
uniforms.specular_albedo = glGetUniformLocation(this->program_id, "specular_albedo");
8085
uniforms.specular_power = glGetUniformLocation(this->program_id, "specular_power");
8186
uniforms.ambient = glGetUniformLocation(this->program_id, "ambient");
87+
uniforms.opacity = glGetUniformLocation(this->program_id , "opacity");
8288
uniforms.modelview_matrix = glGetUniformLocation(this->program_id , "mv_matrix");
8389
uniforms.view_matrix = glGetUniformLocation(this->program_id , "view_matrix");
8490
uniforms.normal_matrix = glGetUniformLocation(this->program_id , "normal_matrix");
@@ -89,7 +95,6 @@ void PhongShader::CustomSetup()
8995
this->subroutine_indices.push_back(glGetSubroutineIndex(this->program_id , GL_FRAGMENT_SHADER, "PerPixelLighting"));
9096
this->subroutine_indices.push_back(glGetSubroutineIndex(this->program_id , GL_FRAGMENT_SHADER , "PPLWithTexture"));
9197
this->subroutine_indices.push_back(glGetSubroutineIndex(this->program_id , GL_FRAGMENT_SHADER , "ShaderToy1"));
92-
this->subroutine_indices.push_back(glGetSubroutineIndex(this->program_id , GL_FRAGMENT_SHADER , "PPLWithTextureAndVignette"));
9398
Shader::UnUse();
9499
}
95100

phong_shader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class PhongShader : public Shader
1515
void CustomSetup();
1616
void SetGlobalTime(float global_time);
1717
void EnableTexture(ILContainer & ilcontainer , GLuint texture_unit);
18+
void SetOpacity(float opacity);
1819

1920
enum SubroutineIndices
2021
{
2122
CONSTANT,
2223
BASIC_PHONG,
2324
PHONG_WITH_TEXTURE,
2425
SHADER_TOY_1,
25-
VIGNETTE,
2626
NUMBER_OF_SUBROUTINES
2727
};
2828

@@ -42,6 +42,7 @@ class PhongShader : public Shader
4242
GLuint light_position;
4343
GLuint base_texture_location;
4444
GLuint global_time;
45+
GLuint opacity;
4546
GLuint param1;
4647
GLuint param2;
4748
} uniforms;

0 commit comments

Comments
 (0)