Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
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  
  projective texturing  (Read 2353 times)
0 Members and 1 Guest are viewing this topic.
Offline deepthought
« Posted 2012-05-03 15:00:57 »

I am attempting to project a texture onto a quad. as i understand, i have to create a texture matrix

1  
  tmatrix= lightview*lightprojection *cameraview.


and then the texcoord is
1  
texcord= tmatrix*vertex position


am i getting this right?

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

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #1 - Posted 2012-05-03 15:28:08 »

Not entirely sure...

1  
2  
3  
4  
gl_Position = projectionMatrix * modelViewMatrix * inVertex;
texCoord = lightProjectionMatrix * lightViewMatrix * inVertex;
//Change texCoord from [-1, 1] to [0, 1]
texCoord = texCoord * 0.5 + 0.5;

is what I did for my shadow mapper.

Myomyomyo.
Offline pitbuller
« Reply #2 - Posted 2012-05-03 17:21:04 »

I do shadow projection
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
attribute vec3 a_position;
uniform mat4 modelViewProjection;
uniform mat4 shadowViewProjection;

varying vec4 shadowCoord;
void main()
{
    vec4 pos = vec4(a_position,1.0);
    shadowCoord = shadowViewProjection * pos;
    shadowCoord.xy += shadowCoord.w;
    shadowCoord.xy *= 0.5;
   
    gl_Position = modelViewProjection * pos;
   
}


Then I can read from projected texture with
1  
texture2DProj(u_texture, coord);


I have pretty much tested every way and concluded that this is fastest way to do it. At least on mobile gpu's becouse touching texcoords on fragment shader cause dependant texture reads on some hardware. Notable example is PowerVR which is pretty popular.(all ios + some android devices)




ps. Just noticed that some gpu can pre multiply 3-4 matrix outside of vertex shader for free. After that cost can skyrocket really fast so I wont trust that and keep doing it at cpu level.
1  
2  
3  
    gl_Position = A * B * C * pos;
//is same as
    gl_Position = ABC * pos;
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #3 - Posted 2012-05-03 18:07:19 »

shadowCoord.xy += shadowCoord.w;
?
What. Is this some kind of hack to add 1 to x and y to go from [-1, 1] to [0, 1]?

Myomyomyo.
Offline pitbuller
« Reply #4 - Posted 2012-05-03 18:32:26 »

shadowCoord.xy += shadowCoord.w;
?
What. Is this some kind of hack to add 1 to x and y to go from [-1, 1] to [0, 1]?

Short version for theagent:
It's not a "hack". But that its just for that.

Long version with explanation:
Basically we want to shift from unit cube coordinates to texture coordinates.
[-1:1] -> [0:1]  So: (uv +1) / 2 is what we want to do.
But that can be done only after we are back from homogeneous coordinates. And this can be done by dividing coordinate with w. But that is not linear operation and can't be done at vertex shader becouse of interpolation. So it has to be done at fragment level. This can be done by using textureProj2d wich is just simply texture2d with cheap w divide by hardware.

So instead of doing this at fragment level:
1  
2  
shadowCoord.xy /= shadowCoord.w;
shadowCoord.xy = shadowCoord.w * 0.5 + 0.5;


So It can be done:
1  
2  
3  
4  
5  
6  
7  
//vertex
shadowCoord.xy += shadowCoord.w;
shadowCoord.xy *= 0.5;

//fragment
shadowCoord.xy /= shadowCoord.w;
//which can then be replaced with texture2DProj





Edit: Just thinked it trougth and noticed that simple method work allways when w is 1. But can that be guaranteed for shadow mapping? But this method work also for deferred renderer where you want to map world position to screen coordinates without additional fragment load.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #5 - Posted 2012-05-03 19:11:19 »

Everything in rendering is a "hack" except for ray tracing Wink

It's awesome to see practical shader discussions going on though Smiley
Offline deepthought
« Reply #6 - Posted 2012-05-03 20:00:06 »

why would i need to use texture2dproj?  i thought the vertex shader gave the correct texcoords.

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

JGO Knight


Medals: 32



« Reply #7 - Posted 2012-05-03 21:52:54 »

Everything in rendering is a "hack" except for ray tracing Wink

It's awesome to see practical shader discussions going on though Smiley


Even ray tracing is a hack.  You need to start doing photon mapping to stop being a hack, but current implementations of that only map a few thousand photons so it's still hacky at the end of the day.

Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-05-03 22:12:56 »

What constitutes a "hack"?

Offline _Al3x

Senior Devvie


Medals: 7


Indie Games FTW!


« Reply #9 - Posted 2012-05-04 01:04:15 »

Maybe something "hard coded"? I often see it as non-oop code, but, who knows! Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #10 - Posted 2012-05-04 01:56:01 »

I suppose any simulation could be called a hack, but ray-tracing is at least based somewhat on the real physical model, whereas other approaches are basically stagecraft.  But as long as the result is believable, there's nothing wrong with it.
Offline deepthought
« Reply #11 - Posted 2012-05-04 13:24:46 »

speaking of hacks i have an idea to make rayfaster, but can we please get back on topic?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline pitbuller
« Reply #12 - Posted 2012-05-04 13:33:12 »

Why you need to use texture2dProj is explained above allready. You "need" to divide with w but its cheaper use hardware for that instead of shader code.
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.

toopeicgaming1999 (27 views)
2014-11-26 15:22:04

toopeicgaming1999 (23 views)
2014-11-26 15:20:36

toopeicgaming1999 (7 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (24 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!