PDA

View Full Version : Box texture mapping



enigmagame
04-10-2008, 06:46 AM
Hi!
Anyone know a "box" texture mapping representation?
If I have a model, how can I implement this method?
For example:
1) I make six images of the model (front, back, left, right, top, bottom). This image must made in orthographic mode?
2) With this 6 images, how can I implement the box texture mapping?
Thanks!
___________________________________________

Ingrater
04-10-2008, 08:02 AM
You could simply compute the texture coordinates out of the vertexpositions and normals and blend the different textur layers into each other depending on the normal. The blending for example is described in nvidias cascades demo secrets. The texturemapping itself is simple math. I'm doing it with a vertexshader if you want I can post it.

enigmagame
04-10-2008, 08:28 AM
One moment ;)
First, I must make six images (renering) of my model (front, back, top, bottom, left, right). Those images must be taken in orthographic mode, it's correct?
Thanks!

Ingrater
04-10-2008, 09:08 AM
Well if you simply want to map your object from 6 sides with some textures, and your not a artist, there is no need for taking images. It is enough if you compute 3 different sets of Texture coordinates (for each axis 1) and pass them down to the shader or fixed function pipeline.

enigmagame
04-10-2008, 09:22 AM
I'm confused.
I try to explain what I must do.
I must implement a "box" texturing scheme.
For example in my application I have a model, and I make six rendering of this model (front, back, top, bottom, left, right) with different colors or with different light...
I report a paper description:
We have implemented two schemes for representing the surface texture data. The
first is a “box” scheme that represents the surface textures using 6 orthographic (nonperspective)
“canonical” views of the model (top view, front view etc).

Then I would texture my model with this images, the paper say:
For each triangle
we texture map using the “best” view for that triangle. This is the canonical view with
the highest texture resolution out of all views in which the triangle is completely visible
(no occlusion). The benefits of this approach are that it is very simple and the 2D texture
coordinates can be calculated implicitly from the 3D vertex data.
I hope to being more clear!
Thanks!

Ingrater
04-10-2008, 10:24 AM
Thats exactly what I'm talking about. They compute the 2d texture coordinates out of the 3D vertex data. Depending on the normal they are choosing from wich of the 6 sides to map. That doesn't mean you have to render anything, its just the way it's explained so that you can understand it better.

If the normal is facying mostly into the x+ direction (x part of the normal is the largest) they map it along the yz-plane for example

vec2 texcoords = Vertex.yz
If it fits mostly on the y+ direction you would use

vec2 texcoords = Vertex.xz
and so on. You don't need to render anything, you can compute everyhting with maths.

enigmagame
04-10-2008, 11:38 AM
That doesn't mean you have to render anything

But I don't have any textures!
The paper assume that we have a 3D model and a set of images of this 3D model. This images is used for texturing.
But now, I'm simulating that! I have a 3D model, for example the classic Utah Teapot, I must make some virtual "photo" of this model with, for example, differnet color or light.

-NiCo-
04-10-2008, 11:53 AM
Considering the amount of info you gave here I believe Ingrater was doing a fine job on helping you. So maybe you want to be a bit more specific and provide more details about what you're trying to do.

Correct me if I'm wrong, but from what I read you have an untextured teapot, you want to take six snapshots of this model, store those in textures, and after that you want to texture the model with these textures again!?

enigmagame
04-10-2008, 12:19 PM
Correct me if I'm wrong, but from what I read you have an untextured teapot, you want to take six snapshots of this model, store those in textures, and after that you want to texture the model with these textures again!?

Exactly! Six snapshots with differences (color, light, ...).
Then, I want to texture the model with these textures.
Seems senseless!
With this, I obtain a textured model with some problems, for example seams...
The goal is process the textures to avoid those problems.
Sorry, I hope to be more clear :o!

enigmagame
04-11-2008, 12:53 AM
The paper in question is this:
http://www.bmva.ac.uk/bmvc/2002/papers/49/full_49.pdf
And what I described is at "texture representation" section (1.3).
Thanks!

-NiCo-
04-11-2008, 02:19 AM
Well, that doesn't look too complicated but you need to be familiar with cube maps, shaders and shadow mapping.

First of all, you need to create the images and depth maps for the six views and load them into cube maps.

To simplify things, I'm assuming your model is located in a cube within the ranges [-1,1]x[-1,1]x[-1,1] and the normals have unit length.

In the vertex shader, copy the vertex positions and normals to a varying.



varying vec4 myTexCoord;
varying vec3 myNormal;

void main()
{
myTexCoord = gl_Vertex;
myNormal = gl_Normal;
gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex
}


Next, in the fragment shader



varying vec4 myTexCoord;
varying vec3 myNormal;
samplerCube colorcube;

