Advertisement
DaniDesu

Why does my loadShader function read non-ASCII characters?

Jul 16th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.21 KB | None | 0 0
  1. // Expected result: Draws a simple red colored triangle to the screen
  2. // Problem to debug: Why does my loadShader function read non-ASCII characters?
  3.  
  4. #include <glad/glad.h>
  5. #define GLFW_DLL
  6. #include <GLFW\glfw3.h>
  7. #include <cstdio>
  8. #include <iostream>
  9.  
  10. // TODO: Debug
  11. /* Loads shader text files from a given file name (extension required)
  12.  * and returns the shader code as a null terminated string from that file.
  13.  */
  14. const char * loadShader(const char * shaderFileName) {
  15.     FILE * shaderFile{};
  16.     fopen_s(&shaderFile, shaderFileName, "r");
  17.     if (!shaderFile) {
  18.         std::cerr << "ERROR: Cannot open file" << std::endl;
  19.         return "\0";
  20.     }
  21.     // Tell file size
  22.     fseek(shaderFile, 0L, SEEK_END);
  23.     unsigned long shaderFileSize{};
  24.     shaderFileSize = ftell(shaderFile);
  25.     std::cout << "DEBUG: shaderFileSize: " << shaderFileSize << std::endl; // Debug output
  26.     rewind(shaderFile);
  27.     // Read from file
  28.     char * buffer = (char *)malloc(sizeof(char)*(shaderFileSize+1UL));
  29.     if (!buffer) {
  30.         std::cerr << "ERROR: Failed to allocate memory" << std::endl;
  31.         return "\0";
  32.     }
  33.     int c{};
  34.     int i = 0;
  35.     while ((c = fgetc(shaderFile))!= EOF) {
  36.         buffer[i++] = c;
  37.     }
  38.     // Put '\0' at the end of the buffer (required for OpenGL)
  39.     buffer[shaderFileSize] = '\0';
  40.     std::cout << "DEBUG: buffer: " << buffer << std::endl; // Debug output
  41.     std::cout << "DEBUG: strlen: " << strlen(buffer) << std::endl; // Debug output
  42.     fclose(shaderFile);
  43.     return buffer;
  44. } // end of loadShader()
  45.  
  46. int main() {
  47.     // Initialize GLFW
  48.     if (!glfwInit()) {
  49.         std::cerr << "ERROR: Failed to initialize GLFW3" << std::endl;
  50.         return -1;
  51.     }
  52.     // Create window
  53.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  54.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  55.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  56.     GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL Game", nullptr, nullptr);
  57.     if (!window) {
  58.         std::cerr << "ERROR: Failed to create window with GLFW3" << std::endl;
  59.         glfwTerminate();
  60.         return -1;
  61.     }
  62.     glfwMakeContextCurrent(window);
  63.     // Load all OpenGL function pointers.
  64.     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
  65.         std::cerr << "ERROR: Failed to initialize GLAD" << std::endl;
  66.         return -1;
  67.     }
  68.     // Get info from renderer
  69.     const GLubyte* rendererName = glGetString(GL_RENDERER);
  70.     const GLubyte* OpenGLVersionSupported = glGetString(GL_VERSION);
  71.     std::cout << rendererName << std::endl << OpenGLVersionSupported << std::endl;
  72.     // Enable depth
  73.     glEnable(GL_DEPTH_TEST);
  74.     glDepthFunc(GL_LESS);
  75.     // Define triangle
  76.     GLfloat points[] = { 0.0f, 0.5f, 0.0f,
  77.                         0.5f, -0.5f, 0.0f,
  78.                         -0.5f, -0.5f, 0.0f };
  79.     // Create buffer object
  80.     GLuint vertexBufferObject = 0;
  81.     glGenBuffers(1, &vertexBufferObject);
  82.     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
  83.     glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
  84.     // Create vertex attribute object
  85.     GLuint vertexAttributeObject = 0;
  86.     glGenVertexArrays(1, &vertexAttributeObject);
  87.     glBindVertexArray(vertexAttributeObject);
  88.     glEnableVertexAttribArray(0);
  89.     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
  90.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
  91.     // Load shaders
  92.     const char * vertexShaderCode = loadShader("VertexShader.glsl");
  93.     const char * fragmentShaderCode = loadShader("FragmentShader.glsl");
  94.     // Compile shaders
  95.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  96.     glShaderSource(vertexShader, 1, &vertexShaderCode, nullptr);
  97.     glCompileShader(vertexShader);
  98.     // Check vertex shader for compile errors
  99.     int success = 0;
  100.     char message[512] = "";
  101.     glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  102.     if (!success) {
  103.         glGetShaderInfoLog(vertexShader, 512, nullptr, message);
  104.         std::cerr << "ERROR: Failed to compile vertex shader" << std::endl << message;
  105.     }
  106.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  107.     glShaderSource(fragmentShader, 1, &fragmentShaderCode, nullptr);
  108.     glCompileShader(fragmentShader);
  109.     // Check fragment shader for compile errors
  110.     success = 0;
  111.     glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
  112.     if (!success) {
  113.         glGetShaderInfoLog(fragmentShader, 512, nullptr, message);
  114.         // TODO: Specify error type in message
  115.         std::cerr << "ERROR: Failed to compile fragment shader" << std::endl << message;
  116.     }
  117.     // Create shader program and link it
  118.     GLuint shaderProgram = glCreateProgram();
  119.     glAttachShader(shaderProgram, vertexShader);
  120.     glAttachShader(shaderProgram, fragmentShader);
  121.     glLinkProgram(shaderProgram);
  122.     // Check for linking errors
  123.     glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  124.     if (!success) {
  125.         glGetShaderInfoLog(shaderProgram, 512, nullptr, message);
  126.         // TODO: Specify error type in message
  127.         std::cerr << "ERROR: Failed to link shaders" << std::endl << message;
  128.     }
  129.     // Render loop
  130.     while (!glfwWindowShouldClose(window)) {
  131.         // Wipe the drawing surface clear
  132.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  133.         // Use shader program and vertex attribute object
  134.         glUseProgram(shaderProgram);
  135.         glBindVertexArray(vertexAttributeObject);
  136.         // Draw from the currently bound vertex attribute object
  137.         glDrawArrays(GL_TRIANGLES, 0, 3);
  138.         glfwPollEvents();
  139.         glfwSwapBuffers(window);
  140.     }
  141.     // Exit program
  142.     glfwTerminate();
  143.     return 0;
  144. } // end of main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement