dragonlux

Untitled

Dec 17th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.65 KB | None | 0 0
  1. //texture loaders
  2. #define STB_IMAGE_IMPLEMENTATION
  3. #include "stb_image.h"
  4. #include "SOIL.h"
  5. //shaders
  6. #include "FragmentShaders.h"
  7. #include "VertexShaders.h"
  8. //input/output
  9. #include <iostream>
  10. //glut stuff
  11. #include <GL/glew.h>
  12. #include <GL/freeglut.h>
  13. //maths
  14. #include <glm/glm.hpp>
  15. #include <glm/gtc/matrix_transform.hpp>
  16. #include <glm/gtc/type_ptr.hpp>
  17.  
  18. GLuint window;
  19. GLdouble width = 800, height = 600;
  20.  
  21. glm::vec3 eye(0.0f, 0.0f, 3.0f);
  22. glm::vec3 target(0.0f, 0.0f, 0.0f);
  23. glm::vec3 up(0.0f, 1.0f, 0.0f);
  24.  
  25. //===============================================
  26. // GLUT
  27. //===============================================
  28.  
  29. void myDisplay();
  30. void myReshape(int width, int height);
  31. void myMouse(int button, int state, int x, int y);
  32. void myKeyboard(unsigned char theKey, int mouseX, int mouseY);
  33. void myIdle();
  34.  
  35. //===============================================
  36. // Function prototypes
  37. //===============================================
  38.  
  39. void checkShaderCompileStatus(unsigned int ID);
  40. void checkShaderLinkStatus(unsigned int ID);
  41. bool loadTexture(const std::string &filename, unsigned char*& buffer, int* width, int* height, int* nrChannels);
  42. //===============================================
  43. // global data
  44. //===============================================
  45. unsigned char* texture_data = NULL;
  46. int tex_width;
  47. int tex_height;
  48. int nchannels;
  49. unsigned int texture;
  50. unsigned int VBO, VAO, EBO, FBO;
  51. //shaders
  52. unsigned int vertexShader;
  53. unsigned int fragmentShader;
  54. unsigned int ourShader;
  55.  
  56. int main(int argc, char** argv) {
  57.     glutInit(&argc, argv);
  58.     glutInitContextVersion(3, 3); //support OpenGL (>=3.2)
  59.     glutInitContextProfile(GLUT_CORE_PROFILE);
  60.  
  61.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  62.     glutInitWindowSize(width, height);
  63.     glutInitWindowPosition(100, 100);
  64.  
  65.     window = glutCreateWindow("Window");
  66.     glutReshapeFunc(myReshape);
  67.     glutDisplayFunc(myDisplay);
  68.    
  69.     glewExperimental = true;
  70.     GLenum err = glewInit();
  71.     if (GLEW_OK != err)
  72.     {
  73.         fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  74.     }
  75.  
  76.     float vertices[] = {
  77.         // positions          // colors           // texture coords
  78.          0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // top right
  79.          0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // bottom right
  80.         -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // bottom left
  81.         -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // top left
  82.     };
  83.     float texCoords[] = {
  84.     0.0f, 0.0f,  // lower-left corner  
  85.     1.0f, 0.0f,  // lower-right corner
  86.     0.5f, 1.0f   // top-center corner
  87.     };
  88.  
  89.     unsigned int indices[] = {
  90.        0, 1, 3, // first triangle
  91.        1, 2, 3  // second triangle
  92.     };
  93.  
  94.  
  95.     //VAO
  96.     glGenVertexArrays(1, &VAO);
  97.     glGenBuffers(1, &VBO);
  98.     glGenBuffers(1, &EBO);
  99.  
  100.     glBindVertexArray(VAO);
  101.  
  102.     glBindBuffer(GL_ARRAY_BUFFER, VBO);
  103.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  104.  
  105.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  106.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  107.  
  108.     // Vertex Attributes
  109.     glEnableVertexAttribArray(0); //position
  110.     glVertexAttribPointer(
  111.         0,
  112.         3,
  113.         GL_FLOAT,
  114.         GL_FALSE,
  115.         8 * sizeof(GLfloat),
  116.         (void*)(0)
  117.     );
  118.     glEnableVertexAttribArray(1); //color
  119.     glVertexAttribPointer(1,
  120.         3,
  121.         GL_FLOAT,
  122.         GL_FALSE,
  123.         8 * sizeof(GLfloat),
  124.         (void*)(3 * sizeof(GLfloat))
  125.     );
  126.     glEnableVertexAttribArray(2); //texture
  127.     glVertexAttribPointer(2,
  128.         2,
  129.         GL_FLOAT,
  130.         GL_FALSE,
  131.         8 * sizeof(GLfloat),
  132.         (void*)(6 * sizeof(GLfloat))
  133.     );
  134.  
  135.     glGenTextures(1, &texture);
  136.    
  137.     //std::string path = "C:\\Projects\\Graphics\\OpenGL\\TextureTest\\container.jpg";
  138.     bool success = loadTexture(path,texture_data,&tex_width, &tex_height, &nchannels);
  139.     if (success)
  140.     {
  141.         glActiveTexture(GL_TEXTURE+0);
  142.         glBindTexture(GL_TEXTURE_2D, texture);
  143.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (GLsizei)tex_width, (GLsizei)tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_data);
  144.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  145.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  146.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  147.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  148.  
  149.         glGenerateMipmap(GL_TEXTURE_2D);
  150.     }
  151.     else
  152.     {
  153.         std::cout << "ERROR::STBI::FAILED TO LOAD TEXTURE" << std::endl;
  154.     }
  155.  
  156.     stbi_image_free(texture_data);
  157.  
  158.     vertexShader = glCreateShader(GL_VERTEX_SHADER);
  159.     glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
  160.     glCompileShader(vertexShader);
  161.     checkShaderCompileStatus(vertexShader);
  162.    
  163.     fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  164.     glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
  165.     glCompileShader(fragmentShader);
  166.     checkShaderCompileStatus(fragmentShader);
  167.  
  168.     ourShader = glCreateProgram();
  169.     glAttachShader(ourShader, vertexShader);
  170.     glAttachShader(ourShader, fragmentShader);
  171.     glLinkProgram(ourShader);
  172.     checkShaderCompileStatus(ourShader);
  173.     checkShaderLinkStatus(ourShader);
  174.    
  175.     glUseProgram(ourShader);
  176.     glUniform1i(glGetUniformLocation(ourShader, "ourTexture"), 0);
  177.  
  178.     glDeleteShader(vertexShader);
  179.     glDeleteShader(fragmentShader);
  180.  
  181.     glEnable(GL_DEPTH_TEST);
  182.     glDepthFunc(GL_LESS);
  183.  
  184.     glutMainLoop();
  185. }
  186.  
  187. void myDisplay(void)
  188. {
  189.     glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
  190.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  191.    
  192.     glm::mat4 proj = glm::perspective<GLfloat>(
  193.         glm::radians(45.0f),
  194.         width / height,
  195.         0.1f,
  196.         100.0f
  197.         );
  198.     glm::mat4 view = glm::lookAt(eye, target, up);
  199.     glm::mat4 model = glm::mat4(1.0f);
  200.  
  201.     glBindVertexArray(VAO);
  202.     glActiveTexture(GL_TEXTURE0);
  203.     glBindTexture(GL_TEXTURE_2D, texture);
  204.  
  205.     glUseProgram(ourShader);
  206.     GLint proj_loc = glGetUniformLocation(ourShader, "u_proj");
  207.     GLint view_loc = glGetUniformLocation(ourShader, "u_view");
  208.     GLint model_loc = glGetUniformLocation(ourShader, "u_model");
  209.     GLint tex_uniform0 = glGetUniformLocation(ourShader, "ourTexture");
  210.  
  211.     glUniformMatrix4fv(proj_loc, 1, GL_FALSE, glm::value_ptr(proj));
  212.     glUniformMatrix4fv(view_loc, 1, GL_FALSE, glm::value_ptr(view));
  213.     glUniformMatrix4fv(model_loc, 1, GL_FALSE, glm::value_ptr(model));
  214.     glUniform1i(tex_uniform0, 0);
  215.  
  216.     //glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  217.     glDrawArrays(GL_TRIANGLES, 0, 6);
  218.  
  219.     glutSwapBuffers();      
  220. }
  221.  
  222.  
  223. void myReshape(int w, int h)
  224. {
  225.     width = glutGet(GLUT_WINDOW_WIDTH);
  226.     height = glutGet(GLUT_WINDOW_HEIGHT);
  227.    
  228.     glutPostRedisplay();
  229. }
  230.  
  231. bool loadTexture(const std::string &filename, unsigned char*& buffer, int* width, int* height, int* nrChannels) {
  232.     if (FILE *file = fopen(filename.c_str(), "r")) {
  233.         fclose(file);
  234.         std::cout << "File exists" << std::endl;
  235.     }
  236.     else {
  237.         std::cout << "File doesn't exist" << std::endl;
  238.         return false;
  239.     }
  240.    
  241.     buffer = stbi_load(filename.c_str(), width, height, nrChannels, 0);
  242.  
  243.     std::cout << "width: "<< tex_width << std::endl;
  244.     std::cout << "height: " << tex_height << std::endl;
  245.  
  246.     if (buffer) {
  247.         return true;
  248.     }
  249.     return false;
  250. }
  251.  
  252. void checkShaderLinkStatus(unsigned int ID) {
  253.     int status;
  254.     GLchar infoLog[1024];
  255.     glGetProgramiv(ID, GL_LINK_STATUS, &status);
  256.     if (!status) {
  257.         glGetProgramInfoLog(ID, 512, NULL, infoLog);
  258.         std::cout << "ERROR::SHADER::" << ID <<"::LINKING_FAILURE\n" << infoLog << std::endl;
  259.     }
  260.     else {
  261.         std::cout << "SHADER::" << ID << "::LINKING_SUCCESS" << std::endl;
  262.     }
  263. }
  264.  
  265. void checkShaderCompileStatus(unsigned int ID) {
  266.     int status;
  267.     GLchar infolog[1024];
  268.     glGetShaderiv(ID, GL_COMPILE_STATUS, &status);
  269.     if (!status) {
  270.         glGetProgramInfoLog(ID, 512, NULL, infolog);
  271.         std::cout << "ERROR::SHADER::" << ID << "::COMPILATION_FAILURE\n" << infolog << std::endl;
  272.     }
  273.     else {
  274.         std::cout << "SHADER::" << ID << "::COMPILATION_SUCCESS" << std::endl;
  275.     }
  276. }
Add Comment
Please, Sign In to add comment