diff --git a/OpenGL/OpenGL.vcxproj b/OpenGL/OpenGL.vcxproj
index 36502c7..804288a 100644
--- a/OpenGL/OpenGL.vcxproj
+++ b/OpenGL/OpenGL.vcxproj
@@ -1,4 +1,4 @@
-
+
@@ -77,6 +77,7 @@
true
true
.\include;%(AdditionalIncludeDirectories)
+ _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)
.\lib;%(AdditionalLibraryDirectories)
@@ -124,11 +125,15 @@
+
+
-
-
-
+
+
+
+
+
diff --git a/OpenGL/OpenGL.vcxproj.filters b/OpenGL/OpenGL.vcxproj.filters
index e315d3d..6951a5e 100644
--- a/OpenGL/OpenGL.vcxproj.filters
+++ b/OpenGL/OpenGL.vcxproj.filters
@@ -5,21 +5,34 @@
+
+ helpers
+
+
{3e9ef4e5-bcd4-4161-8243-90e67d85dc43}
+
+ {3b3efd97-0e64-421a-b292-4d37dfc52e96}
+
-
+
headers
-
+
headers
-
+
headers
+
+ headers
+
+
+ helpers
+
\ No newline at end of file
diff --git a/OpenGL/display.cpp b/OpenGL/display.cpp
index b2cfd05..61ba2a8 100644
--- a/OpenGL/display.cpp
+++ b/OpenGL/display.cpp
@@ -1,7 +1,7 @@
#include
#include
-#include "headers/display.h"
+#include "display.h"
Display::Display(int width, int height, const std::string& title) {
SDL_Init(SDL_INIT_VIDEO);
diff --git a/OpenGL/headers/display.h b/OpenGL/display.h
similarity index 100%
rename from OpenGL/headers/display.h
rename to OpenGL/display.h
diff --git a/OpenGL/main.cpp b/OpenGL/main.cpp
index 22bb368..4d09851 100644
--- a/OpenGL/main.cpp
+++ b/OpenGL/main.cpp
@@ -1,29 +1,39 @@
#include
#include
-#include "headers/display.h"
-#include "headers/mesh.h"
-#include "headers/shader.h"
+#include "display.h"
+#include "mesh.h"
+#include "shader.h"
+#include "texture.h"
#undef main
int main(int argc, char** argv) {
- Display display(800, 600, "Crumpet Engine");
+ Display display(800, 800, "Crumpet Engine");
glClearColor(0.1f, 0.45f, 0.9f, 1.0f);
GLfloat vertices[] = {
- -0.5f, -0.5f, 0.0f,
- 0.5f, -0.5f, 0.0f,
- 0.0f, 0.5f, 0.0f
+ // positions // colors // texture coords
+ 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
+ 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
+ -0.5f,-0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
+ -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
- Mesh mesh(vertices, sizeof(vertices) / sizeof(vertices[0]));
- Shader shader;
+ unsigned int indices[] = {
+ 0, 1, 3, // first triangle
+ 1, 2, 3 // second triangle
+ };
+
+ Mesh mesh(vertices, indices, sizeof(vertices) / sizeof(vertices[0]));
+ Shader shader("E:/Games/Practicing/OpenGL/resources/shaders/simple2d");
+ Texture chanceCube("E:/Games/Practicing/OpenGL/resources/textures/chance-cube.jpg");
while(!display.isClosed()) {
glClear(GL_COLOR_BUFFER_BIT);
shader.Bind();
+ chanceCube.Bind(0);
mesh.Draw();
display.Update();
diff --git a/OpenGL/mesh.cpp b/OpenGL/mesh.cpp
index 1decc73..1818fa5 100644
--- a/OpenGL/mesh.cpp
+++ b/OpenGL/mesh.cpp
@@ -1,20 +1,32 @@
-#include "headers/mesh.h"
+#include "mesh.h"
-Mesh::Mesh(GLfloat *vertices, unsigned int numVerticies) {
+Mesh::Mesh(GLfloat *vertices, unsigned int *indices, unsigned int numVerticies) {
m_drawCount = numVerticies;
glGenVertexArrays(1, &m_VAO);
+ glGenBuffers(1, &m_VBO);
+ glGenBuffers(1, &m_EBO);
+
glBindVertexArray(m_VAO);
- glGenBuffers(NUM_BUFFERS, m_VBO);
- glBindBuffer(GL_ARRAY_BUFFER, m_VBO[POSITION_VB]);
- glBufferData(GL_ARRAY_BUFFER, numVerticies * sizeof(vertices[0]), vertices, GL_STATIC_DRAW);
+ glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
+
+ // position attribute
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
+ // color attribute
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
+ glEnableVertexAttribArray(1);
+ // texture coord attribute
+ glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
+ glEnableVertexAttribArray(2);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
+
std::cout << "Mesh loaded successfully" << std::endl;
}
diff --git a/OpenGL/headers/mesh.h b/OpenGL/mesh.h
similarity index 51%
rename from OpenGL/headers/mesh.h
rename to OpenGL/mesh.h
index e1a371e..f11f711 100644
--- a/OpenGL/headers/mesh.h
+++ b/OpenGL/mesh.h
@@ -1,21 +1,25 @@
#pragma once
#include
+#include
+#include
#include
class Mesh {
public:
- Mesh(GLfloat *vertices, unsigned int numVerticies);
+ Mesh(GLfloat *vertices, unsigned int *indices, unsigned int numVerticies);
void Draw();
virtual ~Mesh();
private:
enum {
POSITION_VB,
- NUM_BUFFERS
+ NUM_BUFFERS,
+ TEXCOORD_VB
};
unsigned int m_VAO;
- unsigned int m_VBO[NUM_BUFFERS];
+ unsigned int m_VBO;
+ unsigned int m_EBO;
unsigned int m_drawCount;
};
diff --git a/OpenGL/shader.cpp b/OpenGL/shader.cpp
index e60566b..891cbb7 100644
--- a/OpenGL/shader.cpp
+++ b/OpenGL/shader.cpp
@@ -1,22 +1,12 @@
-#include "headers/shader.h"
+#include "shader.h"
-const char *vertexShaderSource = "#version 330 core\n"
-"layout (location = 0) in vec3 aPos;\n"
-"void main()\n"
-"{\n"
-" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
-"}\0";
-const char *fragmentShaderSource = "#version 330 core\n"
-"out vec4 FragColor;\n"
-"void main()\n"
-"{\n"
-" FragColor = vec4(0.25f, 1.0f, 0.49f, 1.0f);\n"
-"}\n\0";
+Shader::Shader(std::string path) {
+ std::string vertexPath = path + "_vertex.glsl";
+ std::string fragmentPath = path + "_fragment.glsl";
-Shader::Shader() {
m_program = glCreateProgram();
- m_shaders[0] = CreateShader(vertexShaderSource, GL_VERTEX_SHADER);
- m_shaders[1] = CreateShader(fragmentShaderSource, GL_FRAGMENT_SHADER);
+ m_shaders[0] = CreateShader(LoadFile(vertexPath), GL_VERTEX_SHADER);
+ m_shaders[1] = CreateShader(LoadFile(fragmentPath), GL_FRAGMENT_SHADER);
for (unsigned int i = 0; i < NUM_SHADERS; i++) {
glAttachShader(m_program, m_shaders[i]);
@@ -70,9 +60,24 @@ void Shader::Bind() {
}
std::string Shader::LoadFile(std::string path) {
+ std::ifstream file;
+ file.open((path).c_str());
+ std::string output;
+ std::string line;
+
+ if (file.is_open()) {
+ while (file.good()) {
+ getline(file, line);
+ output.append(line + "\n");
+ }
+ }
+ else {
+ std::cout << "Unable to load shader: " << path << std::endl;
+ }
std::cout << "Successfully loaded " + path << std::endl;
+ return output;
}
Shader::~Shader() {
diff --git a/OpenGL/headers/shader.h b/OpenGL/shader.h
similarity index 89%
rename from OpenGL/headers/shader.h
rename to OpenGL/shader.h
index 3246f46..a6db243 100644
--- a/OpenGL/headers/shader.h
+++ b/OpenGL/shader.h
@@ -2,11 +2,12 @@
#include
#include
+#include
#include
class Shader {
public:
- Shader();
+ Shader(std::string path);
void Bind();
virtual ~Shader();
private:
diff --git a/OpenGL/texture.cpp b/OpenGL/texture.cpp
new file mode 100644
index 0000000..ffa3aab
--- /dev/null
+++ b/OpenGL/texture.cpp
@@ -0,0 +1,37 @@
+#include "texture.h"
+#include "stb_image.h"
+#include
+#include
+
+Texture::Texture(std::string fileName) {
+ int width, height, numComponents;
+ unsigned char* imageData = stbi_load(fileName.c_str(), &width, &height, &numComponents, 4);
+
+ if (imageData == NULL)
+ std::cout << "Loading failed for texture: " << fileName << std::endl;
+
+ glGenTextures(1, &m_texture);
+ glBindTexture(GL_TEXTURE_2D, m_texture);
+
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
+
+ stbi_image_free(imageData);
+ std::cout << "Loaded texture: " << fileName << std::endl;
+}
+
+void Texture::Bind(unsigned int unit) {
+ assert(unit >= 0 && unit <= 31);
+
+ glActiveTexture(GL_TEXTURE0 + unit);
+ glBindTexture(GL_TEXTURE_2D, m_texture);
+}
+
+Texture::~Texture() {
+ glDeleteTextures(1, &m_texture);
+}
diff --git a/OpenGL/texture.h b/OpenGL/texture.h
new file mode 100644
index 0000000..5b11396
--- /dev/null
+++ b/OpenGL/texture.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#include
+#include
+
+class Texture {
+public:
+ Texture(std::string fileName);
+
+ void Bind(unsigned int unit);
+
+ virtual ~Texture();
+private:
+ GLuint m_texture;
+};
+
diff --git a/resources/shaders/simple2d_fragment.glsl b/resources/shaders/simple2d_fragment.glsl
new file mode 100644
index 0000000..c244f30
--- /dev/null
+++ b/resources/shaders/simple2d_fragment.glsl
@@ -0,0 +1,11 @@
+#version 330 core
+out vec4 FragColor;
+
+in vec3 ourColor;
+in vec2 TexCoord;
+
+uniform sampler2D ourTexture;
+
+void main() {
+ FragColor = texture(ourTexture, TexCoord);
+}
diff --git a/resources/shaders/simple2d_vertex.glsl b/resources/shaders/simple2d_vertex.glsl
new file mode 100644
index 0000000..7bb7d54
--- /dev/null
+++ b/resources/shaders/simple2d_vertex.glsl
@@ -0,0 +1,13 @@
+#version 330 core
+layout (location = 0) in vec3 aPos;
+layout (location = 1) in vec3 aColor;
+layout (location = 2) in vec2 aTexCoord;
+
+out vec3 ourColor;
+out vec2 TexCoord;
+
+void main() {
+ gl_Position = vec4(aPos, 1.0);
+ ourColor = aColor;
+ TexCoord = aTexCoord;
+}
diff --git a/resources/textures/chance-cube.jpg b/resources/textures/chance-cube.jpg
new file mode 100644
index 0000000..ff09f42
Binary files /dev/null and b/resources/textures/chance-cube.jpg differ