Skip to content

Commit 14630ce

Browse files
committed
Updated code a bit
1 parent 23282f2 commit 14630ce

File tree

8 files changed

+202
-63
lines changed

8 files changed

+202
-63
lines changed

5-tranformations/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.12)
22

33
project(transformations)
44

5-
add_executable(main.o main.cpp glWindow.cpp mesh.cpp shader.cpp stb_image.cpp)
5+
add_executable(main.o main.cpp glWindow.cpp mesh.cpp shader.cpp texture.cpp stb_image.cpp)
66

77
find_package(GLEW REQUIRED)
88
target_link_libraries(main.o GLEW::GLEW)

5-tranformations/main.cpp

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "glWindow.h"
22
#include "shader.h"
33
#include "mesh.h"
4+
#include "texture.h"
45
#include "stb_image.h"
56

67
#include <glm/glm.hpp>
@@ -9,13 +10,13 @@
910

1011

1112
// Window dimensions
12-
constexpr GLint WIDTH = 800, HEIGHT = 600;
13-
std::string TITLE = "Main Window";
13+
GLint width = 800, height = 600;
14+
std::string title = "Main Window";
1415
// Working directory
15-
std::string PWD = std::filesystem::current_path().string();
16+
std::string pwd = std::filesystem::current_path().string();
1617
// Shader sources
17-
std::string VERTEX_SHADER_PATH = PWD + "/../shaders/vShader.glsl";
18-
std::string FRAGMENT_SHADER_PATH = PWD + "/../shaders/fShader.glsl";
18+
std::string vShaderPath = pwd + "/../shaders/vShader.glsl";
19+
std::string fShaderPath = pwd + "/../shaders/fShader.glsl";
1920

2021
// Matrix 4x4 for tranformation calculations
2122
glm::mat4 transform;
@@ -29,31 +30,23 @@ float scaleValue = 0.2;
2930
float rotationValue = 0;
3031

3132
// Float array for triangle shape, each point has 3 coordinated (x,y,z)
32-
GLfloat RECTANGLE[] =
33+
GLfloat rectangle[] =
3334
{ // Positions // Colors
3435
-1.0f, -1.0f, 0.0f, //1.0f, 0.0f, 0.0f, // Bottom left
3536
1.0f, -1.0f, 0.0f, //0.0f, 1.0f, 0.0f, // Bottom right
3637
1.0f, 1.0f, 0.0f, //0.0f, 0.0f, 1.0f, // Top right
3738
-1.0f, 1.0f, 0.0f, //1.0f, 0.0f, 1.0f, // Top left
3839
};
3940

40-
GLfloat COLORS[] =
41-
{
42-
1.0f, 0.0f, 0.0f, // Red
43-
0.0f, 1.0f, 0.0f, // Green
44-
0.0f, 0.0f, 1.0f, // Blue
45-
1.0f, 0.0f, 1.0f, // Red + Blue
46-
};
47-
48-
GLfloat TEXTURE[] =
41+
GLfloat texture[] =
4942
{
5043
0.0f, 0.0f, // Bottom left
5144
0.0f, 1.0f, // Top left
5245
1.0f, 1.0f, // Top right
5346
1.0f, 0.0f, // Bottom right
5447
};
5548

56-
unsigned int INDICES[] =
49+
unsigned int indices[] =
5750
{
5851
0, 1, 2, // First triangle (bottom left + bottom right + top right)
5952
0, 2, 3, // Second triangle (bottom left + top right + top left)
@@ -108,50 +101,48 @@ void processInput(GLFWwindow* window)
108101
{
109102
translationVec = glm::vec3(translationVec.x + translationSpeed, translationVec.y, 0.0f);
110103
}
104+
105+
// If user presses ESC key button - exit program
106+
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
107+
exit(0);
111108
}
112109

113110
int main()
114111
{
115-
glWindow window(TITLE, WIDTH, HEIGHT);
112+
glWindow window(title, width, height);
116113

117114
// Setup Shaders
118115
// Create new shader object from shader files and compile shader program
119-
Shader shader(VERTEX_SHADER_PATH.c_str(), FRAGMENT_SHADER_PATH.c_str());
116+
Shader shader(vShaderPath.c_str(), fShaderPath.c_str());
120117

121118
// Create new Mesh object to handle triangle shape.
122-
Mesh mesh(RECTANGLE, sizeof(RECTANGLE), COLORS, sizeof(COLORS), TEXTURE, sizeof(TEXTURE), INDICES, sizeof(INDICES), GL_FLOAT, false, 0);
119+
Mesh mesh(rectangle, sizeof(rectangle), texture, sizeof(texture), indices, sizeof(indices), GL_FLOAT, false, 0);
123120
// Initialise VAO (vertex array object) and VBO (vertex buffer object) for mesh
124121
mesh.initialise(GL_FLOAT, GL_FALSE);
125122
// Add indices (elements) attribute to mesh
126-
mesh.addAttribute(0, GL_ELEMENT_ARRAY_BUFFER, 3, 0, 0, INDICES, sizeof(INDICES), &mesh.elementBuffer);
127-
// Add color attribute to mesh
128-
mesh.addAttribute(1, GL_ARRAY_BUFFER, 3, GL_FLOAT, GL_FALSE, COLORS, sizeof(COLORS), &mesh.colorBuffer);
123+
mesh.addAttribute(0, GL_ELEMENT_ARRAY_BUFFER, 3, GL_UNSIGNED_INT, 0, indices, sizeof(indices), &mesh.elementBuffer);
129124
// Add texture attribute to mesh
130-
mesh.addAttribute(2, GL_ARRAY_BUFFER, 2, GL_FLOAT, GL_FALSE, TEXTURE, sizeof(TEXTURE), &mesh.textureBuffer);
125+
mesh.addAttribute(1, GL_ARRAY_BUFFER, 2, GL_FLOAT, GL_FALSE, texture, sizeof(texture), &mesh.textureBuffer);
131126
// Unbind mesh from global state
132127
mesh.unbind();
133128

134129
// Generating texture and configuring parameters
135-
GLuint texture1, texture2;
136-
shader.loadTexture(std::filesystem::current_path().concat("/../../assets/container.jpg"), &texture1, GL_TEXTURE_2D, GL_REPEAT, GL_LINEAR, 0, GL_RGB, 0, GL_RGB, GL_UNSIGNED_BYTE);
137-
shader.loadTexture(std::filesystem::current_path().concat("/../../assets/awesomeface.png"), &texture2, GL_TEXTURE_2D, GL_REPEAT, GL_LINEAR, 0, GL_RGBA, 0, GL_RGBA, GL_UNSIGNED_BYTE);
138-
139-
// OpenGL should have provide 16 texture units from the GPU
140-
// Texture units can be activated using "GL_TEXTURE0" to "GL_TEXTURE15"
141-
// They are defined in order so we could also get GL_TEXTURE8 via GL_TEXTURE0 + 8 for example,
142-
// which is useful when we'd have to loop over several texture units.
143-
glActiveTexture(GL_TEXTURE0); // Activate texture unit 0
144-
glBindTexture(GL_TEXTURE_2D, texture1); // Bind texture unit 0 to "texture1"
145-
glActiveTexture(GL_TEXTURE1); // Activate texture unit 1
146-
glBindTexture(GL_TEXTURE_2D, texture2); // Bind texture unit 1 to "texture2"
130+
Texture container(0, GL_TEXTURE_2D, GL_REPEAT, GL_LINEAR, 0, 0, pwd + "/../../assets/container.jpg", GL_TEXTURE0);
131+
Texture smileyFace(1, GL_TEXTURE_2D, GL_REPEAT, GL_LINEAR, 0, 0, pwd + "/../../assets/awesomeface.png", GL_TEXTURE1);
132+
133+
container.loadTexture();
134+
smileyFace.loadTexture();
135+
136+
container.useTexture();
137+
smileyFace.useTexture();
147138

148139
// Use shader program before setting uniforms
149140
shader.use();
150141

151142
// Set uniform sampler2D "texture1" to texture unit 0
152-
shader.setInt("texture1", 0);
143+
shader.setInt("textures[0]", 0);
153144
// Set uniform sampler2D "texture1" to texture unit 1
154-
shader.setInt("texture2", 1);
145+
shader.setInt("textures[1]", 1);
155146

156147
// Main loop
157148
// Run until window should close
@@ -179,7 +170,7 @@ int main()
179170
// Render triangle
180171
mesh.render(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
181172

182-
// Uncomment to render second container with smiley face :)
173+
// Uncomment to render second container with a smiley face :)
183174
// transform = glm::mat4(1.0f);
184175
// transform = glm::translate(transform, glm::vec3(-0.3f, 0.3f, 0.0f));
185176
// transform = glm::scale(transform, glm::vec3(scaleValue));

5-tranformations/mesh.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#include "mesh.h"
22

3-
Mesh::Mesh(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(GLfloat* vertices, int verticesSize, GLfloat* textureCoordinates, int textureCoordinatesSize, unsigned int *indices, int indicesSize, GLenum type, bool normalize, GLsizei stride) :
44
vertices(vertices), verticesSize(verticesSize), // Initialise vertices
5-
colors(colors), colorsSize(colorsSize), // Initialise colors
65
textureCoordinates(textureCoordinates), textureCoordinatesSize(textureCoordinatesSize), // Initialise texture coordinates
76
indices(indices), indicesSize(indicesSize), // Initialise indices (elements)
87
vertexArray(0),
98
vertexBuffer(0),
10-
colorBuffer(0),
119
textureBuffer(0),
1210
elementBuffer(0)
1311
{
@@ -18,7 +16,6 @@ Mesh::~Mesh()
1816
{
1917
clear();
2018
vertices = nullptr;
21-
colors = nullptr;
2219
textureCoordinates = nullptr;
2320
indices = nullptr;
2421
}
@@ -100,12 +97,6 @@ void Mesh::clear()
10097
vertexBuffer = 0;
10198
}
10299

103-
if(colorBuffer != 0)
104-
{
105-
glDeleteBuffers(1, &colorBuffer);
106-
colorBuffer = 0;
107-
}
108-
109100
if (textureBuffer != 0)
110101
{
111102
glDeleteBuffers(1, &textureBuffer);

5-tranformations/mesh.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Mesh
1515
* @param vertices Vertices (points) of mesh in space.
1616
* @param verticesSize Size of vertices in memory.
1717
*/
18-
Mesh(GLfloat *vertices, int verticesSize, GLfloat *colors, int colorsSize, GLfloat *textureCoordinates, int textureCoordinatesSize, unsigned int *indices, int indicesSize, GLenum type, bool normalize, GLsizei stride);
18+
Mesh(GLfloat *vertices, int verticesSize, GLfloat *textureCoordinates, int textureCoordinatesSize, unsigned int *indices, int indicesSize, GLenum type, bool normalize, GLsizei stride);
1919
/**
2020
* @brief Destructor.
2121
*/
@@ -47,7 +47,7 @@ class Mesh
4747
GLuint vertexArray, vertexBuffer, colorBuffer, textureBuffer, elementBuffer;
4848

4949
private:
50-
GLfloat *vertices, *colors, *textureCoordinates;
51-
int verticesSize, colorsSize, textureCoordinatesSize, indicesSize;
50+
GLfloat *vertices, *textureCoordinates;
51+
int verticesSize, textureCoordinatesSize, indicesSize;
5252
GLuint *indices;
5353
};

5-tranformations/shaders/fShader.glsl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#version 330 core
22

3-
// in vec3 color;
3+
#define NUM_TEXTURES 2
4+
45
in vec2 texCoord;
56

67
out vec4 fragColor;
78

8-
uniform sampler2D texture1;
9-
uniform sampler2D texture2;
10-
// uniform float mixValue;
9+
uniform sampler2D textures[NUM_TEXTURES];
1110

1211
void main()
1312
{
14-
fragColor = mix(texture(texture1, texCoord), texture(texture2, vec2(texCoord.y, 1.0 - texCoord.x)), 0.5);
13+
fragColor = mix(texture(textures[0], texCoord), texture(textures[1], vec2(texCoord.y, texCoord.x)), 0.5);
1514
}

5-tranformations/shaders/vShader.glsl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
#version 330 core
22

33
layout (location = 0) in vec3 aPos;
4-
layout (location = 1) in vec3 aColor;
5-
layout (location = 2) in vec2 aTexCoord;
4+
layout (location = 1) in vec2 aTexCoord;
65

7-
// out vec3 color;
86
out vec2 texCoord;
97

108
uniform mat4 transform;
119

1210
void main()
1311
{
1412
gl_Position = transform * vec4(aPos, 1.0);
15-
16-
// color = aColor;
17-
13+
1814
texCoord = aTexCoord;
1915
}

5-tranformations/texture.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include "texture.h"
2+
#include "stb_image.h"
3+
4+
Texture::Texture()
5+
{
6+
textureId = 0;
7+
target = DEFAULT_TARGET;
8+
wrappingParams = DEFAULT_WRAPPING;
9+
filterParams = DEFAULT_FILTERING;
10+
width = 0;
11+
height = 0;
12+
nrComponents = 0;
13+
path = "";
14+
textureUnit = GL_TEXTURE0;
15+
}
16+
17+
Texture::Texture(GLuint textureId, GLenum target, GLenum wrappingParams, GLenum filterParams, int width, int height, std::string path, unsigned int textureUnit)
18+
{
19+
this->textureId = textureId;
20+
this->target = target;
21+
this->wrappingParams = wrappingParams;
22+
this->filterParams = filterParams;
23+
this->width = width;
24+
this->height = height;
25+
this->path = path;
26+
this->textureUnit = textureUnit;
27+
}
28+
29+
Texture::~Texture()
30+
{
31+
clearTexture();
32+
}
33+
34+
void Texture::loadTexture()
35+
{
36+
// Load image via "stbi_image" library
37+
unsigned char* textureData = stbi_load(path.c_str(), &width, &height, &nrComponents, 0);
38+
if(!textureData)
39+
{
40+
std::cerr << "ERROR::Texture::loadTexture()::can't load texture file " << path << std::endl;
41+
return;
42+
}
43+
44+
// Generate 1 texture and bind it to "textureId"
45+
glGenTextures(1, &textureId);
46+
glBindTexture(target, textureId);
47+
48+
// Set texture wrapping params
49+
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrappingParams);
50+
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrappingParams);
51+
52+
// Set texture filtering params
53+
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filterParams);
54+
glTexParameterf(target, GL_TEXTURE_MAG_FILTER, filterParams);
55+
56+
stbi_set_flip_vertically_on_load(true);
57+
58+
// Define the format of image according to number of channels in image (number of color components)
59+
GLint format;
60+
switch(nrComponents)
61+
{
62+
case 1:
63+
format = GL_RED;
64+
break;
65+
case 2:
66+
format = GL_RG;
67+
break;
68+
case 3:
69+
format = GL_RGB;
70+
break;
71+
case 4:
72+
format = GL_RGBA;
73+
break;
74+
}
75+
// Create 2D texture image
76+
glTexImage2D(target, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, textureData);
77+
// Generate mipmap for image
78+
glGenerateMipmap(target);
79+
// Set state back to default after configuration
80+
glBindTexture(target, 0);
81+
// Free pointer to texture data
82+
free(textureData);
83+
}
84+
85+
void Texture::useTexture()
86+
{
87+
glActiveTexture(textureUnit);
88+
glBindTexture(target, textureId);
89+
}
90+
91+
void Texture::clearTexture()
92+
{
93+
glDeleteTextures(1, &textureId);
94+
textureId = 0;
95+
target = 0;
96+
wrappingParams = 0;
97+
filterParams = 0;
98+
width = 0;
99+
height = 0;
100+
nrComponents = 0;
101+
path = "";
102+
textureUnit = 0;
103+
}

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