Untitled
unknown
c_cpp
a month ago
4.5 kB
4
Indexable
Never
#include <glad/glad.h> #include <GLFW/glfw3.h> #include <iostream> const char* vertex_shader_source = "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" "void main() {\n" "gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n" "}\0"; const char* fragment_shader_source = "#version 330 core\n" "out vec4 FragColor;\n" "void main() {\n" "FragColor = vec4(1.0f, 1.0f, 0.0f, 1.0f);\n" "}\0"; void framebuffer_size_callback(GLFWwindow* window, int width, int height); void error_callback(int id, const char* description); void processInput(GLFWwindow* window); bool compile_shader(GLuint* id, GLenum shader_type, GLsizei count, const GLchar* const* source); void triangle_vao(GLuint* vao_id, GLuint* vbo_id, const float vertices[], unsigned int size); int main() { glfwSetErrorCallback(error_callback); glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); GLFWwindow* window = glfwCreateWindow(800, 600, "Hello, world!", NULL, NULL); if (window == NULL) { std::cout << "Unable to create a window" << std::endl; glfwTerminate(); return 1; } glfwMakeContextCurrent(window); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Unable to load GL loader" << std::endl; glfwTerminate(); return 1; } glViewport(0, 0, 800, 600); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); unsigned int vertex_shader_id; compile_shader(&vertex_shader_id, GL_VERTEX_SHADER, 1, &vertex_shader_source); unsigned int fragment_shader_id; compile_shader(&fragment_shader_id, GL_FRAGMENT_SHADER, 1, &fragment_shader_source); unsigned int shader_program; shader_program = glCreateProgram(); glAttachShader(shader_program, vertex_shader_id); glAttachShader(shader_program, fragment_shader_id); glLinkProgram(shader_program); glDeleteShader(vertex_shader_id); glDeleteShader(fragment_shader_id); const float vertices_1[] = { -0.55f, -0.50f, 0.0f, -0.05f, -0.50f, 0.0f, -0.30f, 0.50f, 0.0f }; unsigned int vao_1, vbo_1; triangle_vao(&vao_1, &vbo_1, vertices_1, sizeof(vertices_1)); const float vertices_2[] = { 0.05f, -0.50f, 0.0f, 0.55f, -0.50f, 0.0f, 0.30f, 0.50f, 0.0f }; unsigned int vao_2, vbo_2; triangle_vao(&vao_2, &vbo_2, vertices_2, sizeof(vertices_2)); while (!glfwWindowShouldClose(window)) { processInput(window); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(shader_program); glBindVertexArray(vao_1); glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(vao_2); glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); glfwSwapBuffers(window); glfwPollEvents(); } glDeleteVertexArrays(1, &vao_1); glDeleteBuffers(1, &vbo_1); glDeleteVertexArrays(1, &vao_2); glDeleteBuffers(1, &vbo_2); glDeleteProgram(shader_program); glfwTerminate(); return 0; } void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); } void error_callback(int id, const char* description) { std::cout << description << std::endl; } void processInput(GLFWwindow* window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); } } bool compile_shader(GLuint* id, GLenum shader_type, GLsizei count, const GLchar* const* source) { GLuint shader_id; shader_id = glCreateShader(shader_type); glShaderSource(shader_id, count, source, NULL); glCompileShader(shader_id); GLint result; glGetShaderiv(shader_id, GL_COMPILE_STATUS, &result); if (result) { *id = shader_id; return true; } else { const GLsizei buffer_length = 512; GLchar buffer[buffer_length]; glGetShaderInfoLog(shader_id, buffer_length, NULL, buffer); std::cout << buffer_length << std::endl; return false; } } void triangle_vao(GLuint *vao_id, GLuint *vbo_id, const float vertices[], unsigned int size) { GLuint vao, vbo; glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); *vao_id = vao; *vbo_id = vbo; }
Leave a Comment