当前位置:网站首页>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 分别各准备一套。
边栏推荐
猜你喜欢

【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】

JVM笔记

转:管理是对可能性的热爱,管理者要有闯进未知的勇气

深度学习——以CNN服装图像分类为例,探讨怎样评价神经网络模型

See how DevExpress enriches chart styles and how it empowers fund companies to innovate their business

Mini program + e-commerce, fun new retail

【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network

Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析

Stop behind.

外卖店优先级
随机推荐
元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
TL431的基本特性以及振荡电路
什么是数字孪生智慧城市应用场景
Learn iframes and use them to solve cross-domain problems
Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis
大型连锁百货运维审计用什么软件好?有哪些功能?
if,case,for,while
How to automatically export or capture abnormal login ip and logs in elastic to the database?
Embedded database development programming MySQL (full)
自定义通用分页标签02
本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
内网服务器访问远程服务器的端口映射
MRS: Introduction to the use of Alluxio
FFmpeg —— 通过修改yuv,将视频转为黑白并输出(附源码)
How to keep the source code confidential in the development under the burning scenario
拿捏JVM性能优化(自己笔记版本)
manipulation of file contents
Structure function exercise
数据集类型转换—TFRecords文件