void main
{
vec4 result;
result = myNormal.x*myNormal.x*textureCube(colorcube,vec3(s ign(myNormal.x),myTexCoord.y,myTexCoord.z);
result+= myNormal.y*myNormal.y*textureCube(colorcube,vec3(m yTexCoord.x,sign(myNormal.y),myTexCoord.z);
result+= myNormal.z*myNormal.z*textureCube(colorcube,vec3(m yTexCoord.x,myTexCoord.y,sign(myNormal.z));
gl_FragColor = result;
}


Note that I ignored the shadow mapping, that's left as an exercise ;) The weights are distributed so that the sum of the three weights equals 1. If a fragment is occluded in one of the three accessed cube sides, you probably want to distribute the weights over the remaining non-occluded sides.

enigmagame
04-11-2008, 06:38 AM
First of all, you need to create the images and depth maps for the six views and load them into cube maps.

For the images I must use the orthographic projection?


To simplify things, I'm assuming your model is located in a cube within the ranges [-1,1]x[-1,1]x[-1,1] and the normals have unit length.

Yes, for example I can try the first experiment with a simple cube model.


Note that I ignored the shadow mapping

Why shadow mapping?
I'm learning the shaders, but I don't understand very well the code.
With my six images, what I must do?
Thanks you very much!!

-NiCo-
04-11-2008, 07:01 AM
For the images I must use the orthographic projection?


yes



Why shadow mapping?


Because, for example, when you are processing a fragment of which the normal is facing in the positive x-direction, it is possible that there is another part of the object between the 3D position of that fragment and the cube side you are performing the lookup in. In this case, the fragment is occluded and the texture value from the positive x-side should not be applied



With my six images, what I must do?


load them into a cube map

enigmagame
04-14-2008, 11:36 PM
Thanks!
In another discussion, I was advised this method:
For each triangle of my model determine which of the six box planes this triangle is facing. Then project all points from the face onto my box plane. Where the points plane onto the orthogonal plane gives me the UV coordinate for that triangle.
It's correct this method?
I've tried to implement that, but I've problems with the texture generation.

-NiCo-
04-15-2008, 03:49 AM
Thanks!
In another discussion, I was advised this method:
For each triangle of my model determine which of the six box planes this triangle is facing. Then project all points from the face onto my box plane. Where the points plane onto the orthogonal plane gives me the UV coordinate for that triangle.
It's correct this method?
I've tried to implement that, but I've problems with the texture generation.

Yes, it's basically the same. The problem with that approach is that if the triangle is not visible in the box plane its facing it will (or should not, anyway) get textured. So it's best to use some weighted sum of the textures on the 3 box planes its facing , distributed according to their visibility of the triangle/fragment.

enigmagame
04-15-2008, 05:05 AM
I've tried a simple example with the method say in my last post.
The model is a cube of unitary length, and is positioned at the center of the scene (0,0,0).
Using orthographic projection with this parameters: (the windows dimension is 512x512)


glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

I obtain this screen:
http://img525.imageshack.us/img525/4479/38966250is0.th.png (http://img525.imageshack.us/my.php?image=38966250is0.png)
Then I've projected each vertex and I've used the new vertex as texture coordinate. For example:


float v[2];

for(i = 0; i < numberOfTriangles; i++)
{
glBegin(GL_TRIANGLES);
for(j = 0; j < 3; j++)
{
v = projectVertices(mesh.getVertex(i, j));
glTexCoord2f(v[0], v[1]);
glVertex3fv(mesh.getVertex(i, j));
}
glEnd();
}

I obtain this:
http://img215.imageshack.us/img215/7659/22834950dv4.th.png (http://img215.imageshack.us/my.php?image=22834950dv4.png)
The square color is wrong (not red). I have added to the first image, eight green points.
I have added to the first image, eight green points. Four at the square corners, and four at the images corners.
The mapping is wrong...
Where is the problem? In texture generation or in texture mapping?
This simple example is only for one face of the cube!
Thanks!

enigmagame
04-15-2008, 07:39 AM
I've solved the past problem in this manner: I project each vertex and then if this new vertex is less then zero I set them to zero and if the vertex is greater then one I set them to one.
Probably is wrong!
I've tried with a new model, a cylinder of lenght, width and depth two.
My windows is 512x512.
This is the correct rendering of the model, no texture, only light.
http://img151.imageshack.us/img151/4823/29985673mf4.th.png (http://img151.imageshack.us/my.php?image=29985673mf4.png)
This is the texture created using the orthographic projection set to (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0).
http://img175.imageshack.us/img175/6602/26932309gl3.th.png (http://img175.imageshack.us/my.php?image=26932309gl3.png)
It's correct?
And this is a model textured, using the precedent image.
http://img135.imageshack.us/img135/403/65143147wy4.th.png (http://img135.imageshack.us/my.php?image=65143147wy4.png)
It's wrong... :(:(
Where is the problem? Or... where are the problems?
Thanks!

-NiCo-
04-15-2008, 10:31 AM
Okay, here's an easy one:

Set your projection to

glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

Then draw a cube of unitary length, positioned at the center of the scene (0,0,0).

You've already done that to generate the orthographic texture.

Now... your view frustum ranges from -1 to 1 in all three directions because of your ortho call, but texture coordinates range from 0 to 1. So the texture coordinates for a vertex (Vx,Vy,Vz) becomes (0.5*Vx+0.5,0.5*Vy+0.5) if you are projecting parallel to the Z-axis.

enigmagame
04-16-2008, 01:14 AM
Ok, now I've a cube of unitary length, positioned at the centre of the scene.
The projection is set to glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)
This is the texture:
http://img168.imageshack.us/img168/6264/texturetn0.th.png (http://img168.imageshack.us/my.php?image=texturetn0.png)
The cube textured is the same image of the precedent.
The correct rendering, whitout texture is this:
http://img138.imageshack.us/img138/332/correctee7.th.png (http://img138.imageshack.us/my.php?image=correctee7.png)
So, in the textured cube are included the black border of the texture.
Is a texture generation problem or a texture mapping problem?
Thanks!

-NiCo-
04-16-2008, 04:11 AM
What do you mean by "the correct rendering"? If I were to draw a cube with unitary length in an orthogonal view frustum with a side length of 2 I would expect it to cover only 1/4th of the screen...

enigmagame
04-16-2008, 04:21 AM
What do you mean by "the correct rendering"? If I were to draw a cube with unitary length in an orthogonal view frustum with a side length of 2 I would expect it to cover only 1/4th of the screen...
In the "correct rendering" I setup the projection view, and I render the cube.
I use the orthogonal view only for the texture generation.
Then I would apply the texture on my model, and setup the projection view.