#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include"openglshader.h"
#include"camera.h"
#include "model.h"
#include"mouse.h"
#include"picking_texture.h"
//#include"stb_image.h"
//#include"assimp/anim.h"
//using namespace FileSystem
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
// camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
Camera* camerax = &camera;//主要是要一个这个对象的指针 在鼠标那个函数里面可以传进去
// timing
float deltaTime = 0.0f;
float lastFrame = 0.0f;
LeftMouse* lm;//用于存储左键点击坐标的结构体指针
HWND hwnd;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
// tell GLFW to capture our mouse
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// configure global opengl state
// -----------------------------
glEnable(GL_DEPTH_TEST);
lm = (LeftMouse*)calloc(1, sizeof(LeftMouse));//开辟空间
// build and compile shaders
// -------------------------
Shader ourShader("modelShader.vs","modelShader.fs","modelShader.gs");
Shader pickShader("pick.vs", "pick.fs"); ///拾取着色器
Shader simpleShader("simple.vs", "simple.fs");
PickingTexture pickingtexture;
// load models
// -----------
//Model ourModel("C:/Users/Howard Wilson/source/repos/模型载入1/装甲/nanosuit.obj");
Model ourModel("装甲/nanosuit.obj");
pickingtexture.Init(SCR_WIDTH, SCR_HEIGHT);//拾取纹理初始化
hwnd = GetActiveWindow();//获取窗口句柄
// draw in wireframe
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// per-frame time logic
// --------------------
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// input
// -----
processInput(window);
mouseMove(window, camerax, hwnd,*lm);//这里获取了鼠标左键点击的位置,并且传出来给lm
// render
// ------
glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// don't forget to enable shader before setting uniforms
// view/projection transformations
/*这里把相应数据存入帧缓冲*/
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, -1.75f, 0.0f)); // translate it down so it's at the center of the scene
model = glm::scale(model, glm::vec3(0.2f, 0.2f, 0.2f)); // it's a bit too big for our scene, so scale it down
pickShader.use();//将复合矩阵传递给拾取着色器,为了在帧缓冲中绘制一次模型场景
pickShader.setMat4("view", view);
pickShader.setMat4("projection", projection);
pickShader.setMat4("model", model);
pickingtexture.EnableWriting();//开启帧缓存
glEnable(GL_DEPTH_TEST); // enable depth testing (is disabled for rendering screen-space quad)
// make sure we clear the framebuffer's content
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*绘制一次需要被点击的场景,但不是真正的绘制出来*/
/*每绘制一个模型就传一个对象索引进去*/
for (int i = 0; i < 1; i++)
{
pickShader.setInt("gobjectIndex", i);//传对象索引进着色器
ourModel.Draw(pickShader);//传绘制索引和原始索引进着色器
}
glBindVertexArray(0);
pickingtexture.DisableWriting();
/*本来是点击的模型绘制成红色,现在变成把点击处的三元索引显示出来*/
if (lm->IsPress == true)
{
//return 0;
PickingTexture::PixelInfo Pixel = pickingtexture.ReadPixel(lm->x, SCR_HEIGHT - lm->y - 1);//这里得到像素信息 (从拾取帧缓冲处获得像素)参数是鼠标在屏幕上的二维坐标
printf("x:%d y:%d ob:%f dr:%f pr:%f", lm->x, SCR_HEIGHT - lm->y - 1, Pixel.objectID, Pixel.drawID, Pixel.primID);
printf("\n");
}
/*绘制所有场景*/
ourShader.use();
ourShader.setMat4("projection", projection);
ourShader.setMat4("view", view);
// render the loaded model
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, -1.75f, 0.0f)); // translate it down so it's at the center of the scene
model = glm::scale(model, glm::vec3(0.2f, 0.2f, 0.2f)); // it's a bit too big for our scene, so scale it down
ourShader.setMat4("model", model);
ourShader.setFloat("time", glfwGetTime());
ourModel.Draw(ourShader);//这里是绘制模型
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 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);
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);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be signi
没有合适的资源?快使用搜索试试~ 我知道了~
3d拾取修改demo
共71个文件
png:17个
h:11个
tlog:7个
1星 需积分: 41 22 下载量 17 浏览量
2020-12-10
17:15:41
上传
评论 2
收藏 22.13MB ZIP 举报
温馨提示
重新修改了一下
资源详情
资源评论
资源推荐
收起资源包目录
3d拾取demo.zip (71个子文件)
assimp-vc140-mt.dll 10.24MB
glad
glad.h 282KB
pick.vs 197B
picking_texture.cpp 2KB
modelShader.gs 1KB
camera.h 4KB
pick.fs 230B
实现3d拾取.sln 1KB
simple.vs 198B
openglshader.h 7KB
loadTexture.cpp 4KB
picking_texture.h 925B
实现3d拾取.vcxproj.filters 2KB
mouse.h 336B
mouse.cpp 2KB
all.h 443B
simple.fs 95B
left_mouse.h 193B
装甲
leg_showroom_spec.png 1.69MB
arm_showroom_ddn.png 846KB
hand_dif.png 640KB
helmet_showroom_ddn.png 729KB
nanosuit.obj 1.77MB
arm_showroom_spec.png 1.59MB
leg_dif.png 1.13MB
nanosuit.mtl 1KB
hand_showroom_ddn.png 976KB
glass_ddn.png 6KB
helmet_showroom_spec.png 1.49MB
body_showroom_ddn.png 840KB
nanosuit.blend 2.83MB
helmet_diff.png 513KB
arm_dif.png 562KB
emissionPhoto.jpg 291KB
glass_dif.png 1KB
body_showroom_spec.png 1.69MB
leg_showroom_ddn.png 1.31MB
hand_showroom_spec.png 1.66MB
body_dif.png 621KB
GLFW
glfw3.h 200KB
glfw3native.h 16KB
loadTexture.h 238B
实现3d拾取.vcxproj 8KB
modelShader.vs 399B
model.h 10KB
Debug
vc142.pdb 764KB
loadTexture.obj 615KB
picking_texture.obj 53KB
实现3d拾取.pdb 1.64MB
实现3d拾取.ilk 1.62MB
glfw3.dll 387KB
实现3d拾取.exe 743KB
vcpkg.applocal.log 73B
leftMouse.obj 817B
源.obj 956KB
实现3d拾取.vcxproj.CopyComplete 0B
mouse.obj 75KB
vc142.idb 1.01MB
实现3d拾取.tlog
CL.write.1.tlog 5KB
CL.read.1.tlog 320KB
实现3d拾取.lastbuildstate 218B
CL.command.1.tlog 4KB
link.write.1.tlog 868B
link.command.1.tlog 2KB
link.read.1.tlog 6KB
实现3d拾取.write.1u.tlog 20KB
实现3d拾取.log 469B
modelShader.fs 179B
assimp-vc140-mt.lib 236KB
实现3d拾取.vcxproj.user 168B
源.cpp 8KB
共 71 条
- 1
彭同学她同桌
- 粉丝: 982
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Surfer,线性函数
- MyBatis 的动态 SQL 是其核心特性之一.txt
- 时代的sdddsddsddsd
- 基于哈希链表的简单人员信息管理系统
- 其他类别JdonFramework开源框架 v5.1 Build20071025-jdonframework-5.1.rar
- 2001~2022年上市公司数字赋能指数.dta
- 2001~2022年上市公司数字赋能指数.xlsx
- 信息办公石大在线财务管理系统(含源码)-shidacaiwu.rar
- 信息办公电信计费系统完整代码-netctossconformity.rar
- matlab实现TD-SCDMA中初始同步捕捉DwPTS下行同步导频时隙的仿真.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1