How do you map a texture on the side of cube?

Started by
5 comments, last by FirstStep 8 years, 2 months ago

Been trying to map my texture on a cube. The first front bue is easy to visualize. But the sides and back and the bottom and top becomes harder. say for example this

cube1.jpg

I draw the only half of the square for better visualization. From this tutorial the texture coordinate is something like this

tex_coords.jpg

The case is If I am going to move my camera so that the left side becomes the front it will look like this

vis1.jpg

This is the vertex coord for this image



-0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,  0.0f,  1.0f,
-0.5f, -0.5f,  0.5f, -1.0f,  0.0f,  0.0f,  0.0f,  0.0f,
-0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,  1.0f,  0.0f,

If i will base the mapping of the coord to this image

gdvis1.jpg

if you look closely, the


-0.5f, -0.5f, -0.5f,

should be map at 0.0f, 0.0f, not on 0.0f, 1.0f. Note that the image is the left side of the cube. I just move the camera to and make the scene the left side to appear on front,to better visualize the mapping.

-0.5f, -0.5f, 0.5f, should be at 1.0f, 0.0f and -0.5f, 0.5f, 0.5f, should be at 1.0f, 1.0f.

If i will follow the coorect texture coords this is what I will get

wrong.jpg


-0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,  1.0f,  1.0f,
-0.5f,  0.5f, -0.5f, -1.0f,  0.0f,  0.0f,  1.0f,  0.0f,
-0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,  0.0f,  0.0f,


-0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,  0.0f,  0.0f,
-0.5f, -0.5f,  0.5f, -1.0f,  0.0f,  0.0f,  1.0f,  0.0f,
-0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,  1.0f,  1.0f,

The could should be right. If I will base the tutorial to that.

How do you manage to correctly map a 2D texture on a cube specially when it comes to side, bottom and top. Do you visualize each side as 2D and imagine positioning the camera to make the side on front and then map the 2D? just like I did here? or something else?

Thanks

Advertisement

How do you manage to correctly map a 2D texture on a cube specially when it comes to side, bottom and top?

I don't. I use a tool that figures out those details for me. I don't hard-code data.

If I need a textured cube, I can open my favorite 3D editor, drop in a cube, drop in a texture, and let the program do the mappings for me.

Then I load the model as data.

Later when I want the cube replaced with something more artistic, I can pass it to someone who knows how to make high quality models, they use the tools, and the model is automatically replaced in my game. Or if it is a change I can do, I'll make those changes to the data myself, then save it off.

Treat data as data and your programming adventures will go much smoother.


I don't. I use a tool that figures out those details for me. I don't hard-code data

Hehe, I remember modelling my first 3D models on graph paper with pencil, because I felt it was faster for me then to figure out how to load a 3D model and learn an editor. (not to mention just finding out which I should get, and which format was the most suitable...)

Just draw it up from 1 to 3 sides(depending on complexity), number all vertices, and write down coords and indices, for simple shapes and spaceships, it really doesn't take that long.

At the time, I was more interested in making my software rasterizer perform correctly and well, and didn't want to bother with reading up on file formats.

There was enough new things to juggle in my head already.

Plus, I felt it was a good exercise for really understanding vertices, texture coordinates and indices.

In general, as part of your real content pipeline, I agree it is not a good idea though, and you should listen to frob smile.png

But that is what I would do if for some reason a 3d editor was not available, or I for whatever reason didn't want to use one (learning? masochism?)

I would draw it up on graph paper, then figure it our from there.. so yea, visualizing it in 2D.

How do you manage to correctly map a 2D texture on a cube specially when it comes to side, bottom and top. Do you visualize each side as 2D and imagine positioning the camera to make the side on front and then map the 2D? just like I did here? or something else?

You don't need to imagine the camera at all. A triangle is always flat. When texturing the triangle, forget about 3D entirely, forget about the camera. The triangle isn't in the world, the triangle is the only thing that exists.

When positioning a triangle, you need to think of the world.


Treat data as data and your programming adventures will go much smoother.

I shouldnt have to bother with this 2D texture mapping? I mean in my early opengl thing should I use 3D software to model a cube?

I also dont know what you mean by data. Is it like for example passing position to shaders and stuff like that? because I think every part of opengl is a data being pass on to shaders. unsure.png


But that is what I would do if for some reason a 3d editor was not available, or I for whatever reason didn't want to use one (learning? masochism?)

Uhm you cound say that. Thing is, when I get really curious as to how things happen, I really wanna know how. I know its a bad thing maybe. Also a reason for that cube is because I want it to scale it big enough to make it look like a room. Then add some lights like ambien diffuse and specular as an exercise for me. Im thinking that you have gone the same path like me? happy.png


You don't need to imagine the camera at all. A triangle is always flat. When texturing the triangle, forget about 3D entirely, forget about the camera. The triangle isn't in the world, the triangle is the only thing that exists.

So uhm how is it that if like you said the triangles is always flat, how can you map it? in the vertices above, I dont know how the wrong texture coords map correctly on the 2D triangle side. If you see the graphing, its different. The author of the tutorial never mentioned anything about how he get the texture coords map to the coordinates. Is it posible that its from some 3D modelling app and exported as data file? If that is the case then Im definitely wasting my time and energy on learning something that would not even help me in the future. But i hope not.

If that is the case then Im definitely wasting my time and energy on learning something that would not even help me in the future. But i hope not.

You're not wasting your time.

So uhm how is it that if like you said the triangles is always flat, how can you map it?


Take any two points anywhere in 3D space. Any two points. You can always draw a strait line.
Take any three points anywhere in 3D space. Any three points. You can always form a flat surface between them.

In OpenGL, you take a flat 2D texture (or a 3D texture), you pretend the three corners of the triangle are a flat surface when choosing your texture coords, and then the triangle is moved, rotated, stretched, using all the other information (camera, triangle positions, etc...). But the texture mapping can ignore all that because it happens at a later step.

But you only really need to figure this stuff out once or twice in your code, and once you write it generically and get it working, your code works for whatever data you throw at it, unless the data is wrong.

in the vertices above, I dont know how the wrong texture coords map correctly on the 2D triangle side. If you see the graphing, its different.



-0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,  0.0f,  1.0f,

Do you understand that only the last two numbers are your texture coordinates? (see the "Applying textures" part of the tutorial)
The first three is the position in space, the next three are for coloration... the last two are the texture coordinates.

The tutorial doesn't code it too clearly, but in actual code people usually write it similarly to this:


struct Position
{
     float x, y, z;
};
 
struct Color
{
     float r, g, b;
};
 
struct TexCoord
{
     float u, v;
};
 
struct Vertex
{
     Position position;
     Color coloration;
     TexCoord texCoord;
};

...which is much easier to understand than 8 floats in a row.

Okay I think I get it. If you could take of the parts of each plane of the cube and lay it down. The image by default is pointing down. So the reason I am so confuse is that I was thinking the image as pointing up. meaning the normal default look of image without rotating it. Now If I will put each plane to form a cube it will look like this

looking from outside

image.jpg

looking inside of cube

image.jpg

Had to use different image from LoL to see whats going on. As to why the image of left and right is pointing of the side rather than down is a bit blurry to me

This topic is closed to new replies.

Advertisement