2012-03-05 8 views
0

整数に格納された何かのメモリ位置を持っていれば、どのようにオブジェクトをメモリ位置に格納できますか?私はこれを使用する必要がどのようにオブジェクトをメモリ位置に取得するにはどうすればよいですか?

#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)); 
} 
+0

'_buffer'と' Renderdata.vertex'を 'int *'の代わりに 'int'と宣言した理由は? – tia

+0

@tia:私は通常のポインタを持っていないので。変更されたバイト数を手動で制御できるように、データのバイト位置をメモリに格納する必要があります。例えば、私がint配列へのポインタを持っている場合、ポインタ+ 1 =ポインタ+ 3バイト。メモリ位置を格納するintがある場合、ポインタ+ 1 =ポインタ+ 1バイト。 –

+0

代わりに 'char *'や 'void *'を使うのはどうですか? – tia

答えて

2

ポインタ(int *buffer)メモリ・アドレスを含む変数です。ポインタが指し示すアドレスでメモリを読み出すことは、ポインタの逆参照と呼ばれます。あなたがやっていることは、実際にポインタそのもののアドレスを取っていることです。だから、これに代えて:

_buffer = &buffer;

..あなたはこのbufferポインタが指す値読み取りにポインタを間接参照する必要があります:私はあなたがPointer Basics記事を読むことをお勧めします

_buffer = *buffer;

を、それこれらのことを頭の中で少し包み込むのを手伝ってくれるでしょう。

希望します。がんばろう!

UPDATE:あなたのケースでは

は、あなたが...に渡されるオブジェクトと同じ型のポインタを持ったほうが良いですが、そうでなければ、それは非常に多くの混乱です。ここではそのポインタでの作業の例です:

#include <stdio.h> 

typedef struct { 
    double x; 
    double y; 
    double z; 
} Vertex; /* Just to make a simple code compile.. */ 
typedef Vertex Normal; /* Same story... */ 

typedef struct { 
    Vertex position; 
    Normal normal; 
} VertexData; 

static const VertexData mesh[] = { 
    { {-0.000000, -1.125000, 0.000000}, {0.059877, -0.998169, 0.007874} }, 
    { {-0.000000, -0.986528, -0.475087}, {0.114078, -0.863674, -0.490890} }, 
    { {0.357184, -0.948670, -0.284845}, {0.427045, -0.850368, -0.307321} }, 
    { {-0.000000, -1.125000, 0.000000}, {0.059877, -0.998169, 0.007874} }, 
    { {0.357184, -0.948670, -0.284845}, {0.427045, -0.850368, -0.307321} }, 
    { {0.449795, -0.958029, 0.102663}, {0.477462, -0.877438, 0.045442} }, 
    { {-0.000000, -1.125000, 0.000000}, {0.059877, -0.998169, 0.007874} }, 
    { {0.449795, -0.958029, 0.102663}, {0.477462, -0.877438, 0.045442} } 
}; 

void celBindBuffer(const VertexData *data) 
{ 
    const Vertex *v0 = &data[0].position; 
    const Normal *n0 = &data[0].normal; 

    const Vertex *v1 = &data[1].position; 
    const Normal *n1 = &data[1].normal; 

    printf("Vertex#1: %f/%f/%f...\n", v0->x, v0->y, v0->z); 
    printf("Vertex#2: %f/%f/%f...\n", v1->x, v1->y, v1->z); 
} 

int main(void) 
{ 
    celBindBuffer(mesh); 
    return 0; 
} 

あなたが見ることができるように、pointer arithmeticsの少しは仕事をしていません。 しかし、あなたの関数には少し問題があります。あなたがしようとしているのは、VertexDataオブジェクトの配列を渡すことです。しかし問題は、関数celBindBuffer()はその配列にいくつの要素があるのか​​わからないということです。その関数に数値を渡して、ベクトルにいくつの要素があるかを伝えなければなりません。そうしないと、簡単にundefined behaviorに入ることができます。

+0

可変バッファは単一の値だけでなく構造体の配列なので、そのバッファのメモリ位置を持つ必要があります。私はそれを持っていれば、どのようにその場所(行:頂点v =(頂点)(_ renderData.vertex + i * _renderData.vertexStride + _renderData.vertexOffset);)でオブジェクトを受け取ることができますか? –

+0

データの場所を取得するには、_buffer =&(* buffer);を使用します。 –

+0

Stasでは、 'celBindBuffer'関数に正確に何が渡されているかを見ることなく、あなたの質問に答えることは難しいですが、' buffer'変数はデータの場所です。それを得るには、何もする必要はありません。そこから値を読み取る場合は、POD型である限り逆参照することができます。また、ポインタ演算を使用して、 'buffer'が指す複数の要素を移動することもできます。さもなければ 'memcpy'を使ってメモリ領域をコピーすることができます。私は推測する...より良い答えを得るために少し質問を明確にしなければならない... –

0

_buffer = &buffer; 

整数変数_bufferに(ポインタであることを起こる)可変bufferのアドレスを割り当てることを試みています。つまり、ポインタ型のポインタを整数型変数に代入しています。(&オペレータが「のアドレス」を意味する。)おそらく整数_bufferに(整数になります)bufferのアドレスに内容を割り当て

_buffer = *buffer; 

をしたいです。 *演算子は、「このアドレスの内容」を意味します。

関連する問題