OpenGL學習(七)攝像機

OpenGL學習(七)攝像機

參考官方文檔:https://learnopengl-cn.github.io/01%20Getting%20started/09%20Camera/
OpenGL本身沒有攝像機的概念,我們通過把場景中的所有物體往相反方向移動的方式來模擬攝像機。

首先我們設置攝像機的位置:

glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);

攝像機的方向爲:

glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraDirection = glm::normalize(cameraPos - cameraTarget);

我們還需要一個向量爲一個右向量,它代表攝像機空間的x軸的正方向。我們先定義上向量,然後上向量和攝像機方向叉乘得到

glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f); 
glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraDirection));

然後我們容易得到攝像機的正y軸向量:

glm::vec3 cameraUp = glm::cross(cameraDirection, cameraRight);

然後我們可以創建一個LookAt矩陣

我們可以把我們的攝像機在場景中旋轉:

float radius = 10.0f;
float camX = sin(glfwGetTime()) * radius;
float camZ = cos(glfwGetTime()) * radius;
glm::mat4 view;
view = glm::lookAt(glm::vec3(camX, 0.0, camZ), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0)); 

根據我們上一節的知識我們很容易實現這個效果,如圖
在這裏插入圖片描述

接下來我們要想辦法實現自己控制攝像機的移動。

首先定義攝像機變量:

glm::vec3 cameraPos   = glm::vec3(0.0f, 0.0f,  3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp    = glm::vec3(0.0f, 1.0f,  0.0f);

LookAt函數:

view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);

修改我們之前就有的鍵盤響應函數:

void processInput(GLFWwindow *window)
{
    ...
    float cameraSpeed = 0.05f; // adjust accordingly
    if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
        cameraPos += cameraSpeed * cameraFront;
    if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
        cameraPos -= cameraSpeed * cameraFront;
    if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
        cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
    if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
        cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}

這個效果也不能實現
在這裏插入圖片描述

由於不同的處理器每秒繪製的幀數是不同的,如果我們的移動速度是常量的話,不同的電腦上的移動速度可能就會相差很大。這樣我們需要保證它在所有設備上具有相同的移動速度的話,就需要特別的設定移動速度的數值。我們可以跟蹤一個時間差變量,它爲相鄰兩幀的時間間隔。

float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;

移動速度設定爲

void processInput(GLFWwindow *window)
{
  float cameraSpeed = 2.5f * deltaTime;
  ...
}

這個也不難做到
在這裏插入圖片描述

但大多數時候我們更希望通過鼠標來控制視角。接下來我們就來實現這個效果。

其中涉及一些相關的數學知識。不知道也問題不大。

我們首先需要GLFW監聽鼠標移動事件:

//使光標不顯示同時不離開窗口
	glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
	//讓GLFW監聽鼠標移動事件
	//鼠標移動就會調用綁定的函數
	glfwSetCursorPosCallback(window, mouse_callback);

相關的響應函數我們可以放在main函數前面:

float lastX = 400, lastY = 300;
bool firstMouse = true;
float yaw = 0.0f, pitch = 0.0f;
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
	if (firstMouse)
	{
		lastX = xpos;
		lastY = ypos;
		firstMouse = false;
	}

	float xoffset = xpos - lastX;
	float yoffset = lastY - ypos;
	lastX = xpos;
	lastY = ypos;

	float sensitivity = 0.05;
	xoffset *= sensitivity;
	yoffset *= sensitivity;

	yaw += xoffset;
	pitch += yoffset;

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

在這裏插入圖片描述

實際效果是鼠標的移動會改變窗口中物體的視角。

但這樣還差一個縮放。常見的操作是利用鼠標滾輪來改變視野。

首先我們要註冊鼠標滾輪的回調函數:

glfwSetScrollCallback(window, scroll_callback);

回調函數定義爲:

void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
  if(fov >= 1.0f && fov <= 45.0f)
    fov -= yoffset;
  if(fov <= 1.0f)
    fov = 1.0f;
  if(fov >= 45.0f)
    fov = 45.0f;
}

我們需要修改投影矩陣的定義:

projection = glm::perspective(glm::radians(fov), 800.0f / 600.0f, 0.1f, 100.0f);

這樣就行了。
在這裏插入圖片描述

最後,我們來新建一個Camera類,就像我們前面的Shader類那樣,創建一個Camera可以讓我們的代碼更加的簡潔。

camera.h

#ifndef CAMERA_H
#define CAMERA_H
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

