1

如何使用 OpenGL glDrawPixels()绘制以下动态3D数组?您可以在此处找到文档: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[i] = (float **)malloc(sizeof(float *) * screenY);

        for (j = 0; j < screenY; j++)
          array3d[i][j] = (float *)malloc(sizeof(float) * /*Z_SIZE*/ 3);
    }
}

我只能使用以下头文件:

#include <math.h>
#include <stdlib.h>
#include <windows.h>     

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h> 
4

3 回答 3

4

呃......由于您使用单独的 分配每个单个像素malloc(),因此您也必须通过单独调用来绘制每个像素glDrawPixels()。这(显然)很疯狂;位图图形的想法是像素以相邻的紧凑格式存储,因此从一个像素移动到另一个像素快速且快速( O(1) )。这对我来说看起来很困惑。

一种更明智的方法是分配“3D 数组”(通常称为 2D 像素数组,其中每个像素恰好由红色、绿色和蓝色分量组成),只需调用一次malloc(),就像这样 (在 C) 中:

float *array3d;
array3d = malloc(scene.camera.hres * scene.camera.vres * 3 * sizeof *array3d);
于 2008-11-04T12:44:28.870 回答
1

谢谢放松。我在gamedev.net上得到了同样的建议,所以我实现了以下算法:

typedef struct
{
    GLfloat R, G, B;
} color_t;

color_t *array1d;

void InitScreenArray()
{   
        long screenX = scene.camera.vres;
    long screenY = scene.camera.hres;
        array1d = (color_t *)malloc(screenX * screenY * sizeof(color_t));
}

void SetScreenColor(int x, int y, float red, float green, float blue)
{
    int screenX = scene.camera.vres;
    int screenY = scene.camera.hres;

    array1d[x + y*screenY].R = red;
    array1d[x + y*screenY].G = green;
    array1d[x + y*screenY].B = blue;
}

void onDisplay( ) 
{
    glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glRasterPos2i(0,0); 
    glDrawPixels(scene.camera.hres, scene.camera.vres, GL_RGB, GL_FLOAT, array1d);

    glFinish();
    glutSwapBuffers();
}

我的应用程序还没有工作(屏幕上什么都没有出现),但我认为这是我的错,这段代码可以工作。

于 2008-11-04T15:17:33.537 回答
0

你不想用 glTexImage2D() 代替吗:见这里

于 2009-12-27T00:52:47.730 回答