当前位置:网站首页>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 分别各准备一套。
边栏推荐
- 小程序 + 电商,玩转新零售
- 目标检测-中篇
- 本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
- JVM Notes
- Converts XML tags to TXT format (voc conversion for yolo convenient training)
- PHP高级开发案例(1):使用MYSQL语句跨表查询无法导出全部记录的解决方案
- Introduction and application of go module
- 2022软件测试面试题 最新字节跳动50道真题面试题 刷完已拿下15k 附讲解+答疑
- SQL interview Questions
- 4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
猜你喜欢
随机推荐
图像处理之Bolb分析(一)
Explain详解与实践
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
drools from download to postman request success
使用serve搭建本地服务器
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
JVM Notes
RSS订阅微信公众号初探-feed43
【C语言进阶】程序环境和预处理
Structure function exercise
Based on the statistical QDirStat Qt directory
【技巧】借助Sentinel实现请求的优先处理
4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
解决问题遇到的问题
XSS相关知识点
外卖店优先级
Jenkins export and import Job Pipeline
SQL query String field less than 10 how to check
Introduction and application of go module
Significant differences between Oracle and Postgresql in PLSQL transaction rollback