我将我的opengl程序从c ++重写为webgl / js。
在我的c ++代码中,我使用stb_image来解析文件中的图像。作为选项之一,我可以获得图像的深度,所以我总是知道它是RGB还是RGBA。这允许我使用所需参数加载纹理(来自learnopengl.com):
int width, height, nrComponents;
unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
if (data)
{
GLenum format;
if (nrComponents == 1)
format = GL_RED;
else if (nrComponents == 3)
format = GL_RGB;
else if (nrComponents == 4)
format = GL_RGBA;
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
js中是否有一些方法可以返回图像的nrComponents?
例如,如果我使用该代码:
function LoadTexture2D(filename)
{
var texID;
texID = gl.createTexture();
img = new Image();
img.getColorModel
img.onload = function () { handleTextureLoaded(img, texID)};
img.src = filename;
}
function handleTextureLoaded(image, texture) {
var format = 0;
gl.bindTexture(gl.TEXTURE_2D, texture);
if (image.getDepth()==3)
format = gl.RGB;
if (image.getDepth()==4)
format = gl.RGBA;
gl.texImage2D(gl.TEXTURE_2D, 0, format, format,gl.UNSIGNED_BYTE, image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER,gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
gl.generateMipmap(gl.TEXTURE_2D);
gl.bindTexture(gl.TEXTURE_2D, null);
}
是否有像getDepth()
这样的东西; ?