dreptunghi

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 9

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;

int main(int argc, char** argv)


{
std::string strFullExeFileName = argv[0];
std::string strExePath;
const size_t last_slash_idx = strFullExeFileName.rfind('\\');
if (std::string::npos != last_slash_idx) {
strExePath = strFullExeFileName.substr(0, last_slash_idx);
}

// glfw: initialize and configure


glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

// glfw window creation


GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Lab8 - Maparea
umbrelor", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetKeyCallback(window, key_callback);

// tell GLFW to capture our mouse


glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

glewInit();

// Create camera
pCamera = new Camera(SCR_WIDTH, SCR_HEIGHT, glm::vec3(0.0, 1.0, 3.0));

// configure global opengl state


// -----------------------------
glEnable(GL_DEPTH_TEST);

// build and compile shaders


// -------------------------
Shader shadowMappingShader("ShadowMapping.vs", "ShadowMapping.fs");
Shader shadowMappingDepthShader("ShadowMappingDepth.vs",
"ShadowMappingDepth.fs");

// load textures
// -------------
unsigned int floorTexture = CreateTexture(strExePath + "\\parchet.jpg");
// configure depth map FBO
// -----------------------
const unsigned int SHADOW_WIDTH = 4096, SHADOW_HEIGHT = 4096;
unsigned int depthMapFBO;
glGenFramebuffers(1, &depthMapFBO);
// create depth texture
unsigned int depthMap;
glGenTextures(1, &depthMap);
glBindTexture(GL_TEXTURE_2D, depthMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT,
0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
float borderColor[] = { 1.0, 1.0, 1.0, 1.0 };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
// attach depth texture as FBO's depth buffer
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
depthMap, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

// shader configuration
// --------------------
shadowMappingShader.Use();
shadowMappingShader.SetInt("diffuseTexture", 0);
shadowMappingShader.SetInt("shadowMap", 1);

// lighting info
// -------------
glm::vec3 lightPos(0.0f, 3.0f, 0.0f);

float fIncrement = 0.01f;

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);

// 1. render depth of scene to texture (from light's perspective)


glm::mat4 lightProjection, lightView;
glm::mat4 lightSpaceMatrix;
float near_plane = 1.0f, far_plane = 7.5f;
lightProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane,
far_plane);
lightView = glm::lookAt(lightPos, glm::vec3(0.0f), glm::vec3(0.0, 1.0,
0.0));
lightSpaceMatrix = lightProjection * lightView;

// render scene from light's point of view


shadowMappingDepthShader.Use();
shadowMappingDepthShader.SetMat4("lightSpaceMatrix", lightSpaceMatrix);

glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);


glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, floorTexture);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
renderScene(shadowMappingDepthShader);
glCullFace(GL_BACK);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

// reset viewport
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glm::mat4 model = glm::mat4(1.0f);


//if (rotateLight) {
float lightX = sin(fIncrement) * radius; // Calculăm poziția luminii pe axa
X în funcție de timp
float lightZ = cos(fIncrement) * radius; // Calculăm poziția luminii pe axa
Z în funcție de timp
lightPos = glm::vec3(lightX, lightPos.y, lightZ);
//}
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f)); // Scalați lumina pentru a o
face mai vizibilă

// 2. render scene as normal using the generated depth/shadow map


glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shadowMappingShader.Use();
glm::mat4 projection = pCamera->GetProjectionMatrix();
glm::mat4 view = pCamera->GetViewMatrix();
shadowMappingShader.SetMat4("projection", projection);
shadowMappingShader.SetMat4("view", view);
// set light uniforms
shadowMappingShader.SetVec3("viewPos", pCamera->GetPosition());
shadowMappingShader.SetVec3("lightPos", lightPos);
shadowMappingShader.SetMat4("lightSpaceMatrix", lightSpaceMatrix);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, floorTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, depthMap);
glDisable(GL_CULL_FACE);
renderScene(shadowMappingShader);

// glfw: swap buffers and poll IO events (keys pressed/released, mouse


moved etc.)
glfwSwapBuffers(window);
glfwPollEvents();
}

// optional: de-allocate all resources once they've outlived their purpose:


delete pCamera;

glfwTerminate();
return 0;
}

// renders the 3D scene


// --------------------
void renderScene(const Shader& shader)
{
// floor
glm::mat4 model = glm::mat4(1.0f);
shader.SetMat4("model", model);

renderFloor();

// cube

static float x = 0.0f;


static float y = 0.0f;
static float z = 0.0f;

static float xRotate = 0.0f;


static float yRotate = 0.0f;
static float zRotate = 0.0f;

static float scale = 1.0f;

auto worldTransf = glm::mat4(1.0);

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;
}

worldTransf = translate(worldTransf, glm::vec3(x, y, z));


worldTransf = glm::rotate(worldTransf, glm::radians(xRotate * 100),
glm::vec3(1, 0, 0));
worldTransf = glm::rotate(worldTransf, glm::radians(yRotate * 100),
glm::vec3(0, 1, 0));
worldTransf = glm::rotate(worldTransf, glm::radians(zRotate * 100),
glm::vec3(0, 0, 1));
worldTransf = glm::scale(worldTransf, glm::vec3(scale, scale, scale));

shader.SetMat4("model", worldTransf);
renderCube();
}

unsigned int planeVAO = 0;


void renderFloor()
{
unsigned int planeVBO;

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,
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, 25.0f,
25.0f, -0.5f, -25.0f, 0.0f, 1.0f, 0.0f, 25.0f, 25.0f
};
// plane VAO
glGenVertexArrays(1, &planeVAO);
glGenBuffers(1, &planeVBO);
glBindVertexArray(planeVAO);
glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), planeVertices,
GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
(void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)
(3 * sizeof(float)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)
(6 * sizeof(float)));
glBindVertexArray(0);
}

glBindVertexArray(planeVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
}

// renderCube() renders a 1x1 3D cube in NDC.


// -------------------------------------------------
unsigned int cubeVAO = 0;
unsigned int cubeVBO = 0;

#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);

if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)


pCamera->ProcessKeyboard(FORWARD, (float)deltaTime);
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
pCamera->ProcessKeyboard(BACKWARD, (float)deltaTime);
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
pCamera->ProcessKeyboard(LEFT, (float)deltaTime);
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
pCamera->ProcessKeyboard(RIGHT, (float)deltaTime);
if (glfwGetKey(window, GLFW_KEY_PAGE_UP) == GLFW_PRESS)
pCamera->ProcessKeyboard(UP, (float)deltaTime);
if (glfwGetKey(window, GLFW_KEY_PAGE_DOWN) == GLFW_PRESS)
pCamera->ProcessKeyboard(DOWN, (float)deltaTime);

if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {


int width, height;
glfwGetWindowSize(window, &width, &height);
pCamera->Reset(width, height);

if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS)


rotateLight = !rotateLight; // Pornim rotația luminii la apăsarea tastei
"L"

if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)


{
cubeMovement = Up;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
cubeMovement = Down;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
cubeMovement = Left;
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
cubeMovement = Right;
}
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
{
cubeMovement = Forward;
}
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
{
cubeMovement = Backward;
}
if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS)
{
cubeMovement = XRotate;
}
if (glfwGetKey(window, GLFW_KEY_Y) == GLFW_PRESS)
{
cubeMovement = YRotate;
}
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS)
{
cubeMovement = ZRotate;
}

You might also like