首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LWJGL: glTexImage2D实现

LWJGL: glTexImage2D实现
EN

Stack Overflow用户
提问于 2015-04-03 18:01:04
回答 1查看 1.6K关注 0票数 1

我试图使用这两个类的代码从整数数组中生成纹理。当我把纹理绑起来的时候,我就变黑了。

代码语言:javascript
复制
public class PTexture {

private int id;
private int width;
private int height;

public PTexture(int id, int width, int height)
{
    this.id = id;
    this.width = width;
    this.height = height;
}

public Vector2f[] getRectPortionCoords(int x, int y, int w, int h)
{
    Vector2f[] res = new Vector2f[4];

    res[0] = new Vector2f((float)x / w, (float)y / height);
    res[1] = new Vector2f((float)(x + w) / width,(float)y / height);
    res[2] = new Vector2f((float)(x + w) / width, (float)(y + h) / height);
    res[3] = new Vector2f((float)x / w, (float)(y + h) / height);

    return res;
}

public void bind()
{
    glBindTexture(GL_TEXTURE_2D, id);
}

public int getId() {
    return id;
}

public int getWidth() {
    return width;
}

public int getHeight() {
    return height;
}

public void setId(int id) {
    this.id = id;
}

}

代码语言:javascript
复制
public class TerrainBlend extends PTexture {

private int[] pixels;

public TerrainBlend(int id, int width, int height) {
    super(id, width, height);
    pixels = new int[width * height];
}

public void genPixelsFromHeight(float[][] hMap, Vector3f colorHeights)
{
    for (int y = 0; y < getHeight(); y++)
    {
        for (int x = 0; x < getWidth(); x++)
        {
            if (hMap[x][y] >= colorHeights.getX())
            {
                genPixel(x, y, 0xFF0000FF);
                if (hMap[x][y] >= colorHeights.getY())
                {
                    genPixel(x, y, 0x00FF00FF);
                    if (hMap[x][y] >= colorHeights.getZ())
                    {
                        genPixel(x, y, 0x0000FFFF);
                    }
                }
            }
        }
    }

    genTexture();
}

private void genPixel(int x, int y, int color)
{
    pixels[x + y * getWidth()] = color;
}

public void genTexture()
{   
    IntBuffer iBuffer = BufferUtils.createIntBuffer(getWidth() * getHeight() * 4);
    for (int i = 0; i < pixels.length; i++)
    {
        iBuffer.put(pixels[i]);
    }
    iBuffer.position(0);

    setId(glGenTextures());
    glBindTexture(GL_TEXTURE_2D, getId());
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWidth(), getHeight(), 0, GL_RGBA, GL_UNSIGNED_INT, iBuffer);
}

}

我已经确保数组中的值是正确的,所以我假设OpenGL代码在某种程度上是错误的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-04 01:36:11

你有几个问题:

  • 传递给glTexImage2D()的类型将不像您所希望的那样工作: glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,getWidth(),getHeight(),0,GL_RGBA,GL_UNSIGNED_INT,iBuffer); 将GL_UNSIGNED_INT用于具有GL_RGBA内部格式的纹理意味着来自缓冲区的GLuint值将用于纹理的每个组件。也就是说,每个纹理将使用四个值,R、G、B和A各使用一个值。 根据构建值的方式,看起来每个texel的RGBA组件都打包在一个值中。要以这种方式解释数据,需要指定指定打包值的格式: glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,getWidth(),getHeight(),0,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8_REV,iBuffer);
  • 您需要指定纹理参数。特别是,默认的抽样属性假设纹理有mipmap,而纹理没有mipmap。至少,您需要调用将小型化掩码设置为没有使用mipmap: glTexParameter(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29437363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档