当前位置:网站首页>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 分别各准备一套。
边栏推荐
- 网络工程师入门必懂华为认证体系,附系统学习路线分享
- Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis
- 自定义通用分页标签01
- 文件系统的简单操作
- [Skill] Using Sentinel to achieve priority processing of requests
- JVM Notes
- The video of machine learning to learn [update]
- 一文详解DHCP原理及配置
- 【21天学习挑战赛】图像的旋转问题(二维数组)
- ADC噪声全面分析 -03- 利用噪声分析进行实际设计
猜你喜欢
随机推荐
Learn iframes and use them to solve cross-domain problems
MRS: Introduction to the use of Alluxio
2022年软件测试——精选金融银行面试真题
【源码】使用深度学习训练一个游戏
Implementing a server-side message active push solution based on SSE
小程序 + 电商,玩转新零售
数组相关 内容 解析
嵌入式数据库开发编程MySQL(全)
网络工程师入门必懂华为认证体系,附系统学习路线分享
2.15 keil使用电脑端时间日期
Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
2022杭电多校联赛第五场 题解
The Shell function
打造一份优雅的简历
七夕节,我用代码制作了表白信封
什么是数字孪生智慧城市应用场景
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
Oracle与Postgresql在PLSQL内事务回滚的重大差异
文件内容的操作
Embedded database development programming MySQL (full)





![出现504怎么办?由于服务器更新导致的博客报504错误[详细记录]](/img/e0/32d78fac04dc2deb1cb1f847a7bab5.png)



