C++ 如何在GLSL中表示数组结构的GLVertexAttribute指针的跨步和指针参数
我很难从类似数组的数据结构中绘制顶点。我想这可能是我在C++ 如何在GLSL中表示数组结构的GLVertexAttribute指针的跨步和指针参数,c++,opengl,glsl,vbo,vao,C++,Opengl,Glsl,Vbo,Vao,我很难从类似数组的数据结构中绘制顶点。我想这可能是我在glvertexattributepointer调用中使用跨步和指针参数的方式。我有一个这样的结构: struct RadarReturn_t { float32_t x; float32_t y; float32_t z; float32_t prob; } struct Detections_t { uint32_t currentScanNum; std::array<RadarR
glvertexattributepointer
调用中使用跨步和指针参数的方式。我有一个这样的结构:
struct RadarReturn_t
{
float32_t x;
float32_t y;
float32_t z;
float32_t prob;
}
struct Detections_t
{
uint32_t currentScanNum;
std::array<RadarReturn_t, 64> detections;
}
glGenBuffers(1, &mRadarVbo);
for (uint32_t iScan = 0; iScan < mMaxNumScans; ++iScan)
{
// Set the timestamp to 0U
mPersistentDetections.at(iScan).currentScanNum = 0U;
for (uint32_t iDet = 0; iDet < 64; ++iDet)
{
RadarReturn_t& detection = mPersistentDetections.at(iScan).detections.at(iDet);
detection.x = 0.0F;
detection.y = 0.0F;
detection.z = 0.0F;
detection.probability = 0.0F;
}
}
// Bind the VBO and copy the initial data to the graphics card
glBindBuffer(GL_ARRAY_BUFFER, mRadarVbo);
glBufferData(GL_ARRAY_BUFFER,
mMaxNumScans * sizeof(DetectionData_t),
&mPersistentDetections,
GL_DYNAMIC_DRAW);
struct RadarReturn_t
{
float32_t x;
float32_t y;
float32_t z;
float32_t prob;
int32_t scanCounter;
};
glVertexAttribPointer(0,
4,
GL_FLOAT,
GL_FALSE,
sizeof(RadarReturn_t),
nullptr));
glVertexAttribIPointer(1,
1,
GL_INT,
sizeof(RadarReturn_t),
(void*) (4 * sizeof(GL_FLOAT)));
我在另一个结构中使用了RadarReturn\t
,如下所示:
struct RadarReturn_t
{
float32_t x;
float32_t y;
float32_t z;
float32_t prob;
}
struct Detections_t
{
uint32_t currentScanNum;
std::array<RadarReturn_t, 64> detections;
}
glGenBuffers(1, &mRadarVbo);
for (uint32_t iScan = 0; iScan < mMaxNumScans; ++iScan)
{
// Set the timestamp to 0U
mPersistentDetections.at(iScan).currentScanNum = 0U;
for (uint32_t iDet = 0; iDet < 64; ++iDet)
{
RadarReturn_t& detection = mPersistentDetections.at(iScan).detections.at(iDet);
detection.x = 0.0F;
detection.y = 0.0F;
detection.z = 0.0F;
detection.probability = 0.0F;
}
}
// Bind the VBO and copy the initial data to the graphics card
glBindBuffer(GL_ARRAY_BUFFER, mRadarVbo);
glBufferData(GL_ARRAY_BUFFER,
mMaxNumScans * sizeof(DetectionData_t),
&mPersistentDetections,
GL_DYNAMIC_DRAW);
struct RadarReturn_t
{
float32_t x;
float32_t y;
float32_t z;
float32_t prob;
int32_t scanCounter;
};
glVertexAttribPointer(0,
4,
GL_FLOAT,
GL_FALSE,
sizeof(RadarReturn_t),
nullptr));
glVertexAttribIPointer(1,
1,
GL_INT,
sizeof(RadarReturn_t),
(void*) (4 * sizeof(GL_FLOAT)));
其中mPersistentDetections
为:
std::阵列检测代码>
稍后在我的代码中,我将用新的传入数据更新缓冲区,如下所示,用于currentScanNum
:
// Offset is: 64 Radar returns plus one scanNumber
uint32_t offset = scanNum* ((64*sizeof(RadarReturn_t)) + 1*sizeof(GLuint));
glBufferSubData(GL_ARRAY_BUFFER, offset , sizeof(GLuint), &mPersistentDetections.at(scanNum).currentScanNum)
对于检测,就像这样:
uint32_t dataSize = 64 * sizeof(RadarReturn_t);
glBufferSubData(GL_ARRAY_BUFFER,
offset + sizeof(GLuint),
dataSize,
&mPersistentDetections.at(scanNum).detections);
这就是我代表VAO的方式:
// Bind the VAO
glBindVertexArray(mRadarVao);
// Specify the layout of timestamp data
glVertexAttribIPointer(0,
1,
GL_UNSIGNED_INT,
sizeof(DetectionData_t),
(GLvoid*) 0);
// Specify the layout of the radar return data
glVertexAttribPointer(1,
4,
GL_FLOAT,
GL_FALSE,
sizeof(DetectionData_t),
(GLvoid*) (sizeof(GLuint)));
最后是抽签通知:
gldrawArray(GL_点,0,mMaxNumScans*64)代码>
如果我为mMaxNumScans=100绘制此图,出于某种原因,我无法在此处绘制100x64顶点。你能告诉我哪里出了问题吗
编辑:
根据@rabbi76的建议,我修改了Detections\u t
struct如下:
struct Detections_t
{
std::array<RadarReturn_t, 64> detections;
std::array<uint32_t, 64> scanNumbers;
}
如果我将属性0的步长设置为sizeof(Detection\u t)
,则不会绘制所有点。仅sizeof(RadarReturn\t)
绘制所有点
如果我将属性1的步长设置为sizeof(Detection\u t)
,则检测的颜色(我使用scanNumber来改变alpha值)仅在几次扫描后就会变得透明
如果有人能告诉我在这种情况下属性0和属性1的步长值应该是多少,我将不胜感激。我将回答我的问题。我可以通过如下方式更改RadarReturn\t
结构使其正常工作:
struct RadarReturn_t
{
float32_t x;
float32_t y;
float32_t z;
float32_t prob;
}
struct Detections_t
{
uint32_t currentScanNum;
std::array<RadarReturn_t, 64> detections;
}
glGenBuffers(1, &mRadarVbo);
for (uint32_t iScan = 0; iScan < mMaxNumScans; ++iScan)
{
// Set the timestamp to 0U
mPersistentDetections.at(iScan).currentScanNum = 0U;
for (uint32_t iDet = 0; iDet < 64; ++iDet)
{
RadarReturn_t& detection = mPersistentDetections.at(iScan).detections.at(iDet);
detection.x = 0.0F;
detection.y = 0.0F;
detection.z = 0.0F;
detection.probability = 0.0F;
}
}
// Bind the VBO and copy the initial data to the graphics card
glBindBuffer(GL_ARRAY_BUFFER, mRadarVbo);
glBufferData(GL_ARRAY_BUFFER,
mMaxNumScans * sizeof(DetectionData_t),
&mPersistentDetections,
GL_DYNAMIC_DRAW);
struct RadarReturn_t
{
float32_t x;
float32_t y;
float32_t z;
float32_t prob;
int32_t scanCounter;
};
glVertexAttribPointer(0,
4,
GL_FLOAT,
GL_FALSE,
sizeof(RadarReturn_t),
nullptr));
glVertexAttribIPointer(1,
1,
GL_INT,
sizeof(RadarReturn_t),
(void*) (4 * sizeof(GL_FLOAT)));
然后使用如下属性:
struct RadarReturn_t
{
float32_t x;
float32_t y;
float32_t z;
float32_t prob;
}
struct Detections_t
{
uint32_t currentScanNum;
std::array<RadarReturn_t, 64> detections;
}
glGenBuffers(1, &mRadarVbo);
for (uint32_t iScan = 0; iScan < mMaxNumScans; ++iScan)
{
// Set the timestamp to 0U
mPersistentDetections.at(iScan).currentScanNum = 0U;
for (uint32_t iDet = 0; iDet < 64; ++iDet)
{
RadarReturn_t& detection = mPersistentDetections.at(iScan).detections.at(iDet);
detection.x = 0.0F;
detection.y = 0.0F;
detection.z = 0.0F;
detection.probability = 0.0F;
}
}
// Bind the VBO and copy the initial data to the graphics card
glBindBuffer(GL_ARRAY_BUFFER, mRadarVbo);
glBufferData(GL_ARRAY_BUFFER,
mMaxNumScans * sizeof(DetectionData_t),
&mPersistentDetections,
GL_DYNAMIC_DRAW);
struct RadarReturn_t
{
float32_t x;
float32_t y;
float32_t z;
float32_t prob;
int32_t scanCounter;
};
glVertexAttribPointer(0,
4,
GL_FLOAT,
GL_FALSE,
sizeof(RadarReturn_t),
nullptr));
glVertexAttribIPointer(1,
1,
GL_INT,
sizeof(RadarReturn_t),
(void*) (4 * sizeof(GL_FLOAT)));
顶点及其属性是元组。因此,每个属性的数组元素数必须相同。不能生成属性的排列。在您的实现中,为属性0指定了100个元素,但为属性1指定了64*100个元素。这是不可能的。您还必须为属性0指定100*64个元素。谢谢您的回复!是的,我想可能是这样,但我很高兴得到外界的意见。我希望听到一种高效的方法来实现我想要的功能,而不必创建冗余的属性0元素。当数据存储在一个数组中,且所有属性都占用相同的连续内存区域时,为什么需要两个缓冲区?只要正确使用步幅。@Nicolas:我使用的是一个VBO。