【发布时间】:2012-03-05 03:10:02
【问题描述】:
如果我的内存位置存储在整数中,我如何才能将对象存储在内存位置?
我需要如何使用它:
#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; 收到警告?
编辑:
变量缓冲区是一个结构数组,而不仅仅是一个值,所以我需要它的内存位置。一旦我有了那个,我怎样才能在那个位置接收对象(线路:Vertex v = (Vertex)(_renderData.vertex + i * _renderData.vertexStride + _renderData.vertexOffset);)?
编辑: 所以要获取数据的位置,我会使用 _buffer = &(*buffer); 吗?
编辑 传递给方法的信息:
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:因为我不想有一个常规指针。我需要将数据的字节位置存储在内存中,以便手动控制更改的字节数。例如,如果我有一个指向 int 数组的指针,则指针 + 1 = 指针 + 3 个字节。如果我有一个 int 来存储内存位置,那么指针 + 1 = 指针 + 1 字节。
-
那么改用
char *或void *怎么样?
标签: c arrays memory pointers struct