Skip to content

Commit d367002

Browse files
committed
Updated code with Texture class and removed unnecessary code and comments
1 parent 14630ce commit d367002

File tree

5 files changed

+190
-85
lines changed

5 files changed

+190
-85
lines changed

6-coordinate-systems/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(coordinate_systems)
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)

6-coordinate-systems/main.cpp

Lines changed: 23 additions & 81 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-
GLfloat WIDTH = 800.0f, HEIGHT = 600.0f;
13-
std::string TITLE = "Main Window";
13+
GLfloat width = 800.0f, height = 600.0f;
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
// Model transformation matrix
2122
glm::mat4 model;
@@ -138,62 +139,18 @@ glm::vec3 cubePositions[] = {
138139

139140
void processInput(GLFWwindow* window)
140141
{
141-
// If user press up key button - increase scale
142-
if(glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
143-
scaleValue += 0.01;
144-
// if((scaleValue) >= 1.0)
145-
// scaleValue = 0.1;
146-
147-
// If user press down key button - decrease scale
148-
if(glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
149-
scaleValue -= 0.01;
150-
// if(scaleValue <= 0)
151-
// scaleValue = 0.99;
152-
153-
// If user press right key button - rotate to right
154-
if(glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
155-
rotationValue -= 1;
156-
if(rotationValue <= -360)
157-
rotationValue = 0;
158-
159-
// If user press left key button - rotate to left
160-
if(glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
161-
rotationValue += 1;
162-
if(rotationValue >= 360)
163-
rotationValue = 0;
164-
165-
// If user press "W" key button - move up the y axis
166-
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
167-
{
168-
translationVec = glm::vec3(translationVec.x, translationVec.y + translationSpeed, 0.0f);
169-
}
170-
171-
// If user press "S" key button - move down the y axis
172-
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
173-
{
174-
translationVec = glm::vec3(translationVec.x, translationVec.y - translationSpeed, 0.0f);
175-
}
176-
177-
// If user press "A" key button - move up x axis (move right)
178-
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
179-
{
180-
translationVec = glm::vec3(translationVec.x - translationSpeed, translationVec.y, 0.0f);
181-
}
182-
183-
// If user press "D" key button - move down x axis (move left)
184-
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
185-
{
186-
translationVec = glm::vec3(translationVec.x + translationSpeed, translationVec.y, 0.0f);
187-
}
142+
// If user presses ESC key button - exit program
143+
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
144+
exit(0);
188145
}
189146

190147
int main()
191148
{
192-
glWindow window(TITLE, WIDTH, HEIGHT);
149+
glWindow window(title, width, height);
193150

194151
// Setup Shaders
195152
// Create new shader object from shader files and compile shader program
196-
Shader shader(VERTEX_SHADER_PATH.c_str(), FRAGMENT_SHADER_PATH.c_str());
153+
Shader shader(vShaderPath.c_str(), fShaderPath.c_str());
197154

198155
// Create new Mesh object to handle triangle shape.
199156
Mesh mesh(vertices, sizeof(vertices), nullptr, 0, texture, sizeof(texture), indices, sizeof(indices), GL_FLOAT, false, 0);
@@ -206,38 +163,30 @@ int main()
206163
// Unbind mesh from global state
207164
mesh.unbind();
208165

209-
// Generating texture and configuring parameters
210-
GLuint texture1, texture2;
211-
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);
212-
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);
213-
214-
// OpenGL should have provide 16 texture units from the GPU
215-
// Texture units can be activated using "GL_TEXTURE0" to "GL_TEXTURE15"
216-
// They are defined in order so we could also get GL_TEXTURE8 via GL_TEXTURE0 + 8 for example,
217-
// which is useful when we'd have to loop over several texture units.
218-
glActiveTexture(GL_TEXTURE0); // Activate texture unit 0
219-
glBindTexture(GL_TEXTURE_2D, texture1); // Bind texture unit 0 to "texture1"
220-
glActiveTexture(GL_TEXTURE1); // Activate texture unit 1
221-
glBindTexture(GL_TEXTURE_2D, texture2); // Bind texture unit 1 to "texture2"
166+
// Textures
167+
Texture container(0, GL_TEXTURE_2D, GL_REPEAT, GL_LINEAR, 0, 0, pwd + "/../../assets/container.jpg", GL_TEXTURE0);
168+
Texture smileyFace(1, GL_TEXTURE_2D, GL_REPEAT, GL_LINEAR, 0, 0, pwd + "/../../assets/awesomeface.png", GL_TEXTURE1);
169+
container.loadTexture();
170+
smileyFace.loadTexture();
171+
container.useTexture();
172+
smileyFace.useTexture();
222173

223174
// Use shader program before setting uniforms
224175
shader.use();
225176

226177
// Tell OpenGL to enable depth buffer
227178
shader.enableDepth();
228179

229-
// Set uniform sampler2D "texture1" to texture unit 0
230-
shader.setInt("texture1", 0);
231-
// Set uniform sampler2D "texture1" to texture unit 1
232-
shader.setInt("texture2", 1);
180+
// Set uniform sampler2D at index 0 in sampler2D array to point to 0
181+
shader.setInt("textures[0]", 0);
182+
// Set uniform sampler2D at index 1 in sampler2D array to point to 1
183+
shader.setInt("textures[1]", 1);
233184

234185
// Main loop
235186
// Run until window should close
236187
while (!window.isShouldClose())
237188
{
238189
processInput(window.getGlWindow());
239-
// window.processInput(GLFW_KEY_UP, &mixValue, 0.01f);
240-
// window.processInput(GLFW_KEY_DOWN, &mixValue, -0.01f);
241190

242191
// Clear window to black screen
243192
glClearColor(0, 0, 0, 1);
@@ -279,21 +228,14 @@ int main()
279228
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
280229
shader.setMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(view));
281230

282-
projection = glm::perspective(glm::radians(45.0f), WIDTH / HEIGHT, 0.1f, 100.0f);
231+
projection = glm::perspective(glm::radians(45.0f), width / height, 0.1f, 100.0f);
283232
shader.setMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(projection));
284233

285-
// Render triangle
286234
mesh.render(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
287235

288236
mesh.unbind();
289237

290-
// OpenGL uses a double buffer (front and back buffers) technique for rendering
291-
// All of the rendering commands will go to back buffer
292-
// After all of the rendering commands are done we switch to the front buffer which has the final output image
293-
// If we don't do this we can see flickering in output image, cause the image is build pixel by pixel from left to right and from top to bottom
294-
// So basically we'll see the image being build and that's why will see the flickering (if image is small and not complex than it probably won't matter)
295238
window.swapBuffers();
296-
// Get + Handle glfwWindow I/O
297239
glfwPollEvents();
298240
}
299241

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

3+
#define NUM_TEXTURES 2
4+
35
in vec2 texCoord;
46

57
out vec4 fragColor;
68

7-
uniform sampler2D texture1;
8-
uniform sampler2D texture2;
9+
uniform sampler2D textures[NUM_TEXTURES];
910

1011
void main()
1112
{
12-
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, 1.0 - texCoord.x)), 0.5);
1314
}

