【发布时间】:2025-12-29 12:15:16
【问题描述】:
我一直在研究位图加载器,主要目标只是正确解析数据并在 OpenGL 中渲染。我现在需要在 x/y(即逐个像素)的基础上绘制像素(至少,就渲染而言,这是我认为我需要做的)。我已经绑定了纹理对象并调用了glTexImage2D(...)。
目前,我遇到的问题是逐像素算法。
据我了解,位图(又名 DIB)文件将颜色数据存储在所谓的像素阵列中。每行像素由x 字节数组成,每个像素的字节数可以被 4(每像素 32 位)、3(每像素 24 位)、2(每像素 16 位)或 1(每像素 8 位)。
我认为需要遍历像素,同时计算像素数组中的正确偏移量,该偏移量相对于像素 x/y 坐标。这是真的吗?如果没有,我应该怎么做? 老实说,我有点困惑,尽管按照this question I asked sometime ago 中的指示,这种方法是否正确。
我认为逐个像素进行处理是正确的方法,主要是因为
用glVertex* 和glTexCoord* 渲染一个四边形只会产生一个灰色的矩形(当时我认为 OpenGL 会自己处理这个问题,因此为什么首先尝试这样做)。
我还应该注意,虽然我的问题显示的是 OpenGL 3.1 着色器,但我移到了 SDL 1.2 所以我可以暂时使用即时模式,直到我实现了正确的算法,然后切换回现代 GL。
我正在解析的测试图像:
它是数据输出(由于长度很长而被粘贴): http://pastebin.com/6RVhAVRq
还有代码:
void R_RenderTexture_PixByPix( texture_bmp_t* const data, const vec3 center )
{
glBindTexture( GL_TEXTURE_2D, data->texbuf_id );
glBegin( GL_POINTS );
{
const unsigned width = data->img_data->width + ( unsigned int ) center[ VEC_X ];
const unsigned height = data->img_data->height + ( unsigned int ) center[ VEC_Y ];
const unsigned bytecount = GetByteCount( data->img_data->bpp );
const unsigned char* pixels = data->img_data->pixels;
unsigned color_offset = 0;
unsigned x_pixel;
for ( x_pixel = center[ VEC_X ]; x_pixel < width; ++x_pixel )
{
unsigned y_pixel;
for ( y_pixel = center[ VEC_Y ]; y_pixel < height; ++y_pixel )
{
}
const bool do_color_update = true; //<--- replace true with a condition which checks to see if the color needs to be updated.
if ( do_color_update )
{
glColor3fv( pixels + color_offset );
}
color_offset += bytecount;
}
}
glEnd();
glBindTexture( GL_TEXTURE_2D, 0 );
}
【问题讨论】:
-
但是你为什么要逐个像素地做呢?绘制一个大小合适的四边形(使用 4 个顶点)不是更好吗?正确地意味着最终的四边形将匹配位图的大小。
-
@fan,这实际上是我尝试做的......这让我很困惑,主要是因为结果是一个灰色的矩形。