Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  OpenGL tinting textures  (Read 1477 times)
0 Members and 1 Guest are viewing this topic.
Offline DrewLols

Senior Member


Medals: 1
Projects: 1


Noob going through metamorphosis...


« Posted 2013-04-04 23:13:34 »

Okay, so I'm learning OpenGL through LWJGL, and I am starting to learn to program in a more procedural manner because of it.  (I consider this to be a step back, but what can you do?)  I have some experience with Slick2D, and know a few concepts from LibGDX, but now I'm trying to make my own library that functions like these two.  I feel that by making my own library, I'll be able to know exactly what is going on under the hood and won't rely on these wrappers to talk to OpenGL for me.

BUT I DIGRESS!  Right now, I am trying to program Quad class that will allow shading to occur.  More specifically, I want to apply a tint to textures that I have binded.  Currently, my rendering code looks like this...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
   // Renders quad
  public void render()
   {
      texture.bind();
     
      // Mins and maxes for which parts of the image to select
     float minX = srcX / (float)texture.getImageWidth();
      float minY = srcY / (float)texture.getImageHeight();
      float maxX = srcX2 / (float)texture.getImageWidth();
      float maxY = srcY2 / (float)texture.getImageHeight();
           
      // Begins drawing a quad
     GL11.glBegin(GL11.GL_QUADS);
         GL11.glColor3f(v2Shade, v2Shade, v2Shade);
         GL11.glTexCoord2f(maxX, minY);
         GL11.glVertex2f(srcX2 - srcX, 0);
         GL11.glColor3f(1, 1, 1);
         //
        GL11.glColor3f(v3Shade, v3Shade, v3Shade);
         GL11.glTexCoord2f(maxX, maxY);      //
        GL11.glVertex2f(srcX2 - srcX, srcY2 - srcY);
         //
        GL11.glColor3f(v4Shade, v4Shade, v4Shade);
         GL11.glTexCoord2f(minX, maxY);      //
        GL11.glVertex2f(0, srcY2 - srcY);
         //
        GL11.glColor3f(v1Shade, v1Shade, v1Shade);
         GL11.glTexCoord2f(minX, minY);
         GL11.glVertex2f(0, 0);
      GL11.glEnd();
   }


This is all fine and dandy, but glColor3f only really darkens the colors.  That's good enough for me in most cases, but what if I want to apply a tint?  The tint, of course, could be for lighting.  Maybe the light is particularly yellow, and the texture would be more yellow than usual.  It would be useful if there was a method like...

1  
glTint4f(float red, float green, float blue, float intensity);


As far as I know, though, OpenGL has no such function.  Is their a painless way to do this?  Shading is already painless.

Did you know that 90% of statistics are wrong?
Offline StumpyStrust
« Reply #1 - Posted 2013-04-04 23:16:56 »

...when you give your color for your vertex you can use glColor4f(r,g,b,a) and use same rgba for each coord and it will tint for you over what ever texture you are using. A is alpha which is intensity. So actually opengl does have that.

Offline DrewLols

Senior Member


Medals: 1
Projects: 1


Noob going through metamorphosis...


« Reply #2 - Posted 2013-04-04 23:19:24 »

...when you give your color for your vertex you can use glColor4f(r,g,b,a) and use same rgba for each coord and it will tint for you over what ever texture you are using. A is alpha which is intensity. So actually opengl does have that.

Hmm, maybe I wasn't explicit.  Let's say I wanted to apply a white light to a texture.  How would I go about doing that?  By writing glColor4f(1, 1, 1, 1), I just get the original texture.  I can't really make it brighter.  That's my problem.  Maybe I should have written that in my first post.  Sorry.

I would like something like...

glColor4f(1, 1, 1, 0.5f)

to make the texture half way closer to a white silhouette.

Did you know that 90% of statistics are wrong?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline deepthought
« Reply #3 - Posted 2013-04-04 23:25:24 »

If you wanted it to get brighter you would have to do glcolor4f(2,2,2,1), but unfortunately input is clamped to the 0-1 range.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline DrewLols

