【问题标题】:OpenGL glDrawPixels on dynamic 3D arrays动态 3D 数组上的 OpenGL glDrawPixels
【发布时间】:2010-09-20 13:37:27
【问题描述】:

如何用 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> 

【问题讨论】:

  • 是的,但这只是我无法解决的商场部分。整个作业将是一个光线追踪器,这个结构存储屏幕像素。

标签: c++ arrays opengl graphics


【解决方案1】:

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

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

float *array3d;
array3d = malloc(scene.camera.hres * scene.camera.vres * 3 * sizeof *array3d);

【讨论】:

    【解决方案2】:

    感谢放松。我在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();
    }
    

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

    【讨论】:

      【解决方案3】:

      您不想使用 glTexImage2D() 代替吗:请参阅here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-18
        • 1970-01-01
        • 1970-01-01
        • 2012-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-17
        相关资源
        最近更新 更多