Skip to content

Commit b1ae552

Browse files
committed
Updated colors-and-light directory
1 parent 2f07732 commit b1ae552

File tree

19 files changed

+276
-139
lines changed

19 files changed

+276
-139
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

8-colors/main.cpp renamed to 8-colors-and-light/main.cpp

Lines changed: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ float lastY = HEIGHT / 2.0f;
5959
float ambientStrength = 0.1f;
6060
float lightStrength = 0.1f;
6161
float specularStrength = 0.1f;
62-
float specularIntensity = 64;
62+
float specularIntensity = 32;
6363

6464
// Float array for cube, each point has 3 coordinated (x,y,z)
6565
float vertices[] = {
@@ -106,7 +106,58 @@ float vertices[] = {
106106
-0.5f, 0.5f, -0.5f,
107107
};
108108

109-
GLfloat texture[] =
109+
float normals[] =
110+
{
111+
// Negetive Z axis
112+
0.0f, 0.0f, -1.0f,
113+
0.0f, 0.0f, -1.0f,
114+
0.0f, 0.0f, -1.0f,
115+
0.0f, 0.0f, -1.0f,
116+
0.0f, 0.0f, -1.0f,
117+
0.0f, 0.0f, -1.0f,
118+
119+
// Positive Z axis
120+
0.0f, 0.0f, 1.0f,
121+
0.0f, 0.0f, 1.0f,
122+
0.0f, 0.0f, 1.0f,
123+
0.0f, 0.0f, 1.0f,
124+
0.0f, 0.0f, 1.0f,
125+
0.0f, 0.0f, 1.0f,
126+
127+
// Negetive X axis
128+
-1.0f, 0.0f, 0.0f,
129+
-1.0f, 0.0f, 0.0f,
130+
-1.0f, 0.0f, 0.0f,
131+
-1.0f, 0.0f, 0.0f,
132+
-1.0f, 0.0f, 0.0f,
133+
-1.0f, 0.0f, 0.0f,
134+
135+
// Positive X axis
136+
1.0f, 0.0f, 0.0f,
137+
1.0f, 0.0f, 0.0f,
138+
1.0f, 0.0f, 0.0f,
139+
1.0f, 0.0f, 0.0f,
140+
1.0f, 0.0f, 0.0f,
141+
1.0f, 0.0f, 0.0f,
142+
143+
// Negetive Y axis
144+
0.0f, -1.0f, 0.0f,
145+
0.0f, -1.0f, 0.0f,
146+
0.0f, -1.0f, 0.0f,
147+
0.0f, -1.0f, 0.0f,
148+
0.0f, -1.0f, 0.0f,
149+
0.0f, -1.0f, 0.0f,
150+
151+
// Positive Y axis
152+
0.0f, 1.0f, 0.0f,
153+
0.0f, 1.0f, 0.0f,
154+
0.0f, 1.0f, 0.0f,
155+
0.0f, 1.0f, 0.0f,
156+
0.0f, 1.0f, 0.0f,
157+
0.0f, 1.0f, 0.0f,
158+
};
159+
160+
float texture[] =
110161
{
111162
0.0f, 0.0f,
112163
1.0f, 0.0f,
@@ -192,29 +243,29 @@ void processInput(GLFWwindow* window)
192243
{
193244
// A Cheap trick to create a more real lighting
194245
// The light will always be a little brighter then objects
195-
ambientStrength += 0.01;
246+
// ambientStrength += 0.01;
196247
lightStrength += 0.01;
197-
specularStrength += 0.01;
248+
// specularStrength += 0.01;
198249
// ambientStrength += 0.01f;
199250
if(lightStrength > 1.0f)
200251
lightStrength = 1.0f;
201-
if(ambientStrength > 0.8f)
202-
ambientStrength = 0.8f;
203-
if(specularStrength > 1.0f)
204-
specularStrength = 1.0f;
252+
// if(ambientStrength > 0.8f)
253+
// ambientStrength = 0.8f;
254+
// if(specularStrength > 1.0f)
255+
// specularStrength = 1.0f;
205256
}
206257
// If user presse DOWN key button - decrease lighting on objects
207258
if(glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
208259
{
209-
ambientStrength -= 0.01f;
260+
// ambientStrength -= 0.01f;
210261
lightStrength -= 0.01f;
211262
// ambientStrength -= 0.01f;
212263
if(lightStrength <= 0.0f)
213264
lightStrength = 0.0f;
214-
if(ambientStrength <= 0.0f)
215-
ambientStrength = 0.0f;
216-
if(specularStrength <= 0.0f)
217-
specularStrength = 0.0f;
265+
// if(ambientStrength <= 0.0f)
266+
// ambientStrength = 0.0f;
267+
// if(specularStrength <= 0.0f)
268+
// specularStrength = 0.0f;
218269
}
219270
// If user presses LEFT_SHIFT key button - increase camera movement
220271
if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
@@ -246,28 +297,32 @@ int main()
246297
Shader lightShader(LIGHT_V_SHADER_PATH.c_str(), LIGHT_F_SHADER_PATH.c_str());
247298

248299
// Create new Mesh object
249-
Mesh mesh(0, vertices, sizeof(vertices), nullptr, 0, texture, sizeof(texture), indices, sizeof(indices), GL_FLOAT, false, 0);
300+
// Mesh mesh(0, vertices, sizeof(vertices), nullptr, 0, texture, sizeof(texture), indices, sizeof(indices), GL_FLOAT, false, 0);
301+
Mesh mesh(0, vertices, sizeof(vertices), GL_FLOAT, false, 0);
302+
mesh.setNormals(normals);
303+
mesh.setIndices(indices, sizeof(indices));
304+
mesh.setTextureCoordinates(texture, sizeof(texture));
250305
// Initialise VAO (vertex array object) and VBO (vertex buffer object) for mesh
251306
mesh.initialise(GL_FLOAT, GL_FALSE);
252307
// Add indices (elements) attribute to mesh
253308
mesh.addAttribute(0, GL_ELEMENT_ARRAY_BUFFER, 3, 0, 0, indices, sizeof(indices), &mesh.elementBuffer);
254309
// Add texture attribute to mesh
255-
mesh.addAttribute(1, GL_ARRAY_BUFFER, 2, GL_FLOAT, GL_FALSE, texture, sizeof(texture), &mesh.textureBuffer);
310+
mesh.addAttribute(1, GL_ARRAY_BUFFER, 3, GL_FLOAT, false, normals, sizeof(normals), &mesh.normalsBuffer);
256311
// Unbind mesh from global state
257312
mesh.unbind();
258313

259-
Mesh light(0, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0, GL_FLOAT, false, 0);
314+
Mesh light(0, nullptr, 0, GL_FLOAT, false, 0);
260315
light.initialise(GL_FLOAT, GL_FALSE);
261316

262317
// Use colorShader program before setting uniforms
263318
colorShader.use();
264-
colorShader.setVec3("lightColor", glm::value_ptr(colors[0]));
319+
// colorShader.setVec3("lightColor", glm::value_ptr(colors[0]));
265320
// Tell OpenGL to enable depth buffer
266-
colorShader.enableDepth();
321+
Shader::enableDepth();
267322

268323
// Use lightShader program before setting uniforms
269-
lightShader.use();
270-
lightShader.setVec3("color", glm::value_ptr(colors[0]));
324+
// lightShader.use();
325+
// lightShader.setVec3("color", glm::value_ptr(colors[0]));
271326

272327
// Main loop
273328
// Run until window should close
@@ -292,10 +347,10 @@ int main()
292347
// Vector 3 (x,y,z) to specify the effect of the rotation on each of the axes
293348
glm::vec3 rotationVec;
294349
unsigned int moduloVal = 6;
295-
unsigned int lightCubeLocationIndex = 0;
350+
unsigned int lightCubeIndex = 0;
296351
glm::vec3 lightPosition;
297352
// If index devides by 3, cube will be a light source
298-
if(i == lightCubeLocationIndex)
353+
if(i == lightCubeIndex)
299354
{
300355
lightShader.use();
301356
// Set perspective projection matrix
@@ -308,6 +363,7 @@ int main()
308363
model = glm::translate(model, lightPosition);
309364
lightShader.setMatrix4fv("model", 1, GL_FALSE, glm::value_ptr(model));
310365
// Set light object's light strength
366+
lightShader.setVec3("color", glm::value_ptr(colors[lightCubeIndex]));
311367
lightShader.setFloat("strength", lightStrength);
312368
}
313369
else
@@ -317,28 +373,32 @@ int main()
317373
// Update uniform matrix in colorShader program
318374
colorShader.use();
319375
colorShader.setVec3("objectColor", glm::value_ptr(colors[i]));
320-
// Set light source location in space
321-
colorShader.setVec3("lightPos", glm::value_ptr(lightPosition));
376+
colorShader.setVec3("viewPosition", glm::value_ptr(camera.getPosition()));
377+
colorShader.setVec3("light.position", glm::value_ptr(lightPosition));
378+
colorShader.setVec3("light.ambient", glm::value_ptr(glm::vec3(1.0f) * lightStrength));
379+
colorShader.setVec3("light.diffuse", glm::value_ptr(glm::vec3(1.0f) * lightStrength));
380+
colorShader.setVec3("light.specular", glm::value_ptr(glm::vec3(1.0f) * lightStrength));
381+
382+
colorShader.setVec3("material.ambient", glm::value_ptr(glm::vec3(0.5) * colors[i] * lightStrength));
383+
colorShader.setVec3("material.diffuse", glm::value_ptr(glm::vec3(0.8) * colors[i] * lightStrength));
384+
colorShader.setVec3("material.specular", glm::value_ptr(glm::vec3(1.0) * colors[i] * lightStrength));
385+
colorShader.setFloat("material.shininess", specularIntensity);
322386
// Calculate and set object normal vector so we can calculate in shader how much the light hits the object
323387
colorShader.setMatrix3fv("normalMatrix", 1, GL_TRUE, glm::value_ptr(glm::mat3(glm::inverse(model))));
324-
colorShader.setFloat("ambientStrength", ambientStrength);
325-
colorShader.setFloat("specularStrength", specularStrength);
326-
colorShader.setFloat("specularIntensity", specularIntensity);
327388
// Set perspective projection matrix
328389
projection = glm::perspective(glm::radians(camera.getFov()), WIDTH / HEIGHT, 0.1f, 100.0f);
329390
colorShader.setMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(projection));
330391
// Set view matrix
331392
view = camera.calculateLookAtMatrix(camera.getPosition(), camera.getPosition() + camera.getFront(), camera.getUp());
332393
colorShader.setMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(view));
333-
// model = glm::rotate(model, glm::radians(angle), rotationVec);
394+
model = glm::rotate(model, glm::radians(angle), rotationVec);
334395
colorShader.setMatrix4fv("model", 1, GL_FALSE, glm::value_ptr(model));
335396
}
336397
// Render model
337398
mesh.render(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
338399
}
339400

