Drawing texture in a color?

Started by Role, February 08, 2005, 17:26:29

Previous topic - Next topic

Role

I'm really new in OpenGL world, but I have been doing great Java2D to OpenGL LWJGL conversion so far!
Right now I'm really stuck with how to draw texture in a color, I mean I want for example draw my sprite in solid green, or solid red, in one solid color.
Perhaps this is simple, but most of my OpenGL code is simply copy paste and trial n error from other people draw code, so for this simple task I don't know what to do :(
And because I'm more to Java2D, I don't want to spend much of my time reading the OpenGL red book, or else that makes me hard to sleep :)
I've search this forum and come up with setting like this :
Color color = Color.RED; // I want to draw the texture to solid red color

		GL11.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
		GL11.glColor4f((float) color.getRed() / 255f,
					   (float) color.getGreen() / 255f,
					   (float) color.getBlue() / 255f,
					   (float) color.getAlpha() / 255f);

		// draw the textures
		//................


 		GL11.glColor3f(1.0f, 1.0f, 1.0f);

But it's not working at all :(

Could someone please give me a simply working code...

Thanks


PS: for anyone would like to test my Java2D to OpenGL LWJGL conversion: http://goldenstudios.or.id/products/games/bin/robosick.jnlp
D Java Game Engine
GTGE  Website | GTGE Forum

Fool Running

Do you have blending enabled?

glEnable(GL_BLEND); (or something like that  :oops: )
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Role

Yup, I have.
This is my OpenGL settings :
// init GL
		// enable textures since we're going to use these for our sprites
		GL11.glEnable(GL11.GL_TEXTURE_2D);

		// disable the OpenGL depth test since we're rendering 2D graphics
		GL11.glDisable(GL11.GL_DEPTH_TEST);

		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();

		GL11.glOrtho(0, size.width, size.height, 0, -1, 1);

		// enable transparency
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);


Anyone?
D Java Game Engine
GTGE  Website | GTGE Forum

napier

Are you sure you're not changing any other opengl settings?  Like lighting?

I colorize a texture by initing as you do:

GL11.glEnable(GL11.GL_TEXTURE_2D);
// Enable alpha transparency
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);


then render reddish textured mesh like so:

// clear depth buffer and color
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
// set color to red
GL11.glColor4f(1f,0,0,1f);
// bind texture
GL11.glBindTexture(GL11.GL_TEXTURE_2D,textureHandle);
// render triangles
renderMesh(sphere);


I don't do GL11.glTexEnvi().  One thing I've seen is that the glColor() has no effect if lighting is enabled.  When lighting is on, I either disable it before rendering:

GL11.glDisable(GL11.GL_LIGHTING);


or adjust the light properties to have a reddish color.
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl

Anonymous

Not working :(
This is my render texture function :
Texture texture;
	int x, y;		


	// store the current model matrix
	GL11.glPushMatrix();

	// bind to the appropriate texture for this sprite
	texture.bind(); // -> GL11.glBindTexture(GL.GL_TEXTURE_2D, textureID);

	// translate to the right location and prepare to draw
	GL11.glTranslatef(x, y, 0);

	// draw a quad textured to match the sprite
	GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2f(0, 0);
		GL11.glVertex2f(0, 0);

		GL11.glTexCoord2f(0, texture.getHeight());
		GL11.glVertex2f(0, texture.getImageHeight());

		GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
		GL11.glVertex2f(texture.getImageWidth(), texture.getImageHeight());

		GL11.glTexCoord2f(texture.getWidth(), 0);
		GL11.glVertex2f(texture.getImageWidth(), 0);
	GL11.glEnd();

	// restore the model view matrix to prevent contamination
	GL11.glPopMatrix();


This is makes me sick, playing texture rendering with trial n error :(
Where can I find the meaning of glEnable(whatever), the documentation of GL11.glXXXXX ??
All things related with 2D rendering in OpenGL....

Role

Whoops forget to login  :oops:
That's me
D Java Game Engine
GTGE  Website | GTGE Forum


Role

Ouw thanks for the link...
But I still can't solved the problem :(
D Java Game Engine
GTGE  Website | GTGE Forum

sysor

Quote from: "Anonymous"Not working :(
This is my render texture function :
Texture texture;
	int x, y;		


	// store the current model matrix
	GL11.glPushMatrix();

	// bind to the appropriate texture for this sprite
	texture.bind(); // -> GL11.glBindTexture(GL.GL_TEXTURE_2D, textureID);

	// translate to the right location and prepare to draw
	GL11.glTranslatef(x, y, 0);
                GL11.glEnable(GL11.GL_TEXTURE_2D); // <- new
	// draw a quad textured to match the sprite
	GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2f(0, 0); // <- correction
		GL11.glVertex2f(0, 0);

		GL11.glTexCoord2f(0, 1.0f); // <- correction
		GL11.glVertex2f(0, texture.getImageHeight());

		GL11.glTexCoord2f(1.0f,1.0f); // <- correction
		GL11.glVertex2f(texture.getImageWidth(), texture.getImageHeight());

		GL11.glTexCoord2f(1.0f, 0); // <- correction
		GL11.glVertex2f(texture.getImageWidth(), 0);
	GL11.glEnd();
                GL11.glDisable(GL11.GL_TEXTURE_2D); // <-new

	// restore the model view matrix to prevent contamination
	GL11.glPopMatrix();


<- i think so it's ok (=

Anonymous

Color color = Color.RED; // I want to draw the texture to solid red color

		GL11.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);
		GL11.glColor4f((float) color.getRed() / 255f,
					   (float) color.getGreen() / 255f,
					   (float) color.getBlue() / 255f,
					   (float) color.getAlpha() / 255f);

		// draw the textures
		//................


 		GL11.glColor3f(1.0f, 1.0f, 1.0f);

But it's not working at all :(

Could someone please give me a simply working code...

Thanks


PS: for anyone would like to test my Java2D to OpenGL LWJGL conversion: http://goldenstudios.or.id/products/games/bin/robosick.jnlp[/quote]



  Texture texture;
  int x, y;      


  // store the current model matrix
  GL11.glPushMatrix();


  texture.bind(); // -> GL11.glBindTexture(GL.GL_TEXTURE_2D, textureID);

  GL11.glTranslatef(x, y, 0);
  GL11.glEnable(GL11.GL_TEXTURE_2D);  
  GL11.glBegin(GL11.GL_QUADS);

     GL11.glColor3f(1.0f,1.0f,1.0f);
     GL11.glTexCoord2f(0, 0);  
     GL11.glVertex2f(0, 0);

     GL11.glColor3f(1.0f,1.0f,1.0f);
     GL11.glTexCoord2f(0, 1.0f);  
     GL11.glVertex2f(0, texture.getImageHeight());

     GL11.glColor3f(1.0f,0.0f,0.0f);
     GL11.glTexCoord2f(1.0f,1.0f);
     GL11.glVertex2f(texture.getImageWidth(), texture.getImageHeight());

     GL11.glColor3f(1.0f,0.0f,0.0f);
     GL11.glTexCoord2f(1.0f, 0);
     GL11.glVertex2f(texture.getImageWidth(), 0);
  GL11.glEnd();
  GL11.glDisable(GL11.GL_TEXTURE_2D);

  // restore the model view matrix to prevent contamination
  GL11.glPopMatrix();

ps: turn out any lights and this sample should work.. if you have to use lights you have to use a
GL11.glEnable(GL11.GL_COLOR_MATERIAL);
statement before you draw the quad