我一直在研究位图加载器,主要目标就是正确解析数据并在 OpenGL 中渲染它。我现在需要在 x/y(即逐个像素)的基础上绘制像素(至少,就渲染而言,这是我认为我需要做的)。我已经绑定(bind)了纹理对象并调用了 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 );
}
最佳答案
您完全错过了代码中 OpenGL 纹理的要点。纹理为您保留图像,光栅化器为您对像素数据进行所有迭代。无需自己编写慢速像素推进器循环。
正如您现在的代码所表明的那样,纹理完全是伪造的并且什么都不做。您可以完全省略对 glBindTexture 的调用,它仍然可以工作——或者不工作,因为您实际上并没有绘制任何东西,您只是设置了 glColor 状态。要绘制某些东西,您必须调用 glVertex。
那么为什么不利用现代 GPU 的像素插入性能并实际使用纹理呢?这个怎么样:
void R_RenderTexture_PixByPix( texture_bmp_t* const data, const vec3 center )
{
if( 0 == data->texbuf_id ) {
glGenTextures(1, &(data->texbuf_id));
glBindTexture( GL_TEXTURE_2D, data->texbuf_id );
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// there are a few more, but the defaults are ok
// if you didn't change them no need for further unpack settings
GLenum internal_format;
GLenum format;
GLenum type;
switch(data->img_data->bpp) {
case 8:
// this could be a palette or grayscale
internal_format = GL_LUMINANCE8;
format = GL_LUMINANCE;
type = GL_UNSIGNED_BYTE;
break;
case 15:
internal_format = GL_RGB5;
format = GL_BGR; // BMP files have BGR pixel order
type = GL_UNSIGNED_SHORT_1_5_5_5;
break;
case 16:
internal_format = GL_RGB8;
format = GL_BGR; // BMP files have BGR pixel order
type = GL_UNSIGNED_SHORT_5_6_5;
break;
case 24:
internal_format = GL_RGB8;
format = GL_BGR; // BMP files have BGR pixel order
type = GL_UNSIGNED_BYTE;
break;
case 32:
internal_format = GL_RGB8;
format = GL_BGR; // BMP files have BGR pixel order
type = GL_UNSIGNED_INT_8_8_8_8;
break;
}
glTexImage2D( GL_TEXTURE_2D, 0, internal_format,
data->img_data->width, data->img_data->height, 0,
format, type, data->img_data->pixels );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
} else {
glBindTexture( GL_TEXTURE_2D, data->texbuf_id );
}
static GLfloat verts[] = {
0, 0,
1, 0,
1, 1,
0, 1
};
// the following is to address texture image pixel centers
// tex coordinates 0 and 1 are not on pixel centers!
float const s0 = 1. / (2.*tex_width);
float const s1 = ( 2.*(tex_width-1) + 1.) / (2.*tex_width);
float const t0 = 1. / (2.*tex_height);
float const t1 = ( 2.*(tex_height-1) + 1.) / (2.*tex_height);
GLfloat texcoords[] = {
s0, t0,
s1, t0,
s1, t1,
s0, t1
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
glVertexPointer(2, GL_FLOAT, 0, verts);
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
glColor4f(1., 1., 1., 1.);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture( GL_TEXTURE_2D, 0 );
}
关于c - 关于逐像素渲染位图算法的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14332201/