По сути, мой подход следующий:
- Передать массив объектов Vertex () и uint16_t s (
Код: Выделить всё
vertices) в конструкторКод: Выделить всё
indices - Allocate m_vertices с использованием calloc (sizeof (vertex), sizeof (vertices)) , и сделайте что -то похожее для m_indices < /code> < /li>
Вершины массив в недавно выделенные m_vertices и еще раз сделайте что -то похожее для индексов Когда объект сетки уничтожен
Код: Выделить всё
memcpy
Vertex struct:
Код: Выделить всё
typedef struct
{
float x;
float y;
float z;
uint32_t abgr;
} Vertex;
Код: Выделить всё
static Vertex cubeVertices[] =
{
{-1.0f, 1.0f, 1.0f, 0xff888888 },
{ 1.0f, 1.0f, 1.0f, 0xff8888ff },
{-1.0f, -1.0f, 1.0f, 0xff88ff88 },
{ 1.0f, -1.0f, 1.0f, 0xff88ffff },
{-1.0f, 1.0f, -1.0f, 0xffff8888 },
{ 1.0f, 1.0f, -1.0f, 0xffff88ff },
{-1.0f, -1.0f, -1.0f, 0xffffff88 },
{ 1.0f, -1.0f, -1.0f, 0xffffffff },
};
static uint16_t cubeIndices[] =
{
0, 1, 2,
1, 3, 2,
4, 6, 5,
5, 6, 7,
0, 2, 4,
4, 2, 6,
1, 5, 3,
5, 7, 3,
0, 4, 1,
4, 5, 1,
2, 3, 6,
6, 3, 7,
};
Код: Выделить всё
Mesh::Mesh(Vertex* vertices, uint16_t* indices)
{
m_vertices = (Vertex*) calloc(sizeof(Vertex), sizeof(vertices));
m_indices = (uint16_t*) calloc(sizeof(uint16_t), sizeof(indices));
memcpy(m_vertices, vertices, sizeof(vertices));
memcpy(m_indices, indices, sizeof(indices));
for (int i = 0; i < sizeof(m_vertices); i++)
{
printf("Vertex %d\nx: %f, y: %f, z: %f\n",
i,
m_vertices[i].x,
m_vertices[i].y,
m_vertices[i].z
);
}
}
Код: Выделить всё
class Mesh
{
public:
Mesh(Vertex* vertices, uint16_t* indices);
virtual ~Mesh();
Vertex* getVertices() { return m_vertices; }
uint16_t* getIndices() { return m_indices; }
private:
Vertex* m_vertices;
uint16_t* m_indices;
};
Это результат моего кода отладки в конструкторе:
Код: Выделить всё
Vertex 0
x: -1.000000, y: 1.000000, z: 0.000000
Vertex 1
x: 0.000000, y: 0.000000, z: 0.000000
Vertex 2
x: 0.000000, y: 0.000000, z: 0.000000
Vertex 3
x: 0.000000, y: 0.000000, z: 0.000000
Vertex 4
x: 0.000000, y: 0.000000, z: 0.000000
Vertex 5
x: 0.000000, y: 0.000000, z: 0.000000
Vertex 6
x: 0.000000, y: 0.000000, z: 0.000000
Vertex 7
x: 0.000000, y: 0.000000, z: 0.000000
Подробнее здесь: https://stackoverflow.com/questions/793 ... er-variabl