If I have the memory location of something stored in an integer how can I get the object stored at the memory location?
How I need to use this:
#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);
}
}
How am I supposed to get the object at that location, and why am I getting a warning at _buffer = &buffer;?
EDIT:
The variable buffer is an array of structs, not just a single value so I need to have the memory location of it. Once I have that, how can I receive the object at that location (Line:Vertex v = (Vertex)(_renderData.vertex + i * _renderData.vertexStride + _renderData.vertexOffset);)?
EDIT:
So to get the location of the data would I use _buffer = &(*buffer);?
EDIT
Information passed onto method:
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));
}
A pointer (
int *buffer) is a variable that contains a memory address. Reading the memory at an address pointed by a pointer is called dereferencing a pointer. What you are doing is actually taking an address of the pointer itself. So instead of this:_buffer = &buffer;.. you have to dereference a pointer to read a value pointed by this
bufferpointer:_buffer = *buffer;I recommend you to read Pointer Basics article, it can help you wrap your head around these things a little bit.
Hope it helps. Good luck!
UPDATE:
In your case, you are better off having a pointer of the same type as the object that is being passed in.. Otherwise it is quite a lot confusing. Here is an example of working with that pointer:
As you can see, a little bit of pointer arithmetics does the job.
However, there is a bit of a problem with your function – what you are trying to do is to pass an array of
VertexDataobjects. But the problem is that functioncelBindBuffer()does not know how many elements are there in that array… You have to pass a number to that function to tells how many elements are there in a vector. Otherwise you can easily run into undefined behavior.