0
votes

I am starting to use LWJGL/OpenGL in Java to create a 2D game, however I am having difficulty rendering a simple 32x32 quad (square) that is textured. I have created a texture loading system and have followed the proper instructions to texture, but the texture will not display. My code is below:

devBlock64.bind();

glEnable(GL_TEXTURE_2D);

GL11.glBegin(GL11.GL_QUADS);
    GL11.glTexCoord2f(0, 0);
    GL11.glVertex2f(0, 0);

    GL11.glTexCoord2f(0, 1);
    GL11.glVertex2f(32, 0);

    GL11.glTexCoord2f(1, 1);
    GL11.glVertex2f(32, 32);

    GL11.glTexCoord2f(1, 0);
    GL11.glVertex2f(0, 32);
GL11.glEnd();

glDisable(GL_TEXTURE_2D);

The above is code for each time "render" is called. "devBlock64" is simply a Texture object that has loaded a 64x64 texture (but in this case it's 32x32, because I saved it as the wrong size)

Furthermore, this is the options and functions I have called after loading a texture and generating its texture id:

this.bind();

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

The image loads properly but it won't be displayed. this.bind(); calls my function that simply does this: glBindTexture(GL_TEXTURE_2D, id):

And, my game loop if anyone is curious, everything else was done according to OpenGL's tutorials for setting up a window:

public void loop() {
    // Binds GLFW with OpenGL
    GL.createCapabilities();

    glOrtho(0f, 800, 600, 0f, 0f, 1f);

    glClearColor(1.0f, 1f, 1f, 1f);
    //glLoadIdentity();

    world.loadTextures();

    while(!hasWindowRequestedClose()) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        world.onUpdate();
        world.render();

        glfwSwapBuffers(handle);

        glfwPollEvents();
    }

    destroy();
}
1
You want the quad be fitted in the window?Matso
No, I want to texture the quad. I edited the title to disrupt any confusion.ThisIsntSparta
A ByteBuffer loaded using BufferUtils.ThisIsntSparta

1 Answers

0
votes

Hey I've checked your code and everything seems to be fine on my end. I have a few suggestions.

  • Which opengl version do you use? glBegin() and glEnd() are removed from the newer versions (3.2 onward). Instead you need to upload the vertex data to Vertex Buffer Objects. Then use glVertexAttribPointer to tell openGL how the data is laid out.

  • A beter way is to use shaders and bind the texture to the shader instead.

Here is my code for loading textures.

public static int loadTexture(String path) {
 int[] pixels = null;
 int width = 0;
 int height = 0;
 try {
  InputStream resourceBuff = Loader.class.getResourceAsStream(path);
  BufferedImage image = ImageIO.read(resourceBuff);
  width = image.getWidth();
  height = image.getHeight();
  pixels = new int[width * height];
  image.getRGB(0, 0, width, height, pixels, 0, width);
 } catch (IOException e) {
  e.printStackTrace();
 }

 int[] data = new int[width * height];
 for (int i = 0; i < width * height; i++) {
  int a = (pixels[i] & 0xff000000) >> 24;
  int r = (pixels[i] & 0xff0000) >> 16;
  int g = (pixels[i] & 0xff00) >> 8;
  int b = (pixels[i] & 0xff);

  data[i] = a << 24 | b << 16 | g << 8 | r;
 }

 int result = glGenTextures();
 glBindTexture(GL_TEXTURE_2D, result);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
    storeDataInIntBuffer((data)));
 glBindTexture(GL_TEXTURE_2D, 0);
 return result;
}

Hope this wil help you.