各种变换
添加CubeData.h CubeData.cpp
//首先需要对生成物体数据的辅助类进行修改,使其生成可以组装成立方体的数据
#ifndef VULKANEXBASE_BALLDATA_H
#define VULKANEXBASE_BALLDATA_H
class CubeData
{
public:
static float* vdata;
static int dataByteCount;
static int vCount;
static void genBallData();
};
#endif
#include "CubeData.h"
#include <vector>
#include <math.h>
#include <string.h>
float* CubeData::vdata;
int CubeData::dataByteCount;
int CubeData::vCount;
void CubeData::genBallData()
{
float UNIT_SIZE = 1;
float vertices[] =
{
//前面
0,0,UNIT_SIZE,
UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
-UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
0,0,UNIT_SIZE,
-UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
-UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
0,0,UNIT_SIZE,
-UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
0,0,UNIT_SIZE,
UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
//后面
0,0,-UNIT_SIZE,
UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
0,0,-UNIT_SIZE,
UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
-UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
0,0,-UNIT_SIZE,
-UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
-UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
0,0,-UNIT_SIZE,
-UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
//左面
-UNIT_SIZE,0,0,
-UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,
-UNIT_SIZE,UNIT_SIZE,-UNIT_SIZE,
-UNIT_SIZE,0,0,
-UNIT_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IT_SIZE,
0,-UNIT_SIZE,0,
-UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
0,-UNIT_SIZE,0,
UNIT_SIZE,-UNIT_SIZE,-UNIT_SIZE,
UNIT_SIZE,-UNIT_SIZE,UNIT_SIZE,
};
float colors[] = {
//前面
1,1,1,//中间为白色
1,0,0,
1,0,0,
1,1,1,//中间为白色
1,0,0,
1,0,0,
1,1,1,//中间为白色
1,0,0,
1,0,0,
1,1,1,//中间为白色
1,0,0,
1,0,0,
//后面
1,1,1,//中间为白色
0,0,1,
0,0,1,
1,1,1,//中间为白色
0,0,1,
0,0,1,
1,1,1,//中间为白色
0,0,1,
0,0,1,
1,1,1,//中间为白色
0,0,1,
0,0,1,
//左面
1,1,1,//中间为白色
1,0,1,
1,0,1,
1,1,1,//中间为白色
1,0,1,
1,0,1,
1,1,1,//中间为白色
1,0,1,
1,0,1,
1,1,1,//中间为白色
1,0,1,
1,0,1,
//右面
1,1,1,//中间为白色
1,1,0,
1,1,0,
1,1,1,//中间为白色
1,1,0,
1,1,0,
1,1,1,//中间为白色
1,1,0,
1,1,0,
1,1,1,//中间为白色
1,1,0,
1,1,0,
//上面
1,1,1,//中间为白色
0,1,0,
0,1,0,
1,1,1,//中间为白色
0,1,0,
0,1,0,
1,1,1,//中间为白色
0,1,0,
0,1,0,
1,1,1,//中间为白色
0,1,0,
0,1,0,
//下面
1,1,1,//中间为白色
0,1,1,
0,1,1,
1,1,1,//中间为白色
0,1,1,
0,1,1,
1,1,1,//中间为白色
0,1,1,
0,1,1,
1,1,1,//中间为白色
0,1,1,
0,1,1,
};
vCount = (sizeof(vertices) / sizeof(vertices[0])) / 3;
// 顶点的数量为坐标值数量的1/3,因为一个顶点有3个坐标
dataByteCount = (sizeof(vertices) / sizeof(vertices[0])) * 2 * sizeof(float);
vdata = new float[(sizeof(vertices) / sizeof(vertices[0])) * 2];
int index = 0;
for (int i = 0; i<vCount; i++)
{
vdata[index++] = vertices[i * 3 + 0];
vdata[index++] = vertices[i * 3 + 1];
vdata[index++] = vertices[i * 3 + 2];
vdata[index++] = colors[i * 3 + 0];
vdata[index++] = colors[i * 3 + 1];
vdata[index++] = colors[i * 3 + 2];
}
}
准备好cube数据后,修改void MyVulkanManager::createDrawableObject()
//创建绘制用物体
void MyVulkanManager::createDrawableObject()
{
//TriangleData::genVertexData();//生成三色三角形顶点数据和颜色数据
//SixPointedStar::genStarData(0.2,0.5,0);
//triForDraw = new DrawableObjectCommonLight(TriangleData::vdata, TriangleData::dataByteCount, TriangleData::vCount, device, memoryroperties);//创建绘制用三色三角形对象
//objForDraw = new DrawableObjectCommonLight(SixPointedStar::vdata, SixPointedStar::dataByteCount, SixPointedStar::vCount, device, memoryroperties);
objForDraw = new DrawableObjectCommonLight(CubeData::vdata, CubeData::dataByteCount, CubeData::vCount, device, memoryroperties);
}
准备好cube数据后,修改void MyVulkanManager::drawObject()中代码
MatrixState3D::pushMatrix();
//这是鼠标旋转的代码
MatrixState3D::rotate(xAngle, 1, 0, 0);
MatrixState3D::rotate(yAngle, 0, 1, 0);
/*
for (int i = 0; i <= 5; i++)
{
//循环绘制所有六角星
MatrixState3D::pushMatrix(); //保护现场
MatrixState3D::translate(0, 0, i); //沿Z轴平移
objForDraw->drawSelf(cmdBuffer, sqsCL->pipelineLayout,sqsCL->pipeline, &(sqsCL->descSet[0]));//绘制物体
MatrixState3D::popMatrix();
}*/
//修改为
//pushMatrix目的保护现场 防止破坏当前的矩阵
MatrixState3D::pushMatrix(); //保护现场
objForDraw->drawSelf(cmdBuffer, sqsCL->pipelineLayout,sqsCL->pipeline,
&(sqsCL->descSet[0]));//绘制物体
MatrixState3D::popMatrix(); //恢复现场
MatrixState3D::pushMatrix(); //保护现场
//平移后在画另外一个正方体
MatrixState3D::translate(3.5f, 0, 0); //沿x轴平移
objForDraw->drawSelf(cmdBuffer, sqsCL->pipelineLayout, sqsCL->pipeline,
&(sqsCL->descSet[0])); //绘制变换后的立方体
MatrixState3D::popMatrix();
MatrixState3D::popMatrix();
z轴旋转30度
//第二个旋转30度
MatrixState3D::rotate(30, 0, 0, 1); //绕z轴旋转30°
缩放
MatrixState3D::scale(0.4f, 2.0f, 0.6f); //x、y、z 3个方向按各自的缩放因子进行缩放