Hello Guest

Drawing texture in a color?

  • 9 Replies
  • 15431 Views
*

Offline Role

  • *
  • 17
    • http://www.goldenstudios.or.id/
Drawing texture in a color?
« on: February 08, 2005, 17:26:29 »
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 :
Code: [Select]

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

hmmmmm...
« Reply #1 on: February 08, 2005, 19:38:49 »
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

*

Offline Role

  • *
  • 17
    • http://www.goldenstudios.or.id/
Drawing texture in a color?
« Reply #2 on: February 09, 2005, 03:18:43 »
Yup, I have.
This is my OpenGL settings :
Code: [Select]

// 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

*

Offline napier

  • ***
  • 104
    • http://potatoland.org
Drawing texture in a color?
« Reply #3 on: February 09, 2005, 04:01:19 »
Are you sure you're not changing any other opengl settings?  Like lighting?

I colorize a texture by initing as you do:

Code: [Select]
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:

 
Code: [Select]
// 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:

Code: [Select]
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

Drawing texture in a color?
« Reply #4 on: February 09, 2005, 05:53:48 »
Not working :(
This is my render texture function :
Code: [Select]

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....

*

Offline Role

  • *
  • 17
    • http://www.goldenstudios.or.id/
Drawing texture in a color?
« Reply #5 on: February 09, 2005, 05:56:26 »
Whoops forget to login  :oops:
That's me
D Java Game Engine
GTGE  Website | GTGE Forum

*

Offline tomb

  • ***
  • 148
Drawing texture in a color?
« Reply #6 on: February 09, 2005, 14:15:04 »

*

Offline Role

  • *
  • 17
    • http://www.goldenstudios.or.id/
Drawing texture in a color?
« Reply #7 on: February 10, 2005, 11:17:23 »
Ouw thanks for the link...
But I still can't solved the problem :(
D Java Game Engine
GTGE  Website | GTGE Forum

*

sysor

Drawing texture in a color?
« Reply #8 on: March 10, 2005, 00:59:34 »
Quote from: "Anonymous"
Not working :(
This is my render texture function :
Code: [Select]

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

Re: Drawing texture in a color?
« Reply #9 on: March 10, 2005, 01:06:36 »
Code: [Select]

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