Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- //GLEW
- #define GLEW_STATIC
- #include <GL\glew.h>
- //GLFW
- #include <glfw3.h>
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
- // WINDOW DIMENSIONS
- const GLuint WIDTH = 1280, HEIGHT = 720;
- // SHADERS
- const GLchar* vertexShaderSource = "#version 330 core\n"
- "layout (location = 0) in vec3 position;\n"
- "void main()\n"
- "{\n"
- "gl_Position = vec4(position.x, position.y, position.z 1.0);\n"
- "}\0";
- const GLchar* fragmentShaderSource = "#version 330 core\n"
- "out vec4 color;\n"
- "void main()\n"
- "{\n"
- "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
- "}\0";
- int main(void)
- {
- // INIT CODE
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearningOpenGL", nullptr, nullptr);
- if (window == nullptr) {
- printf("Failed to create GLFW window\n");
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, key_callback);
- glewExperimental = GL_TRUE;
- if (glewInit() != GLEW_OK) {
- printf("Failed to init GLEW\n");
- return -1;
- }
- int width, height;
- glfwGetFramebufferSize(window, &width, &height);
- glViewport(0, 0, width, height);
- // SETUP RENDER SETTINGS
- GLfloat vertices[] = {
- -0.5f, -0.5f, 0.0f,
- 0.5f, -0.5f, 0.0f,
- 0.0f, 0.5f, 0.0f
- };
- GLuint VBO;
- glGenBuffers(1, &VBO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- // SHADERS!!!!
- // VERTEX SHADER
- GLuint vertexShader;
- vertexShader = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
- glCompileShader(vertexShader);
- GLint success;
- GLchar infoLog[512];
- glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
- if (!success) {
- glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
- printf("ERROR SHADER VERTEX COMPILATION_FAILED\n%s", infoLog);
- }
- // FRAGMENT SHADER
- GLuint fragmentShader;
- fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
- glCompileShader(fragmentShader);
- glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
- if (!success) {
- glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
- printf("ERROR SHADER FRAGMENT COMPILATION_FAILED\n%s", infoLog);
- }
- // SHADER LINKING IN SHADER PROGRAM
- GLuint shaderProgram;
- shaderProgram = glCreateProgram();
- glAttachShader(shaderProgram, vertexShader);
- glAttachShader(shaderProgram, fragmentShader);
- glLinkProgram(shaderProgram);
- glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
- if (!success) {
- glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
- printf("ERROR SHADER PROGRAM LINKING_FAILED\n%s", infoLog);
- }
- glDeleteShader(vertexShader);
- glDeleteShader(fragmentShader);
- glUseProgram(shaderProgram);
- // MAIN GAME LOOP
- while (!glfwWindowShouldClose(window)) {
- glfwPollEvents();
- glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- glfwSwapBuffers(window);
- }
- glfwTerminate();
- return 0;
- }
- // KAY CALLBACK FUNCTIONS
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
- {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
- glfwSetWindowShouldClose(window, GL_TRUE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement