Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Process.hpp"
- unsigned int CompileShader(const unsigned int type, const std::string source) {
- unsigned int id = glCreateShader(type);
- const char* src = source.c_str();
- glShaderSource(id, 1, &src, nullptr);
- glCompileShader(id);
- int result;
- glGetShaderiv(id, GL_COMPILE_STATUS, &result);
- if (!result) {
- int length;
- glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
- char* message = (char*)alloca(length * sizeof(char));
- glGetShaderInfoLog(id, length, &length, message);
- std::cerr << "Failed to compile shader\n" << message << std::endl;
- glDeleteShader(id);
- return 0;
- }
- // TODO: Error handling
- return id;
- }
- int CreateShader(const std::string& vertexShader, const std::string& fragmentShader) {
- unsigned int program = glCreateProgram();
- unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
- unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
- glAttachShader(program, vs);
- glAttachShader(program, fs);
- glLinkProgram(program);
- glValidateProgram(program);
- glDeleteShader(vs);
- glDeleteShader(fs);
- return program;
- }
- void Process::m_update(const float deltaTime) {
- // handle input
- // poll events
- }
- int Process::Start(const unsigned int updatesPerSecond, int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share, const char* iniFileName) {
- m_millisecondsPerUpdate = 1000.0 / updatesPerSecond;
- m_deltaTime = 1.0f / updatesPerSecond;
- if (!glfwInit()) return 1;
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- m_window = glfwCreateWindow(width, height, title, monitor, share);
- if (!m_window) {
- glfwTerminate();
- return 1;
- }
- glfwMakeContextCurrent(m_window);
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
- std::cerr << "Failed to initialize GLAD" << std::endl;
- return 1;
- }
- glViewport(0, 0, width, height);
- float positions[] = {
- -0.5f, -0.5f,
- 0.5f, -0.5f,
- 0.5f, 0.5f,
- -0.5f, 0.5f,
- };
- unsigned int indices[] = {
- 0, 1, 2,
- 2, 3, 0
- };
- unsigned int buffer;
- glGenBuffers(1, &buffer);
- glBindBuffer(GL_ARRAY_BUFFER, buffer);
- glBufferData(GL_ARRAY_BUFFER, 4 * 2 * sizeof(float), positions, GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
- unsigned int ibo;
- glGenBuffers(1, &ibo);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
- std::ifstream stream;
- stream.open("./res/shaders/vertex.glsl");
- std::string vertexShader = std::string((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
- stream.close();
- stream.open("./res/shaders/fragment.glsl");
- std::string fragmentShader = std::string((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
- stream.close();
- unsigned int shader = CreateShader(vertexShader, fragmentShader);
- glUseProgram(shader);
- while (!glfwWindowShouldClose(m_window)) {
- glClear(GL_COLOR_BUFFER_BIT);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
- glfwSwapBuffers(m_window);
- glfwPollEvents();
- }
- glDeleteProgram(shader);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement