// 引入GLEW库 定义静态链接
#define GLEW_STATIC
#include <GL/glew.h>
// 引入GLFW库
#include <GLFW/glfw3.h>
// 引入SOIL库
#include <SOIL/SOIL.h>
// 引入GLM库
#include <GLM/glm.hpp>
#include <GLM/gtc/matrix_transform.hpp>
#include <GLM/gtc/type_ptr.hpp>
#include <iostream>
#include <vector>
// 包含着色器加载库
#include "shader.h"
// 包含相机控制辅助类
#include "camera.h"
// 包含纹理辅助类
#include "texture.h"
#include"simpleObjLoader.h"
// 键盘回调函数原型声明
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
// 鼠标移动回调函数原型声明
void mouse_move_callback(GLFWwindow* window, double xpos, double ypos);
// 鼠标滚轮回调函数原型声明
void mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
// 场景中移动
void do_movement();
// 定义程序常量
const int WINDOW_WIDTH = 800, WINDOW_HEIGHT = 600;
// 用于相机交互参数
GLfloat lastX = WINDOW_WIDTH / 2.0f, lastY = WINDOW_HEIGHT / 2.0f;
bool firstMouseMove = true;
bool keyPressedStatus[1024]; // 按键情况记录
GLfloat deltaTime = 0.0f; // 当前帧和上一帧的时间差
GLfloat lastFrame = 0.0f; // 上一帧时间
Camera camera(glm::vec3(0.0f, 0.0f, 4.0f));
glm::vec3 lampPos(2.6f, 0.4f, 0.5f);
int main(int argc, char** argv)
{
if (!glfwInit()) // 初始化glfw库
{
std::cout << "Error::GLFW could not initialize GLFW!" << std::endl;
return -1;
}
// 开启OpenGL 3.3 core profile
std::cout << "Start OpenGL core profile version 3.3" << std::endl;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// 创建窗口
GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT,
"Santa Claus boxes and planets in the sun", NULL, NULL);
if (!window)
{
std::cout << "Error::GLFW could not create winddow!" << std::endl;
glfwTerminate();
return -1;
}
// 创建的窗口的context指定为当前context
glfwMakeContextCurrent(window);
// 注册窗口键盘事件回调函数
glfwSetKeyCallback(window, key_callback);
// 注册鼠标事件回调函数
glfwSetCursorPosCallback(window, mouse_move_callback);
// 注册鼠标滚轮事件回调函数
glfwSetScrollCallback(window, mouse_scroll_callback);
// 鼠标捕获 停留在程序内
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// 初始化GLEW 获取OpenGL函数
glewExperimental = GL_TRUE; // 让glew获取所有拓展函数
GLenum status = glewInit();
if (status != GLEW_OK)
{
std::cout << "Error::GLEW glew version:" << glewGetString(GLEW_VERSION)
<< " error string:" << glewGetErrorString(status) << std::endl;
glfwTerminate();
return -1;
}
// 设置视口参数
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
// Section1 准备顶点数据
// 指定顶点属性数据 顶点位置 纹理 法向量
GLfloat vertices[] = {
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, // A
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, // B
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // C
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // C
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // D
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, // A
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, // E
-0.5f, 0.5f, -0.5f, 0.0, 1.0f, 0.0f, 0.0f, -1.0f, // H
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, -1.0f, // G
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, -1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5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, // A
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, // E
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, -1.0f, 0.0f, // F
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, -1.0f, 0.0f, // F
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, // B
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, // A
};
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),
glm::vec3(-4.3f, 1.0f, -1.5f)
};
// 创建物体缓存对象
GLuint VAOId, VBOId;
// Step1: 创建并绑定VAO对象
glGenVertexArrays(1, &VAOId);
glBindVertexArray(VAOId);
// Step2: 创建并绑定VBO 对象 传送数据
glGenBuffers(1, &VBOId);
glBindBuffer(GL_ARRAY_BUFFER, VBOId);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Step3: 指定解析方式 并启用顶点属性
// 顶点位置属性
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
8 * sizeof(GL_FLOAT), (GLvoid*)0);
glEnableVertexAttribArray(0);
// 顶点纹理坐标
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
8 * sizeof(GL_FLOAT), (GLvoid*)(3 * sizeof(GL_FLOAT)));
glEnableVertexAttribArray(1);
// 顶点法向量属性
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE,
8 * sizeof(GL_FLOAT), (GLvoid*)(5 * sizeof(GL_FLOAT)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//Section1 从obj文件加载数据
std::vector<Vertex> vertData;
if (!ObjLoader::loadFromFile("ballwithtexturecoordinate.obj", vertData))
{
std::cerr << "Could not load obj model, exit now.";
std::system("pause");
exit(-1);
}
// Section2 准备纹理
GLint textureId = TextureHelper::loadDDS("cat_ghost.dds");//这里注释了这行
// Section3 建立Mesh对象
Mesh mesh(vertData, textureId);
// 创建光源的VAO
GLuint lampVAOId;
glGenVertexArrays(1, &lampVAOId);
glBindVertexArray(lampVAOId);
glBindBuffer(GL_ARRAY_BUFFER, VBOId); // 重用上面的数据 无需重复发送顶点数据
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GL_FLOAT), (GLvoid*)0);
glEnableVertexAttribArray(0); // 只需要顶点位置即可
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Section2 准备着色器程序
Shader shader("cube.vertex", "cube.frag");
Shader lampShaer("lamp.vertex", "lamp.frag");
// Section3 准备diffuseMap和specularMap
GLint diffuseMap = TextureHelper::load2DTexture("shengdanlaoren.png");
GLint specularMap = TextureHelper::load2DTexture("shengdanlaoren.png");
shader.use();
glUniform1i(glGetUniformLocation(shader.programId, "material.diffuseMap"), 0);
glUniform1i(glGetUniformLocation(shader.programId, "material.specularMap"), 1);
glEnable(GL_DEPTH_TEST);
// 开始游戏主循环
while (!glfwWindowShouldClose(window))
{
GLfloat currentFrame = (GLfloat)glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glfwPollEvents(); // 处理例如鼠标 键盘等事件
do_movement(); // 根据用户操作情况 更新相机属性
// 清除颜色缓冲区 重置为指定颜色
glClearColor(0.18f, 0.04f, 0.14f, 1.0f);
//glClearColor(0.0f, 0.0f, 0.0f, 1.0
![avatar](https://profile-avatar.csdnimg.cn/f5da90d104c64e32aa0043f39e33e09d_tanshengjunjun.jpg!1)
谭舜心
- 粉丝: 20
- 资源: 5
最新资源
- 【独家首发】鲸鱼算法WOA优化Transformer-LSTM负荷数据回归预测【含Matlab源码 6386期】.zip
- 【独家首发】开普勒算法KOA优化Transformer-LSTM负荷数据回归预测【含Matlab源码 6387期】.zip
- MATLAB轴承动力学模拟:不同故障类型下的滚动轴承性能分析与时域波形输出,MATLAB轴承动力学模拟:不同故障类型下的滚动轴承性能分析与时域波形输出,MATLAB轴承动力学代码(正常、外圈故障、内圈
- 【JCR1区】鸽群算法PIO-CNN-SVM故障诊断分类预测【含Matlab源码 5787期】.zip
- 【独家首发】金枪鱼算法TSO优化Transformer-LSTM负荷数据回归预测【含Matlab源码 6385期】.zip
- 【BiLSTM数据预测】双向长短时记忆BiLSTM(多输入单输出)数据预测【含Matlab源码 1826期】.zip
- 【BiLSTM数据预测】双向长短时记忆BiLSTM数据预测【含Matlab源码 1793期】.zip
- 【LSTM时间序列预测】深度学习的长短期记忆网络LSTM时间序列预测未来【含Matlab源码 2345期】.zip
- 【LSTM数据预测】BP+ELM+LSTM+BiLSTM+SAELSTM数据预测【含Matlab源码 1825期】.zip
- 【风速预测】DBN算法风速预测【含Matlab源码 1400期】.zip
- 【LSTM回归预测】长短期记忆网络数据回归预测(多输入多输出)【含Matlab源码 3200期】.zip
- 【ARMA仿真】 ARMA模型卡尔曼滤波【含Matlab源码 2431期】.zip
- 【ARMA故障预测】ARMA故障预测【含Matlab源码 2432期】.zip
- 【ARMA时间序列分析】ARMA时间序列分析【含Matlab源码 2430期】.zip
- 【预测模型】模糊小波神经网络目标威胁评估【含Matlab源码 1621期】.zip
- 【BiLSTM数据预测】双向长短时记忆BiLSTM数据预测【含Matlab源码 1824期】.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)