Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define GLEW_STATIC
- #include <gl/glew.h>
- #include <gl/gl.h>
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_image.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <iostream>
- #include <sstream>
- #include <fstream>
- std::string loadShaderFile(std::string fileName)
- {
- std::fstream file;
- file.open(fileName.c_str(), std::ios::in);
- std::stringstream buffer;
- buffer << file.rdbuf();
- std::string data(buffer.str());
- return data;
- }
- int main(int argc, char*args[])
- {
- // zmienne:
- SDL_Window* window = nullptr;
- SDL_GLContext glContext;
- SDL_Event event;
- GLuint vao, vbo, ebo;
- GLuint texture;
- GLuint shaderID;
- // tworzenie okna, inicjalizacja SDL:
- if (SDL_Init( SDL_INIT_EVERYTHING ) < 0) return -1;
- if (!SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ))
- std::cout << "Engine::init: Filtrowanie liniowe nie zostalo wlaczone\n";
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
- SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
- window = SDL_CreateWindow("OpenGL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600,
- SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
- if (window == nullptr) return -1;
- glContext = SDL_GL_CreateContext(window);
- int imgFlags = IMG_INIT_PNG;
- if (!(IMG_Init(imgFlags) & imgFlags)) return -1;
- // ustawienia opengl:
- glEnable(GL_DEPTH_TEST);
- //glEnable(GL_FRAMEBUFFER_SRGB);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_FRONT);
- glFrontFace(GL_CW);
- glewExperimental = GL_TRUE;
- glewInit();
- // wczytywanie shadera:
- shaderID = glCreateProgram();
- GLuint s = glCreateShader(GL_VERTEX_SHADER);
- const GLchar *source = loadShaderFile("frag.glsl").c_str();
- glShaderSource(s, 1, &source, nullptr);
- glCompileShader(s);
- glAttachShader(shaderID, s);
- s = glCreateShader(GL_FRAGMENT_SHADER);
- source = loadShaderFile("vert.glsl").c_str();
- glShaderSource(s, 1, &source, nullptr);
- glCompileShader(s);
- glAttachShader(shaderID, s);
- GLint vShaderCompiled = GL_TRUE;
- glGetShaderiv(s, GL_COMPILE_STATUS, &vShaderCompiled);
- if (vShaderCompiled != GL_TRUE)
- {
- GLint length, len;
- glGetShaderiv(s, GL_INFO_LOG_LENGTH, &length);
- GLchar *info = new GLchar[length];
- glGetShaderInfoLog(s, length, &len, info);
- std::stringstream err;
- err << "\nShaderProgram::createShader: Nie mozna skompilowac shadera: " << ". \nBlad: " << info;
- std::cout << err.str() << std::endl;
- }
- glLinkProgram(shaderID);
- GLint programSuccess = GL_TRUE;
- glGetProgramiv(shaderID, GL_LINK_STATUS, &programSuccess);
- if(programSuccess != GL_TRUE)
- {
- std::string error;
- switch (glGetError())
- {
- case 1280:
- error = "GL_INVALID_ENUM";
- break;
- case 1281:
- error = "GL_INVALID_VALUE";
- break;
- case 1282:
- error = "GL_INVALID_OPERATION";
- break;
- case 1283:
- error = "GL_STACK_OVERFLOW";
- break;
- case 1284:
- error = "GL_STACK_UNDERFLOW";
- break;
- case 1285:
- error = "GL_OUT_OF_MEMORY";
- break;
- default:
- error = "UNKNOWN";
- }
- std::stringstream err;
- err << "ShaderProgram::ShaderProgram: Nie mozna utworzyc programu shadera: " << "\nblad: " << error;
- std::cout << err.str() << std::endl;
- }
- // ustawienie zmiennych shadera:
- glBindFragDataLocation(shaderID, 0, "outColor");
- glm::mat4 model;
- model = glm::translate(model, glm::vec3(0.0f, 0.0f, -3.0f));
- model = glm::rotate(model, glm::radians(55.0f), glm::vec3(1.0, 0.0, 0.0));
- glm::mat4 projection;
- projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 1000.0f);
- glUseProgram(shaderID);
- glUniform1i(glGetUniformLocation(shaderID, "u_textureSampler"), 0);
- glUniformMatrix4fv(glGetUniformLocation(shaderID, "u_model"), 1, GL_FALSE, glm::value_ptr(model));
- glUniformMatrix4fv(glGetUniformLocation(shaderID, "u_projection"), 1, GL_FALSE, glm::value_ptr(projection));
- // tworzenie obiektów do rysowania:
- glGenVertexArrays(1, &vao);
- glGenBuffers(1, &vbo);
- glGenBuffers(1, &ebo);
- glGenTextures(1, &texture);
- GLfloat vertices[] =
- {
- -1.0f, 1.0f, -0.5f, 0.0f, 1.0f,
- 1.0f, 1.0f, -0.5f, 1.0f, 1.0f,
- 1.0f, -1.0f, -0.5f, 1.0f, 0.0f,
- -1.0f, -1.0f, -0.5f, 0.0f, 0.0f
- };
- GLuint identices[] =
- {
- 1, 3, 2,
- 1, 0, 3
- };
- glBindVertexArray(vao);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(identices), identices, GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), 0);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GLfloat), (void*)(3*sizeof(GLfloat)));
- SDL_Surface *img;
- glBindTexture(GL_TEXTURE_2D, texture);
- img = IMG_Load("texture.png");
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, img->pixels);
- SDL_FreeSurface(img);
- glGenerateMipmap(GL_TEXTURE_2D);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- // glowna petla:
- while (true)
- {
- while (SDL_PollEvent(&event))
- {
- if (event.type == SDL_QUIT)
- {
- SDL_Quit();
- exit(0);
- }
- }
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glBindTexture(GL_TEXTURE_2D, texture);
- //glBindVertexArray(vao);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- SDL_GL_SwapWindow(window);
- }
- // koniec:
- glDeleteBuffers(1, &ebo);
- glDeleteBuffers(1, &vbo);
- glDeleteVertexArrays(1, &vao);
- glDeleteTextures(1, &texture);
- SDL_GL_DeleteContext(glContext);
- SDL_DestroyWindow(window);
- IMG_Quit();
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement