【发布时间】:2016-04-26 13:15:37
【问题描述】:
glDrawElements 一直在工作,最初是一个简单的盒子,然后是由大量顶点组成的更复杂的形状。然后它只是停止绘制网格。我已经将代码恢复为最基本的,只需绘制 2 个三角形来制作一个 2D 正方形。这也不再有效。
void createMesh(void) {
float vertices[12];
vertices[0] = -0.5; vertices[1] = -0.5; vertices[2] = 0.0; // Bottom left corner
vertices[3] = -0.5; vertices[4] = 0.5; vertices[5] = 0.0; // Top left corner
vertices[6] = 0.5; vertices[7] = 0.5; vertices[8] = 0.0; // Top Right corner
vertices[9] = 0.5; vertices[10] = -0.5; vertices[11] = 0.0; // Bottom right corner
short indices[] = { 0, 1, 2, 0, 2, 3};
glEnableClientState(GL_VERTEX_ARRAY); // Enable Vertex Arrays
glVertexPointer(3, GL_FLOAT, 0, vertices); // Set The Vertex Pointer To Our Vertex Data
glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_SHORT, indices);
glDisableClientState(GL_VERTEX_ARRAY);
}
以前可以工作的更高级的代码如下所示:
void createMesh(void) {
float vertices[(amountOfHorizontalScans * 480 * 3)];// Amount of vertices
//build the array of vertices from a matrix of data
int currentVertex = -1;
std::vector <std::vector<double>> currentPointCloudMatrix = distanceCalculator.getPointCloudMatrix();
double plotY = 0;
double plotX = 0;
for (int j = 0; j < currentPointCloudMatrix.size(); j++){
std::vector <double> singleDistancesVector = currentPointCloudMatrix.at(j);
for (int i = 0; i < singleDistancesVector.size(); i++){
if (singleDistancesVector.at(i) != 0){
vertices[++currentVertex] = plotX;
vertices[++currentVertex] = plotY;
vertices[++currentVertex] = singleDistancesVector.at(i);
}
plotX += 0.1;
}
plotX = 0;
plotY += 0.2; //increment y by 0.02
}
//Creating the array of indices, 480 is the amount of columns
int i = 0;
short indices2[(amountOfHorizontalScans * 480 * 3)];
for (int row = 0; row<amountOfHorizontalScans - 1; row++) {
if ((row & 1) == 0) { // even rows
for (int col = 0; col<480; col++) {
indices2[i++] = col + row * 480;
indices2[i++] = col + (row + 1) * 480;
}
}
else { // odd rows
for (int col = 480 - 1; col>0; col--) {
indices2[i++] = col + (row + 1) * 480;
indices2[i++] = col - 1 + +row * 480;
}
}
}
glEnableClientState(GL_VERTEX_ARRAY); // Enable Vertex Arrays
glVertexPointer(3, GL_FLOAT, 0, vertices); // Set The Vertex Pointer To Our Vertex Data
glDrawElements(GL_TRIANGLE_STRIP, (amountOfHorizontalScans * 480 * 3), GL_UNSIGNED_SHORT, indices2);
glDisableClientState(GL_VERTEX_ARRAY);
}
我完全不知道它为什么停止工作,因为它运行了很多次都可以完美运行,然后就完全停止了。我已经调试过了,所有代码都被访问了,顶点和索引也填充了数据。什么可能导致它停止工作?
编辑: 所以我现在真的很困惑。今天早上我回到了这个问题,一切都再次正常,因为在网格中绘制没有问题。在做了一些测试并多次运行程序后,它再次停止绘制网格!
这可能与记忆有关吗?我不是 100% 确定 glDrawElements 如何存储传递给它的数据,所以我是否必须在某个地方清除一些我不断填充数据的东西?
【问题讨论】:
-
“已停止工作”有点含糊。当您运行代码时会发生什么,它与您的预期有何不同?是否有任何编译时/运行时错误?
-
啊,我的错,它曾经为我拥有的顶点创建一个可见的网格,无论是简单的正方形还是矩阵中的一大组顶点。然后它只是停止创建网格,看不到任何错误,它不再绘制任何东西。
-
如果您还没有这样做,请尝试致电
glGetError()。您正在创建什么类型/版本的上下文?
标签: c++ visual-studio opengl graphics vertex