Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define GLEW_STATIC
- #define GLM_FORCE_RADIANS
- #include <glew.h>
- #include <GLFW\glfw3.h>
- #include <glm\glm.hpp>
- #include <glm\gtc\matrix_transform.hpp>
- #include <glm\gtc\type_ptr.hpp>
- #include <SOIL.h>
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <chrono>
- #include <vector>
- #include "utils.h"
- #include "object.h"
- #include "camera.h"
- using namespace std;
- // cube vertices
- float cube[] =
- {
- -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
- 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
- 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
- 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
- -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
- -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
- -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
- 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
- -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
- -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
- -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
- -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
- -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
- -0.5f, -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0.5f, 1.0f, 0.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
- 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
- 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
- 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
- -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
- -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
- -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
- 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
- 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
- -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
- -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
- };
- // not this, it's the quad to render the framebuffer texture to
- float quad [] =
- {
- -1.0f, 1.0f, 0.0f, 1.0f,
- -1.0f, -1.0f, 0.0f, 0.0f,
- 1.0f, -1.0f, 1.0f, 0.0f,
- -1.0f, 1.0f, 0.0f, 1.0f,
- 1.0f, -1.0f, 1.0f, 0.0f,
- 1.0f, 1.0f, 1.0f, 1.0f
- };
- bool keys[1024];
- camera cam(glm::vec3(0.0f, 0.0f, 3.5f), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f), 0.05f);
- float deltaT = 0.0f;
- float lastFrame = 0.0f;
- float lastX = 512.0f;
- float lastY = 384.0f;
- void error(GLenum e)
- {
- switch (e)
- {
- case GL_INVALID_ENUM:
- cout << "Error: GL_INVALID_ENUM" << endl;
- system("pause");
- glfwTerminate();
- break;
- case GL_INVALID_VALUE:
- cout << "Error: GL_INVALID_VALUE" << endl;
- system("pause");
- glfwTerminate();
- break;
- case GL_INVALID_OPERATION:
- cout << "Error: GL_INVALID_OPERATION" << endl;
- system("pause");
- glfwTerminate();
- break;
- case GL_INVALID_FRAMEBUFFER_OPERATION:
- cout << "Error: GL_INVALID_FRAMEBUFFER_OPERATION" << endl;
- system("pause");
- glfwTerminate();
- break;
- case GL_OUT_OF_MEMORY:
- cout << "Error: GL_OUT_OF_MEMORY" << endl;
- system("pause");
- glfwTerminate();
- break;
- case GL_NO_ERROR:
- cout << "No error reported" << endl;
- break;
- }
- }
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
- {
- if (key >= 0 && key < 1024)
- {
- if (action == GLFW_PRESS)
- keys[key] = true;
- else if (action == GLFW_RELEASE)
- keys[key] = false;
- }
- }
- void mouse_callback(GLFWwindow* window, double xpos, double ypos)
- {
- float x_offset = xpos - lastX;
- float y_offset = lastY - ypos;
- lastX = xpos;
- lastY = ypos;
- float sensitivity = 0.1f;
- x_offset *= sensitivity;
- y_offset *= sensitivity;
- cam.yaw += x_offset;
- cam.pitch += y_offset;
- if (cam.pitch > 89.0f)
- cam.pitch = 89.0f;
- if (cam.pitch < -89.0f)
- cam.pitch = -89.0f;
- cam.update();
- }
- void processKeys()
- {
- if (keys[GLFW_KEY_W])
- {
- cam.cameraPos += cam.speed * cam.cameraFront;
- }
- if (keys[GLFW_KEY_S])
- {
- cam.cameraPos -= cam.speed * cam.cameraFront;
- }
- if (keys[GLFW_KEY_A])
- {
- cam.cameraPos -= glm::normalize(glm::cross(cam.cameraFront, cam.cameraUp)) * cam.speed;
- }
- if (keys[GLFW_KEY_D])
- {
- cam.cameraPos += glm::normalize(glm::cross(cam.cameraFront, cam.cameraUp)) * cam.speed;
- }
- }
- int main()
- {
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- glfwWindowHint(GLFW_SAMPLES, 4);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- GLFWwindow* window = glfwCreateWindow(1024, 768, "opengl tutorial", nullptr, nullptr);
- if (window == nullptr)
- {
- cout << "Failed to create window" << endl;
- glfwTerminate();
- system("pause");
- return -1;
- }
- glfwMakeContextCurrent(window);
- glfwSetCursorPos(window, 1024 / 2, 768 / 2);
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
- glfwSetKeyCallback(window, (GLFWkeyfun) key_callback);
- glfwSetCursorPosCallback(window, (GLFWcursorposfun) mouse_callback);
- cout << "Address of window: " << window << endl;
- glewExperimental = GL_TRUE;
- glewInit();
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_STENCIL_TEST);
- GLuint vao;
- glGenVertexArrays(1, &vao);
- glBindVertexArray(vao);
- GLuint prog = setupProgram("vertex.glsl", "fragment.glsl");
- GLuint prog2 = setupProgram("vertex_2.glsl", "fragment_2.glsl");
- GLuint progOutline = setupProgram("vertex_outline.glsl", "frag_outline.glsl");
- GLuint progQuad = setupProgram("vertex_quad.glsl", "frag_quad.glsl");
- glUseProgram(progQuad);
- GLuint vboQuad;
- glGenBuffers(1, &vboQuad);
- glBindBuffer(GL_ARRAY_BUFFER, vboQuad);
- glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW);
- GLint quadPos = glGetAttribLocation(progQuad, "pos");
- glEnableVertexAttribArray(quadPos);
- glVertexAttribPointer(quadPos, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
- GLint texCoord = glGetAttribLocation(progQuad, "texcoord");
- glEnableVertexAttribArray(texCoord);
- glVertexAttribPointer(texCoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*) (2 * sizeof(float)));
- glUseProgram(prog);
- float rot = 0.0f;
- float camX = 1.0f;
- float camY = 1.2f;
- float camZ = 3.5f;
- glm::mat4 model;
- model = glm::mat4(1.0f) * glm::rotate(model, glm::radians(rot), glm::vec3(0.0f, 1.0f, 0.0f));
- glm::mat4 view;
- view = cam.lookAt();
- glm::mat4 proj;
- proj = glm::perspective(glm::radians(45.0f), 1024.0f / 768.0f, 1.0f, 100.0f);
- object x(cube, 36, 8, "sand.png", prog, glm::vec3(0.0f, 0.0f, 0.0f), view, proj);
- x.init();
- glUseProgram(prog);
- object block(cube, 36, 8, "box-texture.png", prog, glm::vec3(0.0f, 0.0f, 0.0f), view, proj, true, false);
- block.init();
- GLuint fbo;
- glGenFramebuffers(1, &fbo);
- glBindFramebuffer(GL_FRAMEBUFFER, fbo);
- GLuint texFbo;
- glGenTextures(1, &texFbo);
- glBindTexture(GL_TEXTURE_2D, texFbo);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 768, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texFbo, 0);
- GLuint rbo;
- glGenRenderbuffers(1, &rbo);
- glBindRenderbuffer(GL_RENDERBUFFER, rbo);
- glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 1024, 768);
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
- if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
- cout << "Error: Framebuffer not complete" << endl;
- while (!glfwWindowShouldClose(window))
- {
- if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- float currentFrame = glfwGetTime();
- deltaT = currentFrame - lastFrame;
- lastFrame = currentFrame;
- cam.speed = 4.0f * deltaT;
- glfwPollEvents();
- processKeys();
- block.view = cam.lookAt();
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glUseProgram(prog);
- glBindFramebuffer(GL_FRAMEBUFFER, fbo);
- glEnable(GL_DEPTH_TEST);
- block.update();
- block.draw();
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- glUseProgram(progQuad);
- glDisable(GL_DEPTH_TEST);
- glBindBuffer(GL_ARRAY_BUFFER, vboQuad);
- glEnableVertexAttribArray(quadPos);
- glVertexAttribPointer(quadPos, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
- glEnableVertexAttribArray(texCoord);
- glVertexAttribPointer(texCoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*) (2 * sizeof(float)));
- glBindTexture(GL_TEXTURE_2D, texFbo);
- glDrawArrays(GL_TRIANGLES, 0, 6);
- glfwSwapBuffers(window);
- }
- glDeleteVertexArrays(1, &vao);
- glDeleteFramebuffers(1, &fbo);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement