我正在尝试动态分配一个float数组(distances
),但单步执行调试器会显示它只是出于某种原因只分配一个。
我已经尝试过std :: vector并且它可以正常运行,但它在Vec2 * points = Util::BCurvePerpPoint(i, p1, p2, p3, this->distances, this->m_NumGeoms * 2)
失败了。问题是距离不会改变对象生命周期的大小,因此使用std :: vector有点过分。我唯一使用std :: vector in的是Draw
函数(我认为我可以修复它的大小是静态的,但是如果step
改变了,那么大小阵列会改变......是啊......)。
注意:Util :: BCurvePerpPoint返回一个Vec2数组,其中第一个是时间i
处的Bézier曲线点。它所做的只是在Bézier曲线的切线的垂直线上提供点。功能原型:
BCurvePerpPoint(float time, Vec2 p1, Vec2 p2, Vec2 p3, float * distance, const int numDists);
部首:
#ifndef _H_HOLDTAIL_
#define _H_HOLDTAIL_
#pragma once
#include "../../OD_Draw2d.h"
namespace Game {
struct HoldTailGeom {
float d1,d2;
ColorF color1, color2;
uint32 packedCol1, packedCol2;
HoldTailGeom() :
d1(0.0f),
d2(0.0f),
color1(0.0f, 0.0f, 0.0f, 0.0f),
color2(0.0f, 0.0f, 0.0f, 0.0f),
packedCol1(0),
packedCol2(0)
{};
};
class HoldTail {
public:
HoldTail(HoldTailGeom * geoms, const unsigned int numGeoms);
~HoldTail();
void Draw(Vec2 p1, Vec2 p2, Vec2 p3, float tStart, float tEnd, float step = 0.05f, bool isolatedDraw = false);
void Draw(Vec2 p1, Vec2 p2, Vec2 p3, float tStart, float tEnd, int numPoints, bool isolatedDraw = false);
private:
HoldTailGeom * m_Geoms;
unsigned int m_NumGeoms;
float * distances;
//std::vector<float> distances;
std::shared_ptr<OD_Draw2d> OD_Draw2dPtr;
std::vector<SVF_P3F_C4B_T2F> *line;
};
}
#endif //_H_HOLDTAIL_
代码:
#include <StdAfx.h>
#include "HoldTail.h"
namespace Game {
HoldTail::HoldTail(HoldTailGeom * geoms, const unsigned int numGeoms) : m_Geoms(geoms), m_NumGeoms(numGeoms) {
this->line = new std::vector<SVF_P3F_C4B_T2F>[numGeoms];
this->distances = new float[numGeoms * 2]();
//distances = new std::vector<float>();
for (int i = 0; i < numGeoms; i++) { //for each geometry
//convert the colors to uint32 for quicker assignment to vector data.
this->m_Geoms[i].packedCol1 = this->m_Geoms[i].color1.pack_argb8888();
this->m_Geoms[i].packedCol2 = this->m_Geoms[i].color2.pack_argb8888();
//convert the distances to an array that we can use.
//int i1 = i * 2;
//int i2 = (i * 2) + 1;
this->distances[i * 2] = m_Geoms[i].d1;
this->distances[(i * 2) + 1] = m_Geoms[i].d2;
//this->distances.push_back(m_Geoms[i].d1);
//this->distances.push_back(m_Geoms[i].d2);
}
//this->distances.shrink_to_fit();
}
HoldTail::~HoldTail() {
for (int i = 0; i < this->m_NumGeoms; i++) this->line[i].clear(); //clear the data just to be sure.
delete[] this->line;
delete[] this->distances;
//this->distances.clear();
}
void HoldTail::Draw(Vec2 p1, Vec2 p2, Vec2 p3, float tStart, float tEnd, int numPoints, bool isolatedDraw) {
if (tStart >= tEnd) return;
this->Draw(p1, p2, p3, tStart, tEnd, (float)((float)(tEnd - tStart) / (float)(numPoints)), isolatedDraw);
}
void HoldTail::Draw(Vec2 p1, Vec2 p2, Vec2 p3, float tStart, float tEnd, float step, bool isolatedDraw) {
if (tStart >= tEnd) return;
for (float i = tStart; i < tEnd+step; i += step) { //from start time to end time with a step between each
Vec2 * points = Util::BCurvePerpPoint(i, p1, p2, p3, this->distances, this->m_NumGeoms * 2); //calculate the distances at time i.
for (int i2 = 0; i2 < this->m_NumGeoms; i2++) { //for each geometry
SVF_P3F_C4B_T2F tmp;
//push back the vectors
tmp.xyz = Vec3(points[(i2 * 2)+1].x, points[(i2 * 2)+1].y, 1);
tmp.color.dcolor = this->m_Geoms[i2].packedCol1;
tmp.st = Vec2(0, 0);
this->line[i2].push_back(tmp);
tmp.xyz = Vec3(points[(i2 * 2)+2].x, points[(i2 * 2)+2].y, 1);
tmp.color.dcolor = this->m_Geoms[i2].packedCol2;
this->line[i2].push_back(tmp);
}
}
if (isolatedDraw) this->OD_Draw2dPtr->BeginDraw2d(1280, 720);
for (int i = 0; i < this->m_NumGeoms; i++) { //for each geometry
this->OD_Draw2dPtr->DrawTriangleStrip(&this->line[i][0], this->line[i].size()); //draw the line
this->line[i].clear(); //done using the line, clear it for next pass.
}
if (isolatedDraw) this->OD_Draw2dPtr->EndDraw2d();
}
}
答案 0 :(得分:0)
修正了......
显然m_Geoms
没有得到任何数据,即使在构造函数中我已经使用m_Geoms(geoms)
进行了分配。
它没有复制数组...
所以我分别调整了构造函数和解构函数:
HoldTail::HoldTail(HoldTailGeom * geoms, const unsigned int numGeoms) : m_NumGeoms(numGeoms), distances(nullptr), line(nullptr) {
this->line = new std::vector<SVF_P3F_C4B_T2F>[numGeoms];
this->distances = new float[numGeoms * 2]();
this->m_Geoms = new HoldTailGeom[numGeoms];
//distances = new std::vector<float>();
for (int i = 0; i < numGeoms; i++) { //for each geometry
//copy data...
this->m_Geoms[i].d1 = geoms[i].d1;
this->m_Geoms[i].d2 = geoms[i].d2;
this->m_Geoms[i].color1 = geoms[i].color1;
this->m_Geoms[i].color2 = geoms[i].color2;
//convert the colors to uint32 for quicker assignment to vector data.
this->m_Geoms[i].packedCol1 = geoms[i].color1.pack_argb8888();
this->m_Geoms[i].packedCol2 = geoms[i].color2.pack_argb8888();
//convert the distances to an array that we can use.
//int i1 = i * 2;
//int i2 = (i * 2) + 1;
this->distances[i * 2] = m_Geoms[i].d1;
this->distances[(i * 2) + 1] = m_Geoms[i].d2;
//this->distances.push_back(m_Geoms[i].d1);
//this->distances.push_back(m_Geoms[i].d2);
}
//this->distances.shrink_to_fit();
}
HoldTail::~HoldTail() {
for (int i = 0; i < this->m_NumGeoms; i++) this->line[i].clear(); //clear the data just to be sure.
delete[] this->line;
delete[] this->distances;
delete[] this->m_Geoms;
//this->distances.clear();
}
修复了这个问题。 猜猜我已经在java上编程太久了......&gt;。&lt;