Professional Documents
Culture Documents
Paralel o Gram
Paralel o Gram
Paralel o Gram
h>
#include <stdio.h>
#include <math.h>
#include <glew.h>
#define GLM_FORCE_CTOR_INIT
#include <GLM.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>
#include <glfw3.h>
#include <iostream>
#include <fstream>
#include <sstream>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
#pragma comment (lib, "glfw3dll.lib")
#pragma comment (lib, "glew32.lib")
#pragma comment (lib, "OpenGL32.lib")
// Light position
glm::vec3 lightPos(2.0f, 1.0f, 2.0f);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
bool rotateLight = false;
float lightAngle = 0.0f;
float lightRadius = 2.0f;
enum ECameraMovementType
{
UNKNOWN,
FORWARD,
BACKWARD,
LEFT,
RIGHT,
UP,
DOWN
};
enum EMovementType
{
None,
Up,
Down,
Left,
Right,
Forward,
Backward,
XRotate,
YRotate,
ZRotate,
Grow,
Shrink
};
class Camera
{
private:
// Default camera values
const float zNEAR = 0.1f;
const float zFAR = 500.f;
const float YAW = -90.0f;
const float PITCH = 0.0f;
const float FOV = 45.0f;
glm::vec3 startPosition;
public:
Camera(const int width, const int height, const glm::vec3& position)
{
startPosition = position;
Set(width, height, position);
}
void Set(const int width, const int height, const glm::vec3& position)
{
this->isPerspective = true;
this->yaw = YAW;
this->pitch = PITCH;
this->FoVy = FOV;
this->width = width;
this->height = height;
this->zNear = zNEAR;
this->zFar = zFAR;
UpdateCameraVectors();
}
ProcessMouseMovement(xChange, yChange);
}
private:
void ProcessMouseMovement(float xOffset, float yOffset, bool constrainPitch =
true)
{
yaw += xOffset;
pitch += yOffset;
void UpdateCameraVectors()
{
// Calculate the new forward vector
this->forward.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
this->forward.y = sin(glm::radians(pitch));
this->forward.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
this->forward = glm::normalize(this->forward);
// Also re-calculate the Right and Up vector
right = glm::normalize(glm::cross(forward, worldUp)); // Normalize the
vectors, because their length gets closer to 0 the more you look up or down which
results in slower movement.
up = glm::normalize(glm::cross(right, forward));
}
protected:
const float cameraSpeedFactor = 2.5f;
const float mouseSensitivity = 0.1f;
// Perspective properties
float zNear;
float zFar;
float FoVy;
int width;
int height;
bool isPerspective;
glm::vec3 position;
glm::vec3 forward;
glm::vec3 right;
glm::vec3 up;
glm::vec3 worldUp;
// Euler Angles
float yaw;
float pitch;
class Shader
{
public:
// constructor generates the shaderStencilTesting on the fly
// ------------------------------------------------------------------------
Shader(const char* vertexPath, const char* fragmentPath)
{
Init(vertexPath, fragmentPath);
}
~Shader()
{
glDeleteProgram(ID);
}
// MVP
unsigned int loc_model_matrix;
unsigned int loc_view_matrix;
unsigned int loc_projection_matrix;
private:
void Init(const char* vertexPath, const char* fragmentPath)
{
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensure ifstream objects can throw exceptions:
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
// open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure e) {
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
}
const char* vShaderCode = vertexCode.c_str();
const char* fShaderCode = fragmentCode.c_str();
// 2. compile shaders
unsigned int vertex, fragment;
// vertex shaderStencilTesting
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
CheckCompileErrors(vertex, "VERTEX");
// fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
CheckCompileErrors(fragment, "FRAGMENT");
// shaderStencilTesting Program
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
CheckCompileErrors(ID, "PROGRAM");
// 3. delete the shaders as they're linked into our program now and no
longer necessery
glDeleteShader(vertex);
glDeleteShader(fragment);
}
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
return textureId;
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
{
height += 0.1f;
}
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
{
height /= 2;
}
if (glfwGetKey(window, GLFW_KEY_EQUAL) == GLFW_PRESS)
{
cubeMovement = Grow;
}
if (glfwGetKey(window, GLFW_KEY_MINUS) == GLFW_PRESS)
{
cubeMovement = Shrink;
}
if (glfwGetKey(window, GLFW_KEY_INSERT) == GLFW_PRESS)
{
radius += 0.1f;
}
if (glfwGetKey(window, GLFW_KEY_DELETE) == GLFW_PRESS)
{
radius -= 0.1f;
}
}
void renderScene(const Shader& shader);
void renderCube();
void renderFloor();
// timing
double deltaTime = 0.0f; // time between current frame and last frame
double lastFrame = 0.0f;
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetKeyCallback(window, key_callback);
glewInit();
// Create camera
pCamera = new Camera(SCR_WIDTH, SCR_HEIGHT, glm::vec3(0.0, 1.0, 3.0));
// load textures
// -------------
unsigned int floorTexture = CreateTexture(strExePath + "\\parchet.jpg");
// shader configuration
// --------------------
shadowMappingShader.Use();
shadowMappingShader.SetInt("diffuseTexture", 0);
shadowMappingShader.SetInt("shadowMap", 1);
// lighting info
// -------------
glm::vec3 lightPos(0.0f, 3.0f, 0.0f);
glEnable(GL_CULL_FACE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
fIncrement += 0.01f;
// per-frame time logic
// --------------------
float currentFrame = (float)glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// reset viewport
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwTerminate();
return 0;
}
renderFloor();
// cube
switch (cubeMovement)
{
case None:
break;
case Up:
y += 0.01f;
cubeMovement = None;
break;
case Down:
y -= 0.01f;
cubeMovement = None;
break;
case Left:
x -= 0.01f;
cubeMovement = None;
break;
case Right:
x += 0.01f;
cubeMovement = None;
break;
case Forward:
z += 0.01f;
cubeMovement = None;
break;
case Backward:
z -= 0.01f;
cubeMovement = None;
break;
case XRotate:
xRotate += 0.01f;
cubeMovement = None;
break;
case YRotate:
yRotate += 0.01f;
cubeMovement = None;
break;
case ZRotate:
zRotate += 0.01f;
cubeMovement = None;
break;
case Grow:
scale += 0.1f;
cubeMovement = None;
break;
case Shrink:
scale -= 0.1f;
cubeMovement = None;
break;
}
shader.SetMat4("model", worldTransf);
renderCube();
}
if (planeVAO == 0) {
// set up vertex data (and buffer(s)) and configure vertex attributes
float planeVertices[] = {
// positions // normals // texcoords
25.0f, -0.5f, 25.0f, 0.0f, 1.0f, 0.0f, 25.0f, 0.0f,
-25.0f, -0.5f, 25.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-25.0f, -0.5f, -25.0f, 0.0f, 1.0f, 0.0f, 0.0f, 25.0f,
glBindVertexArray(planeVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
void renderCube()
{
constexpr float widthBottom = 0.5f;
constexpr float widthTop = 0.5f;
constexpr float length = 0.7f;
constexpr float offsetX = 0.2f; // Offset to create the parallelogram effect
const float vertices[] = {
// bottom face
-widthBottom / 2, 0.0f, -length / 2, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
widthBottom / 2, 0.0f, -length / 2, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
widthBottom / 2, 0.0f, length / 2, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
widthBottom / 2, 0.0f, length / 2, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-widthBottom / 2, 0.0f, length / 2, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
-widthBottom / 2, 0.0f, -length / 2, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
// top face
-widthTop / 2 + offsetX, height, -length / 2, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.0f,
widthTop / 2 + offsetX, height, -length / 2, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.0f,
widthTop / 2 + offsetX, height, length / 2, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
1.0f,
widthTop / 2 + offsetX, height, length / 2, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
1.0f,
-widthTop / 2 + offsetX, height, length / 2, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f,
-widthTop / 2 + offsetX, height, -length / 2, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.0f,
// front face
-widthBottom / 2, 0.0f, length / 2, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
widthBottom / 2, 0.0f, length / 2, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
widthTop / 2 + offsetX, height, length / 2, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 1.0f,
widthTop / 2 + offsetX, height, length / 2, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 1.0f,
-widthTop / 2 + offsetX, height, length / 2, 0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f,
-widthBottom / 2, 0.0f, length / 2, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
// back face
-widthBottom / 2, 0.0f, -length / 2, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.0f,
widthBottom / 2, 0.0f, -length / 2, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f,
widthTop / 2 + offsetX, height, -length / 2, 0.0f, 0.0f, -1.0f, 0.0f,
0.0f, 1.0f,
widthTop / 2 + offsetX, height, -length / 2, 0.0f, 0.0f, -1.0f, 0.0f,
0.0f, 1.0f,
-widthTop / 2 + offsetX, height, -length / 2, 0.0f, 0.0f, -1.0f, 1.0f,
1.0f, 0.0f,
-widthBottom / 2, 0.0f, -length / 2, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.0f,
// left face
-widthBottom / 2, 0.0f, -length / 2, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-widthBottom / 2, 0.0f, length / 2, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-widthTop / 2 + offsetX, height, length / 2, -1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f,
-widthTop / 2 + offsetX, height, length / 2, -1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f,
-widthTop / 2 + offsetX, height, -length / 2, -1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f,
-widthBottom / 2, 0.0f, -length / 2, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
// right face
widthBottom / 2, 0.0f, -length / 2, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
widthBottom / 2, 0.0f, length / 2, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
widthTop / 2 + offsetX, height, length / 2, 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f,
widthTop / 2 + offsetX, height, length / 2, 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f,
widthTop / 2 + offsetX, height, -length / 2, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f,
widthBottom / 2, 0.0f, -length / 2, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f
};
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &cubeVBO);
// fill buffer
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// link vertex attributes
glBindVertexArray(cubeVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)(3 *
sizeof(float)));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void*)(6 *
sizeof(float)));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// render Cube
glBindVertexArray(cubeVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
// process all input: query GLFW whether relevant keys are pressed/released this
frame and react accordingly
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
// glfw: whenever the window size changed (by OS or user resize) this callback
function executes
//
-----------------------------------------------------------------------------------
----------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width
and
// height will be significantly larger than specified on retina displays.
pCamera->Reshape(width, height);
}