-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfa3862
commit 73433cc
Showing
7 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <glad/glad.h> | ||
#include <GLFW/glfw3.h> | ||
#include "shader.hpp" | ||
#include "triangle_mesh.hpp" | ||
|
||
|
||
int main() | ||
try { | ||
GLFWwindow* window; | ||
|
||
if (!glfwInit()) { | ||
std::cerr << "Failed to initialize GLFW" << std::endl; | ||
return -1; | ||
} | ||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); | ||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); | ||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | ||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); | ||
|
||
window = glfwCreateWindow(640, 480, "Domain Flow Scheduler and Mapper", NULL, NULL); | ||
glfwMakeContextCurrent(window); | ||
|
||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { | ||
std::cerr << "Failed to initialize GLAD" << std::endl; | ||
glfwTerminate(); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
glClearColor(0.25f, 0.25f, 0.25f, 1.0f); | ||
int w, h; | ||
glfwGetFramebufferSize(window, &w, &h); | ||
glViewport(0, 0, w, h); | ||
|
||
TriangleMesh* triangle = new TriangleMesh(); | ||
|
||
unsigned int shader = make_shader( | ||
"../src/shaders/vertex.txt", | ||
"../src/shaders/fragment.txt" | ||
); | ||
|
||
while (!glfwWindowShouldClose(window)) { | ||
|
||
glfwPollEvents(); | ||
|
||
glClear(GL_COLOR_BUFFER_BIT); | ||
|
||
glUseProgram(shader); | ||
|
||
triangle->draw(); | ||
|
||
glfwSwapBuffers(window); | ||
|
||
} | ||
|
||
glDeleteProgram(shader); | ||
|
||
delete triangle; | ||
|
||
glfwTerminate(); | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
catch(...) { | ||
std::cerr << "Caught unexpected exception" << std::endl; | ||
} | ||
|
||
|
||
#ifdef FIXED | ||
unsigned int make_shader() { | ||
unsigned int vertexShaderModule = glCreateShader(GL_VERTEX_SHADER); | ||
const char* vertexShaderSource = "#version 330 core\n" | ||
"layout (location = 0) in vec2 vertexPos;\n" | ||
"layout (location = 1) in vec3 vertexColor;\n" | ||
"out vec3 fragmentColor;\n" | ||
"void main() {\n" | ||
" gl_Position = vec4(vertexPos, 0.0, 1.0);\n" | ||
" fragmentColor = vertexColor;\n" | ||
"}\0"; | ||
glShaderSource(vertexShaderModule, 1, &vertexShaderSource, NULL); | ||
glCompileShader(vertexShaderModule); | ||
int success; | ||
glGetShaderiv(vertexShaderModule, GL_COMPILE_STATUS, &success); | ||
if (!success) { | ||
char infoLog[512]; | ||
glGetShaderInfoLog(vertexShaderModule, 512, NULL, infoLog); | ||
std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; | ||
} | ||
#define FIRST | ||
unsigned int fragmentShaderModule = glCreateShader(GL_FRAGMENT_SHADER); | ||
const char* fragmentShaderSource = | ||
#ifdef FIRST | ||
"#version 330 core\n" | ||
"in vec3 fragmentColor;\n"; | ||
"out vec4 screenColor;\n" | ||
"void main() {\n" | ||
" screenColor = vec4(fragmentColor, 1.0);\n"; | ||
"}\0"; | ||
#else | ||
"#version 330 core\n" | ||
"out vec4 screenColor;\n" | ||
"void main() {\n" | ||
" screenColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);\n" | ||
"}\0"; | ||
#endif | ||
glShaderSource(fragmentShaderModule, 1, &fragmentShaderSource, NULL); | ||
glCompileShader(fragmentShaderModule); | ||
glGetShaderiv(fragmentShaderModule, GL_COMPILE_STATUS, &success); | ||
if (!success) { | ||
char infoLog[512]; | ||
glGetShaderInfoLog(fragmentShaderModule, 512, NULL, infoLog); | ||
std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; | ||
} | ||
|
||
unsigned int shader = glCreateProgram(); | ||
glAttachShader(shader, vertexShaderModule); | ||
glAttachShader(shader, fragmentShaderModule); | ||
glLinkProgram(shader); | ||
// check for linker errors | ||
glGetProgramiv(shader, GL_LINK_STATUS, &success); | ||
if (!success) { | ||
char infoLog[512]; | ||
glGetProgramInfoLog(shader, 512, NULL, infoLog); | ||
std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; | ||
} | ||
// clean up the shaders | ||
glDeleteShader(vertexShaderModule); | ||
glDeleteShader(fragmentShaderModule); | ||
|
||
return shader; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <sstream> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include <glad/glad.h> | ||
#include <GLFW/glfw3.h> | ||
|
||
|
||
unsigned int make_module(const std::string& filepath, unsigned int module_type) { | ||
|
||
std::ifstream file; | ||
std::stringstream bufferedLines; | ||
std::string line; | ||
|
||
file.open(filepath); | ||
while (std::getline(file, line)) { | ||
//std::cout << line << std::endl; | ||
bufferedLines << line << '\n'; | ||
} | ||
std::string shaderSource = bufferedLines.str(); | ||
const char* shaderSrc = shaderSource.c_str(); | ||
bufferedLines.str(""); | ||
file.close(); | ||
|
||
unsigned int shaderModule = glCreateShader(module_type); | ||
glShaderSource(shaderModule, 1, &shaderSrc, NULL); | ||
glCompileShader(shaderModule); | ||
|
||
int success; | ||
glGetShaderiv(shaderModule, GL_COMPILE_STATUS, &success); | ||
if (!success) { | ||
char errorLog[1024]; | ||
glGetShaderInfoLog(shaderModule, 1024, NULL, errorLog); | ||
std::cout << "Shader Module compilation error:\n" << errorLog << std::endl; | ||
} | ||
|
||
return shaderModule; | ||
} | ||
|
||
unsigned int make_shader(const std::string& vertex_filepath, const std::string& fragment_filepath) { | ||
|
||
//To store all the shader modules | ||
std::vector<unsigned int> modules; | ||
|
||
//Add a vertex shader module | ||
modules.push_back(make_module(vertex_filepath, GL_VERTEX_SHADER)); | ||
|
||
//Add a fragment shader module | ||
modules.push_back(make_module(fragment_filepath, GL_FRAGMENT_SHADER)); | ||
|
||
//Attach all the modules then link the program | ||
unsigned int shader = glCreateProgram(); | ||
for (unsigned int shaderModule : modules) { | ||
glAttachShader(shader, shaderModule); | ||
} | ||
glLinkProgram(shader); | ||
|
||
//Check the linking worked | ||
int success; | ||
glGetProgramiv(shader, GL_LINK_STATUS, &success); | ||
if (!success) { | ||
char errorLog[1024]; | ||
glGetProgramInfoLog(shader, 1024, NULL, errorLog); | ||
std::cout << "Shader linking error:\n" << errorLog << '\n'; | ||
} | ||
|
||
//Modules are now unneeded and can be freed | ||
for (unsigned int shaderModule : modules) { | ||
glDeleteShader(shaderModule); | ||
} | ||
|
||
return shader; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include <string> | ||
|
||
// forward declaration | ||
unsigned int make_shader(const std::string& vertex_filepath, const std::string& fragment_filepath); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#version 330 core | ||
|
||
in vec3 fragmentColor; | ||
|
||
out vec4 screenColor; | ||
|
||
void main() | ||
{ | ||
screenColor = vec4(fragmentColor, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#version 330 core | ||
|
||
layout (location=0) in vec3 vertexPos; | ||
layout (location=1) in vec3 vertexColor; | ||
|
||
out vec3 fragmentColor; | ||
|
||
void main() | ||
{ | ||
gl_Position = vec4(vertexPos, 1.0); | ||
fragmentColor = vertexColor; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <vector> | ||
#include <glad/glad.h> | ||
#include <GLFW/glfw3.h> | ||
#include "triangle_mesh.hpp" | ||
|
||
TriangleMesh::TriangleMesh() { | ||
|
||
std::vector<float> data = { | ||
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, | ||
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, | ||
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f | ||
}; | ||
vertex_count = 3; | ||
|
||
glGenVertexArrays(1, &VAO); | ||
glBindVertexArray(VAO); | ||
|
||
glGenBuffers(1, &VBO); | ||
glBindBuffer(GL_ARRAY_BUFFER, VBO); | ||
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), data.data(), GL_STATIC_DRAW); | ||
|
||
// position | ||
// vertexAttribPointer(index, size, type, normalized, stride, pointer) | ||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 24, (void*)0); | ||
glEnableVertexAttribArray(0); | ||
|
||
// color | ||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 24, (void*)12); | ||
glEnableVertexAttribArray(1); | ||
|
||
} | ||
|
||
void TriangleMesh::draw() { | ||
glBindVertexArray(VAO); | ||
glDrawArrays(GL_TRIANGLES, 0, vertex_count); | ||
} | ||
|
||
TriangleMesh::~TriangleMesh() { | ||
glDeleteVertexArrays(1, &VAO); | ||
glDeleteBuffers(1, &VBO); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
class TriangleMesh { | ||
public: | ||
TriangleMesh(); | ||
~TriangleMesh(); | ||
|
||
void draw(); | ||
|
||
private: | ||
unsigned int vertex_count; | ||
unsigned int VBO; // VBO - Vertex Buffer Object | ||
unsigned int VAO; // VAO - Vertex Array Object | ||
|
||
}; |