#include "myopenglwidget.h"
#include <QOpenGLVersionFunctionsFactory>
#include "vertices.h"
#define timeOutmSec 50
QVector3D viewInitPos(0.0,5.0,25.0);
float _near=0.1f,_far=100.0f;
QMatrix4x4 model;
QMatrix4x4 view;
QMatrix4x4 projection;
QPoint lastPos;
unsigned int skyboxVAO;
unsigned int skyboxVBO;
MyOpenglWidget::MyOpenglWidget(QWidget *parent)
: QOpenGLWidget{parent}
{
connect(&m_timer,&QTimer::timeout,this,&MyOpenglWidget::on_timeOut);
m_timer.start(timeOutmSec);
m_time.start();
m_camera.Position = viewInitPos;
setFocusPolicy(Qt::StrongFocus);
setMouseTracking(true);
}
MyOpenglWidget::~MyOpenglWidget()
{
for(auto iter=m_Models.begin();iter!=m_Models.end();iter++)
{
ModelInfo *modelInfo=&iter.value();
delete modelInfo->model;
}
}
void MyOpenglWidget::loadModel(string path)
{
static int i = 0;
makeCurrent();
Model *_model = new Model(QOpenGLVersionFunctionsFactory::get<QOpenGLFunctions_3_3_Core>(QOpenGLContext::currentContext()),
path.c_str());
//m_camera.Position = getCameraInitPos(_model->m_minY,_model->m_maxY);
m_Models["张三"+QString::number(i++)]=
ModelInfo{_model,QVector3D(0,0,0),0.0,0.0,0.0,false,"张三"};
doneCurrent();
}
void MyOpenglWidget::initializeGL()
{
initializeOpenGLFunctions();
bool success;
m_shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,":/shaders/shaders/shapes.vert");
m_shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,":/shaders/shaders/shapes.frag");
success=m_shaderProgram.link();
if(!success) qDebug()<<"ERR:"<<m_shaderProgram.log();
m_skyBoxShaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,":/shaders/shaders/skybox.vert");
m_skyBoxShaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,":/shaders/shaders/skybox.frag");
success=m_skyBoxShaderProgram.link();
if(!success) qDebug()<<"ERR:"<<m_skyBoxShaderProgram.log();
m_refractionShaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,":/shaders/shaders/refraction.vert");
m_refractionShaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,":/shaders/shaders/refraction.frag");
success=m_refractionShaderProgram.link();
if(!success) qDebug()<<"ERR:"<<m_refractionShaderProgram.log();
m_BoxDiffuseTex=new
QOpenGLTexture(QImage(":/images/images/container2.png").mirrored());
m_PlaneDiffuseTex=new
QOpenGLTexture(QImage(":/images/images/wall.jpg").mirrored());
m_CubeMap=new QOpenGLTexture(QOpenGLTexture::TargetCubeMap);
QImage _right = QImage(":/images/images/skybox/right.jpg").convertToFormat(QImage::Format_RGB888);
QImage _left = QImage(":/images/images/skybox/left.jpg").convertToFormat(QImage::Format_RGB888);
QImage _top = QImage(":/images/images/skybox/top.jpg").convertToFormat(QImage::Format_RGB888);
QImage _bottom = QImage(":/images/images/skybox/bottom.jpg").convertToFormat(QImage::Format_RGB888);
QImage _front = QImage(":/images/images/skybox/front.jpg").convertToFormat(QImage::Format_RGB888);
QImage _back = QImage(":/images/images/skybox/back.jpg").convertToFormat(QImage::Format_RGB888);
m_CubeMap->setSize(_right.width(), _right.height());
m_CubeMap->setFormat(QOpenGLTexture::RGBFormat);
m_CubeMap->allocateStorage(QOpenGLTexture::RGB, QOpenGLTexture::UInt8);
m_CubeMap->setData(0, 0, QOpenGLTexture::CubeMapPositiveX,QOpenGLTexture::RGB, QOpenGLTexture::UInt8, (const void *)_right.bits());
m_CubeMap->setData(0, 0, QOpenGLTexture::CubeMapNegativeX,QOpenGLTexture::RGB, QOpenGLTexture::UInt8, (const void *)_left.bits());
m_CubeMap->setData(0, 0, QOpenGLTexture::CubeMapPositiveY,QOpenGLTexture::RGB, QOpenGLTexture::UInt8, (const void *)_top.bits());
m_CubeMap->setData(0, 0, QOpenGLTexture::CubeMapNegativeY,QOpenGLTexture::RGB, QOpenGLTexture::UInt8, (const void *)_bottom.bits());
m_CubeMap->setData(0, 0, QOpenGLTexture::CubeMapPositiveZ,QOpenGLTexture::RGB, QOpenGLTexture::UInt8, (const void *)_front.bits());
m_CubeMap->setData(0, 0, QOpenGLTexture::CubeMapNegativeZ,QOpenGLTexture::RGB, QOpenGLTexture::UInt8, (const void *)_back.bits());
m_CubeMap->setMinificationFilter(QOpenGLTexture::Linear); //纹理放大或缩小时,像素的取值方法 ,线性或就近抉择
m_CubeMap->setMagnificationFilter(QOpenGLTexture::Linear);
m_CubeMap->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::ClampToEdge); //设置纹理边缘的扩展方法
m_CubeMap->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::ClampToEdge);
// m_CubeMap->setWrapMode(QOpenGLTexture::DirectionR, QOpenGLTexture::ClampToEdge);
// skybox VAO
glGenVertexArrays(1, &skyboxVAO);
glGenBuffers(1, &skyboxVBO);
glBindVertexArray(skyboxVAO);
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
m_PlaneMesh = processMesh(planeVertices,6,m_PlaneDiffuseTex->textureId());
}
void MyOpenglWidget::resizeGL(int w, int h)
{
Q_UNUSED(w);
Q_UNUSED(h);
}
void MyOpenglWidget::paintGL()
{
model.setToIdentity();
view.setToIdentity();
projection.setToIdentity();
//float time=m_time.elapsed()/50.0;
projection.perspective(m_camera.Zoom,(float)width()/height(),_near,_far);
view=m_camera.GetViewMatrix();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_shaderProgram.bind();
//m_shaderProgram.setUniformValue("light.position",lightPos);
m_shaderProgram.setUniformValue("viewPos",m_camera.Position);
m_shaderProgram.setUniformValue("projection", projection);
m_shaderProgram.setUniformValue("view", view);
//model.rotate(1.0f, 1.0f, 1.0f, 0.5f);
m_shaderProgram.setUniformValue("model", model);
// light properties, note that all light colors are set at full intensity
m_shaderProgram.setUniformValue("light.ambient", 0.7f, 0.7f, 0.7f);
m_shaderProgram.setUniformValue("light.diffuse", 0.9f, 0.9f, 0.9f);
m_shaderProgram.setUniformValue("light.specular", 1.0f, 1.0f, 1.0f);
// material properties
m_shaderProgram.setUniformValue("material.shininess", 32.0f);
m_shaderProgram.setUniformValue("light.direction", -0.2f, -1.0f, -0.3f);
m_PlaneMesh->Draw(m_shaderProgram);
m_refractionShaderProgram.bind();
m_refractionShaderProgram.setUniformValue("viewPos",m_camera.Position);
m_refractionShaderProgram.setUniformValue("projection", projection);
m_refractionShaderProgram.setUniformValue("view", view);
foreach(auto modelInfo,m_Models)
{
model.setToIdentity();
model.translate(modelInfo.worldPos);
model.rotate(modelInfo.pitch,QVector3D(1.0,0.0,0.0));
model.rotate(modelInfo.yaw,QVector3D(0.0,1.0,0.0));
model.rotate(modelInfo.roll,QVector3D(0.0,0.0,1.0));
m_refractionShaderProgram.setUniformValue("model", model);
modelInfo.model->Draw(m_refractionShaderProgram);
}
// draw skybox as last
glDepthFunc(GL_LEQUAL); // 确保天空盒=1也能通过测试
m_skyBoxShaderProgram.bind();
QMatrix4x4 skyboxView=view;// remove translation from the view matrix
skyboxView.setColumn(3,QVector4D(0.0f,0.0f,0.0f,1.0f));
m_skyBoxShaderProgram.setUniformValue("projection", projection);
m_skyBoxShaderProgram.setUniformValue("view", skyboxView);
// skybox cube
glBindVertexArray(skyboxVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_CubeMap->textureId());
glDrawArrays(GL_T
没有合适的资源?快使用搜索试试~ 我知道了~
LearnOpenGL(二十)之立方体贴图
共128个文件
jpg:32个
h:28个
cpp:20个
需积分: 5 0 下载量 75 浏览量
2024-05-21
13:40:10
上传
评论
收藏 16.4MB RAR 举报
温馨提示
详细介绍:
资源推荐
资源详情
资源评论
收起资源包目录
LearnOpenGL(二十)之立方体贴图 (128个子文件)
myopenglwidget.cpp 13KB
myopenglwidget.cpp 13KB
myopenglwidget.cpp 13KB
myopenglwidget.cpp 13KB
model.cpp 5KB
model.cpp 4KB
model.cpp 4KB
model.cpp 4KB
mesh.cpp 3KB
mesh.cpp 2KB
mesh.cpp 2KB
mesh.cpp 2KB
mainwindow.cpp 1006B
mainwindow.cpp 1006B
mainwindow.cpp 1006B
mainwindow.cpp 1006B
main.cpp 187B
main.cpp 187B
main.cpp 187B
main.cpp 187B
shapes.frag 2KB
shapes.frag 1KB
shapes.frag 1KB
shapes.frag 1KB
refraction.frag 329B
reflection.frag 288B
skybox.frag 152B
skybox.frag 152B
skybox.frag 152B
skybox.frag 152B
camera.h 4KB
camera.h 4KB
camera.h 4KB
camera.h 4KB
ui_mainwindow.h 4KB
ui_mainwindow.h 4KB
ui_mainwindow.h 4KB
ui_mainwindow.h 4KB
myopenglwidget.h 2KB
myopenglwidget.h 2KB
myopenglwidget.h 2KB
myopenglwidget.h 2KB
vertices.h 1KB
vertices.h 1KB
vertices.h 1KB
vertices.h 1KB
model.h 1KB
model.h 1KB
model.h 1KB
model.h 1KB
mesh.h 836B
mesh.h 836B
mesh.h 836B
mesh.h 836B
mainwindow.h 472B
mainwindow.h 472B
mainwindow.h 472B
mainwindow.h 472B
back.jpg 723KB
back.jpg 723KB
back.jpg 723KB
back.jpg 723KB
left.jpg 588KB
left.jpg 588KB
left.jpg 588KB
left.jpg 588KB
right.jpg 525KB
right.jpg 525KB
right.jpg 525KB
right.jpg 525KB
front.jpg 462KB
front.jpg 462KB
front.jpg 462KB
front.jpg 462KB
top.jpg 338KB
top.jpg 338KB
top.jpg 338KB
top.jpg 338KB
matrix.jpg 291KB
matrix.jpg 291KB
matrix.jpg 291KB
matrix.jpg 291KB
bottom.jpg 274KB
bottom.jpg 274KB
bottom.jpg 274KB
bottom.jpg 274KB
wall.jpg 251KB
wall.jpg 251KB
wall.jpg 251KB
wall.jpg 251KB
container2.png 457KB
container2.png 457KB
container2.png 457KB
container2.png 457KB
lighting_maps_specular_color.png 142KB
lighting_maps_specular_color.png 142KB
lighting_maps_specular_color.png 142KB
lighting_maps_specular_color.png 142KB
container2_specular.png 141KB
container2_specular.png 141KB
共 128 条
- 1
- 2
资源评论
码农客栈
- 粉丝: 1557
- 资源: 40
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功