Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

878991 Posts in 32951 Topics- by 24353 Members - Latest Member: kanki

May 23, 2013, 03:34:20 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Need help with color operations in Open GL
Pages: [1] 2
Print
Author Topic: Need help with color operations in Open GL  (Read 1721 times)
garlandobloom
Level 0
***



View Profile WWW Email
« on: May 02, 2012, 10:59:27 AM »

The game that I am making has 2d graphics which are built of alpha-mapped images which can be arbitrarily positioned, rotated and scaled. I am also wanting to allow some runtime manipulation of the color of the images.

Basically, I want to allow for a tint, and also an adjustable atmospheric density. In the sense that the atmosphere is just like a fogging effect which affects the overall color of the image, slowly moving towards a solid color.

Tinting is simple enough, I can just use GLTexEnv and set the mode to GL_MODULATE or GL_ADD and then use GLcolor to tint it around a bit. The issue is that I want to have this effect, but I also want to be able to uniformly blend the image with a base color, after the first operation. (or at all, for that matter) I've tried setting GL_COMBINERGB to GL_INTERPOLATE, and leaving the alpha on modulate, but that just replaces the color channels completely, with no blending of the original texture, I can just draw the asset twice, normal the first time and solid color the second, then change the alpha of the solid color version, but then it bleeds off at the edges of the alpha map, and the coloration is not uniform.

I assume that I am missing something about how all this works, but staring at API references hasn't been terribly helpful. I just want to know what would be the best approach to do this two-step tinting operation. Do I need to write a shader, or should it be possible with the basic texture blending modes.
Logged

Rob Lach
Level 10
*****


Pierog


View Profile WWW Email
« Reply #1 on: May 02, 2012, 01:12:33 PM »

You're jumping through so many hoops. Just use a pixel/fragment shader.
Logged

Ludophonic
Level 1
*


View Profile
« Reply #2 on: May 02, 2012, 01:27:38 PM »

I agree. You could do it with combine and texture blending stages. But your time is better spent just getting shaders going. Then you can express the exact algorithm you want instead of trying to wedge it into multiple texture/combine stages.
« Last Edit: May 02, 2012, 09:55:12 PM by Ludophonic » Logged
Fallsburg
Level 10
*****


Fear the CircleCat


View Profile WWW
« Reply #3 on: May 02, 2012, 02:21:05 PM »

Shaders thirded.  Plus, they are fun to write (at least for me).
Logged

garlandobloom
Level 0
***



View Profile WWW Email
« Reply #4 on: May 02, 2012, 08:27:38 PM »

Thanks everyone.

However I have never written a single line of GLSL or shader code at all. Any tips or references that you recommend for starters?

So, it should be fairly trivial to pass variables from C++ code to a shader as parameters, I assume?
Logged

Fallsburg
Level 10
*****


Fear the CircleCat


View Profile WWW
« Reply #5 on: May 03, 2012, 06:40:11 AM »

Well, I too have never written any GLSL in my life, but it's not too complex. 

Start with the hello world of the pixel shader world, the one that does standard rendering.
i.e. You get a UV coordinate and a texture, look up the proper location in the texture and then output that.

Vertex Shader
Code:
varying vec2 texture_coordinate;

void main()
{
    // Transforming The Vertex
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
 
    // Passing The Texture Coordinate Of Texture Unit 0 To The Fragment Shader
    texture_coordinate = vec2(gl_MultiTexCoord0);
}

Fragment Shader
Code:
varying vec2 texture_coordinate; uniform sampler2D my_color_texture;

void main()
{
    // Sampling The Texture And Passing It To The Frame Buffer
    gl_FragColor = texture2D(my_color_texture, texture_coordinate);
}

And then start messing with that.
Logged

harkme
Level 1
*


Surprise!


View Profile Email
« Reply #6 on: May 03, 2012, 12:31:05 PM »

Just a word of caution, most of the Intel line of GPUs do not support GLSL programs since they only support up to OpenGL version 1.5.
Logged

