Jac*_*bob 4 c++ opengl glew texture-mapping
好的,所以我需要创建自己的纹理/图像数据,然后将其显示在 OpenGL 中的四边形上。我让四边形工作,我可以用我自己的纹理加载器在它上面显示一个 TGA 文件,它完美地映射到四边形。
但是如何创建自己的“自制图像”,即每个像素 1000x1000 和 3 个通道(RGB 值)?纹理数组的格式是什么,例如如何将像素(100,100)设置为黑色?
这就是我对完全白色图像/纹理的想象:
#DEFINE SCREEN_WIDTH 1000
#DEFINE SCREEN_HEIGHT 1000
unsigned int* texdata = new unsigned int[SCREEN_HEIGHT * SCREEN_WIDTH * 3];
for(int i=0; i<SCREEN_HEIGHT * SCREEN_WIDTH * 3; i++)
texdata[i] = 255;
GLuint t = 0;
glEnable(GL_TEXTURE_2D);
glGenTextures( 1, &t );
glBindTexture(GL_TEXTURE_2D, t);
// Set parameters to determine how the texture is resized
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR_MIPMAP_LINEAR );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR );
// Set parameters to determine how the texture wraps at edges
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_REPEAT );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_REPEAT );
// Read the texture data from file and upload it to the GPU
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCREEN_WIDTH, SCREEN_HEIGHT, 0,
GL_RGB, GL_UNSIGNED_BYTE, texdata);
glGenerateMipmap(GL_TEXTURE_2D);
Run Code Online (Sandbox Code Playgroud)
编辑:下面的答案是正确的,但我也发现 OpenGL 不处理我使用的普通整数,但它与 uint8_t 一起工作正常。我认为这是因为我在上传到 GPU 时使用了 GL_RGB 和 GL_UNSIGNED_BYTE(只有 8 位,正常的 int 不是 8 位)标志。
但是如何创建自己的“自制图像”,即每个像素 1000x1000 和 3 个通道(RGB 值)?
std::vector< unsigned char > image( 1000 * 1000 * 3 /* bytes per pixel */ );
Run Code Online (Sandbox Code Playgroud)
纹理数组的格式是什么
红色字节,然后是绿色字节,然后是蓝色字节。重复。
例如,我如何将像素 (100,100) 设置为黑色?
unsigned int width = 1000;
unsigned int x = 100;
unsigned int y = 100;
unsigned int location = ( x + ( y * width ) ) * 3;
image[ location + 0 ] = 0; // R
image[ location + 1 ] = 0; // G
image[ location + 2 ] = 0; // B
Run Code Online (Sandbox Code Playgroud)
上传方式:
// the rows in the image array don't have any padding
// so set GL_UNPACK_ALIGNMENT to 1 (instead of the default of 4)
// https://www.khronos.org/opengl/wiki/Pixel_Transfer#Pixel_layout
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glTexImage2D
(
GL_TEXTURE_2D, 0,
GL_RGB, 1000, 1000, 0,
GL_RGB, GL_UNSIGNED_BYTE, &image[0]
);
Run Code Online (Sandbox Code Playgroud)