当前位置:网站首页>OpenGL绘制一个圆锥
OpenGL绘制一个圆锥
2022-08-04 04:27:00 【后知后觉】
绘制圆锥暂时没有找到一个模型完整绘制,暂时使用两个物体拼接: 圆和锥面。
缺点:这需要绘制两次才能将圆锥体绘制成功。
#include "cone.h"
#include <QTimer>
#include <QDateTime>
#include <QImage>
#include <glm-master/glm/gtc/matrix_transform.hpp>
#include <glm-master/glm/gtc/type_ptr.hpp>
#include <QtMath>
#define POINT_COUNT (362)
static float circleVertices[POINT_COUNT*3];
static float coneVertices[POINT_COUNT*3];
Cone::Cone(QWidget* parent)
{
QTimer * timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(17);
mProjectionMatrix = glm::mat4(1.0f);
mViewMatrix = glm::mat4(1.0f);
mModelMatrix = glm::mat4(1.0f);
//center
circleVertices[0] = 0;
circleVertices[1] = 0;
circleVertices[2] = 0;
coneVertices[0] = 0;
coneVertices[1] = 0;
coneVertices[2] = 1.0;
float r = 0.5f;
for(int i = 1; i <= POINT_COUNT - 1; i++) {
int index = i*3;
circleVertices[index] = r * sin(i * M_PI/ 180.0f);
circleVertices[index+1] = r * cos(i * M_PI/ 180.0f);
circleVertices[index+2] = 0;
coneVertices[index] = r * sin(i * M_PI/ 180.0f);
coneVertices[index+1] = r * cos(i * M_PI/ 180.0f);
coneVertices[index+2] = 0;
}
}
void Cone::initializeGL()
{
bool enabled = initializeOpenGLFunctions();
qDebug() << "initializeGL" << enabled;
glGenVertexArrays(1, &mCircleVAO);
glGenBuffers(1, &mCircleVBO);
glBindVertexArray(mCircleVAO);
glBindBuffer(GL_ARRAY_BUFFER, mCircleVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(circleVertices), circleVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &mConeVAO);
glGenBuffers(1, &mConeVBO);
glBindVertexArray(mConeVAO);
glBindBuffer(GL_ARRAY_BUFFER, mConeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(coneVertices), coneVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
mConeProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/cone.vsh");
mConeProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/cone.fsh");
bool success = mConeProgram.link();
if(!success){
qDebug() << "ERR: " << mConeProgram.log();
}
mCircleProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/circle.vsh");
mCircleProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/circle.fsh");
success = mCircleProgram.link();
if(!success){
qDebug() << "ERR: " << mCircleProgram.log();
}
mConeProjectionLoc = mConeProgram.uniformLocation("projection");
mConeViewLoc = mConeProgram.uniformLocation("view");
mConeModelLoc = mConeProgram.uniformLocation("model");
mCircleProjectionLoc = mCircleProgram.uniformLocation("projection");
mCircleViewLoc = mCircleProgram.uniformLocation("view");
mCircleModelLoc = mCircleProgram.uniformLocation("model");
}
void Cone::resizeGL(int w, int h)
{
mViewMatrix = glm::translate(mViewMatrix, glm::vec3(0.0f, 0.0f, -3.0f));
mProjectionMatrix = glm::perspective(glm::radians(45.0f), (float)w / (float)h, 0.1f, 100.0f);
glEnable(GL_DEPTH_TEST);
}
void Cone::paintGL()
{
mModelMatrix = glm::rotate(mModelMatrix, 0.01f, glm::vec3(1.0f, 0.0f, 0.0f));
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
mCircleProgram.bind();
glBindVertexArray(mCircleVAO);
glUniformMatrix4fv(mCircleProjectionLoc, 1, GL_FALSE, glm::value_ptr(mProjectionMatrix));
glUniformMatrix4fv(mCircleViewLoc, 1, GL_FALSE, glm::value_ptr(mViewMatrix));
glUniformMatrix4fv(mCircleModelLoc, 1, GL_FALSE, glm::value_ptr(mModelMatrix));
glDrawArrays(GL_TRIANGLE_FAN, 0, POINT_COUNT);
mConeProgram.bind();
glBindVertexArray(mConeVAO);
glUniformMatrix4fv(mConeProjectionLoc, 1, GL_FALSE, glm::value_ptr(mProjectionMatrix));
glUniformMatrix4fv(mConeViewLoc, 1, GL_FALSE, glm::value_ptr(mViewMatrix));
glUniformMatrix4fv(mConeModelLoc, 1, GL_FALSE, glm::value_ptr(mModelMatrix));
glDrawArrays(GL_TRIANGLE_FAN, 0, POINT_COUNT);
}
为了方便理解,VAO, VBO, Program 分别各准备一套。
边栏推荐
- Stop behind.
- 中信证券网上开户怎么开的?安全吗?
- 大型连锁百货运维审计用什么软件好?有哪些功能?
- Introduction to mq application scenarios
- 什么是数字孪生智慧城市应用场景
- 杭电多校-Slipper-(树图转化+虚点建图)
- Hangdian Multi-School-Slipper- (tree map conversion + virtual point mapping)
- MRS: Introduction to the use of Alluxio
- 元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
- How to systematically plan and learn software testing?
猜你喜欢

Based on the statistical QDirStat Qt directory

十一种概率分布

结构体指针知识要点总结

A Preliminary Study of RSS Subscription to WeChat Official Account-feed43

Functions, recursion and simple dom operations

8. Haproxy builds a web cluster

SQL query String field less than 10 how to check

7-3 LVS+Keepalived Cluster Description and Deployment

Mini program + e-commerce, fun new retail
![The video of machine learning to learn [update]](/img/e7/c9a17b4816ce8d4b0787c451520ac3.png)
The video of machine learning to learn [update]
随机推荐
How to systematically plan and learn software testing?
mysql index notes
Hey, I had another fight with HR in the small group!
2022支付宝C2C现金红包PHP源码DEMO/兼容苹果/安卓浏览器和扫码形式
Jenkins export and import Job Pipeline
七夕节,我用代码制作了表白信封
【C语言进阶】程序环境和预处理
The Shell function
pnpm 是凭什么对 npm 和 yarn 降维打击的
网络工程师入门必懂华为认证体系,附系统学习路线分享
7-1 LVS+NAT load balancing cluster, NAT mode deployment
How to open a CITIC Securities online account?is it safe?
马尔可夫链
看DevExpress丰富图表样式,如何为基金公司业务创新赋能
Shell 函数
2022杭电多校联赛第五场 题解
【id类型和NSObject指针 ObjectIve-C中】
移动支付线上线下支付场景
一文详解DHCP原理及配置
Simple operation of the file system