I'm wondering how to generate tex coords easily if all I have is a list of vertices. I've found some stuff on the web (ex: http://paulyg.f2s.com/uv.htm) but I can't seem to get it to work with my simple cube example.
Basically I have an array of floats, 3 per verticy * 36 to make up a cube. Applying the math on the above page leads to two faces being correct, but at least two being incorrect (end up being stripes).
You can unwrap the vertices with Blender and export to Wavefront OBJ. Or you just want a list of vertices for a cube here?
I have the vertices, I would like to calculate the texture coordinates.
The texture coordinates are vertices, too. (albeit they're in 2D) 
You want a mini example program for a cube to get the numbers or you want the general case?
I don't see what's wrong with that link you posted, maybe you should try putting different colors at each corner to check your assumptions about which corner is which.
[EDIT]
It just occurs to me that the 0.0 - 1.0 thing in the link you posted works for a separate image on each face, but the video would show stuff like 0.6 etc. to map one image across all faces.
I have no assumptions.
That code doesn't make assumptions either.
Here is the code I'm using:
The second last two items are the u/v cords. ignore tx_* variables. tx_fact is 1.0, tx_x, and tx_y are both 0.0. They will be used, but im still trying to get the basics to work...
Assuming I've got the math right, it results in this:
{"name":"608942","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/d\/1d266d38100cf683540896fa2e1de3d6.png","w":1030,"h":795,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/d\/1d266d38100cf683540896fa2e1de3d6"}
Not quite correct 
It just occurs to me that the 0.0 - 1.0 thing in the link you posted works for a separate image on each face, but the video would show stuff like 0.6 etc. to map one image across all faces.
The link is actually trying to map a single texture onto a mesh... So maybe it isn't the right thing to use here, but I was hoping it'd work. I'm currently just trying to use a single texture, with it being identical on all sides (not stretching over the cube, or doing a kind of wrapping of the image around the cube, later i might use cube maps or something, but not right now).
I made a little program to draw one image on each face of a cube, which wobbles around so you can see all sides. Each face is tinted differently.
I compiled it with
gcc -s -O2 -Wall t.c -o t -lallegro -lallegro_image -lGLU -lm
{"name":"608943","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/7\/17c89e4e445aa05757fa10ebd170bd77.png","w":802,"h":626,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/7\/17c89e4e445aa05757fa10ebd170bd77"}
[EDIT]
Hey, now I know where I'd seen your texture before! You're trying to make a Minecraft clone, aren't you?
Hey, now I know where I'd seen your texture before! You're trying to make a Minecraft clone, aren't you?
I don't know if it'll get that far. So far its a little C++ Allegro5 MC Map Viewer. I had it actually loading and drawing a chunk, but textureless, which is rather anticlimactic.
Hm, your code doesn't actually calculate the uv coords though
If you used the same vertex values I could temporarily thief your uv coords, but I'd like a way to generate the uv coords... Though for later things I may be using 3d models.
Hm, your code doesn't actually calculate the uv coords though
If you look again, all the glTextureCoord2f() calls repeat exactly the same for each face.
glTexCoord2f(0.0,1.0); //upper left glTexCoord2f(0.0,0.0); //lower left glTexCoord2f(1.0,1.0); //upper right glTexCoord2f(1.0,1.0); //upper right again glTexCoord2f(0.0,0.0); //lower left glTexCoord2f(1.0,0.0); //lower right
And if you used triangle strip you could eliminate the second "upper right" call.
If you look again, all the glTextureCoord2f() calls repeat exactly the same for each face.
You're hardcoding them, not calculating them 
And if you used triangle strip you could eliminate the second "upper right" call.
Sharing vertices can break some things. Especially when you get into texture atlasing. You will need completely separate uv coords per vertex especially if you want to use more than one texture per block (grass has three, dirt, grass top, and dirt+grass sides).
You're hardcoding them, not calculating them
A minor detail. Since they repeat endlessly, you could have had a loop that iterated over an array, with the array values grabbed from the example.
Sharing vertices can break some things. Especially when you get into texture atlasing. You will need completely separate uv coords per vertex especially if you want to use more than one texture per block (grass has three, dirt, grass top, and dirt+grass sides).
So you'd need to start/stop each face, no problem. In other words, I'd have used glBegin(GL_TRIANGLE_STRIP) for each face with a glEnd() following each face instead of GL_TRIANGLES
A minor detail. Since they repeat endlessly, you could have had a loop that iterated over an array, with the array values grabbed from the example.
You seem to be misunderstanding. By calculate or generate I mean using actual math and code, and not hardcoding them.
So you'd need to start/stop each face, no problem. In other words, I'd have used glBegin(GL_TRIANGLE_STRIP) for each face with a glEnd() following each face instead of GL_TRIANGLES
I suppose I could. But its just as easy to use a triangle list.
Also I'm not using the gl immediate api, but OpenGL 3, so the old apis aren't available. I have to use Shaders and Vertex Buffer Objects. (but don't worry, I have all the code for it already)
append: For now I've just stolen data from an exported blender .obj after figuring out how to actually get it to export the uv coordinates properly.
I restructured things a little bit as well.
So yeah, it works now. But I'd still like to see if theres a way to generate the uv coords from the vtx coords.
append:
So I finally have something that resembles something.
{"name":"EKwPne1.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/d\/0d9a54103ddc56f86db2906233403003.png","w":1030,"h":795,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/d\/0d9a54103ddc56f86db2906233403003"}
Now with hacked in leaf, grass and log textures:
{"name":"Ze4gsEU.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/9\/b99dde961377b18fa6df5f746670de35.png","w":1030,"h":795,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/9\/b99dde961377b18fa6df5f746670de35"}
I'd still like to see if theres a way to generate the uv coords from the vtx coords.
I don't ever remember seeing such a calculation except for some gluCylinder etc. which is just a convenience, you could find the code in the Mesa source.
Googling a bit found this for Direct X and this for Open GL. Maybe the Mesa code used the latter, I don't remember.
That said, why would mapping a few square faces need such a thing?
To use that, he'd have to define 6 planes to project his cube to each plane, to get the UV coordinates for each side. And the result would be 0/0, 0/1, 1/0, 1/1 every time :p
That said, why would mapping a few square faces need such a thing?
Learning exercises. Also I may have other shapes. but I'll probably just use models for the non cubic objects.
append:
{"name":"OtR0m0b.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/3\/a36e3fa9ad430ec858fe52ea0223922d.png","w":1030,"h":795,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/3\/a36e3fa9ad430ec858fe52ea0223922d"}
Latest version \o/
Looks promising Thomas.
How did you get the world to curve?
That's probably an artifact of the FOV, and the shape of the terrain. Mostly the FOV though. It's set to 90 right now
I meant to reply to this a while ago, but I forgot.
I've attached an excerpt from a primitive generator I made awhile ago. It should contain what you're looking for and then some.
It does vertices, texture, normals, tangents and bitangents for planes, cylinders, cubes, spheres, and cones, with configurable stacks and slices for each.
OOh, thanks man. I'll take a look at that while I'm in the philippines (or thailand). Assuming work isn't too hectic of course.