我一直在研究位图加载器,其主要目标只是解析数据并在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 。
测试图像我解析:
这是数据输出(由于pastebinned其长度非常长): 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个顶点)并不会更好。正确意味着最终的四元组将匹配位图的大小。 – fen
@fan,这实际上是我尝试做的......这让我感到困惑,主要是因为结果是一个灰色的矩形。 – zeboidlund