Site Name

Copyright © 2019 All rights reserved | This template is made with ♥ by Gulshan

Example of a triangle with OpenGL in C25

/* ============================================================================ Name : GLFWwithC2.c Author : Gulshan Chaurasia Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ * following libraries have been included for compiling this * gcc -o "HelloGLFW" ./Main.o -lGLEW -lglfw3 -lGL -lm -ldl -lX11 -lpthread -lXrandr -lXi * * you need to have following libraries installed for it to compile correctly: * -GLEW * -GLFW3 * -GL * -GLU * On Ubuntu, all of these can be installed by running below command: * sudo apt-get install libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev libglew-dev */ #include #include // GLEW #define GLEW_STATIC #include // GLFW #include void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode); int main() { printf("Hello opengL\n"); 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(800, 600, "LearnOpenGL", NULL,NULL); if (window == NULL) { printf("Failed to create GLFW window\n"); glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { printf("Failed to initialize GLEW\n"); return -1; } glViewport(0, 0, 800, 600); //registering callback, for event handling!! glfwSetKeyCallback(window, key_callback); //INITIALIZING opengl parameters before drawing //triangle coordinates GLfloat vertices[] = { -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f }; //vertex buffer objects (VBO) that can store a large number //of vertices in the GPU’s memory //Just like any object in OpenGL this buffer has a unique ID corresponding to that //buffer, so we can generate one with a buffer ID using the glGenBuffers function: GLuint VBO; glGenBuffers(1, &VBO); //We can bind the newly created buffer to the GL_ARRAY_BUFFER target //with the glBindBuffer function: glBindBuffer(GL_ARRAY_BUFFER, VBO); //From that point on any buffer calls we make (on the GL_ARRAY_BUFFER target) will be used //to configure the currently bound buffer, which is VBO. //Then we can make a call to glBufferData //function that copies the previously defined vertex data into the buffer’s memory: glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //vertex shader program source code char* vertexShaderSource = "#version 330 core\nlayout (location = 0) in vec3 position;\nvoid main()\n{\ngl_Position = vec4(position.x, position.y, position.z, 1.0);\n}"; //dynamically compiling vertex shader program, i.e. compiling at run time GLuint vertexShader; vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); glCompileShader(vertexShader); //checking if compilation succeeded 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"); } //fragment shader program soure code char* fragmentShaderSource = "#version 330 core\nout vec4 color;\nvoid main()\n{\ncolor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n}"; GLuint fragmentShader; fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); glCompileShader(fragmentShader); //checking if compilation succeeded glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if(!success) { glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n"); } //A shader program object is the final linked version of multiple shaders combined. To use the //recently compiled shaders we have to link them to a shader program object and then activate this //shader program when rendering objects. GLuint shaderProgram; shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); //checking if linking succeeded glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if(!success) { glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); printf("ERROR::SHADERS::PROGRAM::LINKING_FAILED\n"); } //don’t forget to delete the shader objects once we’ve linked them into the program //object; we no longer need them anymore: glDeleteShader(vertexShader); glDeleteShader(fragmentShader); //we can tell OpenGL how it should interpret the vertex data (per vertex //attribute) using glVertexAttribPointer: glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(0); //The process to generate a VAO looks simliar to that of a VBO: GLuint VAO; glGenVertexArrays(1, &VAO); // 1. Bind Vertex Array Object glBindVertexArray(VAO); // 2. Copy our vertices array in a buffer for OpenGL to use glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // 3. Then set our vertex attributes pointers glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), ( GLvoid*)0); glEnableVertexAttribArray(0); //4. Unbind the VAO glBindVertexArray(0); //this is the game loop while(!glfwWindowShouldClose(window)) { glfwPollEvents(); //all the rendering is done here!! glClearColor(0.5f, 0.3f, 0.7f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); //The result is a program object that we can activate by calling glUseProgram with the newly //created program object as its argument: glUseProgram(shaderProgram); //actual drawing takes place here glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); glfwSwapBuffers(window); } glfwTerminate(); return 0; } void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) { // When a user presses the escape key, we set the WindowShouldClose property to true, // closing the application if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE); }

published by gulshan on 2019-12-13 06:54:57

0 likes 0 dislikes