6-coordinate-systems/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+
}

6-coordinate-systems/texture.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#pragma once
2+
3+
#include <GL/glew.h>
4+
#include <GLFW/glfw3.h>
5+
#include <GL/gl.h>
6+
#include <string>
7+
#include <iostream>
8+
9+
constexpr GLenum DEFAULT_TARGET = GL_TEXTURE_2D;
10+
constexpr GLenum DEFAULT_WRAPPING = GL_REPEAT;
11+
constexpr GLenum DEFAULT_FILTERING = GL_LINEAR;
12+
13+
/**
14+
* @brief Class Texture to handle texture operations such as - loading, using and clearing textures.
15+
*/
16+
class Texture
17+
{
18+
private:
19+
GLuint textureId; // ID to reference texture
20+
GLenum target; // Defines the type of texture (1D/2D/3D), for some reason OpenGL is calling it "target" instead of "type".
21+
GLenum wrappingParams; // Specifies how to wrap the texture on mesh
22+
GLenum filterParams; // Specifies filter options for texture
23+
int width, height, nrComponents; // Specifies the width, height and depth in space for texture
24+
std::string path; // Path to texture file
25+
unsigned int textureUnit; // Specifies which texture unit should this texture use (usually most computers will have a minimum of 16 units), starting from GL_TEXTURE0 - GL_TEXTURE[N]
26+
27+
public:
28+
Texture();
29+
Texture(GLuint textureId, GLenum target, GLenum wrappingParams, GLenum filterParams, int width, int height, std::string path, unsigned int textureUnit);
30+
~Texture();
31+
32+
void loadTexture();
33+
void useTexture();
34+
void clearTexture();
35+
36+
GLuint getTextureId() { return textureId; };
37+
void setTextureId(GLuint textureId) { this->textureId = textureId; };
38+
39+
GLenum getTarget() { return target; };
40+
void setTarget(GLenum target) { this->target = target; };
41+
42+
GLenum getWrappingParams() { return wrappingParams; };
43+
void setWrappingParams(GLenum wrappingParams) { this->wrappingParams = wrappingParams; };
44+
45+
GLenum getFilterParams() { return filterParams; };
46+
void setFilterParams(GLenum filterParams) { this->filterParams = filterParams; };
47+
48+
int getWidth() { return width; };
49+
void setWidth(int width) { this->width = width; };
50+
51+
int getHeight() { return height; };
52+
void setHeight(int height) { this->height = height; };
53+
54+
int getNrComponents() { return nrComponents; };
55+
void setNrComponents(int nrComponents) { this->nrComponents = nrComponents; };
56+
57+
unsigned int getTextureUnit() { return textureUnit; };
58+
void setTextureUnit(unsigned int textureUnit) { this->textureUnit = textureUnit; };
59+
};

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