1
votes

Last question I asked was about how to draw different models in OpenGL. I got that covered, but now I'm stuck at doing textures. Once again I can easily get textures to work, as long as I only ever use one texture.

The class that loads the textures:

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);
    }
}

Now, I would think that simply calling glBindTexture(GL43.GL_TEXTURE_2D, textureID) will change the texture, but no. If I at any time call the glBindTexture method everything will be rendered black.

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?

1
Here's an idea; before wrapping OpenGL stuff in objects, first master using OpenGL without the objects. Once you're used to it that way, then you can build your structure around it.Nicol Bolas
What is that API? JOGL? Can you Show your fragment shader?Michael IV

1 Answers

2
votes

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.