#include <vector>
enum Camera_Movement {
	FORWARD,BACKWARD,LEFT,RIGHT
};
const float YAW = -90.0f;
const float PITCH = 0.0f;
const float SPEED = 2.5f;
const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f;
class Camera {
public:
	glm::vec3 Position;
	glm::vec3 Front;
	glm::vec3 Up;
	glm::vec3 Right;
	glm::vec3 WorldUp;
	float Yaw;
	float Pitch;
	float MovementSpeed;
	float MouseSensitivity;
	float Zoom;
    Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
    {
        Position = position;
        WorldUp = up;
        Yaw = yaw;
        Pitch = pitch;
        updateCameraVectors();
    }
    // constructor with scalar values
    Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
    {
        Position = glm::vec3(posX, posY, posZ);
        WorldUp = glm::vec3(upX, upY, upZ);
        Yaw = yaw;
        Pitch = pitch;
        updateCameraVectors();
    }

    // returns the view matrix calculated using Euler Angles and the LookAt Matrix
    glm::mat4 GetViewMatrix()
    {
        return glm::lookAt(Position, Position + Front, Up);
    }

    // processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
    void ProcessKeyboard(Camera_Movement direction, float deltaTime)
    {
        float velocity = MovementSpeed * deltaTime;
        if (direction == FORWARD)
            Position += Front * velocity;
        if (direction == BACKWARD)
            Position -= Front * velocity;
        if (direction == LEFT)
            Position -= Right * velocity;
        if (direction == RIGHT)
            Position += Right * velocity;
    }

    // processes input received from a mouse input system. Expects the offset value in both the x and y direction.
    void ProcessMouseMovement(float xoffset, float 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;
        }

        // update Front, Right and Up Vectors using the updated Euler angles
        updateCameraVectors();
    }

    // processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
    void ProcessMouseScroll(float yoffset)
    {
        Zoom -= (float)yoffset;
        if (Zoom < 1.0f)
            Zoom = 1.0f;
        if (Zoom > 45.0f)
            Zoom = 45.0f;
    }

private:
    // calculates the front vector from the Camera's (updated) Euler Angles
    void updateCameraVectors()
    {
        // calculate the new Front vector
        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));
        Front = glm::normalize(front);
        // also re-calculate the Right and Up vector
        Right = glm::normalize(glm::cross(Front, 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, Front));
    }
};
#endif // !CAMERA_H

main.cpp

#include<iostream>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include"shader.h"
#include"stb_image.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include"camera.h"

float transparency = 0.0f;
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
float deltaTime = 0.0f;
float lastTime = 0.0f;

