[英]using a struct to iterate through coordinates in cmake
我在 cmake 的网格系统上工作,我想使用结构自动创建手柄,这是我正在使用的示例
struct Vertex {
float xyzw[4];
};
该结构将用于保存顶点
Vertex verts[] = {}
目前,我的网格系统是这样构建的
Vertex L1 = { 1, 0, 0, 1 };
Vertex L2 = { -1, 0, 0, 1 };
L1 和 L2 将 go 放入我的 verts 中
verts[] = {L1, L2}
这将创建一条线。 现在我在想办法用for循环自动化这个过程时遇到了麻烦。 任何建议都会很棒
除了使用 arrays,您还可以使用int
指针,它们基本上是 arrays。我认为您可以这样做:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
int verts1[] = { 1, 0, 0, 1 };
int verts2[] = { -1, 0, 0, 1 };
int** vertsOut;
vertsOut = (int**) malloc(sizeof(int*) * 4); // Initialize the out pointer to pointers, with 8 vertices
for(int i = 0; i < 4; i++) {
vertsOut[i] = (int*)malloc(sizeof(int) * 2); // Initialize each individual vertex
}
// Copy over the vertices
for(int i = 0; i < 4; i++) {
vertsOut[i][0] = verts1[i];
}
for(int i = 0; i < 4; i++) {
vertsOut[i][1] = verts2[i];
}
for(int i = 0; i < 4; i++) {
cout << "x: " << vertsOut[i][0] << " y: " << vertsOut[i][1] << endl;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.