整数に格納された何かのメモリ位置を持っていれば、どのようにオブジェクトをメモリ位置に格納できますか?私はこれを使用する必要がどのようにオブジェクトをメモリ位置に取得するにはどうすればよいですか?
:
#include <stdio.h>
#include "CelGL.h"
/*
Stride: The size of the data structure containing the per-vertex data
Offset: Offset within the structure to find this data
*/
// Example structs that store the data
typedef struct {
float x;
float y;
float z;
} Vertex;
typedef struct {
float x;
float y;
float z;
} Normal;
typedef struct {
float r;
float g;
float b;
} Color;
// Info for rendering
typedef struct {
int vertex;
int vertexStride;
int vertexOffset;
int normal;
int normalStride;
int normalOffset;
int index;
} RenderData;
RenderData _renderData;
int _buffer; // Memory location of array with data
// sets the integer to the memory location of the data
void celBindBuffer(int *buffer)
{
_buffer = &buffer; // Alert:Incompatible pointer to integer conversion assigning to 'int' from 'int **'
}
void celVertexAttribPointer(CelVertexAttrib attrib, int stride/*bytes*/, int offset/*bytes*/)
{
switch (attrib) {
case CelVertexAttribPosition:
_renderData.vertex = _buffer;
_renderData.vertexStride = stride;
_renderData.vertexOffset = offset;
break;
case CelVertexAttribNormal:
_renderData.normal = _buffer;
_renderData.normalStride = stride;
_renderData.normalOffset = offset;
default:
break;
}
}
void printVertex(Vertex v)
{
printf("Vertex[%f,%f,%f]\n", v.x, v.y, v.z);
}
void testPrint(int size/*size in bytes*/)
{
for (int i = 0; i < size; i++) {
// Gets the initial location of the data in which it is stored, gets the size of the struct and multiplies it by the index and then offsets to the
Vertex v = (Vertex)(_renderData.vertex + i * _renderData.vertexStride + _renderData.vertexOffset); // How can I do this?
printVertex(v);
}
}
がどのように私はその場所でオブジェクトを取得することになっていて、なぜ私は_buffer = &buffer;
で警告取得していますか?
EDIT: 可変バッファは、単一の値だけでなく構造体の配列なので、そのメモリの場所が必要です。私はそれを持っていれば、どのようにその場所(ライン:Vertex v = (Vertex)(_renderData.vertex + i * _renderData.vertexStride + _renderData.vertexOffset);
)でオブジェクトを受け取ることができますか?
EDIT: は、だから私は_buffer = &(*バッファ)を使用したデータの位置を取得します;?メソッドに渡さ
EDIT 情報:
typedef struct {
Vertex position;
Normal normal;
} VertexData;
static const VertexData mesh[] = {
{/*v:*/{-0.000000, -1.125000, 0.000000}, /*n:*/{0.059877, -0.998169, 0.007874} },
{/*v:*/{-0.000000, -0.986528, -0.475087}, /*n:*/{0.114078, -0.863674, -0.490890} },
{/*v:*/{0.357184, -0.948670, -0.284845}, /*n:*/{0.427045, -0.850368, -0.307321} },
{/*v:*/{-0.000000, -1.125000, 0.000000}, /*n:*/{0.059877, -0.998169, 0.007874} },
{/*v:*/{0.357184, -0.948670, -0.284845}, /*n:*/{0.427045, -0.850368, -0.307321} },
{/*v:*/{0.449795, -0.958029, 0.102663}, /*n:*/{0.477462, -0.877438, 0.045442} },
{/*v:*/{-0.000000, -1.125000, 0.000000}, /*n:*/{0.059877, -0.998169, 0.007874} },
{/*v:*/{0.449795, -0.958029, 0.102663}, /*n:*/{0.477462, -0.877438, 0.045442} },
...
};
void render() {
celBindBuffer(mesh);
celVertexAttribPointer(CelVertexAttribPosition, sizeof(VertexData), offsetof(VertexData, position));
testPrint(sizeof(mesh)/sizeof(VertexData));
}
'_buffer'と' Renderdata.vertex'を 'int *'の代わりに 'int'と宣言した理由は? – tia
@tia:私は通常のポインタを持っていないので。変更されたバイト数を手動で制御できるように、データのバイト位置をメモリに格納する必要があります。例えば、私がint配列へのポインタを持っている場合、ポインタ+ 1 =ポインタ+ 3バイト。メモリ位置を格納するintがある場合、ポインタ+ 1 =ポインタ+ 1バイト。 –
代わりに 'char *'や 'void *'を使うのはどうですか? – tia