-
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
Electron7-7
committed
Nov 7, 2024
1 parent
b84b38e
commit 98a225a
Showing
3 changed files
with
165 additions
and
1 deletion.
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
File renamed without changes.
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,164 @@ | ||
#include "common.cpp" | ||
|
||
void framebufferSizeCallback(GLFWwindow* window, int width, int height); | ||
void processInput(GLFWwindow* window); | ||
|
||
unsigned int window_width = 800, window_height = 800; | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
glfwInit(); | ||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); | ||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); | ||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | ||
|
||
GLFWwindow* window = glfwCreateWindow(window_width, window_height, "Fucking Graphics", NULL, NULL); | ||
|
||
if (window == NULL) | ||
{ | ||
std::cout << "Failed to create GLFW window" << std::endl; | ||
glfwTerminate(); | ||
return -1; | ||
} | ||
|
||
glfwMakeContextCurrent(window); | ||
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback); | ||
|
||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) | ||
{ | ||
std::cout << "Failed to initialize GLAD" << std::endl; | ||
return -1; | ||
} | ||
|
||
Shader simple_shader("src/shaders/vertex_shader.glsl", "src/shaders/fragment_shader.glsl"); | ||
|
||
float vertices[] | ||
{ | ||
// positions // colors // texture coords | ||
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // top right | ||
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.3f, 1.0f, 0.0f, // bottom right | ||
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.3f, 0.0f, 0.0f, // bottom left | ||
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f // top left | ||
}; | ||
|
||
unsigned int indices[] | ||
{ | ||
0, 1, 3, // first triangle | ||
1, 2, 3 // second triangle | ||
}; | ||
|
||
unsigned int EBO; | ||
glGenBuffers(1, &EBO); | ||
|
||
unsigned int VBO, VAO; | ||
glGenVertexArrays(1, &VAO); | ||
glGenBuffers(1, &VBO); | ||
|
||
glBindVertexArray(VAO); | ||
|
||
glBindBuffer(GL_ARRAY_BUFFER, VBO); | ||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); | ||
|
||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); | ||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); | ||
|
||
/* | ||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); | ||
glEnableVertexAttribArray(0); | ||
*/ | ||
|
||
// 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 coordinates attribute | ||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); | ||
glEnableVertexAttribArray(2); | ||
|
||
|
||
/* | ||
You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other | ||
VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary. | ||
*/ | ||
// glBindBuffer(GL_ARRAY_BUFFER, VAO); | ||
// glBindVertexArray(0); | ||
|
||
|
||
float texture_coordinates[] | ||
{ | ||
0.0f, 0.0f, | ||
1.0f, 0.0f, | ||
0.5f, 1.0f | ||
}; | ||
|
||
unsigned int texture; | ||
glGenTextures(1, &texture); | ||
glBindTexture(GL_TEXTURE_2D, texture); | ||
|
||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, 16); | ||
|
||
int width, height, nr_channels; | ||
unsigned char *data = stbi_load("src/images/COMP04_5.png", &width, &height, &nr_channels, 0); | ||
|
||
if(data) | ||
{ | ||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); | ||
glGenerateMipmap(GL_TEXTURE_2D); | ||
} | ||
else | ||
{ | ||
std::cout << "Failed to load texture" << std::endl; | ||
} | ||
stbi_image_free(data); | ||
|
||
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Wireframe mode | ||
|
||
while(!glfwWindowShouldClose(window)) | ||
{ | ||
processInput(window); | ||
|
||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); | ||
glClear(GL_COLOR_BUFFER_BIT); | ||
|
||
glm::mat4 trans = glm::mat4(1.0f); | ||
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f)); | ||
|
||
unsigned int transform_location = glGetUniformLocation(simple_shader.ID, "transform"); | ||
glUniformMatrix4fv(transform_location, 1, GL_FALSE, glm::value_ptr(trans)); | ||
|
||
simple_shader.use(); | ||
glBindTexture(GL_TEXTURE_2D, texture); | ||
glBindVertexArray(VAO); | ||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); | ||
|
||
glfwSwapBuffers(window); | ||
glfwPollEvents(); | ||
} | ||
|
||
glDeleteVertexArrays(1, &VAO); | ||
glDeleteBuffers(1, &VBO); | ||
glDeleteBuffers(1, &EBO); | ||
// glDeleteProgram(shader_program); | ||
|
||
glfwTerminate(); | ||
return 0; | ||
} | ||
|
||
void processInput(GLFWwindow *window) | ||
{ | ||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) | ||
glfwSetWindowShouldClose(window, true); | ||
} | ||
|
||
void framebufferSizeCallback(GLFWwindow* window, int width, int height) | ||
{ | ||
glViewport(0, 0, width, height); | ||
} |