340-
// Render triangle
341-
mesh.render(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
401+
// mesh.render(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
342402

343403
mesh.unbind();
344404

8-colors/mesh.cpp renamed to 8-colors-and-light/mesh.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include "mesh.h"
22

3-
Mesh::Mesh(unsigned int index, GLfloat* vertices, int verticesSize, GLfloat* colors, int colorsSize, GLfloat* textureCoordinates, int textureCoordinatesSize, unsigned int *indices, int indicesSize, GLenum type, bool normalize, GLsizei stride) :
3+
Mesh::Mesh(unsigned int index, GLfloat *vertices, int verticesSize, GLenum type, bool normalize, GLsizei stride) :
44
index(index),
55
vertices(vertices), verticesSize(verticesSize), // Initialise vertices
6-
colors(colors), colorsSize(colorsSize), // Initialise colors
7-
textureCoordinates(textureCoordinates), textureCoordinatesSize(textureCoordinatesSize), // Initialise texture coordinates
8-
indices(indices), indicesSize(indicesSize), // Initialise indices (elements)
6+
// colors(colors), colorsSize(colorsSize), // Initialise colors
7+
// textureCoordinates(textureCoordinates), textureCoordinatesSize(textureCoordinatesSize), // Initialise texture coordinates
8+
// indices(indices), indicesSize(indicesSize), // Initialise indices (elements)
99
vertexArray(0),
1010
vertexBuffer(0),
11-
colorBuffer(0),
11+
normalsBuffer(0),
1212
textureBuffer(0),
1313
elementBuffer(0)
1414
{
@@ -19,7 +19,6 @@ Mesh::~Mesh()
1919
{
2020
clear();
2121
vertices = nullptr;
22-
colors = nullptr;
2322
textureCoordinates = nullptr;
2423
indices = nullptr;
2524
}
@@ -104,10 +103,10 @@ void Mesh::clear()
104103
vertexBuffer = 0;
105104
}
106105

107-
if(colorBuffer != 0)
106+
if(normalsBuffer != 0)
108107
{
109-
glDeleteBuffers(1, &colorBuffer);
110-
colorBuffer = 0;
108+
glDeleteBuffers(1, &normalsBuffer);
109+
normalsBuffer = 0;
111110
}
112111

113112
if (textureBuffer != 0)

8-colors-and-light/mesh.h

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#pragma once
2+
3+
#include <GL/glew.h>
4+
#include <iostream>
5+
6+
7+
/**
8+
* @brief Class Mesh handles mesh operations such as creating new mesh, rendering, and clearing mesh.
9+
*/
10+
class Mesh
11+
{
12+
private:
13+
unsigned int index;
14+
GLfloat *vertices, *normals, *textureCoordinates;
15+
int verticesSize, textureCoordinatesSize, indicesSize;
16+
GLuint *indices;
17+
18+
public:
19+
/**
20+
* @brief Constructor to build new Mesh object from vertices and their size.
21+
* @param vertices Vertices (points) of mesh in space.
22+
* @param verticesSize Size of vertices in memory.
23+
*/
24+
Mesh(unsigned int index, GLfloat *vertices, int verticesSize, GLenum type, bool normalize, GLsizei stride);
25+
/**
26+
* @brief Destructor.
27+
*/
28+
~Mesh();
29+
30+
// Methods
31+
/**
32+
* @brief Initialise mesh object
33+
*/
34+
void initialise(GLenum type, bool normalize);
35+
36+
void addAttribute(int index, GLenum target, int vertexSize, GLenum type, bool normalize, void *data, int dataSize, GLuint *buffer);
37+
38+
void unbind();
39+
40+
/**
41+
* @brief Render mesh.
42+
*/
43+
void render(GLenum mode, GLsizei count, GLenum type, const void *indices);
44+
45+
void copy(GLenum mode, GLsizei count, GLenum type, const void *indices);
46+
47+
/**
48+
* @brief Clear mesh.
49+
*/
50+
void clear();
51+
52+
// Fields
53+
GLuint vertexArray, vertexBuffer, normalsBuffer, textureBuffer, elementBuffer;
54+
55+
// Setters
56+
void setVertices(GLfloat *newVertices, int newSize)
57+
{
58+
vertices = newVertices;
59+
verticesSize = newSize;
60+
}
61+
62+
void setNormals(GLfloat *newNormals)
63+
{
64+
normals = newNormals;
65+
}
66+
67+
void setTextureCoordinates(GLfloat *newTextureCoordinates, int newSize)
68+
{
69+
textureCoordinates = newTextureCoordinates;
70+
textureCoordinatesSize = newSize;
71+
}
72+
73+
void setIndices(GLuint *newIndices, int newSize)
74+
{
75+
indices = newIndices;
76+
indicesSize = newSize;
77+
}
78+
79+
// Getters
80+
GLfloat* getVertices() const
81+
{
82+
return vertices;
83+
}
84+
85+
GLfloat* getNormals() const
86+
{
87+
return normals;
88+
}
89+
90+
GLfloat* getTextureCoordinates() const
91+
{
92+
return textureCoordinates;
93+
}
94+
95+
GLuint* getIndices() const
96+
{
97+
return indices;
98+
}
99+
100+
int getVerticesSize() const
101+
{
102+
return verticesSize;
103+
}
104+
105+
int getTextureCoordinatesSize() const
106+
{
107+
return textureCoordinatesSize;
108+
}
109+
110+
int getIndicesSize() const
111+
{
112+
return indicesSize;
113+
}
114+
};
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy