Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: ninogenio on October 25, 2006

Title: uv coords
Post by: ninogenio on October 25, 2006
i was just wondering if anyone might know off the top of there head what the uv coords of a cube defined like this might be ive been messing about for an hour and i cant get them quite right.

Code: [Select]
         'push face data
         tmp_data->face[1]->a = 1 : tmp_data->face[1]->b = 3 : tmp_data->face[1]->c = 7
         tmp_data->face[2]->a = 1 : tmp_data->face[2]->b = 7 : tmp_data->face[2]->c = 5
         tmp_data->face[3]->a = 1 : tmp_data->face[3]->b = 2 : tmp_data->face[3]->c = 4
         tmp_data->face[4]->a = 1 : tmp_data->face[4]->b = 4 : tmp_data->face[4]->c = 3
         tmp_data->face[5]->a = 5 : tmp_data->face[5]->b = 7 : tmp_data->face[5]->c = 8
         tmp_data->face[6]->a = 5 : tmp_data->face[6]->b = 8 : tmp_data->face[6]->c = 6
         tmp_data->face[7]->a = 2 : tmp_data->face[7]->b = 1 : tmp_data->face[7]->c = 5
         tmp_data->face[8]->a = 2 : tmp_data->face[8]->b = 5 : tmp_data->face[8]->c = 6
         tmp_data->face[9]->a = 3 : tmp_data->face[9]->b = 4 : tmp_data->face[9]->c = 8
         tmp_data->face[10]->a = 3 : tmp_data->face[10]->b = 8 : tmp_data->face[10]->c = 7
         tmp_data->face[11]->a = 6 : tmp_data->face[11]->b = 2 : tmp_data->face[11]->c = 4
         tmp_data->face[12]->a = 6 : tmp_data->face[12]->b = 4 : tmp_data->face[12]->c = 8
         
         'push vertex data
         tmp_data->vertex[1]->x = 1.0 : tmp_data->vertex[1]->y = 1.0 : tmp_data->vertex[1]->z = 1.0
         tmp_data->vertex[2]->x = 1.0 : tmp_data->vertex[2]->y = 1.0 : tmp_data->vertex[2]->z = -1.0
         tmp_data->vertex[3]->x = 1.0 : tmp_data->vertex[3]->y = -1.0 : tmp_data->vertex[3]->z = 1.0
         tmp_data->vertex[4]->x = 1.0 : tmp_data->vertex[4]->y = -1.0 : tmp_data->vertex[4]->z = -1.0
         tmp_data->vertex[5]->x = -1.0 : tmp_data->vertex[5]->y = 1.0 : tmp_data->vertex[5]->z = 1.0
         tmp_data->vertex[6]->x = -1.0 : tmp_data->vertex[6]->y = 1.0 : tmp_data->vertex[6]->z = -1.0
         tmp_data->vertex[7]->x = -1.0 : tmp_data->vertex[7]->y = -1.0 : tmp_data->vertex[7]->z = 1.0
         tmp_data->vertex[8]->x = -1.0 : tmp_data->vertex[8]->y = -1.0 : tmp_data->vertex[8]->z = -1.0
         
Title: Re: uv coords
Post by: Jim on October 26, 2006
Assuming you have one uv per vertex you can't do it, and still have a different square texture on each face. If you want to put a different texture on each face then you need more vertices, 4 per face.  6 faces, 24 vertices.  Some of the vertices will be in the same place but have different uvs.  How you represent that in your data structure is up to you :)

Jim
Title: Re: uv coords
Post by: ninogenio on October 26, 2006
hmm i thought it would have been possible to have eight uv`s one per vertex im going to use one texture on all the faces heres what i was trying i got close but then went off track

Code: [Select]
function new_box_entity() as new_entity ptr
   
         dim tmp_data as new_entity ptr
         
         tmp_data = callocate(len(new_entity))
         tmp_data->no_vertices = 8
         tmp_data->no_faces = 12
         tmp_data->vertex = callocate( tmp_data->no_vertices * sizeof(new_entity) )
         tmp_data->face = callocate( tmp_data->no_faces * len(new_entity) )
         
         'push face data
         tmp_data->face[1]->a = 1 : tmp_data->face[1]->b = 3 : tmp_data->face[1]->c = 7
         tmp_data->face[2]->a = 1 : tmp_data->face[2]->b = 7 : tmp_data->face[2]->c = 5
         tmp_data->face[3]->a = 1 : tmp_data->face[3]->b = 2 : tmp_data->face[3]->c = 4
         tmp_data->face[4]->a = 1 : tmp_data->face[4]->b = 4 : tmp_data->face[4]->c = 3
         tmp_data->face[5]->a = 5 : tmp_data->face[5]->b = 7 : tmp_data->face[5]->c = 8
         tmp_data->face[6]->a = 5 : tmp_data->face[6]->b = 8 : tmp_data->face[6]->c = 6
         tmp_data->face[7]->a = 2 : tmp_data->face[7]->b = 1 : tmp_data->face[7]->c = 5
         tmp_data->face[8]->a = 2 : tmp_data->face[8]->b = 5 : tmp_data->face[8]->c = 6
         tmp_data->face[9]->a = 3 : tmp_data->face[9]->b = 4 : tmp_data->face[9]->c = 8
         tmp_data->face[10]->a = 3 : tmp_data->face[10]->b = 8 : tmp_data->face[10]->c = 7
         tmp_data->face[11]->a = 6 : tmp_data->face[11]->b = 2 : tmp_data->face[11]->c = 4
         tmp_data->face[12]->a = 6 : tmp_data->face[12]->b = 4 : tmp_data->face[12]->c = 8
         
         'push vertex data
         tmp_data->vertex[1]->x = 1.0 : tmp_data->vertex[1]->y = 1.0 : tmp_data->vertex[1]->z = 1.0
         tmp_data->vertex[2]->x = 1.0 : tmp_data->vertex[2]->y = 1.0 : tmp_data->vertex[2]->z = -1.0
         tmp_data->vertex[3]->x = 1.0 : tmp_data->vertex[3]->y = -1.0 : tmp_data->vertex[3]->z = 1.0
         tmp_data->vertex[4]->x = 1.0 : tmp_data->vertex[4]->y = -1.0 : tmp_data->vertex[4]->z = -1.0
         tmp_data->vertex[5]->x = -1.0 : tmp_data->vertex[5]->y = 1.0 : tmp_data->vertex[5]->z = 1.0
         tmp_data->vertex[6]->x = -1.0 : tmp_data->vertex[6]->y = 1.0 : tmp_data->vertex[6]->z = -1.0
         tmp_data->vertex[7]->x = -1.0 : tmp_data->vertex[7]->y = -1.0 : tmp_data->vertex[7]->z = 1.0
         tmp_data->vertex[8]->x = -1.0 : tmp_data->vertex[8]->y = -1.0 : tmp_data->vertex[8]->z = -1.0
         
         'push the uv data
         tmp_data->vertex[1]->u = 1.0 : tmp_data->vertex[1]->v = 1.0
         tmp_data->vertex[2]->u = 0.0 : tmp_data->vertex[2]->v = -1.0
         tmp_data->vertex[3]->u = 1.0 : tmp_data->vertex[3]->v = 0.0
         tmp_data->vertex[4]->u = 0.0 : tmp_data->vertex[4]->v = 1.0
         tmp_data->vertex[5]->u = 0.0 : tmp_data->vertex[5]->v = 1.0
         tmp_data->vertex[6]->u = 0.0 : tmp_data->vertex[6]->v = 1.0
         tmp_data->vertex[7]->u = 0.0 : tmp_data->vertex[7]->v = 0.0
         tmp_data->vertex[8]->u = 0.0 : tmp_data->vertex[8]->v = 0.0
         
         'set the bounding box values for the entity created
         set_min_max_bbox_vertex( tmp_data )
         
         return tmp_data
         
end function   

and heres my drawing routine
Code: [Select]
function draw_entity( vert as new_entity ptr , texture as GLuint )
         
                 glloadidentity
         
glTranslatef vert->x_pos , vert->y_pos , vert->z_pos

glRotatef vert->xrot,1.0, 0.0, 0.0
glRotatef vert->yrot,0.0, 1.0, 0.0
glRotatef vert->zrot,0.0, 0.0, 1.0
         
glscalef( vert->x_scale , vert->y_scale , vert->z_scale )

                 glBindTexture GL_TEXTURE_2D, texture

glBegin GL_TRIANGLES
       
                 for i=0 to vert->no_faces
           
                         glTexCoord2f vert->vertex[vert->face[i]->a]->u , vert->vertex[vert->face[i]->a]->v   
                         glVertex3f vert->vertex[vert->face[i]->a]->x , vert->vertex[vert->face[i]->a]->y  , vert->vertex[vert->face[i]->a]->z
                         
glTexCoord2f vert->vertex[vert->face[i]->b]->u , vert->vertex[vert->face[i]->b]->v 
                         glVertex3f vert->vertex[vert->face[i]->b]->x , vert->vertex[vert->face[i]->b]->y  , vert->vertex[vert->face[i]->b]->z
                         
glTexCoord2f vert->vertex[vert->face[i]->c]->u , vert->vertex[vert->face[i]->c]->v   
                         glVertex3f vert->vertex[vert->face[i]->c]->x , vert->vertex[vert->face[i]->c]->y  , vert->vertex[vert->face[i]->c]->z
                         
                 next 
                 
glEnd
         
                 return 0
         
end function
Title: Re: uv coords
Post by: Stonemonkey on October 26, 2006
Yep, for a cube you'll have to define 4 verts for each face otherwise the texturing'll get messed up on at least 2 faces and probably reversed on others.
Title: Re: uv coords
Post by: ninogenio on October 27, 2006
ahh right thats cool.

im fine with adding more vertices/uv`s, so does anyone have any cube data with uv`s and faces that would run through my drawing routine lying around?
Title: Re: uv coords
Post by: Clyde on October 27, 2006
If you have Milkshape Models exported as ASC format or know anyone who has got them, then you could get the Points, UV Cords and Faces from those mate. Just ignore the first digit of each line of the Points and Faces data parts.
Title: Re: uv coords
Post by: ninogenio on October 27, 2006
cheers mate ive loads of difrent models on one of my disks somewhere i just cant find it i could download wings3d i just thought id ask if one of you guys might have them kicking about but its cool.
Title: Re: uv coords
Post by: Stonemonkey on October 27, 2006
I've not tested this but something like this might work.

Code: [Select]
cube_data:
data 24,12

data -1,-1,-1,1,1
data -1,-1, 1,0,1
data -1, 1, 1,0,0
data -1, 1,-1,1,0

data -1,-1, 1,1,1
data  1,-1, 1,0,1
data  1, 1, 1,0,0
data -1, 1, 1,1,0

data  1,-1, 1,1,1
data  1,-1,-1,0,1
data  1, 1,-1,0,0
data  1, 1, 1,1,0

data  1,-1,-1,1,1
data -1,-1,-1,0,1
data -1, 1,-1,0,0
data  1, 1,-1,1,0

data -1, 1,-1,1,1
data -1, 1, 1,0,1
data  1, 1, 1 0,0
data  1, 1,-1,1,0

data -1,-1, 1,1,1
data -1,-1,-1,0,1
data  1,-1,-1 0,0
data  1,-1, 1,1,0

data 1,2,3, 3,4,1
data 5,6,7, 7,8,5
data 9,10,11, 11,12,9
data 13,14,15, 15,16,13
data 17,18,19, 19,20,17
data 21,22,23, 23,24,21

function new_box_entity() as new_entity ptr
        restore cube_data
         dim tmp_data as new_entity ptr
         tmp_data = callocate(len(new_entity))
         read tmp_data->no_vertices,tmp_data->no_faces
         tmp_data->vertex = callocate( tmp_data->no_vertices * sizeof(new_entity) )
         tmp_data->face = callocate( tmp_data->no_faces * len(new_entity) )
         dim as integer vertex,face
         for vertex=1 to tmp_data->no_vertices
             read tmp_data->vertex[vertex]->x
             read tmp_data->vertex[vertex]->y
             read tmp_data->vertex[vertex]->z
             read tmp_data->vertex[vertex]->u
             read tmp_data->vertex[vertex]->v
         next
         for face=1 to tmp_data->no_faces
             read tmp_data->face[face]->a
             read tmp_data->face[face]->b
             read tmp_data->face[face]->c
         next
         set_min_max_bbox_vertex( tmp_data )
         return tmp_data
end function
Title: Re: uv coords
Post by: ninogenio on October 28, 2006
cheers stonemonkey mate!

ill test it out and tell you if it works.
Title: Re: uv coords
Post by: Stonemonkey on October 29, 2006
how did that work out? as i said i hadn't tested it or anything.
Title: Re: uv coords
Post by: ninogenio on October 29, 2006
sorry mate just got round to formatting my code with the new data is it the way ive enterd it.its just the model looks a bit like a two tris faceing each other.

Code: [Select]
function new_box_entity() as new_entity ptr
   
         dim tmp_data as new_entity ptr
         
         tmp_data = callocate(len(new_entity))
         tmp_data->no_vertices = 24
         tmp_data->no_faces = 12
         tmp_data->vertex = callocate( tmp_data->no_vertices * sizeof(new_entity) )
         tmp_data->face = callocate( tmp_data->no_faces * len(new_entity) )

         'push face data
         tmp_data->face[1]->a = 1 : tmp_data->face[1]->b = 2 : tmp_data->face[1]->c = 3
         tmp_data->face[2]->a = 3 : tmp_data->face[2]->b = 4 : tmp_data->face[2]->c = 1
         tmp_data->face[3]->a = 5 : tmp_data->face[3]->b = 6 : tmp_data->face[3]->c = 7
         tmp_data->face[4]->a = 7 : tmp_data->face[4]->b = 8 : tmp_data->face[4]->c = 5
         tmp_data->face[5]->a = 9 : tmp_data->face[5]->b = 10 : tmp_data->face[5]->c = 11
         tmp_data->face[6]->a = 11 : tmp_data->face[6]->b = 12 : tmp_data->face[6]->c = 9
         tmp_data->face[7]->a = 13 : tmp_data->face[7]->b = 14 : tmp_data->face[7]->c = 15
         tmp_data->face[8]->a = 15 : tmp_data->face[8]->b = 16 : tmp_data->face[8]->c = 13
         tmp_data->face[9]->a = 17 : tmp_data->face[9]->b = 18 : tmp_data->face[9]->c = 19
         tmp_data->face[10]->a = 19 : tmp_data->face[10]->b = 20 : tmp_data->face[10]->c = 17
         tmp_data->face[11]->a = 21 : tmp_data->face[11]->b = 22 : tmp_data->face[11]->c = 23
         tmp_data->face[12]->a = 23 : tmp_data->face[12]->b = 24 : tmp_data->face[12]->c = 21
         
         'push vertex data
         tmp_data->vertex[1]->x = -1.0 : tmp_data->vertex[1]->y = 1.0 : tmp_data->vertex[1]->z = 1.0
         tmp_data->vertex[2]->x = 1.0 : tmp_data->vertex[2]->y = 0.0 : tmp_data->vertex[2]->z = 1.0
         tmp_data->vertex[3]->x = 1.0 : tmp_data->vertex[3]->y = 0.0 : tmp_data->vertex[3]->z = 0.0
         tmp_data->vertex[4]->x = -1.0 : tmp_data->vertex[4]->y = 1.0 : tmp_data->vertex[4]->z = 0.0
         tmp_data->vertex[5]->x = 1.0 : tmp_data->vertex[5]->y = 1.0 : tmp_data->vertex[5]->z = 1.0
         tmp_data->vertex[6]->x = 1.0 : tmp_data->vertex[6]->y = 0.0 : tmp_data->vertex[6]->z = 1.0
         tmp_data->vertex[7]->x = 1.0 : tmp_data->vertex[7]->y = 0.0 : tmp_data->vertex[7]->z = 0.0
         tmp_data->vertex[8]->x = 1.0 : tmp_data->vertex[8]->y = 1.0 : tmp_data->vertex[8]->z = 0.0
         tmp_data->vertex[9]->x = 1.0 : tmp_data->vertex[9]->y = 1.0 : tmp_data->vertex[9]->z = 1.0
         tmp_data->vertex[10]->x = -1.0 : tmp_data->vertex[10]->y = 0.0 : tmp_data->vertex[10]->z = 1.0
         tmp_data->vertex[11]->x = -1.0 : tmp_data->vertex[11]->y = 0.0 : tmp_data->vertex[11]->z = 0.0
         tmp_data->vertex[12]->x = 1.0 : tmp_data->vertex[12]->y = 1.0 : tmp_data->vertex[12]->z = 0.0
         tmp_data->vertex[13]->x = -1.0 : tmp_data->vertex[13]->y = 1.0 : tmp_data->vertex[13]->z = 1.0
         tmp_data->vertex[14]->x = -1.0 : tmp_data->vertex[14]->y = 0.0 : tmp_data->vertex[14]->z = 1.0
         tmp_data->vertex[15]->x = -1.0 : tmp_data->vertex[15]->y = 0.0 : tmp_data->vertex[15]->z = 0.0
         tmp_data->vertex[16]->x = -1.0 : tmp_data->vertex[16]->y = 1.0 : tmp_data->vertex[16]->z = 0.0
         tmp_data->vertex[17]->x = -1.0 : tmp_data->vertex[17]->y = 1.0 : tmp_data->vertex[17]->z = 1.0
         tmp_data->vertex[18]->x = 1.0 : tmp_data->vertex[18]->y = 0.0 : tmp_data->vertex[18]->z = 1.0
         tmp_data->vertex[19]->x = 1.0 : tmp_data->vertex[19]->y = 0.0 : tmp_data->vertex[19]->z = 0.0
         tmp_data->vertex[20]->x = -1.0 : tmp_data->vertex[20]->y = 1.0 : tmp_data->vertex[20]->z = 0.0
         tmp_data->vertex[21]->x = 1.0 : tmp_data->vertex[21]->y = 1.0 : tmp_data->vertex[21]->z = 1.0
         tmp_data->vertex[22]->x = -1.0 : tmp_data->vertex[22]->y = 0.0 : tmp_data->vertex[22]->z = 1.0
         tmp_data->vertex[23]->x = -1.0 : tmp_data->vertex[23]->y = 0.0 : tmp_data->vertex[23]->z = 0.0
         tmp_data->vertex[24]->x = 1.0 : tmp_data->vertex[24]->y = 1.0 : tmp_data->vertex[24]->z = 0.0
         
         'push the uv data
         tmp_data->vertex[1]->u = -1.0 : tmp_data->vertex[1]->v = -1.0
         tmp_data->vertex[2]->u = -1.0 : tmp_data->vertex[2]->v = -1.0
         tmp_data->vertex[3]->u = -1.0 : tmp_data->vertex[3]->v = 1.0
         tmp_data->vertex[4]->u = -1.0 : tmp_data->vertex[4]->v = 1.0
         tmp_data->vertex[5]->u = -1.0 : tmp_data->vertex[5]->v = -1.0
         tmp_data->vertex[6]->u = 1.0 : tmp_data->vertex[6]->v = -1.0
         tmp_data->vertex[7]->u = 1.0 : tmp_data->vertex[7]->v = 1.0
         tmp_data->vertex[8]->u = -1.0 : tmp_data->vertex[8]->v = 1.0
         tmp_data->vertex[9]->u = 1.0 : tmp_data->vertex[9]->v = -1.0
         tmp_data->vertex[10]->u = 1.0 : tmp_data->vertex[10]->v = -1.0
         tmp_data->vertex[11]->u = 1.0 : tmp_data->vertex[11]->v = 1.0
         tmp_data->vertex[12]->u = 1.0 : tmp_data->vertex[12]->v = 1.0
         tmp_data->vertex[13]->u = 1.0 : tmp_data->vertex[13]->v = -1.0
         tmp_data->vertex[14]->u = -1.0 : tmp_data->vertex[14]->v = -1.0
         tmp_data->vertex[15]->u = -1.0 : tmp_data->vertex[15]->v = 1.0
         tmp_data->vertex[16]->u = 1.0 : tmp_data->vertex[16]->v = 1.0
         tmp_data->vertex[17]->u = -1.0 : tmp_data->vertex[17]->v = 1.0
         tmp_data->vertex[18]->u = -1.0 : tmp_data->vertex[18]->v = 1.0
         tmp_data->vertex[19]->u = 1.0 : tmp_data->vertex[19]->v = 1.0
         tmp_data->vertex[20]->u = 1.0 : tmp_data->vertex[20]->v = 1.0
         tmp_data->vertex[21]->u = -1.0 : tmp_data->vertex[21]->v = -1.0
         tmp_data->vertex[22]->u = -1.0 : tmp_data->vertex[22]->v = -1.0
         tmp_data->vertex[23]->u = 1.0 : tmp_data->vertex[23]->v = -1.0
         tmp_data->vertex[24]->u = 1.0 : tmp_data->vertex[24]->v = -1.0
         
         'set the bounding box values for the entity created
         set_min_max_bbox_vertex( tmp_data )
         
         return tmp_data
         
end function
     
Title: Re: uv coords
Post by: ninogenio on October 29, 2006
hmm your function works great i just took my one out and pasted your one in and its spot on so i wonder what i did that screwed it up.
Title: Re: uv coords
Post by: Stonemonkey on October 29, 2006
Was about to reply that it didn't need formatting and got a warning that another post had been made. Anyway, i've found it's a useful way of testing but I wasn't sure if I had all the values right.
Title: Re: uv coords
Post by: ninogenio on October 29, 2006
doh just realized what i did wrong sorry mate i thought your first two vertex coords were uv`s and i just realized its the last two should have realized really as you dont yousally see -1 in the uv`s :P

anyway thanks very much mate that saved me a bit of work and its shure to come in handy so have some karma
Title: Re: uv coords
Post by: Stonemonkey on October 29, 2006
NP, and it's a fairly simple matter of altering the data to make other shapes too instead of re writing the code.
Title: Re: uv coords
Post by: ninogenio on October 29, 2006
thats for sure ill have to add a sphere and mabey some other objects to the mix now but i think ill write a model converter for those.
Title: Re: uv coords
Post by: Stonemonkey on October 29, 2006
Should be easy enough to write a sphere function with UVs without having to load a model but loading models'll be very useful.
Title: Re: uv coords
Post by: ninogenio on October 29, 2006
to let you understand im working on a sort of plug in blitz3d look a like engine that uses opengl ive got 3ds model support and now im putting static objects in as for simple sphere and box objects i like to be able to call one function.

how would you define a simple sphere

im guessing it would be something like this
                                                .
                                            .   .    .
                                            .   .    .
                                                .

with the points down the center being 0,-1,0  0,-0.5,1  0,0.5,1   0,1,0  somthing like that mabey? 
Title: Re: uv coords
Post by: Jim on October 29, 2006
Spheres are quite complicated to make.  The simpler way is to start by sectioning up the sphere in horizontal slices.  Each slice is a circle which needs to be joined with tris or quads to the cirlces above and below.  An equation that will help is
x^2 + y^2 + z^2 = r^2.  That is, if the centre of the sphere is at (0,0,0) any points that are on the surface of the sphere (x,y,z) then if you square the x,y,z and add them together it equals the square of the radius of the sphere.  You can rearrange that equation to work out the size of the circles as you step down the sphere in slices.

The other way to create spheres is to start with an icosahedron http://www.scienceu.com/geometry/facts/solids/coords/icosa.html which is a 20 sided shape and then subdivide the faces into smaller triangles.  Push all the new points out from the centre so they lie on the surface of the sphere.  This is called a geodesic sphere.

Jim
Title: Re: uv coords
Post by: ninogenio on October 29, 2006
cool im intrested in learning to make static 3d shapes so im definitly going to have a play about.