Senior Member


Medals: 1
Projects: 1


Noob going through metamorphosis...


« Reply #4 - Posted 2013-04-04 23:27:09 »

If you wanted it to get brighter you would have to do glcolor4f(2,2,2,1), but unfortunately input is clamped to the 0-1 range.

That's depressing...  Is that just an lwjgl thing?

Did you know that 90% of statistics are wrong?
Offline StumpyStrust
« Reply #5 - Posted 2013-04-05 00:52:41 »

After rending your image you could render a non textured quad/trianglestrip above it with additive blending on. That is what I do when I want to do lighting without actually doing lighting.


Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2013-04-05 01:36:55 »

There are three solutions for this problem: shaders, shaders, shaders.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online opiop65

JGO Kernel


Medals: 123
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #7 - Posted 2013-04-05 01:38:03 »

I have to agree with this. I recently started using shaders and stopped using old OpenGL coloring, and it's much easier.

Offline deepthought
« Reply #8 - Posted 2013-04-05 02:40:27 »

i'm trying to get into shaders too. Just FYI the fixed function pipeline is a LOT slower then vertex arrays or VBOS and shaders.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline davedes
« Reply #9 - Posted 2013-04-05 02:59:47 »

I agree; shaders are the way to go.

Now's a good time to pimp my lwjgl-basics API, which is kind of like a more minimal and more modernized SlickUtil. It's very lightweight, and reduces a lot of the boilerplate for you. It should also perform hundreds of times better than Slick or plain old glBegin/glEnd. Basically your entire tint rendering will look like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
... create ...
    //create a tint shader, loading source from text files
   tintShader = new ShaderProgram(vertexShader, fragShader, SpriteBatch.ATTRIBUTES);
 
    //create a sprite batch with our shader
   batch = new SpriteBatch(tintShader);

... render ...
    //start rendering with our tint shader
   batch.begin();
   
    //set the tint RGB values to modulate our texture by
   tintShader.setUniformf("tintColor", 1.2f, 1.0f, 0.8f);

    //draw some sprites with the specified tint
   batch.draw(sprite, 10, 10);

    //finish rendering, flushing our batch to the GPU
   batch.end();


I have a number of tutorials on GLSL and other OpenGL concepts that utilize the library. Shader Lesson 3 explains tinting with sepia (first desaturate to grayscale, then multiply by color). I'd suggest starting with the Intro to Shaders before moving on. Also make sure you have a firm grasp of how OpenGL Textures work before diving into shaders.

Check out the rest of my tutorials here:
https://github.com/mattdesl/lwjgl-basics/wiki

If you are really crazy and refuse to use a 3rd party library, here are a couple links that might help you re-invent the wheel:
Writing your own ShaderProgram Utility
Sprite Batch example code

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2013-04-05 03:06:24 »

i'm trying to get into shaders too. Just FYI the fixed function pipeline is a LOT slower then vertex arrays or VBOS and shaders.
You confuse immediate mode with fixed function pipeline.

Fixed function pipeline has nothing to do with how geometry is sent to the GPU. Therefore fixed function pipeline by definition cannot affect performance of 'vertex arrays or VBOs'.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline deepthought
« Reply #11 - Posted 2013-04-05 14:17:29 »

sorry. I hava trouble putting things into words. Immediate mode is slow. The fixed function pipeline is deprecated.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline sproingie
« Reply #12 - Posted 2013-04-05 17:27:39 »

Technically speaking, OpenGL is all immediate mode, since the opposite of that is "Retained Mode", a feature that was in the original Iris GL and really old DirectX.  That's where the driver itself maintained a scenegraph of objects, and to move things or set their color, you just sent it the changes and it recalculated all the states.  Sounds pretty nifty, but it was slow and inflexible.

Stuff like the various buffer objects in OpenGL are, I suppose, "retained mode done right".  Just thought I'd throw out that little nugget o historical trivia Wink
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (11 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (108 views)
2014-04-08 23:06:04

tom_mai78101 (208 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!