1

我问的最后一个问题是关于如何在 OpenGL 中绘制不同的模型。我得到了覆盖,但现在我被困在做纹理。再一次,只要我只使用一种纹理,我就可以轻松地使用纹理。

加载纹理的类:

public class Textures
{

    public static int tex, tex2;

    public static void load() throws IOException
    {
        tex = glGenTextures();
        tex2 = glGenTextures();

        load(tex2, "moreModels/robot.jpg", "jpg");
        load(tex, "moreModels/sample.jpg", "jpg");
    }

    public static void load(int tex, String name, String type) throws IOException
    {
        glBindTexture(GL43.GL_TEXTURE_2D, tex);

        glTexParameterIi(GL43.GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameterIi(GL43.GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

        glGenerateMipmap(GL43.GL_TEXTURE_2D);

        glTexParameterIi(GL43.GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameterIi(GL43.GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);

        Texture texture = TextureLoader.getTexture(type, ResourceLoader.getResourceAsStream(name));

        byte[] pixels = texture.getTextureData();

        ByteBuffer buffer = BufferUtils.createByteBuffer(pixels.length);
        buffer.put(pixels);
        buffer.flip();
        glTexImage2D(GL43.GL_TEXTURE_2D, 0, GL_BGR, texture.getImageWidth(), texture.getImageHeight(), 0, GL_BGR, GL_BYTE, buffer);
    }
}

现在,我认为简单地调用glBindTexture(GL43.GL_TEXTURE_2D, textureID)会改变纹理,但不会。如果我在任何时候调用 glBindTexture 方法,一切都会被渲染为黑色。

Is it the same as before, where I have to do the entire load method every time I need to change the texture? (Perhaps saving the Texture object, so I don't have to load it over and over again). And if so, what's the point of having glGenTextures() when it aparently forgets everything about it when changed?

4

1 回答 1

2

I think you have a little bit wrong understanding of OpenGL texture.You must remember:every time you load a new image you have to create a new texture object using the routine you have shown in your question.Additionally , if you want just to update (replace the pixels) of the existing texture , you should use glTexSubImage2D .This way you don't create new texture object but update the data in the existing one.

Now , if you want to have several textures ,let's say, one as a color map and another as normal map, then you have to create 2 textures from scratch which will be accessed by your fragment shader samplers.

Based on your code it is hard to see how you do the whole setup (rendering loop ,shaders).If you supply more info then it will be possible to assist you further.

于 2013-01-21T19:11:41.793 回答