参考:https://blog.csdn.net/ynsenyu/article/details/39423493
#include <iostream>
#include <cmath>
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "utils/Shader.h"
//图片工具库
#include "utils/stb_image.h"
//矩阵工具库
#include "utils/glm/glm.hpp"
#include "utils/glm/gtc/matrix_transform.hpp"
#include "utils/glm/gtc/type_ptr.hpp"
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void processInput(GLFWwindow *window);
void loadTexture(unsigned int *texture, const char *path);
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
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 lastFrameTime = 0.0f;//上一帧的时间
float lastX = 300;
float lastY = 300;
float yaw;
float pitch;
float fov = 45.0f;//视野比例
/**
* 顶点数组对象:Vertex Array Object,VAO
* 顶点缓冲对象:Vertex Buffer Object,VBO
* 索引缓冲对象:Element Buffer Object,EBO或Index Buffer Object,IBO
* @return
*/
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_OPENGL_CORE_PROFILE);
//第三个参数是标题
GLFWwindow *window = glfwCreateWindow(600, 600, "LearnOpengl", NULL, NULL);
if (window == NULL) {
std::cout << "Fail to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//奇特的写法,本应是个函数,写出来却像个变量,连参数都不需要传了
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
std::cout << "File to initialize GLAD" << std::endl;
return -1;
}
//使窗口隐藏光标并且捕捉它
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
//使用回调
glfwSetCursorPosCallback(window, mouse_callback);
unsigned int texture1, texture2;
const char *path1 = "D:\\cl_workspace\\TestOpengl\\resource\\girl.jpg";
const char *path2 = "D:\\cl_workspace\\TestOpengl\\resource\\awesomeface.jpg";
//两个都要传递引用,这样函数更改的值才是上面的值
loadTexture(&texture1, path1);
loadTexture(&texture2, path2);
glViewport(0, 0, 600, 600);//指定视口大小,跟java一样
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetScrollCallback(window, scroll_callback);
// Shader shader("D:\\cl_workspace\\TestOpengl\\shader\\vertex_shader_two.glsl",
// "D:\\cl_workspace\\TestOpengl\\shader\\fragment_shader_two.glsl");
Shader shader("D:\\cl_workspace\\TestOpengl\\shader\\vertex_shader_three.glsl",
"D:\\cl_workspace\\TestOpengl\\shader\\fragment_shader_three.glsl");
/*//矩形
float vertices[] = {
// ---- 位置 ---- ---- 颜色 ---- - 纹理坐标 -
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // 右上
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // 右下
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // 左下
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // 左上
};*/
//立方体
float 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,

本文介绍了如何利用glfw库来处理键盘、鼠标点击和滚轮的事件,详细讲解了相关编程细节。

7696

被折叠的 条评论
为什么被折叠?



