[英]How do I get an object at a memory location?
如果我有一个存储在整数的东西的内存位置怎么能让对象存储在内存位置?
我需要如何使用它:
#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;
处收到警告_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));
}
指针( 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;
}
正如你所看到的,一点指针算术可以完成这项工作。 但是,您的函数存在一些问题 - 您要做的是传递一个VertexData
对象数组。 但问题是函数celBindBuffer()
不知道该数组中有多少个元素......您必须将一个数字传递给该函数以告知向量中有多少元素。 否则,您很容易遇到未定义的行为 。
这条线
_buffer = &buffer;
试图将变量buffer
的地址(恰好是一个指针)分配给整数变量_buffer
。 换句话说,它将“指针指针”类型分配为整数变量。 ( &
运算符的意思是“地址”。)你可能想要
_buffer = *buffer;
它将buffer
中的地址(将是一个整数)的内容分配给整数_buffer
。 *
运算符表示“此地址的内容”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.