float lastX = 400, lastY = 300;
bool firstMouse = true;
float yaw = 0.0f, pitch = 0.0f;
float fov = 0.0f;
void processInput(GLFWwindow* window);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
Camera camera = Camera();
int main() {
	//initialize
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
	GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);

	
	if (window == NULL) {
		std::cout << "Failed to create GLFW window" << std::endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
		std::cout << "Failed to initialize GlAD" << std::endl;
		return -1;
	}
	//Shader類
	Shader ourShader("shader.vs", "shader.fs");
	
	float vertices[] = {
		//     ---- 位置 ----         - 紋理座標 -
			 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,      0.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,      0.0f, 0.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,      0.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,      0.0f, 0.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,      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,      1.0f, 0.0f,    // 左上
			-0.5f,  -0.5f, 0.5f,      1.0f, 1.0f,   // 右上
			0.5f, -0.5f,0.5f,      0.0f, 1.0f,   // 左下
	};
	unsigned int indices[] = {
		0,1,3,
		1,2,3
	};
	/*
	glm::mat4 trans = glm::mat4(1.0f);
	trans = glm::rotate(trans, glm::radians(90.0f), glm::vec3(0.0, 0.0, 1.0));
	trans = glm::scale(trans, glm::vec3(0.5, 0.5, 0.5));
	*/
	//頂點數組對象VAO,創建
	unsigned int VBO, VAO;
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	//索引緩衝對象
	unsigned int EBO;
	glGenBuffers(1, &EBO);
	//
	glBindVertexArray(VAO);
	//把頂點數組複製到緩衝中供OpenGL使用
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
	//位置屬性
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);
	//顏色屬性
	//glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
	//glEnableVertexAttribArray(1);
	//紋理座標
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
	glEnableVertexAttribArray(1);
	//加載並創建紋理
	//第一個
	unsigned int texture1;
	glGenTextures(1, &texture1);
	glBindTexture(GL_TEXTURE_2D, texture1);
	//環繞方式
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	// set texture wrapping to GL_REPEAT (default wrapping method)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	// set texture filtering parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	int width, height, nrChannels;
	//下面函數的第一個參數輸入本地的圖片文件的路徑就行了
	stbi_set_flip_vertically_on_load(true);
	unsigned char* data = stbi_load("C:\\Users\\xhh\\Downloads\\article_phy\\container.jpg", &width, &height, &nrChannels,0);
	if (data) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else {
		std::cout << "Failed to load texture1" << std::endl;
	}
	stbi_image_free(data);
	
	//第二個
	unsigned int texture2;
	glGenTextures(1, &texture2);
	glBindTexture(GL_TEXTURE_2D, texture2);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	// set texture wrapping to GL_REPEAT (default wrapping method)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	// set texture filtering parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	
	//下面函數的第一個參數輸入本地的圖片文件的路徑就行了
	data = stbi_load("C:\\Users\\xhh\\Downloads\\article_phy\\awesomeface.png", &width, &height, &nrChannels, 0);
	if (data) {
		//注意這個png文件具有透明度屬性,因此有一個alpha信道,要告訴OpenGL數據類型爲GL_RGBA
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else {
		std::cout << "Failed to load texture2" << std::endl;
	}
	stbi_image_free(data);

	ourShader.use(); // don't forget to activate/use the shader before setting uniforms!
	// either set it manually like so:
	glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0);
	// or set it via the texture class
	ourShader.setInt("texture2", 1);

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);

	
	const unsigned int screenWidth = 800, screenHeight = 600;
	glViewport(0, 0, screenWidth, screenHeight);
	void framebuffer_size_callback(GLFWwindow * window, int width, int height);
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

	//使光標不顯示同時不離開窗口
	glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
	//讓GLFW監聽鼠標移動事件
	//鼠標移動就會調用綁定的函數
	glfwSetCursorPosCallback(window, mouse_callback);
	//鼠標滾輪的回調函數
	glfwSetScrollCallback(window, scroll_callback);
	//
	glm::vec3 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)
	};

	while (!glfwWindowShouldClose(window))
	{
		processInput(window);

		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		//每次迭代之前清楚深度緩衝和顏色緩衝
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		//開啓深度測試
		glEnable(GL_DEPTH_TEST);

		glm::mat4 trans = glm::mat4(1.0f);
		
		trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
		trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f));
		//綁定紋理
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, texture1);
		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_2D, texture2);
		//激活着色器程序對象
		ourShader.use();
		//
		glm::mat4 model = glm::mat4(1.0f);
		model = glm::rotate(model,(float)glfwGetTime()* glm::radians(55.0f), glm::vec3(0.5f, 1.0f, 0.0f));
		glm::mat4 view = glm::mat4(1.0f);

		view = glm::lookAt(camera.Position, camera.Position + camera.Front, camera.Up);
		//view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
		//view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
		glm::mat4 projection = glm::mat4(1.0f);
		projection = glm::perspective(glm::radians(camera.Zoom), (float)screenWidth / (float)screenHeight, 0.1f, 100.0f);
		int modelLoc = glGetUniformLocation(ourShader.ID, "model");
		glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
		int viewLoc = glGetUniformLocation(ourShader.ID, "view");
		glUniformMatrix4fv(viewLoc, 1, GL_FALSE, &view[0][0]);
		int projectionLoc = glGetUniformLocation(ourShader.ID, "projection");
		glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
		//
		//ourShader.setFloat("delta", 0.5f);
		//glDrawArrays(GL_TRIANGLES, 0, 3);
		ourShader.setFloat("transparency", transparency);
		glBindVertexArray(VAO);

		for (unsigned int i = 0; i < 10; i++)
		{
			glm::mat4 model=glm::mat4(1.0f);
			model = glm::translate(model, cubePositions[i]);
			float angle = 20.0f * i;
			model = glm::rotate(model,(float)glfwGetTime()* glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
			ourShader.setMat4("model", model);

			glDrawArrays(GL_TRIANGLES, 0, 36);
		}
		glfwSwapBuffers(window);//交換顏色緩衝,它在每一次迭代中被用來繪製,並作爲輸出顯示在屏幕上
		glfwPollEvents();//檢測有沒有觸發什麼事件、更新窗口狀態,並調用對應的回調函數
	}
	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);

	glDeleteBuffers(1, &EBO);

	ourShader.del();
	glfwTerminate();
	return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
	glViewport(0, 0, width, height);
}
void processInput(GLFWwindow* window) {
	float currentFrame = glfwGetTime();
	deltaTime = currentFrame - lastTime;
	lastTime = currentFrame;
	float cameraSpeed = 2.5f * deltaTime; // adjust accordingly
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
		glfwSetWindowShouldClose(window, true);
	else if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
		if (transparency < 1.0f)
			transparency += 0.001f;
		//std::cout << transparency << std::endl;
	}
	else if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
		if (transparency > 0.0f)
			transparency -= 0.001f;
		//std::cout << transparency << std::endl;
	}

	else if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
		camera.ProcessKeyboard(FORWARD, deltaTime);
	if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
		camera.ProcessKeyboard(BACKWARD, deltaTime);
	if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
		camera.ProcessKeyboard(LEFT, deltaTime);
	if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
		camera.ProcessKeyboard(RIGHT, deltaTime);
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
	if (firstMouse)
	{
		lastX = xpos;
		lastY = ypos;
		firstMouse = false;
	}

	float xoffset = xpos - lastX;
	float yoffset = lastY - ypos;
	lastX = xpos;
	lastY = ypos;

	camera.ProcessMouseMovement(xoffset, yoffset);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
	camera.ProcessMouseScroll(yoffset);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章