PDA

View Full Version : Billboard in all axis



raziza
11-28-2010, 06:38 AM
I'm trying to make an single image (quad) facing to the camera in all cases.
which means considering all of this parameters:
Camera position, up vector, forward vector and object position.
I ran over all the tutorials and examples on net. each example solve "kind of", part of the problem. most of them don't consider the target point of the camera, or the up vector, and others don't consider the object position...

Could anyone write for me a CreateBillboard function or explain to me what i did wrong in the attached code..
When i finally thought that i'm getting into OGL programming this topic came and ruined the mood.
I lost hope in this topic. just need it to work.

thank you all in advanced.

I cut most of the code for simplicity.
its written in c# if it will be easier to solve i will post the full files (which is not allot also)


void CreateBillboardMatrix(float[] matrix, Vector3D right, Vector3D up, Vector3D look, Point3D pos)
{
matrix[0] = (float)right.X;
matrix[1] = (float)right.Y;
matrix[2] = (float)right.Z;
matrix[3] = 0;
matrix[4] = (float)up.X;
matrix[5] = (float)up.Y;
matrix[6] = (float)up.Z;
matrix[7] = 0;
matrix[8] = (float)look.X;
matrix[9] = (float)look.Y;
matrix[10] = (float)look.Z;
matrix[11] = 0;
// Add the translation in as well.
matrix[12] = (float)pos.X;
matrix[13] = (float)pos.Y;
matrix[14] = (float)pos.Z;
matrix[15] = 1;
}

void BillboardPoint(Point3D pos, Point3D camPos, Vector3D camUp)
{ // create the look vector: pos -> camPos
Vector3D look = camPos - pos;
look.Normalize();

// right hand rule cross products
Vector3D right = Vector3D.CrossProduct(camUp, look);
Vector3D up = Vector3D.CrossProduct(look, right);

float[] matrix = new float[16];
CreateBillboardMatrix(matrix, right, up, look, pos);

// apply the billboard
GL.glMultMatrixf(matrix);
}


.
.
.

GLU.gluLookAt(m_posCamObs.X, m_posCamObs.Y, m_posCamObs.Z,
m_posCamTarget.X, m_posCamTarget.Y, m_posCamTarget.Z,
m_vecCamUp.X, m_vecCamUp.Y, m_vecCamUp.Z);

BillboardPoint(m_posCamTarget, m_posCamObs, m_vecCamUp);
GL.glEnable(GL.GL_TEXTURE_2D);
GL.glBindTexture(GL.GL_TEXTURE_2D, g_texID);
GL.glBegin(GL.GL_POLYGON);
{
// Face to Y
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex3d(m_posObject.X - dblHSize, m_posObject.Y + dblHSize, m_posObject.Z);

GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex3d(m_posObject.X + dblHSize, m_posObject.Y + dblHSize, m_posObject.Z);

GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex3d(m_posObject.X + dblHSize, m_posObject.Y - dblHSize, m_posObject.Z);

GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex3d(m_posObject.X - dblHSize, m_posObject.Y - dblHSize, m_posObject.Z);

GL.glEnd();
}
GL.glDisable(GL.GL_TEXTURE_2D);

david_f_knight
11-28-2010, 01:21 PM
It's really simple to do what you want if you use a geometry shader. All you need to do in a geometry shader is define your billboard relative to one reference point on it, such as a vertex or center. So, you transform that one reference point to eye coordinates in the vertex shader (just as you would any other point), define your geometry relative to it (by using the same Z coordinate for all vertices of the geometry defined in the geometry shader will cause it to face the viewpoint no matter what).

Is there any good reason you're using an obsolete version of OpenGL?

raziza
11-29-2010, 01:19 AM
This is what i know.. and apparently - not very good :)
I'm still in the learning phase.

I heard about the shader but didn't quiet learned about it.
please correct me if i'm wrong. When u say Shader u mean GLSL ?
It a Additional Dll\s I need to download and use?
Is there any good reference u know to direct me ?

Thanks allot

raziza
11-29-2010, 02:16 AM
I read about GLSL and i guess this is what u meant.

but the problem is that I've already developed a big application in c# using GDI+. and now i'm adding OpenGL drawing code for supporting graphic card mode.
I have a full file of external methods - a wrapper for the GL dlls.

Is it possible to use shading in .Net like i did with the rest?
If not, can you tell me what did i do wrong in the billboard method ? because this is the only thing that i troubled in. I managed to solve all the rest of the problems

Thanks again.

david_f_knight
11-29-2010, 10:52 AM
Yes, shaders are what are written with the set of GLSL languages. GLSL is still part of OpenGL, though, and has the same compatibilities with non-OpenGL code as does obsolete OpenGL. In other words, c#, GDI+, .net etc. have no interaction with OpenGL using GLSL any more than they do with obsolete OpenGL. All OpenGL programs, whether they use GLSL shaders or not, still use the same OpenGL .dll . If you have a full file of external methods (wrappers for the GL functions) as you wrote then you're all set to use GLSL.

However, it's still your decision whether to use modern or obsolete OpenGL. I just wanted to point out that what you want to do is extremely easy (and efficient) using a geometry shader, and not so easy (and inefficient) using obsolete OpenGL. Personally, I don't like to invest my time learning and using obsolete things, especially when the current things are so much better. The reason I wanted to point this out is because nearly all the OpenGL tutorials on the internet and OpenGL books were written years ago and it seems nearly all the computer science professors are still teaching obsolete OpenGL without ever even mentioning that they're teaching only obsolete technology. That all makes it hard to even know that there is a better way of doing things. If you want a long career in the computer world, you had better avoid obsolete technology like the plague. But it's your decision. Once you start down the wrong path, it's just that much harder to switch and go back to start going down the better path.

raziza
12-01-2010, 04:36 AM
Lets make things in order..
I have the red book and started learn from there.
So this is what i know. I am not stubborn about it - or using this way.


1. If u say that there is easier language to develop with,
and i guess u r right with your experience,
can u please direct me to good web sites, books or anything.

2. When u say obsolete OpenGL - that this is what i familiar with (apparently), what is NOT obsolete - modern OpenGL?

3. about the shader. I got that it's just a programming language that we upload to the graphics card which runs over all the vertex and fragments. Is it?

be patient with me. I want to learn :)
thanks man.

david_f_knight
12-01-2010, 12:59 PM
I think OpenGL's biggest problem may be how it's documented, and how it gets documented. It also has a big problem that not all implementations are at the same version (or even close to the same version). So, unfortunately, if you are developing on some (bad) hardware or some (bad) operating systems, you are forced to use some old version of OpenGL. It's ironic... one of OpenGL's strengths is that it is available on so many platforms, and one of its weaknesses is because it is available on so many platforms (and not all vendors care about OpenGL equally).

So, I made an assumption when I responded to your original question, and that was that you could use a version of OpenGL that supports geometry shaders. GLSL was introduced years ago in an older version of OpenGL, but alongside it, what I've called obsolete OpenGL has continued to coexist. Even so, geometry shaders were not part of the first GLSL version. I don't know the history of OpenGL well enough to give the particulars about when what features became available.

To answer your questions:

1) I know of only one introductory OpenGL book that is modern enough to cover geometry shaders. That is the fifth edition of OpenGL SuperBible. It was published August 2, 2010, and covers OpenGL version 3.3 (OpenGL is now up to version 4.1):
http://www.amazon.com/OpenGL-SuperBible-...9246&sr=8-1 (http://www.amazon.com/OpenGL-SuperBible-Comprehensive-Tutorial-Reference/dp/0321712617/ref=sr_1_1?ie=UTF8&qid=1291229246&sr=8-1)
I haven't read it, so I am not endorsing it, but if I was just starting out now I would very seriously consider buying it.

I don't know of any website that has geometry shader tutorials. There are a few tutorials on the net that do cover modern OpenGL, though:
http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Table-of-Contents.html
http://www.arcsynthesis.org/gltut/
http://ogldev.atspace.org/
All of these are works in progress, but they do focus on modern OpenGL.

2) There are probably a couple defining characteristics of modern OpenGL versus "obsolete" OpenGL. Modern OpenGL uses the programmable graphics pipeline (via the GLSL language) rather than the fixed function graphics pipeline. And modern OpenGL uses Vertex Buffer Objects for storing model geometry rather than "immediate mode" (i.e., glBegin() and glEnd()) or display lists.

3) Shaders are little programs written in a programming language (GLSL) which is passed to the OpenGL graphics driver via OpenGL function calls. The graphics driver compiles and links your shader programs (on the CPU) and then passes that linked data to the graphics card. Each of the different shaders lives in a different section of the graphics pipeline and sees the world differently. The vertex shader is the first, and it runs over all the vertices. It's typically used to transform vertices to eye coordinate space (if there's a tessellation and/or geometry shader) or to clip coordinate space (if there is no tessellation or geometry shader). Next (in OpenGL 4) come the tessellation control and tessellation evaluation shaders. These subdivide patches to triangles (or points or lines) and if there is no geometry shader they typically also transform vertices to clip coordinate space. Next is the geometry shader. It processes entire primitives (points, lines, or triangles) in and out. If present it also typically transforms vertices to clip coordinate space. Finally, the fragment shader is executed (after triangles (or points or lines) are rasterized) and applies color and textures and so on.

ugluk
12-01-2010, 01:40 PM
I say learn to batch first, then do other things, batching is very important. Also Photon's VBO caching.

raziza
12-22-2010, 02:02 AM
Thanks guys..
I will keep on learning..

Will post more questions in the future i guess.