LWJGL - Texture rendering plain white

Hello guys.
I’m having a problem: My texture is rendering plain white. Even the parts of it, which should be transparent are rendered white…

This is the texture it SHOULD render:

This is a screenshot:

This is gl-initialization code: (EDIT: unfolded “setupViewport(…)”)


try {
	Display.setDisplayMode(new DisplayMode(
			world.getViewPixelWidth(),
			world.getViewPixelHeight()));
	Display.create();
} catch (LWJGLException e) {
	e.printStackTrace();
}
glEnable(GL_LINE_SMOOTH);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glClearColor(0.3f, 0.3f, 0.3f, 1f);

glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
glMatrixMode(GL_MODELVIEW);

Display.setVSyncEnabled(true);
Display.setResizable(true);

This is the code, which loads the texture and binds it, simply a Tex.java class:



package org.matheusdev.gol.utils;

import static org.lwjgl.opengl.GL11.GL_RGBA;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MIN_FILTER;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glGenTextures;
import static org.lwjgl.opengl.GL11.glTexImage2D;
import static org.lwjgl.opengl.GL11.glTexParameteri;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import de.matthiasmann.twl.utils.PNGDecoder;
import de.matthiasmann.twl.utils.PNGDecoder.Format;

/**
 * @author matheusdev
 *
 */
public class Tex {

	public static final Tex NOTEX = new Tex(0);
	private static int bound = 0;

	public final int id;

	public Tex(InputStream resource, int filter) {
		int tid = 0;
		try {
			PNGDecoder decoder = new PNGDecoder(resource);

			ByteBuffer bufData = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight());
			decoder.decode(bufData, decoder.getWidth()*4, Format.RGBA);
			bufData.flip();

			glEnable(GL_TEXTURE_2D);

			tid = glGenTextures();
			if (tid == 0) {
				throw new IllegalStateException("glGenTextures() returned 0.");
			}
			bind();

			if (filter != -1) {
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
			}

			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, bufData);

			unbind();
		} catch (IOException e) {
			e.printStackTrace();
		}
		id = tid;
	}

	public Tex(int id) {
		this.id = id;
	}

	public void bind() {
		if (id != bound) {
			glBindTexture(GL_TEXTURE_2D, id);
			bound = id;
		}
	}

	public void unbind() {
		NOTEX.bind();
	}

}

And finally this is the code to render the texture:


glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glPushMatrix();
{
	gridTex.bind();

//	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

//	glTranslatef(xbegin, ybegin, 0);

	glColor3f(1f, 1f, 1f);

	glBegin(GL_QUADS);
	{
		glTexCoord2f(0, 0);
		glVertex2f(0, 0);

		glTexCoord2f(1, 0);
		glVertex2f(512, 0);

		glTexCoord2f(1, 1);
		glVertex2f(512, 512);

		glTexCoord2f(0, 1);
		glVertex2f(0, 512);
	}
	glEnd();
}
glPopMatrix();

I already tested the values, the ByteBuffer upon loading contains. It IS complete, including alpha bytes…
Yes… propably not needed to be mentioned, but I also pressed F5 on the Eclipse Project… (happens pretty often to me :confused: but not the source of the problem here).

I dunno what to do :frowning:

just don’t use the immediate mode anymore :wink:

I use shaders and generic vertex attributes for everything, so I am always in full control. This hole default state stuff and so on is just so error prone.

Yeah. awesome.
But if I even can’t get immediate mode working, I don’t even want to touch VAO/VBO again…
Also, every frame the values for glVertex2f and glTexCoord2f (yes even tex coords) change in this case. So it’s VERY dynamic. Using VAO’s (I’m not even talking about VBO’s here :stuck_out_tongue: ) would just be overkill.

Anyways, I hope you can help me with immediate mode… I think I’m now trying to implement VAO’s :confused:

One more thing:
LWJGL Version: 2.8.3
Java version:

[quote]$ java -version
java version “1.7.0_07”
OpenJDK Runtime Environment (IcedTea7 2.3.2) (ArchLinux build 7.u7_2.3.2-2-x86_64)
OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)
[/quote]
Immedate mode rendering is being used in another project on the same machine in the same time, using the same techneque, both GL_TEXTURE_2D (different code), both 2.8.3 both same java version and one works, one doesn’t…

[s]the thing is this, you probably missed some tiny little detail, i.e. one miss placed opengl call.
And in my personal view is that the “old” opengl makes such errors easier to do^^

Sry that I can’t help you that much, because I don’t do any OpenGL 1 anymore.
Perhaps it is something about the rendering mode(lighting) or do you have to enable the texture units like glEnable(Texture_0)
[/s]

ah I found it I guess, you have to activate a texture unit to which your texture should be bound.

gl.glActiveTexture(textureUnit);

I don’t think it has to do with glActiveTexture as you aren’t changing the texture units anywhere.

Instead, the problem is likely because you haven’t set up any mipmaps. By default, GL_TEXTURE_MIN_FILTER is set to GL_NEAREST_MIPMAP_LINEAR. If you don’t specify mipmaps (e.g. with glGenerateMipmap) then the texture will be considered invalid, and thus render all white. Instead, you should specify GL_NEAREST or GL_LINEAR for both min and mag filters, unless you plan to support mipmaps.

Also, you shouldn’t rely on binding texture ID zero, since it may be a valid texture. Instead, if you want to disable texturing in fixed function, you would use glDisable(GL_TEXTURE_2D). In programmable pipeline, you just don’t sample from a texture.

[quote]Also, every frame the values for glVertex2f and glTexCoord2f (yes even tex coords) change in this case. So it’s VERY dynamic. Using VAO’s (I’m not even talking about VBO’s here ) would just be overkill.
[/quote]
Vertex Arrays will be faster than immediate mode. You don’t necessarily need Vertex Array Objects or Vertex Buffer Objects (in certain situations on certain GPUs vertex arrays perform faster than VBOs), but there is no need to use glBegin/glEnd and that crap except purely for debugging.

“The value zero is reserved to represent the default texture for each texture target”

btw the other thing sounds plausible, try changing the min filter

Yes, the “default texture” meaning the texture that doesn’t have a name.

As I understand it, this is how it came about: The earliest versions of OpenGL only supported a single texture. To render multiple textures, you would glTexImage2D your data, render something, then glTexImage2D more data, and render again. Then 1.1 rolled around and brought “named textures” – i.e. through glGenTextures and glBindTextures. For backwards compatibility, the “default texture” stuck around with name 0. Over time, it’s become standard to ignore the default texture and just use named textures instead.

Try it yourself: bind texture unit zero, upload data with glTexImage2D, and render it like normal. It will work because texture unit zero (i.e. the default, unnamed texture) is valid like any other.

With that said, I wouldn’t suggest relying on it for anything – since it’s so rarely used these days, different drivers may do some strange things to it under the hood.

Okey… so I changed the texture class:

public class Tex {

+	public static final Tex NOTEX = new Tex(1, 1, BufferUtils.createByteBuffer(4), GL_NEAREST);
	private static int bound = 0;

	public final int id;

	public Tex(InputStream resource, int filter) {
		int tid = 0;
		try {
			PNGDecoder decoder = new PNGDecoder(resource);

			ByteBuffer bufData = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight());
			decoder.decode(bufData, decoder.getWidth()*4, Format.RGBA);
			bufData.flip();

+			tid = create(decoder.getWidth(), decoder.getHeight(), bufData, filter);
		} catch (IOException e) {
			e.printStackTrace();
		}
		id = tid;
	}

	public Tex(int width, int height, ByteBuffer data, int filter) {
+		this.id = create(width, height, data, filter);
	}

	public Tex(int id) {
		this.id = id;
	}

+	protected int create(int width, int height, ByteBuffer data, int filter) {
+		int id = 0;
+
+		glEnable(GL_TEXTURE_2D);
+
+		id = glGenTextures();
+		if (id == 0) {
+			throw new IllegalStateException("glGenTextures() returned 0.");
+		}
+		bind();
+
+		if (filter != -1) {
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
+		}
+
+		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
+
+		unbind();
+
+		return id;
+	}

	public void bind() {
		if (id != bound) {
			glBindTexture(GL_TEXTURE_2D, id);
			bound = id;
		}
	}

+	public static void bind(int id) {
+		if (id != bound) {
+			glBindTexture(GL_TEXTURE_2D, id);
+			bound = id;
+		}
+	}

	public void unbind() {
+		if (NOTEX == null) {
+			bind(0);
+		} else {
			NOTEX.bind();
+		}
	}

}

And the rendering:

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glPushMatrix();
{
	gridTex.bind();

+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	glTranslatef(xbegin, ybegin, 0);

	glColor3f(1f, 1f, 1f);

	glBegin(GL_QUADS);
	{
		glTexCoord2f(0, 0);
		glVertex2f(0, 0);

		glTexCoord2f(1, 0);
		glVertex2f(512, 0);

		glTexCoord2f(1, 1);
		glVertex2f(512, 512);

		glTexCoord2f(0, 1);
		glVertex2f(0, 512);
	}
	glEnd();
}
glPopMatrix();

EDIT: Oh and… it didn’t work :confused:

Somehow I forgot to mention… ???
I thought I’d already have posted, but I’ve also tried out Vertex Arrays:


FloatBuffer tBuffer = BufferUtils.createFloatBuffer(8);
tBuffer.put(0).put(0);
tBuffer.put(1).put(0);
tBuffer.put(1).put(1);
tBuffer.put(0).put(1);
tBuffer.flip();

FloatBuffer vBuffer = BufferUtils.createFloatBuffer(8);
vBuffer.put(  0).put(  0);
vBuffer.put(512).put(  0);
vBuffer.put(512).put(512);
vBuffer.put(  0).put(512);
vBuffer.flip();

glPushMatrix();
{
	glEnable(GL_TEXTURE_2D);
	gridTex.bind();

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	glColor3f(1f, 1f, 1f);

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glTexCoordPointer(4, 0, tBuffer);
	glVertexPointer(4, 0, vBuffer);
	glDrawArrays(GL_QUADS, 0, 4);

	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);
}
glPopMatrix();

And THIS time NOTHING rendered… just… nothing… screenshot?

The orange thing is rendered per immediate mode (It’s not what we are talking about… It just got screen-shot :P). It’s just built from quads being rendered orange (without any texture…)
It’s a glider btw… Actually… just don’t mind the orange thing. It doesn’t belong there.
As you can see, no texture is rendered… NO TEXTURE…
I could even put glLoadIdentity(); before the call to glDrawArrays and it doesn’t work then either…

Oh man I’m crying out for help! (It’s not my pc… MY OTHER DAMN PROJECT WORKS! GAAAAAH f**kINGRAGE)

Why are you “unbinding” textures? Like I said, if you really don’t want to use texturing, disable it.

Here is a self-contained test demonstrating proper usage:
Main Class
Texture

IMO the best way to learn vertex arrays is to work with shaders, i.e. using custom attributes (no built-ins). Further, most tutorials only show how to use VBOs and vertex arrays with fixed vertices, which isn’t very practical. This afternoon I’ll add a ShaderProgram and SpriteBatch code which demonstrates the bare essentials of a 2D engine.

yeah. And then I’ll go from 300 fps to 10. Believe me, I have a good reason for not using glDisable(GL_TEXTURE_2D); It’s horrobly slow, because opengl has to re-manage the context then…
Also, I tried it, it makes no difference.

Oh, thank you :slight_smile: (I have a plenty of (also) working implementations of Texture classes and how to use textures lying around (propably 5 of them :P) and only this one is giving me problems. But the other ones have big dependencies lying on them, that’s why I can’t use them :confused: )
But I hope these will help me out.

Don’t worry. I’m not going to write an engine. That’s not my goal.
Actually that grid texture is the only texture I’m going to use in the whole program. I’m not actually even going to write a game.

One more thing I’d like to add: I’ve already written Shaders and also my own shader implementations. Actually I’ve already written a (somehow bad) engine, where the stuff at least works… I’ve even written a 2D-Light caster and some OBJ and MD5 model loaders and even implemented my own model format, which is a mix between a derived OBJ format, but binary.

What I’m trying to say is: I’ve already done those things you suggest. Its just that I want to do it trivial. YES I DON’T need performance.
I’ve already had the experience, but it’s propably already 10 months ago or so… So that is propably the problem…
Again, what I’m trying to say is: I’m not going to write shaders, a sprite batcher, a Vertex Buffer wrapper class, a 2D engine, just to draw one simple texture. It was a task, which I thought should take a minute to implement, and I didn’t want more… But It’s something I really need.

So… yeah…
Sorry, I’m a bit mad about this problem. Why is something I’ve already did a thousands of times doesn’t work now?

Also, did you try out your program?
It propably works, but you don’t use the ShaderProgram.
And even if you activated it before calling glBegin(), it wouldn’t work, because you use glTexCoord2f, which isn’t accessible in the shader, as far as I know.

Sorry, I meant to delete the ShaderProgram declaration before pasting the code.

[quote]yeah. And then I’ll go from 300 fps to 10. Believe me, I have a good reason for not using glDisable(GL_TEXTURE_2D); It’s horrobly slow, because opengl has to re-manage the context then…
Also, I tried it, it makes no difference.
[/quote]
Huh? You’re doing something wrong if disabling texturing is giving you 10 FPS.

“Re-manage the context” - not sure what you mean by that.

Not saying your current technique is bad (binding a white 1x1 texture to render plain rectangles), but IMO a texture wrapper with an “unbind” method is misleading, since unbinding textures is not possible in OpenGL. Of course, you’re free to write your own code however you like… :slight_smile:

[quote]And even if you activated it before calling glBegin(), it wouldn’t work, because you use glTexCoord2f, which isn’t accessible in the shader, as far as I know.
[/quote]
glTexCoord2f is accessible through the built-in gl_MultiTexCoord0 attribute.

[quote]Why is something I’ve already did a thousands of times doesn’t work now?
[/quote]
… Because you still don’t understand the pipeline?

The reason I advocate for using vertex arrays and custom shader attributes (as opposed to built-ins) is because it will give you a better grasp of how OpenGL works, especially how the fixed-function pipeline works.

If you’re just trying to draw a single sprite and don’t want to learn about OpenGL, then why not use LibGDX or another high-level library?

Incase you or anybody else is interested, here is the source for my “mini game library.” It uses GLSL with custom attributes and vertex arrays, although VBOs would be fairly easy to implement.

Okey. Thank you :slight_smile: It’s very helpful… And at least it works ;D

I always wanted to write the equivalent of my “Utils” project for LWJGL, which is basicly a collection of utilities one could use in any engine. I don’t want to be tied to the mast of some engine. This is just what I love :slight_smile:

I wanted to do it myself actually… to learn it… but … I understand everything you did there, so thats okey I think :smiley:

And: Your VertexArrayExample.java works perfectly, let’s see how my project works…