Advertisement
Guest User

functions.c

a guest
Apr 16th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. #include <math.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #define GLEW STATIC
  6. #include <GL/glew.h>
  7. #include <GLFW/glfw3.h>
  8.  
  9. #include "functions.h"
  10.  
  11. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
  12. {
  13.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  14.         glfwSetWindowShouldClose(window, GL_TRUE);
  15. }
  16.  
  17. void textFileRead(const char *filename, char **interstring) {
  18.     FILE *fp;
  19.     char *textFile;
  20.     int textFileSize;
  21.     // Open file
  22.     fp = fopen(filename, "r");
  23.     // Find out file length
  24.     fseek(fp, 0L, SEEK_END);
  25.     textFileSize = ftell(fp);
  26.     fseek(fp, 0L, SEEK_SET);
  27.     // Allocate necessary space
  28.     textFile = malloc(textFileSize+10);
  29.     *interstring = malloc(textFileSize+10);
  30.     // Read file into string and put '\0' to end
  31.     fread(textFile, textFileSize, 1, fp);
  32.     textFile[textFileSize+1] = '\0';
  33.     // Copy to external array
  34.     strcpy(*interstring, textFile);
  35.     fclose(fp);
  36. }
  37.  
  38. int aaaInit(GLFWwindow **window, GLuint winWidth, GLuint winHeight, const GLchar **winName) {
  39.     glfwInit();
  40.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  41.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  42.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  43.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  44.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //For MacOS
  45.  
  46.     // Create window
  47.     *window = glfwCreateWindow(winWidth, winHeight, *winName, 0, 0);
  48.     if (*window == 0)   // catching error
  49.     {
  50.         printf("%s", "Failed to Create GLFW window\n");
  51.         glfwTerminate();
  52.         return -1;
  53.     }
  54.     glfwMakeContextCurrent(*window);
  55.  
  56.     // Enable GLEW experimental
  57.     glewExperimental = GL_TRUE;
  58.     if (glewInit() != GLEW_OK)
  59.     {
  60.         printf("%s", "Failed to initialize GLEW");
  61.         return -1;
  62.     }
  63.  
  64.     // Define rht viewport dimensions
  65.     int width, height;
  66.     glfwGetFramebufferSize(*window, &width, &height);
  67.  
  68.     // Settng viewport
  69.     glViewport(0, 0, width, height);
  70.  
  71.     return 0;
  72. }
  73.  
  74. int aaaShaderCompile(const GLchar **vertexShaderSource, const GLchar **fragmentShaderSource, GLuint *shaderProgramInter) {
  75.  
  76.     // Build and compile shader program
  77.     // Vertex shader
  78.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  79.     glShaderSource(vertexShader, 1, vertexShaderSource, 0);
  80.     glCompileShader(vertexShader);
  81.  
  82.     // Check for compile time errors
  83.     GLint success;
  84.     GLchar infoLog[512];
  85.     glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  86.     if (!success)
  87.     {
  88.         glGetShaderInfoLog(vertexShader, 512, 0, infoLog);
  89.         printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n %s", infoLog);
  90.     }
  91.  
  92.     // Fragment shader
  93.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  94.     glShaderSource(fragmentShader, 1, fragmentShaderSource, 0);
  95.     glCompileShader(fragmentShader);
  96.  
  97.     // Check for linking errors
  98.     glGetProgramiv(fragmentShader, GL_COMPILE_STATUS, &success);
  99.     if (!success) {
  100.         glGetProgramInfoLog(fragmentShader, 512, 0, infoLog);
  101.         printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n %s", infoLog);
  102.     }
  103.  
  104.     // Link shaders
  105.     *shaderProgramInter = glCreateProgram();
  106.     glAttachShader(*shaderProgramInter, vertexShader);
  107.     glAttachShader(*shaderProgramInter, fragmentShader);
  108.     glLinkProgram(*shaderProgramInter);
  109.  
  110.     // Check for linking errors
  111.     glGetProgramiv(*shaderProgramInter, GL_LINK_STATUS, &success);
  112.     if (!success) {
  113.         glGetProgramInfoLog(*shaderProgramInter, 512, 0, infoLog);
  114.         printf("ERROR::SHADER::FRAGMENT::LINKING_FAILED\n %s", infoLog);
  115.     }
  116.     glDeleteShader(vertexShader);
  117.     glDeleteShader(fragmentShader);
  118.  
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement