Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "libs.h"
- Vertex vertices[] = {
- //POSITION
- glm::vec3(0.0f, 0.5f, 0.f),
- glm::vec3(-0.5f, -0.5f, 0.f),
- glm::vec3(0.5f, -0.5f, 0.f),
- //COLOR
- glm::vec3(1.f, 0.f, 0.f),
- glm::vec3(0.0f, 1.f, 0.f),
- glm::vec3(0.0f, 0.f, 1.f),
- //TEXCOORDS
- glm::vec2(0.f, 1.f),
- glm::vec2(0.f, 0.f),
- glm::vec2(1.f, 0.f)
- };
- unsigned nrOfVertices = sizeof(vertices) / sizeof(Vertex);
- GLuint indices[] = {
- 0, 1, 2 //no comma here
- };
- bool loadShaders(GLuint &program) {
- bool loadSuccess = true;
- int infoLog(512);
- GLint success;
- std::string temp = "";
- std::string src = "";
- std::ifstream in_file;
- //VERTEX
- in_file.open("fragment.glsl");
- if (in_file.is_open())
- {
- while (std::getline(in_file, temp))
- {
- src += temp + "\n";
- }
- }
- else
- {
- {
- std::cout << "ERROR::LOADSHADERS::COULD_NOT_OPEN";
- return false;
- }
- in_file.close();
- //COMPILE SHADER
- GLuint vertexShader = glCreateShader(GL_FRAGMENT_SHADER);
- const GLchar* vertSrc = src.c_str();
- glShaderSource(vertexShader, 1, &vertSrc, NULL);
- glCompileShader(vertexShader);
- glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- //glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
- std::cout << "ERROR::LOADSHADERS::COULD_NOT_COMPILE_SHADERS" << "\n";
- //std::cout << infoLog << "\n";
- return false;
- }
- temp = "";
- src = "";
- //FRAGMENT
- in_file.open("vertex_core.glsl");
- if (in_file.is_open())
- {
- while (std::getline(in_file, temp))
- {
- src += temp + "\n";
- }
- }
- else
- {
- std::cout << "ERROR::LOADSHADERS::COULD_NOT_OPEN" << "\n";
- return false;
- }
- in_file.close();
- //COMPILE SHADER
- GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);
- const GLchar* fragSrc = src.c_str();
- glShaderSource(fragment, 1, &fragSrc, NULL);
- glCompileShader(fragment);
- glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- // glGetShaderInfoLog(fragment, 512, NULL, infoLog);
- std::cout << "ERROR::LOADSHADERS::COULD_NOT_COMPILE_SHADERS" << "\n";
- //std::cout << infoLog << "\n";
- return false;
- }
- temp = "";
- src = "";
- //Program
- program = glCreateProgram();
- glAttachShader(program, vertexShader);
- glAttachShader(program, fragment);
- glLinkProgram(program);
- glGetProgramiv(program, GL_LINK_STATUS, &success);
- if (!success)
- {
- // glGetProgramInfoLog(program, 512, NULL, infoLog);
- std::cout << "ERROR::LINKSHADERS::COULD_NOT_LINK_SHADERS" << "\n";
- // std::cout << infoLog << "\n";
- }
- //END
- glUseProgram(0);
- glDeleteShader(vertexShader);
- glDeleteShader(fragment);
- return loadSuccess;
- }
- }
- bool updateInput(GLFWwindow* window) {
- if (glfwGetKey(window, GLFW_KEY_DELETE) == GLFW_PRESS)
- {
- glfwTerminate();
- return true;
- }
- return false;
- }
- int main() {
- if (!glfwInit()) {
- fprintf(stderr, "Failed to initialize GLFW\n");
- return -1;
- }
- const int WINDOW_WIDTH = 1920;
- const int WINDOW_HEIGHT = 1080;
- const int framebufferwidth = 0;
- const int framebufferheight = 0;
- glfwWindowHint(GLFW_SAMPLES, 4);
- glfwWindowHint(GLFW_OPENGL_PROFILE,
- GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
- glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
- GLFWmonitor* monitor = glfwGetPrimaryMonitor();
- //if you use the above GLFWmonitor, and replace the first NULL on the window instantiation, it allows a strange version of fullscreen.
- //NOTE TO SELF: try to modify and use this new-founded trick to make a fullscreen feature in games
- GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Ache Engine", monitor, NULL);
- #pragma region CREATE WINDOW
- try
- {
- //for mac support
- //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- glfwGetFramebufferSize(window, framebufferwidth, framebufferheight);
- glViewport(0, 0, framebufferwidth, framebufferheight);
- glfwMakeContextCurrent(window); //IMPORTANT FOR GLEW
- glewExperimental = GL_TRUE;
- if (glewInit() != GLEW_OK)
- {
- std::cout << "error initializing glew\n";
- //glfwTerminate();
- }
- //OPENGL OPTIONS/SETTINGS
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- glFrontFace(GL_CCW);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- //to fill a drawn shapes whole body with a color
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- //to fill a drawn shapes outline with a color
- //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- //SHADERS
- #pragma region SHADERS_01
- bool b = loadShaders(core_program);
- if (b == false)
- {
- glfwTerminate();
- }
- else
- {
- //DO NOTHING
- }
- #pragma endregion
- //VAO, VBO, EBO
- try
- {
- glCreateVertexArrays(1, &VAO);
- glBindVertexArray(VAO);
- GLuint VBO;
- glGenBuffers(1, &VBO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- GLuint EBO;
- glGenBuffers(1, &EBO);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
- GLuint attribloc = glGetAttribLocation(core_program, "vertex_position");
- //POSITION
- glVertexAttribPointer(attribloc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position));
- glEnableVertexAttribArray(attribloc);
- //COLOR
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color));
- glEnableVertexAttribArray(1);
- //TEXCOORD
- glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texcoord));
- glEnableVertexAttribArray(2);
- glBindVertexArray(0);
- }
- catch (const std::exception&)
- {
- }
- }
- catch (const std::exception&)
- {
- }
- #pragma endregion
- #pragma region GAME LOOP
- glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
- do {
- // Clear the screen. It's not mentioned before Tutorial 02, but it can cause flickering, so it's there nonetheless.
- glClear(GL_COLOR_BUFFER_BIT);
- //DRAW THINGS HERE
- // Draw nothing, see you in tutorial 2 !
- // Swap buffers
- glfwSwapBuffers(window);
- glfwPollEvents();
- } // Check if the ESC key was pressed or the window was closed
- while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS & glfwWindowShouldClose(window) == 0)
- ; {
- updateInput(window);
- glUseProgram(core_program);
- //BIND VERTEX ARRAY OBJECT
- glBindVertexArray(VAO);
- glDrawElements(GL_TRIANGLES, nrOfVertices, GL_UNSIGNED_INT, 0);
- }
- #pragma endregion
- glfwDestroyWindow(window);
- glDeleteProgram(core_program);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement