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

Author Topic: Issue with 2D normal maps  (Read 1272 times)

0 Members and 1 Guest are viewing this topic.

Haikarainen

  • Hero Member
  • *****
  • Posts: 545
    • MSN Messenger - fredrik.haikarainen@gmail.com
    • View Profile
    • Email
Issue with 2D normal maps
« on: December 28, 2013, 05:00:24 am »
I'm attempting to create a lighting system for a 2d platformer, which will include normal mapped textures(to give a sense of depth and detail in the textures). However I've ran across an issue, it's a really hard issue to explain so I created a minimal example of what I've got so far regarding normal maps.

To try and explain the problem; It's like the light direction for the normals is static to the whole texture, when I want it to be pixelperfect. I really cant explain it better than that, you have to see the examples.

Minimal example: https://gist.github.com/haikarainen/4c4ab515d9c13a7f467b
Windows Binaries and textures of example: haikarainen.dotgeek.org/release.7z

Any help is hugely appreciated as my math sucks.

Cheers

G.

  • Hero Member
  • *****
  • Posts: 1462
    • View Profile
Re: Issue with 2D normal maps
« Reply #1 on: December 28, 2013, 05:33:35 am »
You use mousePos as the light direction for every pixels. You shouldn't. (unless it's some kind of directional light :p)
Your light direction should be the vector from mousePos (the light position) to the pixel position. You can use gl_FragCoord to get the pixel position in window space. (If you want its position in world space I think you have to compute it in the vertex shader and pass it to the fragment shader with a varying variable)
« Last Edit: December 28, 2013, 05:53:41 am by G. »

Haikarainen

  • Hero Member
  • *****
  • Posts: 545
    • MSN Messenger - fredrik.haikarainen@gmail.com
    • View Profile
    • Email
Re: Issue with 2D normal maps
« Reply #2 on: December 28, 2013, 06:38:08 pm »
Thanks a ton :) Really makes sense for me now when I'm reading what you're typing. Think I was kind of destroyed yesterday (slept like 3 hrs per night whole week until tonight when I've slept like 11 hours, life with kid is hard).

Got a nice result now, although the mouse Y axis is inversed by some weird reason (not the actual mouse position, but how the light position sets itself based on the mouse pos), looking into that right now :)

G.

  • Hero Member
  • *****
  • Posts: 1462
    • View Profile
Re: Issue with 2D normal maps
« Reply #3 on: December 28, 2013, 08:12:03 pm »
Yes, the pixel origin is in the lower left corner of the window. Whereas in SFML it's in the top left corner.
You could do mousePos.y = window.height - mousePos.y before sending it to the shader.
How old is your kid? Good luck. :D

Haikarainen

  • Hero Member
  • *****
  • Posts: 545
    • MSN Messenger - fredrik.haikarainen@gmail.com
    • View Profile
    • Email
Re: Issue with 2D normal maps
« Reply #4 on: December 28, 2013, 08:48:56 pm »
Yeah figured as much. :)

She is 2 years old in march, thanks!