procedural textures

hello

i am trying to generate texture data not from image files but using some functions. so i create a Buffer with some random data.

but when i try to upload them to VRAM using
glTexImage2D(int target, int level, int internalFormat, int width, int height, int border, int format, int type, Buffer pixels)
nothing seems to happen.

what could the problem be?

thanks

There are several steps required to properly creating and rendering textures with OpenGL. I’d recommend you use the TextureIO classes in the com.sun.opengl.util.texture package, in particular the TextureIO.newTexture(BufferedImage, boolean) method to generate OpenGL textures from programmatically-created BufferedImages. See for example the source code for demos.texture.TestTexture in the jogl-demos workspace for how to use the resulting Texture object for OpenGL rendering.

thanks Ken.
but this way i would have to create the bufferedimage programatically. and i want to skip that step by directly creating the Buffer.

also another reason why i want to use glTexImage2D is to set anisotropic filtering after i call glBindTex.

Something that’s easily forgotten is to set the correct min/magnification filters. The default values are GL_NEAREST_MIPMAP_LINEAR and GL_NEAREST repsectively. If the minification filter is set to GL_MIPMAP, all mipmap levels must be defined or texture mapping won’t function properly. To fix this either set the minification filter to GL_NEAREST or GL_LINEAR

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

or

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

or provide all mipmap levels.

Alternatively you could also set the maximum mipmap level.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)

yes, i would like to set textureparameters like these (not only mipmap, but other parameters too), but when i use the TextureIO functions there is no place where i can set these, so i am looking for a way to generate the textures in the original opengl way.

That isn’t true. See the Texture.setTexParameter* methods.

Sounds a bit like me where I’m only using JOGL for the GL and GLU interfaces, trying to avoid using any other classes, especially those in the util packages.

But it doesn’t hurt to look at how the TextureIO class generates a texture: in there you can see all of the bits and pieces required as pre-setup before finally making that call to glTexImage2D()

ok, the problem is, that i want to set anisotropic filtering for all the textures i generate.
so currently i use this code everytime after i do a texture.bind();


		if (gl.isExtensionAvailable("GL_EXT_texture_filter_anisotropic")) {
			FloatBuffer maxAni = FloatBuffer.allocate(1);
			gl.glGetFloatv(GL.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, maxAni);
			gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAni.get(0));
		}

now of course it would be better to set this only once when i create the texture. but i cant since everything seems to happen inside the
texture = TextureIO.newTexture(textureData);
function.

You can just call texture.setTexParameterf(…) once on the resulting Texture object immediately after creating it with the TextureIO class. Texture.setTexParameterf() and similar routines call bind() internally.

Ideally you would want to set anisotropic filtering before glTexImage2D(), but if you’re using JOGL’s Texture classes, you should instead set it once you’ve got your new Texture:

Texture texture = TextureIO.newTexture(blah);
texture.setTexParameteri(target, anisotropy, anisotropylevel); // if anisotropy available

Or, you could always create your own Texture/Image classes so you don’t have to use those in the Texture package. If the normal texture-making procedure is anything to go by, all you need to do is:

gl.glBindTexture(textureid);
gl.glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filterlevel);
gl.glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filterlevel);
gl.glTexParameteri(target, anisotropy, anisotropylevel); // if anisotropy available
gl.glTexImage2D(…);

in the constructor or other initialization of your texture.

ok, thanks!

[quote=“Ultraq,post:10,topic:28434”]
I don’t think calling it before or after glTexImage2D should make any difference. Filtering occurs during rasterization; not when you upload texture data. The same applies for the other texture parameters as well.

No, it doesn’t make any difference. But from some sort of design perspective, having everything set-up during object initialization just feels like how it should be done. ;D