如果我有一个以整数形式存储的内存位置,我如何才能将对象存储在内存位置?
我需要如何使用它:
#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 =&amp;(* 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));
}
答案 0 :(得分:2)
指针(int *buffer
)是包含内存地址的变量。在指针指向的地址处读取内存称为取消引用指针。你在做什么实际上是获取指针本身的地址。所以不要这样:
_buffer = &buffer;
..你必须取消引用一个指针来读取这个buffer
指针指向的值:
_buffer = *buffer;
我建议你阅读Pointer Basics文章,它可以帮助你绕过这些事情。
希望它有所帮助。祝你好运!
<强>更新强>
在你的情况下,你最好使用与传入的对象相同类型的指针。否则它会让人非常困惑。以下是使用该指针的示例:
#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。
答案 1 :(得分:0)
该行
_buffer = &buffer;
尝试将变量buffer
的地址(恰好是指针)分配给整数变量_buffer
。换句话说,它将“指针指针”类型分配为整数变量。 (&
运算符表示“地址”。)您可能需要
_buffer = *buffer;
将buffer
中的地址(将为整数)的内容分配给整数_buffer
。 *
运算符表示“此地址的内容。”