Ludophonic
Level 1
*


View Profile
« Reply #7 on: May 03, 2012, 02:32:38 PM »

Intel chipsets have supported OpenGL 2.0+ for awhile. Older stuff (5+ years) will be a problem. But some support the ARB_fragment_shader extension with OpenGL 1.5.

http://www.intel.com/support/graphics/sb/cs-010479.htm
Logged
garlandobloom
Level 0
***



View Profile WWW Email
« Reply #8 on: May 03, 2012, 08:48:11 PM »

My dev machine is 8 years old, lol. I figured if it ran on there, it was probably good enough to go. Unless everything just gets deprecated out of existence. I'm sure people would barf if they saw my GL code.

To be honest, the gpu is an HD 4650, so it is somewhat newer.

I will see if I can even get shaders up and running, although this is quickly approaching the event horizon of "not worth the time" functionality.
Logged

Ludophonic
Level 1
*


View Profile
« Reply #9 on: May 03, 2012, 09:16:37 PM »

The 5+ years only applies to Intel motherboard graphics. Radeon cards have supported OpenGL 2.0 since 2002. You should be fine.  Wink
Logged
Richard Kain
Level 10
*****



View Profile WWW Email
« Reply #10 on: May 04, 2012, 08:08:49 AM »

If you are shooting for maximum compatibility with the broadest possible audience, than shooting for OpenGL 1.5 would probably be the best bet.

If you are shooting for a crowd that is more familiar with games on the PC, than OpenGL 2.0 and up is a fairly safe target. The majority of individuals who take PC games even slightly seriously are going to have a dedicated GPU in their machine. Even if a motherboard has an on-board Intel GPU, as long as it is running its graphics through a dedicated 3D card, the more recent versions of OpenGL with shader support should run fine.

The biggest danger for trying to run GLSL shaders is in older laptops. The majority of desktop systems are going to be fine. I'm still targeting OpenGL 1.5 for some of my projects. But this is more a choice of ease-of-use than features.
Logged
garlandobloom
Level 0
***



View Profile WWW Email
« Reply #11 on: May 04, 2012, 11:33:47 AM »

Hmmm... GLSL seems like the easy part. Loading the shader files into a C++ program and making them usable is maybe more complex than it should be.

Any good tips or references, (with code?) on how to load up the shaders into OpenGL?

I worked a bit with some old out of date OpenGL 1.5 tutorial, and for some reason, I'm getting undeclared functions when I call GL_LoadShader or even GL_LoadShaderARB. I'm running GL through SDL, by the way. I dunno if that is the issue, but SDL includes OpenGL header files anyway...

[edit] This tutorial looks helpful. I won't be able to try it until later, but yep: http://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/loading.php
« Last Edit: May 04, 2012, 11:40:56 AM by garlandobloom » Logged

BlueSweatshirt
Level 10
*****


the void


View Profile WWW
« Reply #12 on: May 05, 2012, 04:22:53 PM »

Yep, that tutorial looks like it will pretty much cover you.  Hand Thumbs Up Right
Logged

mcc
Level 10
*****


glitch


View Profile WWW Email
« Reply #13 on: May 05, 2012, 05:53:43 PM »

One thing-- you might be overthinking it.

What do these 2D graphics look like?

If these are like, pixel art images, and your color manipulation means retinting a fixed sprite from blue to red, you can (and probably should) just do this by retinting in CPU and uploading new textures.
Logged

My projects:<br />Games: Jumpman Retro-futuristic platforming iJumpman iPhone version Drumcircle PC+smartphone music toy<br />More: RUN HELLO
garlandobloom
Level 0
***



View Profile WWW Email
« Reply #14 on: May 06, 2012, 04:46:02 PM »

Well tinting isn't really that difficult. I just want to do a uniform color blend. Where the darks will be lightened and the brights will be darkened towards a specific color. Haven't sat down and tried implementing the shader tutorials this weekend so far. But I will let everyone know if I figure it out/give up.
Logged

Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic