Skip to main content
GameDev.net gamedev.net
🔒 Locked

OpenGL glDrawPixels on dynamic 3D arrays

Started by artur02 Nov 4, 2008 at 12:43 AM 3 replies 2.1k views
Original Post
artur02
artur02
How do you draw the following dynamic 3D array with OpenGL glDrawPixels()? You can find the documentation here: http://opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/drawpixels.html float ***array3d; void InitScreenArray() { int i, j; int screenX = scene.camera.vres; int screenY = scene.camera.hres; array3d = (float ***)malloc(sizeof(float **) * screenX); for (i = 0 ; i < screenX; i++) { array3d = (float **)malloc(sizeof(float *) * screenY); for (j = 0; j < screenY; j++) array3d[j] = (float *)malloc(sizeof(float) * /*Z_SIZE*/ 3); } } I can use only the following header files: #include #include #include #include #include #include
Brother Bob
Brother Bob
Since OpenGL expects the data passed to glDrawElement to be contiguous, you have to draw the data pixel by pixel, since only the RGB components of a single pixel are contiguous. That is, something like this.
for(int y = 0; y < screenY; ++y){    for(int x = 0; x < screenX; ++x)    {        glRasterPos(x, y);        glDrawPixels(1, 1, GL_RGB, GL_FLOAT, array3d[x][y]);    }}

If you want to draw it in a single call, you need to make the array flat in memory, not a pointer to pointers to pointers to individual pixel's color data.
float *array1d = malloc(screenX * screenY * 3 * sizeof(float));

That array can be passed directly to glDrawPixels.
artur02
artur02
I think pixel-by-pixel rendering is too slow.

I think I can start with 1D array, that's a great idea!
Can you tell me how can I map the [x][y][color] structure to this 1D array?


I would put x,y,color information like array1d[0]=x, array1d[1]=y, array1d[2] = color.r, array1d[3] = color.g, array1d[4] = color.b, ...
Brother Bob
Brother Bob
Easiest would be to factor the array into more distinct components. Start with a simple color structure. Then focus on indexing individual pixels, instead of individual componets of individual pixels.
typedef struct{    GLfloat R, G, B;} colot_t;color_t *array1d = malloc(screenX * screenY * sizeof(color_t));

From the start of the array, going to the next pixel on a row will offset the index by 1. Going to the next row will offset the index by screenX pixels. So...
array1d[x + y*screenY].R = ...array1d[x + y*screenY].G = ...array1d[x + y*screenY].B = ...

There's the index for pixel (x,y) and its R, G and B components.
artur02
artur02
It works now, somehow nothing appears in the window. It can be my fault, I will check it. Thanks for your help!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.