Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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  
  Using LWJGL color API?  (Read 1937 times)
0 Members and 1 Guest are viewing this topic.
Offline EliwoodL

Senior Newbie





« Posted 2013-07-10 16:16:50 »

Could anyone break down how to use the Color API of LWJGL to me?  From my testing, all of these colors are apparently the same:
1  
2  
3  
4  
5  
// Color c = new Color(r, g, b, a);

Color c = new Color(255, 155, 0, 255);
Color c = new Color(155, 255, 0, 255);
Color c = new Color(100, 200, 0, 255);

I searched on google for about an hour and didn't come up with anything substantial.  Although I am aware of bitshifting.  Do I need to shift
Offline tdegroot96

Junior Member


Projects: 1



« Reply #1 - Posted 2013-07-10 16:30:50 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
// Vertices, the order is not important. XYZW instead of XYZ
float[] vertices = {
      -0.5f, 0.5f, 0f, 1f,
      -0.5f, -0.5f, 0f, 1f,
      0.5f, -0.5f, 0f, 1f,
      0.5f, 0.5f, 0f, 1f
};
FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length);
verticesBuffer.put(vertices);
verticesBuffer.flip();

float[] colors = {
      1f, 0f, 0f, 1f,
      0f, 1f, 0f, 1f,
      0f, 0f, 1f, 1f,
      1f, 1f, 1f, 1f,
};
FloatBuffer colorsBuffer = BufferUtils.createFloatBuffer(colors.length);
colorsBuffer.put(colors);
colorsBuffer.flip();
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #2 - Posted 2013-07-10 17:24:34 »

First of all LWJGL does not know about the Color object, so you cant use these.
Secondly, OpenGL(LWJGL) works with floats as color, with values ranging from 0 to 1.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 391
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2013-07-10 18:09:50 »

A wise bunny would use unsigned bytes for colours with OpenGL.

Cas Smiley

Offline Mac70
« Reply #4 - Posted 2013-07-10 18:48:05 »

1  
2  
3  
4  
5  
6  
Color color = new Color(255, 255, 255, 255);
byte red = (byte)(color.getRed()-128);
byte green = (byte)(color.getGreen()-128);
byte blue = (byte)(color.getBlue()-128);
byte alpha = (byte)(color.getAlpha()-128);
GL11.glColor4b(red, green, blue, alpha);

Check out my Devblog! Smiley
Offline princec

JGO Kernel


Medals: 391
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2013-07-10 19:15:44 »

I was thinking more like glColor4ub(color.getRedByte(), color.getGreenByte(), color.getBlueByte(), color.getAlphaByte());

(Assuming this is the LWJGL Color class)

Cas Smiley

Offline davedes
« Reply #6 - Posted 2013-07-10 22:31:33 »

A wise bunny would use unsigned bytes for colours with OpenGL.

