当前位置:网站首页>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 分别各准备一套。
边栏推荐
- Metaverse "Drummer" Unity: Crazy expansion, suspense still exists
- Jenkins 导出、导入 Job Pipeline
- Tensors - Application Cases
- 【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
- Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
- 【C语言进阶】程序环境和预处理
- 如何动态添加script依赖的脚本
- Oracle与Postgresql在PLSQL内事务回滚的重大差异
- 结构体函数练习
- Introduction and application of go module
猜你喜欢
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
复现20字符短域名绕过
The Shell function
Explain详解与实践
企业直播风起:目睹聚焦产品,微赞拥抱生态
8. Haproxy builds a web cluster
Simple operation of the file system
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
2022 Hangzhou Electric Power Multi-School League Game 5 Solution
mysql index notes
随机推荐
Functions, recursion and simple dom operations
Jenkins 导出、导入 Job Pipeline
share总结
docker安装mysql与宿主机相差8小时的问题。
Enterprise live broadcast is on the rise: Witnessing focused products, micro-like embracing ecology
Learn iframes and use them to solve cross-domain problems
【21天学习挑战赛】图像的旋转问题(二维数组)
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
基于 SSE 实现服务端消息主动推送解决方案
劝退背后。
深度学习之 10 卷积神经网络3
2022支付宝C2C现金红包PHP源码DEMO/兼容苹果/安卓浏览器和扫码形式
Metaverse "Drummer" Unity: Crazy expansion, suspense still exists
How to dynamically add script dependent scripts
centos 安装postgresql13 指定版本
结构体指针知识要点总结
转:管理是对可能性的热爱,管理者要有闯进未知的勇气
7.LVS负载均衡群集之原理叙述
如何动态添加script依赖的脚本
看DevExpress丰富图表样式,如何为基金公司业务创新赋能