Advertisement
czaffik

sdl glVertexAttribPointer

Dec 24th, 2017
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.91 KB | None | 0 0
  1. #include <gl/glew.h>
  2. #include <gl/gl.h>
  3. #include <SDL2/SDL.h>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <iostream>
  7. #include <glm/glm.hpp>
  8. #include <SDL2/SDL_image.h>
  9. #include <vector>
  10.  
  11. struct GLTexture
  12. {
  13.     GLuint textureID;
  14.     float width, height;
  15. };
  16.  
  17. struct UV
  18. {
  19.     float u, v;
  20. };
  21.  
  22. struct Position
  23. {
  24.     float x;
  25.     float y;
  26. };
  27.  
  28. struct ColorRGBA8
  29. {
  30.     ColorRGBA8() : r(0), g(0), b(0), a(0) { }
  31.     ColorRGBA8(GLubyte R, GLubyte G, GLubyte B, GLubyte A) :
  32.         r(R), g(G), b(B), a(A) { }
  33.     GLubyte r;
  34.     GLubyte g;
  35.     GLubyte b;
  36.     GLubyte a;
  37. };
  38.  
  39. struct Vertex
  40. {
  41.     Position position;
  42.  
  43.     ColorRGBA8 color;
  44.  
  45.     UV uv;
  46.  
  47.     void setUV(float u, float v)
  48.     {
  49.         uv.u = u;
  50.         uv.v = v;
  51.     }
  52.  
  53.     void setPosition(float x, float y)
  54.     {
  55.         position.x = x;
  56.         position.y = y;
  57.     }
  58.  
  59.     void setColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a)
  60.     {
  61.         color.r = r;
  62.         color.g = g;
  63.         color.b = b;
  64.         color.a = a;
  65.     }
  66. };
  67.  
  68. GLTexture LoadTexture(std::string textureName);
  69.  
  70. GLuint CreateShaderProgram(const GLchar * vertexShaderText, const GLchar* fragmentShaderText);
  71.  
  72. std::string loadFromFile(std::string fileName);
  73.  
  74. void InitVBO(float x, float y, float w, float h, GLuint shaderID);
  75.  
  76. GLuint vbo;
  77. GLuint vao;
  78.  
  79. float time;
  80.  
  81. int main(int arg, char*args[])
  82. {
  83.     SDL_Init(SDL_INIT_EVERYTHING);
  84.  
  85.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  86.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  87.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  88.     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  89.  
  90.     SDL_Window* window = SDL_CreateWindow("window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
  91.     SDL_GLContext context = SDL_GL_CreateContext(window);
  92.  
  93.     SDL_GL_SetSwapInterval(1);
  94.  
  95.     glewExperimental = true;
  96.  
  97.     GLenum result = glewInit();
  98.  
  99.     std::string vertex = loadFromFile("shader.vert");
  100.     std::string frag = loadFromFile("shader.frag");
  101.  
  102.     GLuint shaderProgram = CreateShaderProgram(vertex.c_str(), frag.c_str());
  103.     GLint uniformTime = glGetUniformLocation(shaderProgram, "time");
  104.     GLint uniformUV = glGetUniformLocation(shaderProgram, "mySampler");
  105.  
  106.     glUseProgram(shaderProgram);
  107.  
  108.     InitVBO(1.0, 1.0, -2.0, -2.0, shaderProgram);
  109.  
  110.     GLTexture texture = LoadTexture("texture.png");
  111.  
  112.     std::cout << "IN MAIN ID :" << texture.textureID << std::endl;
  113.  
  114.     SDL_Event *event = new SDL_Event;
  115.     bool quit = false;
  116.  
  117.     while (!quit && event->type != SDL_QUIT)
  118.     {
  119.         SDL_PollEvent(event);
  120.  
  121.         //time = SDL_GetTicks() / 1000;
  122.         time += 0.01;
  123.  
  124.         glClearDepthf(1.0);
  125.  
  126.         //glUniform1f(uniformTime, time);
  127.         glUniform1i(uniformUV, 0);
  128.  
  129.         glClearColor(0.0, 0.0, 0.0, 1.0);
  130.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  131.  
  132.         glActiveTexture(GL_TEXTURE0);
  133.         glBindTexture(GL_TEXTURE_2D, texture.textureID);
  134.  
  135.         glBindVertexArray(vao);
  136.  
  137.         glEnable(GL_CULL_FACE);
  138.         glCullFace(GL_FRONT);
  139.         glFrontFace(GL_CW);
  140.  
  141.         glDrawArrays(GL_TRIANGLES, 0, 6);
  142.  
  143.         glBindTexture(GL_TEXTURE_2D, 0);
  144.         SDL_GL_SwapWindow(window);
  145.     }
  146.  
  147.     glDeleteBuffers(1, &vbo);
  148.     glDeleteBuffers(1, &vao);
  149.  
  150.     return 0;
  151. }
  152.  
  153. GLTexture LoadTexture(std::string textureName)
  154. {
  155.     //textureName = textureName + ".png";
  156.     GLTexture texture{};
  157.     SDL_Surface *surface = IMG_Load(textureName.c_str());
  158.  
  159.     texture.width = surface->w;
  160.     texture.height = surface->h;
  161.  
  162.     glGenTextures(1, &texture.textureID);
  163.     glBindTexture(GL_TEXTURE_2D, texture.textureID);
  164.  
  165.     int Format = GL_RGB;
  166.  
  167.     if (surface->format->BytesPerPixel == 4) {
  168.         Format = GL_RGBA;
  169.     }
  170.  
  171.     std::cout << "IN LOADTEXTURE ID :" << texture.textureID << std::endl;
  172.  
  173.     glTexImage2D(GL_TEXTURE_2D, 0, Format, texture.width, texture.height, 0, Format, GL_UNSIGNED_BYTE, surface->pixels);
  174.  
  175.     SDL_FreeSurface(surface);
  176.  
  177.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  178.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  179.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  180.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  181.  
  182.     glGenerateMipmap(GL_TEXTURE_2D);
  183.  
  184.     glBindTexture(GL_TEXTURE_2D, 0);
  185.  
  186.     return texture;
  187. }
  188.  
  189. GLuint CreateShaderProgram(const GLchar * vertexShaderText, const GLchar* fragmentShaderText)
  190. {
  191.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  192.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  193.  
  194.     GLuint shaderProgram = glCreateProgram();
  195.  
  196.     glShaderSource(vertexShader, 1, &vertexShaderText, NULL);
  197.     glCompileShader(vertexShader);
  198.  
  199.     GLint isCompiled = 0;
  200.     glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isCompiled);
  201.     if (isCompiled == GL_FALSE)
  202.     {
  203.         GLint maxLength = 0;
  204.         glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength);
  205.  
  206.         std::vector<GLchar> infoLog(maxLength);
  207.         glGetShaderInfoLog(vertexShader, maxLength, &maxLength, &infoLog[0]);
  208.  
  209.         glDeleteShader(vertexShader);
  210.  
  211.         std::cout << '%s\n' << &infoLog[0] << std::endl;
  212.  
  213.         return 0;
  214.     }
  215.  
  216.     glShaderSource(fragmentShader, 1, &fragmentShaderText, NULL);
  217.     glCompileShader(fragmentShader);
  218.  
  219.     glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &isCompiled);
  220.     if (isCompiled == GL_FALSE)
  221.     {
  222.         GLint maxLength = 0;
  223.         glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &maxLength);
  224.  
  225.         std::vector<GLchar> infoLog(maxLength);
  226.         glGetShaderInfoLog(fragmentShader, maxLength, &maxLength, &infoLog[0]);
  227.  
  228.         glDeleteShader(fragmentShader);
  229.         glDeleteShader(vertexShader);
  230.  
  231.         std::cout << '%s\n' << &infoLog[0] << std::endl;
  232.  
  233.         return 0;
  234.     }
  235.  
  236.     glAttachShader(shaderProgram, vertexShader);
  237.     glAttachShader(shaderProgram, fragmentShader);
  238.  
  239.     glLinkProgram(shaderProgram);
  240.  
  241.     GLint isLinked = 0;
  242.     glGetProgramiv(shaderProgram, GL_LINK_STATUS, (int *)&isLinked);
  243.     if (isLinked == GL_FALSE)
  244.     {
  245.         GLint maxLength = 0;
  246.         glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &maxLength);
  247.  
  248.         std::vector<GLchar> infoLog(maxLength);
  249.         glGetProgramInfoLog(shaderProgram, maxLength, &maxLength, &infoLog[0]);
  250.  
  251.         glDeleteProgram(shaderProgram);
  252.         glDeleteShader(vertexShader);
  253.         glDeleteShader(fragmentShader);
  254.  
  255.         std::cout << '%s\n' << &infoLog[0] << std::endl;
  256.  
  257.         return 0;
  258.     }
  259.  
  260.     glDetachShader(shaderProgram, vertexShader);
  261.     glDetachShader(shaderProgram, fragmentShader);
  262.  
  263.     glDeleteShader(vertexShader);
  264.     glDeleteShader(fragmentShader);
  265.  
  266.     return shaderProgram;
  267. }
  268.  
  269. std::string loadFromFile(std::string fileName)
  270. {
  271.     std::ifstream openFile(fileName);
  272.     std::stringstream sstream;
  273.  
  274.     if (!openFile.is_open())
  275.     {
  276.         std::cout << "Could not find the file: " << fileName << std::endl;
  277.     }
  278.  
  279.     sstream << openFile.rdbuf();
  280.  
  281.     return sstream.str();
  282. }
  283.  
  284. void InitVBO(float x, float y, float w, float h, GLuint shaderID)
  285. {
  286.     Vertex vertex[6];
  287.  
  288.     vertex[0].setPosition(x + w, y + h);
  289.     vertex[0].setUV(1.0f, 1.0f);
  290.  
  291.     vertex[1].setPosition(x, y + h);
  292.     vertex[1].setUV(0.0f, 1.0f);
  293.  
  294.     vertex[2].setPosition(x, y);
  295.     vertex[2].setUV(0.0f, 0.0f);
  296.  
  297.     vertex[3].setPosition(x, y);
  298.     vertex[3].setUV(0.0f, 0.0f);
  299.  
  300.     vertex[4].setPosition(x + w, y);
  301.     vertex[4].setUV(1.0f, 0.0f);
  302.  
  303.     vertex[5].setPosition(x + w, y + h);
  304.     vertex[5].setUV(1.0f, 1.0f);
  305.  
  306.     for (int i = 0; i < 6; ++i)
  307.     {
  308.         vertex[i].setColor(255, 0, 255, 255);
  309.     }
  310.  
  311.     vertex[1].setColor(0, 0, 255, 255);
  312.     vertex[4].setColor(0, 255, 0, 255);
  313.  
  314.     glGenVertexArrays(1, &vao);
  315.     glGenBuffers(1, &vbo);
  316.  
  317.     glBindVertexArray(vao);
  318.  
  319.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  320.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
  321.  
  322.     GLint value1 = glGetAttribLocation(shaderID, "pos");
  323.     glEnableVertexAttribArray(value1);
  324.     glVertexAttribPointer(value1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
  325.  
  326.     GLint value2 = glGetAttribLocation(shaderID, "col");
  327.     glEnableVertexAttribArray(value2);
  328.     glVertexAttribPointer(value2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));
  329.  
  330.     GLint value3 = glGetAttribLocation(shaderID, "uv");
  331.     glEnableVertexAttribArray(value3);
  332.     glVertexAttribPointer(value3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uv));
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement