[osg-users] how to get the real world coordinates(x, y, z) in shader

507 views
Skip to first unread message

ttaw

unread,
Jul 2, 2014, 7:47:05 AM7/2/14
to osg-users
Hi,all:
  I want to get world coordinates of vertices in shader. I used:
    osg_ViewMatrixInverse*(gl_ModelViewMatrix*vertex);
  But the result is wrong if the geode is a child of an HUD camera node which is in the scenegraph.It seems that the uniform osg_ViewMatrixInverse is not for the HUD camera. How can I get world coordinates under this situation?Thanks.
------------------
Failure is the mother of success.
Wu Zhicheng
 

Solkar Graphics

unread,
Jul 2, 2014, 12:44:18 PM7/2/14
to osg-...@lists.openscenegraph.org

ttaw wrote:
> I want to get world coordinates of vertices in shader. I used:
> osg_ViewMatrixInverse*(gl_ModelViewMatrix*vertex);
> But the result is wrong if the geode is a child of an HUD camera node
(emphasis mine)
What are the symptoms?


ttaw wrote:
> which is in the scenegraph.It seems that the uniform osg_ViewMatrixInverse is not for the HUD camera.

If this is that fact, you could create a custom mat4 in the shader and provide the inverse ViewMatrix to it via an osg::Uniform.


Best regards,
S.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60113#60113





_______________________________________________
osg-users mailing list
osg-...@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

ttaw

unread,
Jul 2, 2014, 4:50:46 PM7/2/14
to osg-users
Hi, Solkar:
  Following is the symptoms: There's an earth node under scene root whose coordinates are between 5000000 and 7000000. And also an ortho-projecting HUD camera with width 1600 and height 900 is under root.Under the HUD camera, a geode with text whose position is (1300,500,0) and whose size is (200,75) exists.Eye position is above the earth surface. The x coords of the geode I got by osg_viewMatrixInverse*gl_ModelViewMatrix*vertex is less than -1000000.But the value I want is (1300,500,0). 
 
------------------
Failure is the mother of success.
Wu Zhicheng
 


------------------ Original ------------------
From:  "Solkar Graphics";<sol...@freenet.de>;
Date:  Thu, Jul 3, 2014 00:45 AM
Subject:  Re: [osg-users] how to get the real world coordinates(x, y,z) in shader
.

Solkar Graphics

unread,
Jul 2, 2014, 8:12:05 PM7/2/14
to osg-...@lists.openscenegraph.org

ttaw wrote:
> Following is the symptoms: There's an earth node under scene root whose coordinates are between 5000000 and 7000000. And also an ortho-projecting HUD camera with width 1600 and height 900 is under root.Under the HUD camera, a geode with text whose position is (1300,500,0) and whose size is (200,75) exists.Eye position is above the earth surface. The x coords of the geode I got by osg_viewMatrixInverse*gl_ModelViewMatrix*vertex is less than -1000000.But the value I want is (1300,500,0).


7M-5M = 2M, thus the -1M would roughly be at the viewers side of the surface in earth coodinates.

Thus is reads like the text node was parented to your earth node, not directly to world.

Cheers,
S

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60117#60117

ttaw

unread,
Jul 2, 2014, 8:57:26 PM7/2/14
to osg-users
No, the text node is parented to root,sibling with the earth node.I think if osg_ViewMatrixInverse is from eye position, the result could also be about -x000000 because gl_ModelViewMatrix*vertex is between -1 and 1, after transforming to world coords, the value could be millions.
 
------------------
Failure is the mother of success.
Wu Zhicheng
 


------------------ Original ------------------
From:  "Solkar Graphics";<sol...@freenet.de>;
Date:  Thu, Jul 3, 2014 08:13 AM
Subject:  Re: [osg-users] how to get the real world coordinates(x, y,z) in shader
.

Solkar Graphics

unread,
Jul 3, 2014, 3:04:27 AM7/3/14
to osg-...@lists.openscenegraph.org

ttaw wrote:
> No, the text node is parented to root,sibling with the earth node.I think if osg_ViewMatrixInverse is from eye position, the result could also be about -x000000 because gl_ModelViewMatrix*vertex is between -1 and 1, after transforming to world coords, the value could be millions.


It's a little hard to help you as long as long you neither provide details about how you examine your app and its data, nor post the respective code.

Also a SSCCE (http://www.sscce.org/)
(E.g a sphere to mock the your earth globe, a cube to mock the text node, and a plain toon shader that colorizes depending on magnitude of the respective coord ...)
would not be hard to assemble, or would it?

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60119#60119

ttaw

unread,
Jul 3, 2014, 3:54:51 AM7/3/14
to osg-users
ok.I'll make an sscce.It may be completed this week.
------------------ Original ------------------
From: "Solkar Graphics"<sol...@freenet.de>
Date: Thu, Jul 3, 2014 03:05 PM
Subject: Re: [osg-users] how to get the real world coordinates(x, y,z) in shader
.

ttaw

unread,
Jul 3, 2014, 5:00:27 AM7/3/14
to osg-users
Hi, Solkar:
 My SSCCE is as the attachment.Run the application, and drag the earth and release, the earth would rotate, and you'll see a red block shows near the right side, which indicates the coordinates I got is less than -1000000.Thanks.
 
------------------
Failure is the mother of success.
Wu Zhicheng
 

------------------ Original ------------------
From:  "Solkar Graphics";<sol...@freenet.de>;
Date:  Thu, Jul 3, 2014 03:05 PM
Subject:  Re: [osg-users] how to get the real world coordinates(x, y,z) in shader


.
osgviewer - copy.zip

Solkar Graphics

unread,
Jul 4, 2014, 8:51:54 AM7/4/14
to osg-...@lists.openscenegraph.org
Hi,

Well...I was more thinking about a SSCCE than a whole project, but anyway:

Does your compiler support the currently effective (2011) standard for C++ sufficiently?
try e.g. to compile this:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///@file bind_recursion.cxx
///@author solkar http://stackoverflow.com/users/2378245/solkar
///@see http://stackoverflow.com/questions/24491402/compile-time-recursion-via-stdfunction-variadic-templates/24494430#24494430
///@date Jul 1st, 2014

#include <functional> // bind, is_placeholder
#include <exception> // runtime_error
#include <iostream>
#include <cmath> // M_PI
#include <complex>

#define VOODOO 1

#if VOODOO
/**
* It's rather pointless binding the N parameters
* of N-ary callable to N placeholders in-sequence
*
* This framework will start making sense if there are
* some free parameters to be bound; see HINT below.
*/

// placeholder
template<int N> struct PH {};

// It's ugly to extend std,
// but there's no other way to make arbitrary
// placeholders known to std::bind
namespace std {
template<int N>
struct is_placeholder<PH<N>>
: public integral_constant<int, N> {};
}

// recursive sequential binder
// this simply pushes the placeholder types into the Args
// result is in-order
template<class F, int N, class... Args>
struct SeqBinder : public SeqBinder<F, N-1, PH<N>, Args...> {};

// recursive sequential binder terminator specialization
template<class F, class... Args>
struct SeqBinder<F, 0, Args...> {
static auto bind(F&& f)
/*
* HINT
* here is to point to add whatever invariant params
* to the bind
*/
-> decltype(std::bind(f, /* +whatever ,*/ Args()...)) {
return std::bind(f, /* +whatever ,*/ Args()...);
} // bind()
}; // SeqBinder (terminator specialization)

template<typename F, int N>
auto seq_bind(F&& f, const PH<N>&&)
-> decltype(SeqBinder<F, N>::bind(f)) {
return SeqBinder<F, N>::bind(f);
} // template seq_bind()

#endif // #if VOODOO

// no clue why gcc (4.8.2) wants a
// void output(void) (see below)
// for the recursive template below
void output(void) {

struct void_call : public std::runtime_error {
void_call(const char* msg)
: std::runtime_error(msg) {}
};

// trace this
throw void_call("void output(void) called");
} // void output(void)

// recursive template
template<typename T, typename... Args>
void output(const T& t, const Args&... args) {
std::cout << typeid(t).name() << " : " << t << std::endl;
if (sizeof...(Args) > 0)
// no clue why gcc (4.8.2) looks for a
// void output(void)
output(args...);
} // template void output()

// test class
template <
typename... Args
> struct LHC {
std::function<void(Args...)> cauldron;

///@TODO delete unwanted ctors

LHC() :
#if VOODOO
cauldron(seq_bind(output<Args...>, PH<sizeof...(Args)>()))
#else
cauldron(output<Args...>)
#endif // #if VOODOO
{}

void operator () (const Args&...args) const{
cauldron(args...);
}
}; // struct LHC

template <
typename... Args
> void make_GUT(Args... args) {
LHC<Args...> smasher;
smasher(args...);
}

void test() {
auto cminus = std::exp(std::complex<double>(0.,M_PI));
auto phone_number = 137L;
make_GUT("GUT essentials: ", 42, NULL, M_PI, cminus, phone_number);
} // void test()

int main() {
try {
test();
} catch (const std::runtime_error& e) {
std::cerr << e.what() << std::endl;
}
} // int main()
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
with both(0,1) settings for "VOODOO", and so-called C++11-"support" enabled in your compiler.

I'd dislike having to publish code that is already sort-of deprecated at the time of publishing.

Cheers,
Solkar

@moderators: How to save the "<",">" in
#include <myheader>
in code-tags from being parsed. I thought especially code-tags would prevent that.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60154#60154

ttaw

unread,
Jul 4, 2014, 6:28:05 PM7/4/14
to osg-users
thanks.I see.I will post an attachment when i finish the sscce.
------------------ Original ------------------
From: "Solkar Graphics"<sol...@freenet.de>
Date: Fri, Jul 4, 2014 08:53 PM
Subject: Re: [osg-users] how to get the real world coordinates(x, y,z) in shader
.

Solkar Graphics

unread,
Jul 5, 2014, 10:48:56 AM7/5/14
to osg-...@lists.openscenegraph.org

ttaw wrote:
> thanks.I see.I will post an attachment when i finish the sscce

You got me wrong, no need anymore for you to provide code.

I already fixed that little bug in your app, I just want to know if your compiler supports current C++ - it would be rather cynical to give you a bug fix you cannot compile, wouldn't it.

Cheers, S.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60178#60178

ttaw

unread,
Jul 5, 2014, 10:57:20 AM7/5/14
to osg-users
Really?I will have a test on your code when I'm ready next monday.Thank you so much:)
 
------------------
Failure is the mother of success.
Wu Zhicheng
 


------------------ Original ------------------
From:  "Solkar Graphics";<sol...@freenet.de>;
Date:  Sat, Jul 5, 2014 10:50 PM
Subject:  Re: [osg-users] how to get the real world coordinates(x, y,z) in shader
.

Solkar Graphics

unread,
Jul 5, 2014, 3:45:13 PM7/5/14
to osg-...@lists.openscenegraph.org

ttaw wrote:
> Really?I will have a test on your code when I'm ready next monday.Thank you so much:)

You again got me wrong .
The code I posted yet is not the bug fix itself, but just a test for C++11 support for your compiler.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60181#60181

ttaw

unread,
Jul 5, 2014, 8:28:30 PM7/5/14
to osg-users
-_-||| I meant that i would test my compiler on monday,then i could apply your fix:)
------------------ Original ------------------
From: "Solkar Graphics"<sol...@freenet.de>
Date: Sun, Jul 6, 2014 03:46 AM
Subject: Re: [osg-users] how to get the real world coordinates(x, y,z) in shader
.

Solkar Graphics

unread,
Jul 6, 2014, 3:55:08 PM7/6/14
to osg-...@lists.openscenegraph.org
Very well.

Some preliminary remarks:


The sphere
Code:
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3d(0,0,0), 6500000.0)));

has a radius of 6.5E6, whereas your orthographic frustum
Code:
node->setProjectionMatrix(osg::Matrix::ortho2D(0, 1600, 0, 900));

is 3 magnitudes smaller along each axis.
That would never fit around the whole sphere, but apparently, so see the sphere nevertheless That gives rise to the notion of some "mangling" being done behind the scenes.

Again the sphere
Code:
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3d(0,0,0), 6500000.0)));


It's at origin. as initially is world respective to cam coords

Code:
node->setViewMatrix(osg::Matrix::identity());

. cam would sit inside the sphere, if no mangling was done.

osgViewer::Viewer::run() http://trac.openscenegraph.org/projects/osg/browser/OpenSceneGraph/trunk/include/osgViewer/Viewer


> /** Execute a main frame loop.
> * Equivalent to while (!viewer.done()) viewer.frame();
> * Also calls realize() if the viewer is not already realized,
> * and installs trackball manipulator if one is not already assigned.
> */
>
and it's obvious that there is a manipulator installed, because the viewer is responsive to mouse events.
If you log the view matrix before the viewer.run() you get the identity matrix you set.
But If log dump the view matrix from within run(), e.g in your

Code:
UpdateUniformsCallback::operator()


you get

1 0 0 0
0 0 1 0
0 -1 0 -4.47409e+07
0 0 0 1

thus world has been tilted by [pi/2,0,0] and shifted ~4.5E7 to the back.


That's it about the maths.
Now about the uniforms -
you have to do

Code:
pref->getState()->setUseModelViewAndProjectionUniforms(true);
pref->getState()->setUseVertexAttributeAliasing(false); // depands on the shader code



for all valid

Code:
osgViewer::ViewerBase::Contexts:.value_type pref;


after the view has been realized once but before the run.

I will give you code for that after you'll have figured out your compiler's C++ support.

Cheers,
Solkar
Code:




------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60183#60183

ttaw

unread,
Jul 6, 2014, 9:40:17 PM7/6/14
to osg-users
I tested my compiler(VisualStudio2010sp1), and found that c++11 is not supported unfortunately.It seems that ... is the problem.Following is the output info:
VOODOO=1:
1>InitializeBuildStatus:
1>  正在对“Debug\test111.unsuccessfulbuild”执行 Touch 任务。
1>ClCompile:
1>  1.cpp
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(39): error C2332: “class”: 缺少标记名
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(39): error C2993: “”: 非类型模板参数“<unnamed-tag>”的类型非法
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(39): error C2143: 语法错误 : 缺少“,”(在“...”的前面)
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(40): error C2065: “Args”: 未声明的标识符
1>          e:\wuzc02\源码\20131011\test111\test111\1.cpp(40): 参见对正在编译的类 模板 实例化“SeqBinder<F,N,<unnamed-tag>>”的引用
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(40): error C2143: 语法错误 : 缺少“,”(在“...”的前面)
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(40): error C2143: 语法错误 : 缺少“;”(在“{”的前面)
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(40): fatal error C1004: 发现意外的文件尾
1>
1>生成失败。
 
VOODOO=0
1>InitializeBuildStatus:
1>  正在对“Debug\test111.unsuccessfulbuild”执行 Touch 任务。
1>ClCompile:
1>  1.cpp
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(79): error C2143: 语法错误 : 缺少“,”(在“...”的前面)
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(80): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(80): error C2143: 语法错误 : 缺少“,”(在“&”的前面)
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(90): error C2143: 语法错误 : 缺少“,”(在“...”的前面)
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(92): error C2065: “Args”: 未声明的标识符
1>          e:\wuzc02\源码\20131011\test111\test111\1.cpp(107): 参见对正在编译的类 模板 实例化“LHC<__formal>”的引用
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(92): error C2143: 语法错误 : 缺少“)”(在“...”的前面)
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(100): error C2065: “Args”: 未声明的标识符
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(100): error C2143: 语法错误 : 缺少“,”(在“...”的前面)
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(100): error C2059: 语法错误:“)”
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(102): error C2143: 语法错误 : 缺少“;”(在“{”的前面)
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(104): error C2143: 语法错误 : 缺少“;”(在“{”的前面)
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(106): error C2143: 语法错误 : 缺少“;”(在“}”的前面)
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(107): error C2143: 语法错误 : 缺少“;”(在“}”的前面)
1>e:\wuzc02\源码\20131011\test111\test111\1.cpp(107): fatal error C1004: 发现意外的文件尾
1>
1>生成失败。
 
------------------
Failure is the mother of success.
Wu Zhicheng
 


------------------ Original ------------------
From:  "Solkar Graphics";<sol...@freenet.de>;
Date:  Sun, Jul 6, 2014 09:58 PM
Subject:  Re: [osg-users] how to get the real world coordinates(x, y,z) in shader
.

Solkar Graphics

unread,
Jul 8, 2014, 7:59:12 PM7/8/14
to osg-...@lists.openscenegraph.org
I received that my last msg of Tue Jul 08, 2014 2:09 was too large for the large mailing list to pass unmoderated, and indeed I would not not want to send mails with a 0.6M attachment.

So @ttaw: the mentioned bugfix is an attachment of a post in the OSG forum.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60217#60217

ttaw

unread,
Jul 8, 2014, 10:47:37 PM7/8/14
to osg-users
  Thank you, Solkar, you are so kind.
  I downloaded your code from osg forum, and ran the application. I found what really matters is  the following two functions: setUseModelViewAndProjectionUniforms, setUseVertexAttributeAliasing, and osg_ModelViewMatrix in shader files.Am I right? But the problem still exists: the position coordinate of the text node remains mag x000000, the same as my original code.(I added following code into frag shader:
  if (wPos.x < 2000000.0)
    discard;
and could see the text flickering while changing eye position which indicates that wPos is less than 2000000.0 periodically.
)
I want coords with mag 1000.0. Any ideas?
 
------------------
Failure is the mother of success.
Wu Zhicheng
 


------------------ Original ------------------
From:  "Solkar Graphics";<sol...@freenet.de>;
Date:  Wed, Jul 9, 2014 08:00 AM
Subject:  Re: [osg-users] how to get the real world coordinates(x, y,z) in shader

Solkar Graphics

unread,
Jul 8, 2014, 11:15:59 PM7/8/14
to osg-...@lists.openscenegraph.org

ttaw wrote:
> But the problem still exists:
[/quote]
This
[quote="ttaw"the position coordinate of the text node remains mag x000000[/quote]
is not a problem.

That is simply the world coordinate corresponding to the of the view the viewer creates for the perspective projection of the sphere.

Your HUD cam simply inherits that from the root node; but because the mere distance has no effect on an orthographic projection that is no problem for your HUD.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60219#60219

ttaw

unread,
Jul 9, 2014, 1:07:04 AM7/9/14
to osg-users
Hi, Solkar:
  The world coord you mentioned is cooresponding to the earth view, but the world coord I calculate by osg::computeLocalToWorld function is not for earth view.For some purpose, I want these two coords should be for a single view position. How can I do it?
 
------------------
Failure is the mother of success.
Wu Zhicheng
 


------------------ Original ------------------
From:  "Solkar Graphics";<sol...@freenet.de>;
Date:  Wed, Jul 9, 2014 11:17 AM
Subject:  Re: [osg-users] how to get the real world coordinates(x, y,z) in shader

Solkar Graphics

unread,
Jul 9, 2014, 5:48:21 AM7/9/14
to osg-...@lists.openscenegraph.org

ttaw wrote:
> The world coord you mentioned is cooresponding to the earth view, but the world coord I calculate by osg::computeLocalToWorld function is not for earth view.

osg::computeLocalToWorld is about ModelMatrices, not about ViewMatrices.



ttaw wrote:
> For some purpose, I want these two coords should be for a single view position.
Please rephrase this; the grammar is not valid.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60222#60222

ttaw

unread,
Jul 9, 2014, 6:02:19 AM7/9/14
to osg-users
Sorry for my English.I meant that those two coords should be corresponding to a single coordinate system, either local coord system or world coord system or view coord system would be ok
------------------ Original ------------------
From: "Solkar Graphics"<sol...@freenet.de>
Date: Wed, Jul 9, 2014 05:49 PM
Subject: Re: [osg-users] how to get the real world coordinates(x, y,z) in shader

Solkar Graphics

unread,
Jul 9, 2014, 2:32:52 PM7/9/14
to osg-...@lists.openscenegraph.org

ttaw wrote:
> Sorry for my English.I meant that those two coords should be corresponding to a single coordinate system, either local coord system or world coord system or view coord system would be ok


Which "two vectors"?

In any case, given two vectors expressed in coords of different reference frames, you just transform them individually if you want both of them to be expressed in coords of a third reference frames system.

That's basic algebra.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60228#60228
Reply all
Reply to author
Forward
0 new messages