Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gl/glew.h>
- #include <gl/gl.h>
- #include <SDL2/SDL.h>
- #include <fstream>
- #include <sstream>
- #include <iostream>
- #include <glm/glm.hpp>
- #include <SDL2/SDL_image.h>
- #include <vector>
- struct GLTexture
- {
- GLuint textureID;
- float width, height;
- };
- struct UV
- {
- float u, v;
- };
- struct Position
- {
- float x;
- float y;
- };
- struct ColorRGBA8
- {
- ColorRGBA8() : r(0), g(0), b(0), a(0) { }
- ColorRGBA8(GLubyte R, GLubyte G, GLubyte B, GLubyte A) :
- r(R), g(G), b(B), a(A) { }
- GLubyte r;
- GLubyte g;
- GLubyte b;
- GLubyte a;
- };
- struct Vertex
- {
- Position position;
- ColorRGBA8 color;
- UV uv;
- void setUV(float u, float v)
- {
- uv.u = u;
- uv.v = v;
- }
- void setPosition(float x, float y)
- {
- position.x = x;
- position.y = y;
- }
- void setColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a)
- {
- color.r = r;
- color.g = g;
- color.b = b;
- color.a = a;
- }
- };
- GLTexture LoadTexture(std::string textureName);
- GLuint CreateShaderProgram(const GLchar * vertexShaderText, const GLchar* fragmentShaderText);
- std::string loadFromFile(std::string fileName);
- void InitVBO(float x, float y, float w, float h, GLuint shaderID);
- GLuint vbo;
- GLuint vao;
- float time;
- int main(int arg, char*args[])
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_Window* window = SDL_CreateWindow("window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
- SDL_GLContext context = SDL_GL_CreateContext(window);
- SDL_GL_SetSwapInterval(1);
- glewExperimental = true;
- GLenum result = glewInit();
- std::string vertex = loadFromFile("shader.vert");
- std::string frag = loadFromFile("shader.frag");
- GLuint shaderProgram = CreateShaderProgram(vertex.c_str(), frag.c_str());
- GLint uniformTime = glGetUniformLocation(shaderProgram, "time");
- GLint uniformUV = glGetUniformLocation(shaderProgram, "mySampler");
- glUseProgram(shaderProgram);
- InitVBO(1.0, 1.0, -2.0, -2.0, shaderProgram);
- GLTexture texture = LoadTexture("texture.png");
- std::cout << "IN MAIN ID :" << texture.textureID << std::endl;
- SDL_Event *event = new SDL_Event;
- bool quit = false;
- while (!quit && event->type != SDL_QUIT)
- {
- SDL_PollEvent(event);
- //time = SDL_GetTicks() / 1000;
- time += 0.01;
- glClearDepthf(1.0);
- //glUniform1f(uniformTime, time);
- glUniform1i(uniformUV, 0);
- glClearColor(0.0, 0.0, 0.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, texture.textureID);
- glBindVertexArray(vao);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_FRONT);
- glFrontFace(GL_CW);
- glDrawArrays(GL_TRIANGLES, 0, 6);
- glBindTexture(GL_TEXTURE_2D, 0);
- SDL_GL_SwapWindow(window);
- }
- glDeleteBuffers(1, &vbo);
- glDeleteBuffers(1, &vao);
- return 0;
- }
- GLTexture LoadTexture(std::string textureName)
- {
- //textureName = textureName + ".png";
- GLTexture texture{};
- SDL_Surface *surface = IMG_Load(textureName.c_str());
- texture.width = surface->w;
- texture.height = surface->h;
- glGenTextures(1, &texture.textureID);
- glBindTexture(GL_TEXTURE_2D, texture.textureID);
- int Format = GL_RGB;
- if (surface->format->BytesPerPixel == 4) {
- Format = GL_RGBA;
- }
- std::cout << "IN LOADTEXTURE ID :" << texture.textureID << std::endl;
- glTexImage2D(GL_TEXTURE_2D, 0, Format, texture.width, texture.height, 0, Format, GL_UNSIGNED_BYTE, surface->pixels);
- SDL_FreeSurface(surface);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glGenerateMipmap(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, 0);
- return texture;
- }
- GLuint CreateShaderProgram(const GLchar * vertexShaderText, const GLchar* fragmentShaderText)
- {
- GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
- GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- GLuint shaderProgram = glCreateProgram();
- glShaderSource(vertexShader, 1, &vertexShaderText, NULL);
- glCompileShader(vertexShader);
- GLint isCompiled = 0;
- glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isCompiled);
- if (isCompiled == GL_FALSE)
- {
- GLint maxLength = 0;
- glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength);
- std::vector<GLchar> infoLog(maxLength);
- glGetShaderInfoLog(vertexShader, maxLength, &maxLength, &infoLog[0]);
- glDeleteShader(vertexShader);
- std::cout << '%s\n' << &infoLog[0] << std::endl;
- return 0;
- }
- glShaderSource(fragmentShader, 1, &fragmentShaderText, NULL);
- glCompileShader(fragmentShader);
- glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &isCompiled);
- if (isCompiled == GL_FALSE)
- {
- GLint maxLength = 0;
- glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &maxLength);
- std::vector<GLchar> infoLog(maxLength);
- glGetShaderInfoLog(fragmentShader, maxLength, &maxLength, &infoLog[0]);
- glDeleteShader(fragmentShader);
- glDeleteShader(vertexShader);
- std::cout << '%s\n' << &infoLog[0] << std::endl;
- return 0;
- }
- glAttachShader(shaderProgram, vertexShader);
- glAttachShader(shaderProgram, fragmentShader);
- glLinkProgram(shaderProgram);
- GLint isLinked = 0;
- glGetProgramiv(shaderProgram, GL_LINK_STATUS, (int *)&isLinked);
- if (isLinked == GL_FALSE)
- {
- GLint maxLength = 0;
- glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &maxLength);
- std::vector<GLchar> infoLog(maxLength);
- glGetProgramInfoLog(shaderProgram, maxLength, &maxLength, &infoLog[0]);
- glDeleteProgram(shaderProgram);
- glDeleteShader(vertexShader);
- glDeleteShader(fragmentShader);
- std::cout << '%s\n' << &infoLog[0] << std::endl;
- return 0;
- }
- glDetachShader(shaderProgram, vertexShader);
- glDetachShader(shaderProgram, fragmentShader);
- glDeleteShader(vertexShader);
- glDeleteShader(fragmentShader);
- return shaderProgram;
- }
- std::string loadFromFile(std::string fileName)
- {
- std::ifstream openFile(fileName);
- std::stringstream sstream;
- if (!openFile.is_open())
- {
- std::cout << "Could not find the file: " << fileName << std::endl;
- }
- sstream << openFile.rdbuf();
- return sstream.str();
- }
- void InitVBO(float x, float y, float w, float h, GLuint shaderID)
- {
- Vertex vertex[6];
- vertex[0].setPosition(x + w, y + h);
- vertex[0].setUV(1.0f, 1.0f);
- vertex[1].setPosition(x, y + h);
- vertex[1].setUV(0.0f, 1.0f);
- vertex[2].setPosition(x, y);
- vertex[2].setUV(0.0f, 0.0f);
- vertex[3].setPosition(x, y);
- vertex[3].setUV(0.0f, 0.0f);
- vertex[4].setPosition(x + w, y);
- vertex[4].setUV(1.0f, 0.0f);
- vertex[5].setPosition(x + w, y + h);
- vertex[5].setUV(1.0f, 1.0f);
- for (int i = 0; i < 6; ++i)
- {
- vertex[i].setColor(255, 0, 255, 255);
- }
- vertex[1].setColor(0, 0, 255, 255);
- vertex[4].setColor(0, 255, 0, 255);
- glGenVertexArrays(1, &vao);
- glGenBuffers(1, &vbo);
- glBindVertexArray(vao);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
- GLint value1 = glGetAttribLocation(shaderID, "pos");
- glEnableVertexAttribArray(value1);
- glVertexAttribPointer(value1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
- GLint value2 = glGetAttribLocation(shaderID, "col");
- glEnableVertexAttribArray(value2);
- glVertexAttribPointer(value2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));
- GLint value3 = glGetAttribLocation(shaderID, "uv");
- glEnableVertexAttribArray(value3);
- glVertexAttribPointer(value3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uv));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement