writing to a texture

Started by
10 comments, last by swiftcoder 14 years, 8 months ago
I'm working on a 2d game and would like write light intensity values into a texture(not a fbo) and then copy the texture onto a screen sized quad at the end of every render pass. Is it possible to write directly into a texture every frame, and if so how would I do this(I'm using opengl es 1.0 if it even matters)?
Advertisement

Basically, all you need to do is get your lighting values in an array, preferably with dimensions that are a power of 2 (for compatiblity purposes) like 64x64 or 128x128, etc.

Then you'll need to free the last texture you used, create a new one, and load the lighting data into it.

Make an array out of unsigned char lighting[128][128];

and then your function for making the texture could look something like this:


unsigned int MakeLightTexture(unsigned char *lightdata){	unsigned int tex;	glGenTextures(1, &tex);	glBindTexture(GL_TEXTURE_2D, tex);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);	gluBuild2DMipmaps(GL_TEXTURE_2D, 1, 128, 128, GL_LUMINANCE, GL_UNSIGNED_BYTE, lightdata);	return tex;}


I am not sure if you can just re-use the texture, and move the glGenTextures call to your initialize and just gluBuild2DMipmaps or glTexImage2D if you want to avoid using the glu32 dll.. so try that, otherwise you'll have to glDeleteTextures(1, &lighttexture); at the end of every frame after you draw your lighting over the screen.

If you are going to use colored lighting (eg: RGB) then make your lighting array have [3] on the end, and change the 2nd parm in the build2dmipmaps call to 3, and GL_LUMINANCE to GL_RGB (I think ?)

Good luck!
That seems like it would be awfully slow, no? One of the platforms I'm building for is the iphone so speed it curcial.

Is there some way to explicitly access the texel data once it has already been created?

Also I don't beieve gluBuild2DMipmaps works in openlg es 1.0.
Okay, as a first step I tried just building a simple light map at the start of my program. Basically just writing light intensity values into a texture. And This does not seems to be working.

Here's the code I'm using. All I get on screen is a white quad(who's brightness is equal to whatever alpha value I pass to glColor4f() ). Also for the sake of testing, the blend function I'm using is GL_ONE, GL_ONE.

void build_light_map_tex( int_t* texture, 					       size_t width, size_t height){			GLfloat image_data[width][height];		float_t light_intensity;		int_t x, y;		for (x = 0; x < width; x++)	{		for (y = 0; y < height; y++)		{			light_intensity = calculate_light_intensity(x,y);						image_data[x][y] = v;               }	}       glGenTextures(1, texture);       glBindTexture(GL_TEXTURE_2D, *texture);       glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_FLOAT, &image_data);       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


Anyone have any ideas?
Quote:Original post by ari556655
That seems like it would be awfully slow, no? One of the platforms I'm building for is the iphone so speed it curcial.
Don't create a new texture each frame. Instead, you want to create the texture once at the beginning of the program, and then use glTexSubImage2D() to upload new data to the texture every frame.
Quote:Okay, as a first step I tried just building a simple light map at the start of my program. Basically just writing light intensity values into a texture. And This does not seems to be working.

Here's the code I'm using. All I get on screen is a white quad(who's brightness is equal to whatever alpha value I pass to glColor4f() ). Also for the sake of testing, the blend function I'm using is GL_ONE, GL_ONE.

*** Source Snippet Removed ***

Anyone have any ideas?
Have you enabled GL_TEXTURE_2D?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Yes it is enabled.
Figured it out, I was trying to pass light values as 0.0-1.0 rather than 0-255.
Is there any faster method than calling glTexSubImage2D() to manipulate a texture's contents?
Quote:Original post by ari556655
Is there any faster method than calling glTexSubImage2D() to manipulate a texture's contents?
You can use render-to-texture with an FBO.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

What I'm trying to do is upload my own array containing lighting and shading info into a texture, and then apply the texture to a screen. How could I use an fbo without calling glTexSubImage2D() to first upload my data to a texture?

This topic is closed to new replies.

Advertisement