1
1
#include " glWindow.h"
2
2
#include " shader.h"
3
3
#include " mesh.h"
4
+ #include " texture.h"
4
5
#include " stb_image.h"
5
6
6
7
#include < glm/glm.hpp>
9
10
10
11
11
12
// 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" ;
14
15
// Working directory
15
- std::string PWD = std::filesystem::current_path().string();
16
+ std::string pwd = std::filesystem::current_path().string();
16
17
// 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" ;
19
20
20
21
// Matrix 4x4 for tranformation calculations
21
22
glm::mat4 transform;
@@ -29,31 +30,23 @@ float scaleValue = 0.2;
29
30
float rotationValue = 0 ;
30
31
31
32
// Float array for triangle shape, each point has 3 coordinated (x,y,z)
32
- GLfloat RECTANGLE [] =
33
+ GLfloat rectangle [] =
33
34
{ // Positions // Colors
34
35
-1 .0f , -1 .0f , 0 .0f , // 1.0f, 0.0f, 0.0f, // Bottom left
35
36
1 .0f , -1 .0f , 0 .0f , // 0.0f, 1.0f, 0.0f, // Bottom right
36
37
1 .0f , 1 .0f , 0 .0f , // 0.0f, 0.0f, 1.0f, // Top right
37
38
-1 .0f , 1 .0f , 0 .0f , // 1.0f, 0.0f, 1.0f, // Top left
38
39
};
39
40
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[] =
49
42
{
50
43
0 .0f , 0 .0f , // Bottom left
51
44
0 .0f , 1 .0f , // Top left
52
45
1 .0f , 1 .0f , // Top right
53
46
1 .0f , 0 .0f , // Bottom right
54
47
};
55
48
56
- unsigned int INDICES [] =
49
+ unsigned int indices [] =
57
50
{
58
51
0 , 1 , 2 , // First triangle (bottom left + bottom right + top right)
59
52
0 , 2 , 3 , // Second triangle (bottom left + top right + top left)
@@ -108,50 +101,48 @@ void processInput(GLFWwindow* window)
108
101
{
109
102
translationVec = glm::vec3 (translationVec.x + translationSpeed, translationVec.y , 0 .0f );
110
103
}
104
+
105
+ // If user presses ESC key button - exit program
106
+ if (glfwGetKey (window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
107
+ exit (0 );
111
108
}
112
109
113
110
int main ()
114
111
{
115
- glWindow window (TITLE, WIDTH, HEIGHT );
112
+ glWindow window (title, width, height );
116
113
117
114
// Setup Shaders
118
115
// 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 ());
120
117
121
118
// 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 );
123
120
// Initialise VAO (vertex array object) and VBO (vertex buffer object) for mesh
124
121
mesh.initialise (GL_FLOAT, GL_FALSE);
125
122
// 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 );
129
124
// 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 );
131
126
// Unbind mesh from global state
132
127
mesh.unbind ();
133
128
134
129
// 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 ();
147
138
148
139
// Use shader program before setting uniforms
149
140
shader.use ();
150
141
151
142
// Set uniform sampler2D "texture1" to texture unit 0
152
- shader.setInt (" texture1 " , 0 );
143
+ shader.setInt (" textures[0] " , 0 );
153
144
// Set uniform sampler2D "texture1" to texture unit 1
154
- shader.setInt (" texture2 " , 1 );
145
+ shader.setInt (" textures[1] " , 1 );
155
146
156
147
// Main loop
157
148
// Run until window should close
@@ -179,7 +170,7 @@ int main()
179
170
// Render triangle
180
171
mesh.render (GL_TRIANGLES, 6 , GL_UNSIGNED_INT, 0 );
181
172
182
- // Uncomment to render second container with smiley face :)
173
+ // Uncomment to render second container with a smiley face :)
183
174
// transform = glm::mat4(1.0f);
184
175
// transform = glm::translate(transform, glm::vec3(-0.3f, 0.3f, 0.0f));
185
176
// transform = glm::scale(transform, glm::vec3(scaleValue));
0 commit comments