Dreptunghi
Dreptunghi
Dreptunghi
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");
// 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);
// 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,
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);
}
#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
// 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);