-
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
Showing
4 changed files
with
156 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
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ include_directories(.) | |
|
||
add_subdirectory(common) | ||
add_subdirectory(LegacyRayTracer) | ||
add_subdirectory(glTests) |
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,8 @@ | ||
add_executable(glTests | ||
main.cpp | ||
) | ||
|
||
create_target_directory_groups(glTests) | ||
|
||
target_link_libraries(glTests PRIVATE common) | ||
target_link_libraries(glTests PRIVATE glad glfw) |
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,144 @@ | ||
#include <iostream> | ||
|
||
#include <glad/glad.h> | ||
|
||
#include <GLFW/glfw3.h> | ||
|
||
static unsigned int CompileShader(unsigned int type, const std::string& source) { | ||
const unsigned int id = glCreateShader(type); | ||
const char* src = source.c_str(); | ||
glShaderSource(id, 1, &src, nullptr); | ||
glCompileShader(id); | ||
|
||
int result; | ||
glGetShaderiv(id, GL_COMPILE_STATUS, &result); | ||
if (result == GL_FALSE) { | ||
int length; | ||
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length); | ||
char* message = new char[length]; | ||
glGetShaderInfoLog(id, length, &length, message); | ||
std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << std::endl; | ||
std::cout << message << std::endl; | ||
glDeleteShader(id); | ||
delete[] message; | ||
return 0; | ||
} | ||
|
||
return id; | ||
} | ||
|
||
static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader) { | ||
const unsigned int program = glCreateProgram(); | ||
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader); | ||
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader); | ||
|
||
glAttachShader(program, vs); | ||
glAttachShader(program, fs); | ||
glLinkProgram(program); | ||
glValidateProgram(program); | ||
|
||
glDeleteShader(vs); | ||
glDeleteShader(fs); | ||
|
||
return program; | ||
} | ||
|
||
int main(void) { | ||
GLFWwindow* window; | ||
|
||
/* Initialize the library */ | ||
if (!glfwInit()) | ||
return -1; | ||
|
||
/* Create a windowed mode window and its OpenGL context */ | ||
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); | ||
if (!window) | ||
{ | ||
glfwTerminate(); | ||
return -1; | ||
} | ||
|
||
/* Make the window's context current */ | ||
glfwMakeContextCurrent(window); | ||
|
||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) | ||
{ | ||
std::cout << "Failed to initialize OpenGL context" << std::endl; | ||
return -1; | ||
} | ||
std::cout << "OpenGL Version " << GLVersion.major << "." << GLVersion.minor << " loaded" << std::endl; | ||
std::cout << glGetString(GL_VERSION) << " " << glGetString(GL_VENDOR) << std::endl; | ||
|
||
float positions[] = { | ||
-0.5f, -0.5f, | ||
0.5f, -0.5f, | ||
0.5f, 0.5f, | ||
-0.5f, 0.5f | ||
}; | ||
|
||
unsigned int indicies[] = { | ||
0,1,2, | ||
2,3,0 | ||
}; | ||
|
||
unsigned int buff; | ||
glGenBuffers(1, &buff); | ||
glBindBuffer(GL_ARRAY_BUFFER, buff); | ||
glBufferData(GL_ARRAY_BUFFER, 8*sizeof(float), positions, GL_STATIC_DRAW); | ||
|
||
glEnableVertexAttribArray(0); | ||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0); | ||
|
||
unsigned int ibo; | ||
glGenBuffers(1, &ibo); | ||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); | ||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*sizeof(unsigned int), indicies, GL_STATIC_DRAW); | ||
|
||
std::string vertexShader = R"( | ||
#version 330 core | ||
layout(location = 0) in vec4 position; | ||
void main() { | ||
gl_Position = position; | ||
} | ||
)"; | ||
|
||
std::string fragmentShader = R"glsl( | ||
#version 330 core | ||
layout(location = 0) out vec4 color; | ||
uniform vec4 u_Color; | ||
void main() { | ||
color = u_Color; | ||
} | ||
)glsl"; | ||
|
||
unsigned int shader = CreateShader(vertexShader, fragmentShader); | ||
glUseProgram(shader); | ||
|
||
int location = glGetUniformLocation(shader, "u_Color"); | ||
glUniform4f(location, 0.0f, 0.5f, 0.0f, 1.0f); | ||
|
||
/* Loop until the user closes the window */ | ||
while (!glfwWindowShouldClose(window)) | ||
{ | ||
/* Render here */ | ||
glClear(GL_COLOR_BUFFER_BIT); | ||
|
||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); | ||
|
||
/* Swap front and back buffers */ | ||
glfwSwapBuffers(window); | ||
|
||
/* Poll for and process events */ | ||
glfwPollEvents(); | ||
} | ||
|
||
glDeleteProgram(shader); | ||
|
||
glfwTerminate(); | ||
return 0; | ||
} |