Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  2d shader lighting  (Read 1418 times)
0 Members and 1 Guest are viewing this topic.
Offline Cenomla

Junior Newbie


Exp: 4-6 months



« Posted 2014-03-20 03:31:46 »

I am working on a 2d tile based game similar to terraria using lwjgl. I want to implement smooth lighting but I can't figure out a good way to do it without killing performance. Any help would be appreciated!
Offline quew8

JGO Coder


Medals: 29



« Reply #1 - Posted 2014-03-22 11:06:16 »

http://www.lighthouse3d.com/tutorials/glsl-tutorial/. I advise you take a look through this. From the thread's title, you clearly know of shaders but the fact that you can't figure out how to do some 2D lighting with them suggests to me that you don't know how to use them. This tutorial will take you through that.
Offline Cenomla

Junior Newbie


Exp: 4-6 months



« Reply #2 - Posted 2014-03-22 23:42:57 »

So now I've got a simple dynamic lighting, but it only works with the first light. I pass a list of light to a shader and one light renders but the rest appear at 0, 0. This is my shader code:
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  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
#version 150 core

    layout (std140) uniform matrixBlock {
        mat4 matrix;
    };

    layout (std140) uniform LightsBlock {
        vec2 light[2];
    } lights;

    in vec4 in_Position;
    in vec4 in_Color;
    in vec2 in_TextureCoord;

    out vec4 pass_Color;
    out vec2 pass_TextureCoord;

    float convertCoordsX(float coord);
    float convertCoordsY(float coord);

    void main(void) {
    gl_Position = matrix*in_Position;

    float closeDist = 999;
    float posX = convertCoordsX(in_Position.x);
    float posY = convertCoordsY(in_Position.y);

    for (int i=0; i<2; i++) {
    float disX = posX-lights.light[i].x;
    float disY = posY-lights.light[i].y;
    float alpha = sqrt((disX*disX)+(disY*disY));
    if (alpha<closeDist) {
        closeDist=alpha;
    }
    }

    pass_Color = vec4(in_Color.r, in_Color.g, in_Color.b, closeDist/128);
    pass_TextureCoord = in_TextureCoord;
    }

    float convertCoordsX(float coord) {
    return (coord+1)*400;
    }
    float convertCoordsY(float coord) {
    return (((coord+1)*300)*-1)+600;
    }


It works when I define the lights in the shader but not when I pass lights to the shader. Here is my code for the lights:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
bindingPoint = 1;
float[] lights = new float[] {600, 400, 400, 200};

FloatBuffer lightsBuffer = BufferUtils.createFloatBuffer(lights.length*4);
lightsBuffer.put(lights);
lightsBuffer.flip();

int lightsIndex = glGetUniformBlockIndex(pId_Tile, "LightsBlock");
System.out.println(lightsIndex + ", " + pId_Tile);
glUniformBlockBinding(pId_Tile, lightsIndex, bindingPoint);

int lightsUbo = glGenBuffers();
glBindBuffer(GL_UNIFORM_BUFFER, lightsUbo);

glBufferData(GL_UNIFORM_BUFFER, lightsBuffer, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, bindingPoint, lightsUbo);
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline The Lion King
« Reply #3 - Posted 2014-03-23 00:39:30 »

1  
float[] lights = new float[] {600, 400, 400, 200};


should be

1  
float[] lights = new float[]{600, 400, 0, 0, 400, 200, 0, 0};


read more into the std140 layout, simply put the vec2[2] is read as a vec4[2] array.

"You have to want it more than you want to breath, then you will be successful"
Offline Cenomla

Junior Newbie


Exp: 4-6 months



« Reply #4 - Posted 2014-03-23 00:44:16 »

It's working! Thank you so much!
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (26 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (71 views)
2014-09-10 06:39:09

Tekkerue (36 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (41 views)
2014-09-07 01:13:42

Longarmx (27 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (33 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
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!