Skip to content

Commit 30fff32

Browse files
committed
serie 8 - Materials
1 parent d208bc6 commit 30fff32

File tree

7 files changed

+83
-46
lines changed

7 files changed

+83
-46
lines changed

s8_casa/draw_utils.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
//
44
#include <iostream>
55
#include "draw_utils.h"
6+
#include "light.h"
67

7-
#define WIREFRAME_COLOR 15, 32, 112
8+
const GLfloat WIREFRAME_COLOR[] = {15/255.0f, 32/255.0f, 112/255.0f};
89

910
void draw_utils::log(const std::string &msg) const {
1011
std::cout << msg << std::endl;
@@ -104,14 +105,24 @@ void draw_utils::draw_triangle3D(Triangle &triangle) {
104105
* @param color
105106
*/
106107
void draw_utils::draw_triangle3D(Vertex &v1, Vertex &v2, Vertex &v3, Color &color) {
107-
glColor3ub(color.r, color.g, color.b);
108+
bool isColorMaterialOn = Light::isColorMaterialOn();
109+
if(isColorMaterialOn)
110+
glColor3fv(color.fv());
111+
else
112+
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color.fv());
113+
108114
glPolygonMode(GL_FRONT,GL_FILL);
109115
internal_triangle3D(v1, v2, v3);
110116

111117
if(_showWireFrame) {
112118
// wireframe
113119
glLineWidth(1.0f);
114-
glColor3ub(WIREFRAME_COLOR);
120+
121+
if(isColorMaterialOn)
122+
glColor3fv(WIREFRAME_COLOR);
123+
else
124+
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, WIREFRAME_COLOR);
125+
115126
glPolygonMode(GL_FRONT, GL_LINE);
116127
internal_triangle3D(v1, v2, v3);
117128
}

s8_casa/draw_utils.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ struct Ortho {
2929
};
3030

3131
struct Color {
32-
GLubyte r{255};
33-
GLubyte g{255};
34-
GLubyte b{255};
32+
Color(GLubyte r, GLubyte g, GLubyte b) : _r{r/255.0f}, _g{g/255.0f}, _b{b/255.0f}, _a{1.0} { }
33+
GLfloat* fv() { return &_r; }
34+
private:
35+
GLfloat _r;
36+
GLfloat _g;
37+
GLfloat _b;
38+
GLfloat _a;
3539
};
3640

3741
struct Triangle {

s8_casa/house.cpp

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
#include "house.h"
66
#include "draw_utils.h"
7+
#include "light.h"
78

8-
const float SW = 10.0f; //SCENE WIDTH
9+
static const GLfloat SW = 10.0f; //SCENE WIDTH
910

1011
const float DOOR_THINK = 0.5f / SW;
1112
const float HALF_DOOR_WIDTH = 2.0f / SW;
@@ -44,10 +45,10 @@ Color COLOR_WALL_EXTERNAL_1 = {238, 238, 238 };
4445
Color COLOR_WALL_EXTERNAL_2 = {188, 170, 164 };
4546

4647

47-
#define COLOR_WALL_INTERNAL 80, 80, 80
48-
#define COLOR_DOOR 141, 110, 99
49-
#define COLOR_FLOOR 160, 160, 160
50-
#define COLOR_FLAG 153,206,255
48+
#define COLOR_WALL_INTERNAL {80, 80, 80}
49+
#define COLOR_DOOR {141, 110, 99}
50+
#define COLOR_FLOOR {160, 160, 160}
51+
#define COLOR_FLAG {153,206,255}
5152

5253
House::House(draw_utils& utils) :
5354
_utils{utils},
@@ -97,6 +98,7 @@ void House::draw() {
9798
}
9899

99100
void House::drawChimney() {
101+
setExternalMaterial();
100102
Rect rectangles[] = {
101103
// LEFT SIDE
102104
{
@@ -179,10 +181,10 @@ void House::drawFloor() {
179181
},
180182
{
181183
// floor outside
182-
{HALF_BASE_WIDTH, 0, -BASE_HEIGHT},
184+
{HALF_BASE_WIDTH, 0, -BASE_HEIGHT},
183185
{HALF_BASE_WIDTH, 0, 0},
184186
{-HALF_BASE_WIDTH, 0, 0},
185-
{-HALF_BASE_WIDTH, 0, -BASE_HEIGHT},
187+
{-HALF_BASE_WIDTH, 0, -BASE_HEIGHT},
186188
COLOR_FLOOR
187189
}
188190
};
@@ -191,6 +193,9 @@ void House::drawFloor() {
191193
}
192194

193195
void House::drawRoof() {
196+
glMaterialfv(GL_FRONT, GL_SPECULAR, MATERIAL_RED);
197+
glMaterialf(GL_FRONT, GL_SHININESS, SHININESS_HIGH);
198+
194199
Rect rectangles[] = {
195200
// right roof wall
196201
{
@@ -226,7 +231,14 @@ void House::drawRoof() {
226231
_utils.draw_parallelepiped(rectangles[2], rectangles[3]);
227232
}
228233

234+
void House::setExternalMaterial() const {
235+
glMaterialfv(GL_FRONT, GL_SPECULAR, MATERIAL_WHITE);
236+
glMaterialf(GL_FRONT, GL_SHININESS, SHININESS_LOW);
237+
}
238+
229239
void House::drawPrismWalls() {
240+
setExternalMaterial();
241+
230242
Triangle triangles[] = {
231243
{
232244
{ -HALF_BASE_WIDTH, WALL_HEIGHT, 0 },
@@ -259,6 +271,7 @@ void House::drawPrismWalls() {
259271
}
260272

261273
void House::drawLateralWalls() {
274+
setExternalMaterial();
262275

263276
Rect rectangles[] = {
264277
// front walls
@@ -345,6 +358,9 @@ void House::drawLateralWalls() {
345358

346359
void House::drawDoor() {
347360
glPushMatrix();
361+
glMaterialfv(GL_FRONT, GL_SPECULAR, MATERIAL_BLACK);
362+
glMaterialf(GL_FRONT, GL_SHININESS, SHININESS_OFF);
363+
348364
glTranslatef(-HALF_DOOR_WIDTH, 0.f, -WALL_THICK);
349365
glRotatef(_doorAngle, 0.0, 1.0, 0.0);
350366
glTranslatef(HALF_DOOR_WIDTH, 0.f, WALL_THICK);
@@ -471,6 +487,9 @@ void House::updateColor() {
471487
}
472488

473489
void House::drawFlag() {
490+
glMaterialfv(GL_FRONT, GL_SPECULAR, MATERIAL_BLACK);
491+
glMaterialf(GL_FRONT, GL_SHININESS, SHININESS_OFF);
492+
474493
Triangle triangles[] = {
475494
{
476495
{ -FLAG_WIDTH, 0, 0 },
@@ -495,6 +514,10 @@ void House::drawFlag() {
495514

496515
// flag cylinder
497516
void House::drawCylinder() const {
517+
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, MATERIAL_BLACK);
518+
glMaterialfv(GL_FRONT, GL_SPECULAR, MATERIAL_BLACK);
519+
glMaterialf(GL_FRONT, GL_SHININESS, SHININESS_OFF);
520+
498521
gluQuadricDrawStyle(_quadric, GLU_LINE);
499522
gluQuadricNormals(_quadric, GLU_SMOOTH);
500523

s8_casa/house.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class House {
6363
GLUquadric *_quadric;
6464
draw_utils& _utils;
6565
Volume _volume;
66+
67+
void setExternalMaterial() const;
6668
};
6769

6870

s8_casa/light.cpp

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <stdexcept>
66
#include "light.h"
77

8-
static const GLfloat black[] = {0.0, 0.0, 0.0, 1.0 };
9-
static const GLfloat white[] = {1.0, 1.0, 1.0, 1.0 };
108
static const GLenum LIGHTS[] = { GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7 };
119

1210
int Light::LastCreated = 0;
@@ -44,20 +42,6 @@ void Light::draw() {
4442
} else {
4543
glEnable(LIGHTS[_lightNum]);
4644
glLightfv(LIGHTS[_lightNum], GL_POSITION, &_position.x);
47-
48-
// For each lights, we defines their ambient, diffuse and specular color
49-
// glLightfv(LIGHTS[_lightNum], GL_AMBIENT, white);
50-
// glLightfv(LIGHTS[_lightNum], GL_DIFFUSE, white);
51-
// glLightfv(LIGHTS[_lightNum], GL_SPECULAR, white);
52-
53-
// GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
54-
// GLfloat mat_shininess[] = { 50.0 };
55-
// GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
56-
// glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
57-
// glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
58-
// glLightfv(GL_LIGHT0, GL_POSITION, light_position);
59-
60-
6145
drawSpot();
6246
drawLightSource();
6347
}
@@ -73,23 +57,19 @@ void Light::drawLightSource() const {
7357
glPopMatrix ();
7458
}
7559

76-
void Light::drawSpot() {
77-
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
78-
glLightfv(LIGHTS[_lightNum], GL_AMBIENT, light_ambient);
79-
80-
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
81-
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
82-
glLightfv(LIGHTS[_lightNum], GL_DIFFUSE, light_diffuse);
83-
glLightfv(LIGHTS[_lightNum], GL_SPECULAR, light_specular);
8460

61+
void Light::drawSpot() {
62+
// For each lights, we defines their ambient, diffuse and specular color
63+
glLightfv(LIGHTS[_lightNum], GL_AMBIENT, MATERIAL_BLACK);
64+
glLightfv(LIGHTS[_lightNum], GL_DIFFUSE, MATERIAL_WHITE);
65+
glLightfv(LIGHTS[_lightNum], GL_SPECULAR, MATERIAL_WHITE);
8566

86-
// http://jerome.jouvie.free.fr/opengl-tutorials/Lesson6.php#Notion
8767
// In OpenGl, we also can defines the light attenuation with the distance.
8868
// OpenGl calculates an attenuation factor (between 0 and 1) that is multiplied to the ambient, diffuse and specular color.
8969
// By default, they are no attenuation (attenuation factor is 1) so you have to defines your own attenuation.
9070
glLightf(LIGHTS[_lightNum], GL_CONSTANT_ATTENUATION, 1.0);
91-
glLightf(LIGHTS[_lightNum], GL_LINEAR_ATTENUATION, 0.0);
92-
glLightf(LIGHTS[_lightNum], GL_QUADRATIC_ATTENUATION, 0.0);
71+
glLightf(LIGHTS[_lightNum], GL_LINEAR_ATTENUATION, 0.2);
72+
glLightf(LIGHTS[_lightNum], GL_QUADRATIC_ATTENUATION, 0.08);
9373

9474
// We have seen that positional source emits light in all the direction.
9575
// But, we can creates a spot that emits lights into an emission cone by restricting the emission area of the light source.
@@ -98,8 +78,15 @@ void Light::drawSpot() {
9878
glLightfv(LIGHTS[_lightNum], GL_SPOT_DIRECTION, &_direction.x);
9979
}
10080

101-
void Light::ambient() {
102-
// set the global ambient color a bit darker so that we can see spot lights
81+
void Light::globalAmbient() {
82+
// set the global ambient color a bit darker
83+
// so that we can see spot lights
10384
GLfloat ambientLight[] = {0.6f, 0.6f, 0.6f, 1.0f};
10485
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
10586
}
87+
88+
GLboolean Light::isColorMaterialOn() {
89+
GLboolean isColorMaterial;
90+
glGetBooleanv(GL_COLOR_MATERIAL, &isColorMaterial);
91+
return isColorMaterial;
92+
}

s8_casa/light.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@
1616

1717
#endif
1818

19+
static const GLfloat MATERIAL_WHITE[] = {1.0, 1.0, 1.0, 1.0 };
20+
static const GLfloat MATERIAL_BLACK[] = {0.0f, 0.0f, 0.0f, 1.0f};
21+
static const GLfloat MATERIAL_RED[] = {1.0f, 0.0f, 0.0f, 1.0f};
22+
static const GLfloat MATERIAL_YELLOW[] = {0.1f, 1.0f, 0.0f, 1.0f};
23+
24+
static const GLfloat SHININESS_OFF = 0.0f;
25+
static const GLfloat SHININESS_LOW = 20.0f;
26+
static const GLfloat SHININESS_HIGH = 100.0f;
27+
1928
class Light {
2029
public:
2130
Light();
2231
explicit Light(const Vertex& pos, const Vertex& dir);
2332
void toggle();
2433
void draw();
2534

26-
static void ambient();
35+
static void globalAmbient();
36+
static GLboolean isColorMaterialOn();
2737

2838
private:
2939
void drawLightSource() const;

s8_casa/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ void keyCallback(unsigned char key, int x, int y) {
346346

347347
void appInit() {
348348

349-
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
349+
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
350350
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
351351
_app->mainWindowID = glutCreateWindow("house");
352352

@@ -369,8 +369,8 @@ void initLight() {
369369
glEnable(GL_DEPTH_TEST);
370370
glEnable(GL_CULL_FACE);
371371
glEnable(GL_LIGHTING);
372-
glEnable(GL_COLOR_MATERIAL);
373-
Light::ambient();
372+
// glEnable(GL_COLOR_MATERIAL);
373+
Light::globalAmbient();
374374
}
375375

376376
void createMenu() {

0 commit comments

Comments
 (0)