Application.cpp
#include "Application.h"
// In TestApp.cpp
extern void mouse_callback(GLFWwindow * window, double xpos, double ypos);
extern void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
Application::Application()
{
running = false;
window = new Window(800 , 500 , "SUA_APP");
testApp = new TestApp();
// For Keyboard
glfwSetKeyCallback(this->window->GetGLFWwindow() , TestApp::controller );
// For Mouse
glfwSetCursorPosCallback(this->window->GetGLFWwindow(), mouse_callback);
// For scroll
glfwSetScrollCallback(this->window->GetGLFWwindow(), scroll_callback);
// For make mouse invisible
glfwSetInputMode(this->window->GetGLFWwindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
running = true;
}
bool Application::Run()
{
running = !glfwWindowShouldClose(this->window->GetGLFWwindow() );
this->testApp->Update();
this->window->Update();
this->SwapBuffers();
return running;
}
void Application::SwapBuffers()
{
glfwSwapBuffers(this->window->GetGLFWwindow() );
}
Application::~Application()
{
delete testApp;
delete window;
}
***********************************************************************
TestApp.h
#pragma once
#include <iostream>
#include <GL/glew.h>
#include <glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Shader.h"
#include "Texture.h"
#include "Camera.h"
class TestApp
{
public:
TestApp();
void Update();
static void controller(GLFWwindow* window, int key, int scancode, int action, int mode);
~TestApp();
GLfloat deltaTime = 0.0f; // Time between current frame and last frame
GLfloat lastFrame = 0.0f; // Time of last frame
private:
GLuint VBO, VAO, EBO;
Shader* ourShader;
Texture* texture;
Texture* texture2;
// cubes 10 positions
glm::vec3 cubePositions[10];
};
******************************************************************************
TestApp.cpp
#include "TestApp.h"
#include <SOIL.h>
bool keys[1024];
Camera* camera = new Camera();
GLfloat lastX = 800 / 2.0;
GLfloat lastY = 500 / 2.0;
bool firstMouse = true;
void mouse_callback(GLFWwindow * window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
GLfloat xoffset = xpos - lastX;
GLfloat yoffset = lastY - ypos; // Reversed since y-coordinates go from bottom to left
lastX = xpos;
lastY = ypos;
camera->ProcessMouseMovement(xoffset, yoffset , true);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera->ProcessMouseScroll(yoffset);
std::cout << yoffset << std::endl;
}
TestApp::TestApp()
: cubePositions{
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f),
glm::vec3(1.5f, 2.0f, -2.5f),
glm::vec3(1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
}
{
glEnable(GL_DEPTH_TEST);
this->ourShader = new Shader("shader.vert", "shader.frag");
GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Vertices
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// Texture
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind
glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO
this->texture = new Texture("awesome_face.jpg");
this->texture2 = new Texture("wall.jpg");
}
void TestApp::Update()
{
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// Camera Movement
camera->DoMovement(keys , 5.0f * deltaTime);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
this->ourShader->Use();
// ******* TEXTURE *******
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->texture->getTextureID() );
glUniform1i(glGetUniformLocation(this->ourShader->Program, "ourTexture1"), 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, this->texture2->getTextureID());
glUniform1i(glGetUniformLocation(this->ourShader->Program, "ourTexture2"), 1);
float timeValue = glfwGetTime();
GLfloat value = (sin(timeValue) / 2) + 0.5;
glUniform1f(glGetUniformLocation(ourShader->Program, "value"), value);
// ************ VALUE OF MODEL , VIEW , PROJECTION ******************
camera->SetView();
camera->SetProjection();
// ************ LOCATION OF MODEL , VIEW , PROJECTION IN SHADER PROGRAM ***************
// ************ AND SET VALUE OF IT TO SHADER PROGRAM *******************
camera->GetModelViewProjectionLocation(this->ourShader);
// ****************** NOW DRAW IT *************************
glBindVertexArray(VAO);
//glDrawArrays(GL_TRIANGLES, 0, 36);
for (GLuint i = 0; i < 10; i++)
{
glm::mat4 model;
model = glm::translate(model, cubePositions[i]);
GLfloat angle = 20.0f * i+20;
model = glm::rotate(model, (GLfloat)glfwGetTime() * glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(camera->modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glBindVertexArray(0);
}
void TestApp::controller(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;
}
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
TestApp::~TestApp()
{
delete this->ourShader;
delete this->texture;
delete this->texture2;
delete camera;
// Properly de-allocate all resources once they've outlived their purpose
glDeleteVertexArrays(1, &this->VAO);
glDeleteBuffers(1, &this->VBO);
// Terminate GLFW, clearing any resources allocated by GLFW.
}
****************************************************************************
Camera.h
#pragma once
#include <iostream>
#include <GL/glew.h>
#include <glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Shader.h"
class Camera
{
public:
Camera();
~Camera();
glm::vec3 cameraPos;
glm::vec3 cameraFront;
glm::vec3 cameraUp;
glm::mat4 model;
glm::mat4 view;
glm::mat4 projection;
GLuint modelLoc, viewLoc, projectionLoc;
void ProcessMouseMovement(GLfloat xoffset , GLfloat yoffset , GLboolean constrainPitch );
void ProcessMouseScroll(GLfloat yoffset);
void DoMovement( bool* keys, GLfloat speed );
void GetModelViewProjectionLocation(Shader* shader);
void SetCameraPosition(float x , float y , float z );
void SetCameraFront(float x, float y, float z);
void SetCameraUp(float x, float y, float z);
void SetView();
void SetProjection();
private:
GLfloat Zoom = 45.0f;
};
*************************************************************************
Camera.cpp
#include "Camera.h"
glm::vec3 front;
GLfloat yaw = -90.0f; // Yaw is initialized to -90.0 degrees since a yaw of 0.0 results in a direction vector pointing to the right (due to how Eular angles work) so we initially rotate a bit to the left.
GLfloat pitch = 0.0f;
GLfloat MouseSensitivity = 0.25f;
Camera::Camera()
{
this->cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
this->cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
this->cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
}
void Camera::GetModelViewProjectionLocation(Shader* shader)
{
this->modelLoc = glGetUniformLocation(shader->Program, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
this->viewLoc = glGetUniformLocation(shader->Program, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
this->projectionLoc = glGetUniformLocation(shader->Program, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
}
void Camera::SetCameraPosition(float x = 0.0f, float y = 0.0f,float z = 3.0f)
{
this->cameraPos = glm::vec3(x, y, z);
}
void Camera::SetCameraFront(float x = 0.0f, float y = 0.0f, float z = -1.0f)
{
this->cameraFront = glm::vec3(x, y, z);
}
void Camera::SetCameraUp(float x = 0.0f, float y = 1.0f, float z = 0.0f)
{
this->cameraUp = glm::vec3(x, y, z);
}
void Camera::SetView()
{
this->view = glm::lookAt(this->cameraPos, this->cameraPos + this->cameraFront, this->cameraUp);
}
void Camera::SetProjection()
{
this->projection = glm::perspective(glm::radians(45.0f), (GLfloat)800 / (GLfloat)500, 0.1f, 100.0f);
}
void Camera::ProcessMouseScroll(GLfloat yoffset)
{
if (this->Zoom >= 1.0f && this->Zoom <= 45.0f)
this->Zoom -= yoffset;
if (this->Zoom <= 1.0f)
this->Zoom = 1.0f;
if (this->Zoom >= 45.0f)
this->Zoom = 45.0f;
}
void Camera::ProcessMouseMovement(GLfloat xoffset, GLfloat yoffset , GLboolean constrainPitch = true)
{
xoffset *= MouseSensitivity;
yoffset *= MouseSensitivity;
yaw += xoffset;
pitch += yoffset;
// Make sure that when pitch is out of bounds, screen doesn't get flipped
if (constrainPitch)
{
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
}
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
void Camera::DoMovement(bool* keys , GLfloat speed = 0.05f)
{
if (keys[GLFW_KEY_W])
this->cameraPos += speed * this->cameraFront;
if (keys[GLFW_KEY_S])
this->cameraPos -= speed * this->cameraFront;
if (keys[GLFW_KEY_A])
this->cameraPos -= glm::normalize(glm::cross(this->cameraFront, this->cameraUp)) * speed;
if (keys[GLFW_KEY_D])
this->cameraPos += glm::normalize(glm::cross(this->cameraFront, this->cameraUp)) * speed;
}
Camera::~Camera()
{
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น