OpenGL纹理映射不使用图像

OpenGL纹理映射不使用图像,opengl,mapping,textures,gradient,Opengl,Mapping,Textures,Gradient,我想将纹理映射到对象(可能是立方体)。 但我不想使用图像。 我知道通常使用图像进行纹理贴图,如下所示: glGenTextures(1, &texName); glBindTexture(GL_TEXTURE_2D, texName); image = SOIL_load_image("cube.jpg", &width, &height, 0, SOIL_LOAD_RGB); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width,

我想将纹理映射到对象(可能是立方体)。 但我不想使用图像。 我知道通常使用图像进行纹理贴图,如下所示:

glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);

image = SOIL_load_image("cube.jpg", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
但是有没有不使用图像的解决方案呢

如果有颜色光谱,我想将颜色信息存储在缓冲区中,并像纹理一样映射该光谱

如果有什么解决办法,请告诉我


如果要创建纹理,必须首先为颜色平面分配内存。 最简单的方法是创建RGBA纹理,因为对于一个纹理,只需要4个字节(32位),并且不必担心对齐问题

在C++中,我建议使用<代码> STD::向量< /代码>:

int width  = ...;
int height = ...;

std::vector<unsignd char> colorPlane( width * height * 4 ); // * 4 because of RGBA
平面内texel的字节索引计算如下:

int posX = ...;
int posY = ...;
int index = (posY * width + posX) * 4;
for ( int iy = 0; iy < height; ++ iy )
{
    for ( int ix = 0; ix < width; ++ ix )
    {
        int   index = (iy * width + ix) * 4;
        float H = 1.0f - (float)iy / height;
        float R = fabs(H * 4.0f - 3.0f) - 1.0f;
        float G = 2.0f - fabs(H * 4.0f - 2.0f);
        float B = 2.0f - fabs(H * 4.0f - 4.0f);
        colorPlane[index + 0] = (unsigned char)(255.0 * R);
        colorPlane[index + 1] = (unsigned char)(255.0 * G);
        colorPlane[index + 2] = (unsigned char)(255.0 * B);
        colorPlane[index + 3] = 255;
    }
}
如果要设置像素,必须在[0255]范围内指定适当的红色、绿色和蓝色通道。对于不透明纹理,必须将alpha通道设置为255:

e、 g:设置红色:

colorPlane[index + 0] = 255; // red component
colorPlane[index + 1] = 0;   // green component
colorPlane[index + 2] = 0;   // blue component
colorPlane[index + 3] = 255; // alpha channel (255 == opaque)
最后,必须将颜色平面设置为纹理

// std::vector<unsigned char>
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, colorPlane.data() );  

// unsigned char*
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, colorPlane );  
//std::vector
GLTEXAGE2D(GL_纹理_2D,0,GL_RGBA8,宽度,高度,0,GL_RGBA,GL_无符号_字节,colorPlane.data());
//无符号字符*
GLTEXAGE2D(GL_纹理_2D,0,GL_RGBA8,宽度,高度,0,GL_RGBA,GL_无符号字节,彩色平面);
可以通过插值颜色组件来创建渐变纹理。 请参见下面的示例

for ( int iy = 0; iy < height; ++ iy )
{
    for ( int ix = 0; ix < width; ++ ix )
    {
        int   index = (iy * width + ix) * 4;
        float gradX = (float)ix / width;
        float gradY = (float)iy / height;
        colorPlane[index + 0] = (unsigned char)(255.0 * (1.0-gradX));
        colorPlane[index + 1] = (unsigned char)(255.0 * (1.0-gradY));
        colorPlane[index + 2] = (unsigned char)(255.0 * gradX * gradY);
        colorPlane[index + 3] = 255;
    }
}
for(int-iy=0;iy
彩虹纹理可以按如下方式创建:

int posX = ...;
int posY = ...;
int index = (posY * width + posX) * 4;
for ( int iy = 0; iy < height; ++ iy )
{
    for ( int ix = 0; ix < width; ++ ix )
    {
        int   index = (iy * width + ix) * 4;
        float H = 1.0f - (float)iy / height;
        float R = fabs(H * 4.0f - 3.0f) - 1.0f;
        float G = 2.0f - fabs(H * 4.0f - 2.0f);
        float B = 2.0f - fabs(H * 4.0f - 4.0f);
        colorPlane[index + 0] = (unsigned char)(255.0 * R);
        colorPlane[index + 1] = (unsigned char)(255.0 * G);
        colorPlane[index + 2] = (unsigned char)(255.0 * B);
        colorPlane[index + 3] = 255;
    }
}
for(int-iy=0;iy
请参见以下WebGL示例:

var ShaderProgram={};
ShaderProgram.Create=函数(shaderList,uniformNames){
var shaderObjs=[];
for(变量i_sh=0;i_sh