Skip to content

Commit

Permalink
more incomplete gl stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
vitor-k committed Nov 27, 2024
1 parent 1a3dac1 commit 42568cd
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ endfunction()

add_subdirectory(externals)
add_subdirectory(src)


set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT glTests)
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ include_directories(.)

add_subdirectory(common)
add_subdirectory(LegacyRayTracer)
add_subdirectory(glTests)
8 changes: 8 additions & 0 deletions src/glTests/CMakeLists.txt
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)
144 changes: 144 additions & 0 deletions src/glTests/main.cpp
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;
}

0 comments on commit 42568cd

Please sign in to comment.