Cas Smiley
Or pack RGBA into a float and interleave it with the rest of your vertex data. Smiley (a la LibGDX's 2D SpriteBatch)

Offline EliwoodL

Senior Newbie





« Reply #7 - Posted 2013-07-12 03:26:24 »

Indeed, the bytes thing is a good idea.  Could anyone give me a small example of how to convert bytes to a color, though?  It'd be very, very helpful indeed.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #8 - Posted 2013-07-12 17:42:10 »

Divide each component by 255f  (need the f or you'll get integer division)
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 802
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2013-07-12 23:06:18 »

I think he meant packing RGBA bytes into a FloatBuffer.

1  
2  
3  
   public static float packRGBA(int r, int g, int b, int a) {
      return Float.intBitsToFloat((r << 0) | (g << 8) | (b << 16) | (a << 24));
   }


Usage:
1  
2  
3  
4  
5  
FloatBuffer fb = ...;
fb.put(x);
fb.put(y);
fb.put(z);
fb.put(packRGBA(r,g,b,a));

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline EliwoodL

Senior Newbie





« Reply #10 - Posted 2013-07-12 23:19:41 »

So would I only need 1 color "vertex" when interleaving color with position in my FloatBuffer?  From what it looks like, that seems to be the case.
Offline EliwoodL

Senior Newbie





« Reply #11 - Posted 2013-07-13 03:45:16 »

Adding this snippet of code to ask:  Am I going about this the correct way?  I need 4 floats (one representing each of r, g, b, a) and they're supposed to be values between 0 and 1.  I'm trying to convert from a hexadecimal integer (eg 0xFFFFFFFF) to the four floats.

1  
2  
3  
4  
5  
6  
7  
8  
   protected float[] getRGBA(int color) {
      return new float[] {
            (color & 0xFF) / 255f,
            (color >> 8 & 0xFF) / 255f,
            (color >> 16 & 0xFF) / 255f,
            (color >> 24 & 0xFF) / 255f
      };
   }


Am I thinking about this the right way?
Offline SHC
« Reply #12 - Posted 2013-07-13 11:35:55 »

Why not use
glColor4f()
instead of
glColor4b()
? Is there any benefit of it? I'm using Java2D's
Color
object in LWJGL like this.

1  
2  
3  
4  
5  
6  
7  
8  
Color c = Color.PURPLE;

int red    = c.getRed();
int blue   = c.getBlue();
int green  = c.getGreen();
int alpha  = c.getAlpha();

glColor4f(red, blue, green, alpha);

And it works for me. Is there any wrong in it?

Offline matheus23

JGO Kernel


Medals: 108
Projects: 3


You think about my Avatar right now!


« Reply #13 - Posted 2013-07-13 11:53:47 »

And it works for me. Is there any wrong in it?

Yes.
glColor4f expects Floats, not ints.

You can give it ints, since it'll convert them to floats implicitly, but the problem is, that the expected floats should be in range 0 to 1, but if you give it int's they can only be either 0 or 1, nothing in between.

I don't really know what
c.getRed();
, etc. return, but assuming they return the color component in range 0 to 255, you'd need to divide by
255f
, as sproingie said:

1  
2  
3  
4  
5  
6  
7  
8  
Color c = Color.PURPLE;

float red    = c.getRed() / 255f;
float blue   = c.getBlue() / 255f;
float green  = c.getGreen() / 255f;
float alpha  = c.getAlpha() / 255f;

glColor4f(red, blue, green, alpha);

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline SHC
« Reply #14 - Posted 2013-07-13 12:03:27 »

Hey, I found that there is
glColor4i
function which directly takes integers ranging from
0...255
saving us from dividing each time. Also, Java2D's Color object gives the components in that range only as ints. This is certainly not a big performance optimization but certainly helps me since I'm somewhat poor in maths.

Offline princec

JGO Kernel


Medals: 391
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #15 - Posted 2013-07-13 12:15:28 »

glColor4i takes ints in the entire range of integers, not just 0...255. It's practically useless.

Cas Smiley

Offline SHC
« Reply #16 - Posted 2013-07-13 12:30:01 »

@princec

Thanks for pointing out. Now I made two functions
setColor()
and
getColor()
as sproingie said.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
private Color color = Color.BLACK;

public void setColor(Color c)
{
    color = c;
    glColor4f(
        (float) (color.getRed() / 255f),
        (float) (color.getBlue() / 255f),
        (float) (color.getGreen() / 255f),
        (float) (color.getAlpha() / 255f)
    );
}

public Color getColor()
{
    return color;
}

Offline princec

JGO Kernel


Medals: 391
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #17 - Posted 2013-07-13 21:10:00 »

... though why you'd be using immediate mode OpenGL for anything serious is a question.

Cas Smiley

Offline Mac70
« Reply #18 - Posted 2013-07-13 22:20:46 »

He can always use much, much faster and still almost as easy to use as immediate mode display lists. Wink

Check out my Devblog! Smiley
Offline princec

JGO Kernel


Medals: 391
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #19 - Posted 2013-07-13 22:44:25 »

Yeah, but it's all deprecated tech that's going nowhere... far better to get the hang of vertex arrays and VBOs, because that's what all the drivers are headed towards.

Cas Smiley

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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (46 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (36 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (51 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!