Advertisement
Guest User

Untitled

a guest
Aug 8th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1. /******************************/
  2. /********** main.cpp **********/
  3. /******************************/
  4.  
  5. #define GLEW_STATIC
  6.  
  7. #include <glew.h>
  8. #include <glfw3.h>
  9. #include <glm\gtc\matrix_transform.hpp>
  10. #include <glm\gtc\type_ptr.hpp>
  11. #include <SOIL.h>
  12. #include "utils.h" // functions for loading shaders and the error() function
  13.  
  14. #include <iostream>
  15.  
  16. using namespace std;
  17.  
  18. float vertices [] =
  19. {
  20.     -0.5f, -0.5f,
  21.     0.5f, -0.5f,
  22.     0.5f, 0.5f,
  23.  
  24.     0.5f, 0.5f,
  25.     -0.5f, 0.5f,
  26.     -0.5f, -0.5f
  27. };
  28.  
  29. int main()
  30. {
  31.     glfwInit();
  32.  
  33.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  34.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  35.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  36.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  37.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  38.  
  39.     GLFWwindow* window = glfwCreateWindow(1280, 1024, "error_test", nullptr, nullptr);
  40.  
  41.     if (window == nullptr)
  42.     {
  43.         cout << "Failed to create window" << endl;
  44.         glfwTerminate();
  45.         system("pause");
  46.         return -1;
  47.     }
  48.  
  49.     glfwMakeContextCurrent(window);
  50.  
  51.     glewExperimental = GL_TRUE;
  52.     glewInit();
  53.  
  54.     // OpenGL initialization goes here
  55.     GLuint vao;
  56.     glGenVertexArrays(1, &vao);
  57.     glBindVertexArray(vao);
  58.  
  59.     GLuint vbo;
  60.     glGenBuffers(1, &vbo);
  61.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  62.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  63.  
  64.     GLuint program = setupProgram("vertex.glsl", "fragment.glsl");
  65.  
  66.     glUseProgram(program);
  67.  
  68.     glEnableVertexAttribArray(0);
  69.     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr);
  70.  
  71.     error(glGetError()); // this thing
  72.  
  73.     while (!glfwWindowShouldClose(window))
  74.     {
  75.         if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  76.         {
  77.             glfwSetWindowShouldClose(window, GL_TRUE);
  78.         }
  79.  
  80.         glfwPollEvents();
  81.  
  82.         glDrawArrays(GL_TRIANGLES, 0, 6);
  83.  
  84.         glfwSwapBuffers(window);
  85.     }
  86.  
  87.     glfwTerminate();
  88.     return 0;
  89. }
  90.  
  91.  
  92. /***************************/
  93. /**********utils.h**********/
  94. /***************************/
  95.  
  96. #pragma once
  97.  
  98. #include <iostream>
  99. #include <fstream>
  100. #include <string>
  101. #include <glew.h>
  102. #include <SOIL.h>
  103.  
  104. using namespace std;
  105.  
  106. string loadShader(string path)
  107. {
  108.     ifstream in(path);
  109.  
  110.     string text = "";
  111.     if (in.is_open())
  112.     {
  113.         string line;
  114.         while (getline(in, line))
  115.         {
  116.             line += "\n";
  117.             text += line;
  118.         }
  119.  
  120.         return text;
  121.     }
  122.     else
  123.     {
  124.         cout << "I can't open it m8" << endl;
  125.         return "err";
  126.     }
  127. }
  128.  
  129. GLuint loadTexture(const char* path)
  130. {
  131.     int w, h;
  132.     unsigned char* texData = SOIL_load_image(path, &w, &h, nullptr, SOIL_LOAD_RGB);
  133.     if (texData == nullptr)
  134.     {
  135.         cout << "Error: Texture is nullptr" << endl;
  136.         cout << SOIL_last_result() << endl;
  137.         return 0;
  138.     }
  139.  
  140.     GLuint tex;
  141.     glGenTextures(1, &tex);
  142.     glBindTexture(GL_TEXTURE_2D, tex);
  143.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, texData);
  144.  
  145.     SOIL_free_image_data(texData);
  146.  
  147.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  148.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  149.  
  150.     glGenerateMipmap(GL_TEXTURE_2D);
  151.  
  152.     glBindTexture(GL_TEXTURE_2D, 0);
  153.  
  154.     return tex;
  155. }
  156.  
  157. GLuint setupProgram(string vsFileName, string fsFileName)
  158. {
  159.     GLuint vs = glCreateShader(GL_VERTEX_SHADER);
  160.     string vsStr = loadShader(vsFileName);
  161.     const char* vsSource = vsStr.c_str();
  162.     glShaderSource(vs, 1, &vsSource, nullptr);
  163.     glCompileShader(vs);
  164.  
  165.     char vsLog[512];
  166.     glGetShaderInfoLog(vs, 512, nullptr, vsLog);
  167.     cout << "Vertex shader output: " << endl;
  168.     cout << vsLog << endl;
  169.  
  170.     GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
  171.     string fsStr = loadShader(fsFileName);
  172.     const char* fsSource = fsStr.c_str();
  173.     glShaderSource(fs, 1, &fsSource, nullptr);
  174.     glCompileShader(fs);
  175.  
  176.     char fsLog[512];
  177.     glGetShaderInfoLog(fs, 512, nullptr, fsLog);
  178.     cout << "Fragment shader output: " << endl;
  179.     cout << fsLog << endl;
  180.  
  181.     GLuint prog = glCreateProgram();
  182.     glAttachShader(prog, vs);
  183.     glAttachShader(prog, fs);
  184.     glLinkProgram(prog);
  185.  
  186.     return prog;
  187. }
  188.  
  189. void error(GLenum e)
  190. {
  191.     switch (e)
  192.     {
  193.     case GL_INVALID_ENUM:
  194.         cout << "Error: GL_INVALID_ENUM" << endl;
  195.         system("pause");
  196.         break;
  197.     case GL_INVALID_VALUE:
  198.         cout << "Error: GL_INVALID_VALUE" << endl;
  199.         system("pause");
  200.         break;
  201.     case GL_INVALID_OPERATION:
  202.         cout << "Error: GL_INVALID_OPERATION" << endl;
  203.         system("pause");
  204.         break;
  205.     case GL_INVALID_FRAMEBUFFER_OPERATION:
  206.         cout << "Error: GL_INVALID_FRAMEBUFFER_OPERATION" << endl;
  207.         system("pause");
  208.         break;
  209.     case GL_OUT_OF_MEMORY:
  210.         cout << "Error: GL_OUT_OF_MEMORY" << endl;
  211.         system("pause");
  212.         break;
  213.     case GL_NO_ERROR:
  214.         cout << "No error reported" << endl;
  215.         break;
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement