Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define GLFW_INCLUDE_NONE
- #include <glad/gl.h>
- #include <GLFW/glfw3.h>
- #include <iostream>
- #include <string>
- void error_callback(int error, const char* description) {
- fprintf(stderr, "Error: %s\n", description);
- }
- static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
- {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GLFW_TRUE);
- }
- int main(void) {
- const int windowWidth = 640;
- const int windowHeight = 480;
- const char* windowTitle = "GLFW TEST";
- //GLFW Init
- std::cout << "Starting GLFW..." << std::endl;
- if (!glfwInit()) {
- //Init Failed
- }
- //Error Setup
- glfwSetErrorCallback(error_callback);
- //Window setup
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //Sets Minimum OpenGL version
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //Sets Minimum OpenGL version, why twice?
- GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, windowTitle, NULL, NULL);
- if (!window) {
- //Window creation Failed
- std::cout << "wtf i wanna die why tf is my window not init???" << std::endl;
- }
- glfwMakeContextCurrent(window);
- gladLoadGL(glfwGetProcAddress); //Loads OpenGL with glad I guess?? REQUIRES CONTEXT TO BE FIRST!!!
- //Learn what the hell this means!!!
- int width, height;
- glfwGetFramebufferSize(window, &width, &height);
- glViewport(0, 0, width, height);
- std::cout << "RENDERER: " << glGetString(GL_RENDERER) << std::endl;
- std::cout << "OpenGL version supported: " << glGetString(GL_VERSION) << std::endl;
- //Keyboard Input
- glfwSetKeyCallback(window, key_callback);
- glfwSwapInterval(1); //swap shit
- //Triangle Shit.
- float points[] = {
- 0.5f, 0.5f, 0.0f, // x,y,z of first point.
- 0.5f, -0.5f, 0.0f, // x,y,z of second point.
- -0.5f, -0.5f, 0.0f // x,y,z of third point.
- };
- float pointsb[] = {
- -0.5f, -0.5f, 0.0f, // x,y,z of first point.
- -0.5f, 0.5f, 0.0f, // x,y,z of second point.
- 0.5f, 0.5f, 0.0f // x,y,z of third point.
- };
- //ts is complicated study it and read the tut: https://antongerdelan.net/opengl/hellotriangle.html
- GLuint vbo = 0;
- glGenBuffers(1, &vbo);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);
- GLuint vbob = 0;
- glGenBuffers(1, &vbob);
- glBindBuffer(GL_ARRAY_BUFFER, vbob);
- glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), pointsb, GL_STATIC_DRAW);
- GLuint vao = 0;
- glGenVertexArrays(1, &vao);
- glBindVertexArray(vao);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
- GLuint vaob = 0;
- glGenVertexArrays(1, &vaob);
- glBindVertexArray(vaob);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, vbob);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
- const char* vertex_shader =
- "#version 410 core\n"
- "in vec3 vp;"
- "void main() {"
- "gl_Position = vec4( vp, 1.0);"
- "}";
- const char* fragment_shader =
- "#version 410 core\n"
- "out vec4 frag_colour;"
- "void main() {"
- " frag_colour = vec4( 1.0, 0.0, 0.0, 1.0 );"
- "}";
- //loads the shaders and compiles the,.
- GLuint vs = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vs, 1, &vertex_shader, NULL);
- glCompileShader(vs);
- GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fs, 1, &fragment_shader, NULL);
- glCompileShader(fs);
- //Combines tyhe shaders into a single shader and links them.
- GLuint shader_program = glCreateProgram();
- glAttachShader(shader_program, fs);
- glAttachShader(shader_program, vs);
- glLinkProgram(shader_program);
- while (!glfwWindowShouldClose(window)) {
- // keep running window.
- glfwPollEvents();
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Put the shader program, and the VAO, in focus in OpenGL's state machine.
- glUseProgram(shader_program);
- glBindVertexArray(vao);
- glBindVertexArray(vaob);
- // Draw points 0-3 from the currently bound VAO with current in-use shader.
- glDrawArrays(GL_TRIANGLES, 0, 3);
- glfwSwapBuffers(window); //Swaps buffers
- }
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment