Vertex cube lighting
Blitz3D Forums/Blitz3D Programming/Vertex cube lighting
| ||
Hi guys Can anyone help with this lighing problem. I have made a cube using the addvertex method. The cube is 1 mesh, 6 surfaces and 24 vertex's. No brush as yet. When I display the cube infront of the camera with a light the color of the sides remains the same and isn't shaded like if you use createcube(). I don;t really understand why this isn;t working. Anyone know? Thanks! |
| ||
I believe your problems would be solved if you shared the vertices. that way you would also only have 8 vertices instead of 24 |
| ||
Thanks nazca but I need to have 6 surfaces per cube. When I create a vertex I assign it to a surface so how would I just have 8 vertexs and 6 surfaces. Cheers. |
| ||
You need to set the vertex normals. They determine how lights will affect the triangles. UpdateNormals should do this for you. Or you can set the normals manually when you create the vertices. |
| ||
Ahh Floyd. That's what I didn't understand. Now I read the docs it makes sense. At first I thought, oh I just make a cube and light will bounce off it. But as I said, this makes more sense. Thanks Mate! Great forum this is. |
| ||
OK, it's shading but the shading is all wrong. I think it's because I'm not stitching the triangles together properly. Can someone please tell me the rules for building triangles out of vertices? Here's a test setup. notice how the cubes don;t shade the same way. Thanks for any help here. Graphics3D 640,400;,16 SetBuffer BackBuffer() Global camera=CreateCamera() CameraViewport camera,0,0,640,480 Global light=CreateLight() LightColor light,255,255,255 Global numcubes=100 Global testcube Dim v1(numcubes): Dim v2(numcubes): Dim v3(numcubes): Dim v4(numcubes) Dim v5(numcubes): Dim v6(numcubes): Dim v7(numcubes): Dim v8(numcubes) Dim v9(numcubes): Dim v10(numcubes): Dim v11(numcubes): Dim v12(numcubes) Dim cubesurface1(numcubes):Dim cubesurface2(numcubes) Dim cubemesh(numcubes) fps_milli=MilliSecs(): fps_counter=0: update_frequency=10 Global frameTimer=CreateTimer(30) setupcubes() While Not KeyHit(1) TurnEntity cubemesh(1),1,1,1 TurnEntity testcube,1,1,1 UpdateWorld RenderWorld fps_counter=fps_counter+1 If fps_counter=update_frequency fps=1000/Float(((MilliSecs()-fps_milli))/update_frequency) fps_milli=MilliSecs() fps_counter=0 EndIf Flip WaitTimer(frameTimer) Wend End Function setupcubes() cubemesh(1)=CreateMesh() cubesurface1(1)=CreateSurface(cubemesh(1)) cubesurface2(1)=CreateSurface(cubemesh(1)) ;EntityFX cubemesh(1),5 v1(1)=AddVertex (cubesurface1(1),-1,1,-1) v2(1)=AddVertex (cubesurface1(1),1,1,-1) v3(1)=AddVertex (cubesurface1(1),-1,-1,-1) v4(1)=AddVertex (cubesurface1(1),1,-1,-1) v5(1)=AddVertex (cubesurface1(1),-1,1,1) v6(1)=AddVertex (cubesurface1(1),1,1,1) v7(1)=AddVertex (cubesurface1(1),-1,-1,1) v8(1)=AddVertex (cubesurface1(1),1,-1,1) v9(1)=AddVertex (cubesurface2(1),-1,1,1) v10(1)=AddVertex (cubesurface2(1),1,1,1) v11(1)=AddVertex (cubesurface2(1),-1,1,-1) v12(1)=AddVertex (cubesurface2(1),1,1,-1) AddTriangle (cubesurface1(1),v3(1),v1(1),v2(1)) AddTriangle (cubesurface1(1),v2(1),v4(1),v3(1)) AddTriangle (cubesurface1(1),v7(1),v5(1),v1(1)) AddTriangle (cubesurface1(1),v1(1),v3(1),v7(1)) AddTriangle (cubesurface1(1),v8(1),v6(1),v5(1)) AddTriangle (cubesurface1(1),v5(1),v7(1),v8(1)) AddTriangle (cubesurface1(1),v4(1),v2(1),v6(1)) AddTriangle (cubesurface1(1),v6(1),v8(1),v4(1)) AddTriangle (cubesurface1(1),v7(1),v3(1),v4(1)) AddTriangle (cubesurface1(1),v4(1),v8(1),v7(1)) AddTriangle (cubesurface2(1),v11(1),v9(1),v10(1)) AddTriangle (cubesurface2(1),v10(1),v12(1),v11(1)) PositionEntity cubemesh(1),-2,0,10 UpdateNormals cubemesh(1) testcube=CreateCube() PositionEntity testcube,2,0,10 End Function |
| ||
The cube is 1 mesh, 6 surfaces and 24 vertex's. No brush as yet. Your cube has only 12 vertices. It really does need 24 for the sides to look flat. Each vertex has only one normal. Each corner of the cube belongs to three sides, and thus needs three different normals. As a result, each corner really needs to be three vertices at a single location. |
| ||
Graphics3D 800,600,0,2 cam=CreateCamera() light=CreateLight() cube=SixSurfaceCube(2,2,2) PositionEntity cam,0,2,-8 PointEntity cam,cube While Not KeyHit(1) TurnEntity cube,.1,.6,.3 UpdateWorld() RenderWorld() Flip Wend End Function SixSurfaceCube(w#,h#,d#) Local m,s Local dw#,dh#,dd# dw=w/2.0 dh=h/2.0 dd=d/2.0 m=CreateMesh() ; front s=CreateSurface(m) AddRect s,-dw,-dh,-dd,-dw,dh,-dd,dw,dh,-dd,dw,-dh,-dd ; back s=CreateSurface(m) AddRect s,dw,-dh,dd,dw,dh,dd,-dw,dh,dd,-dw,-dh,dd ;bottom s=CreateSurface(m) AddRect s,-dw,-dh,dd,-dw,-dh,-dd,dw,-dh,-dd,dw,-dh,dd ; top s=CreateSurface(m) AddRect s,-dw,dh,-dd,-dw,dh,dd,dw,dh,dd,dw,dh,-dd ;left side s=CreateSurface(m) AddRect s,-dw,-dh,dd,-dw,dh,dd,-dw,dh,-dd,-dw,-dh,-dd ;right side s=CreateSurface(m) AddRect s,dw,-dh,-dd,dw,dh,-dd,dw,dh,dd,dw,-dh,dd Return m End Function Function AddRect(s,x1#,y1#,z1#,x2#,y2#,z2#,x3#,y3#,z3#,x4#,y4#,z4#) ; make normal dx1#=x2-x1 dy1#=y2-y1 dz1#=z2-z1 dx2#=x3-x1 dy2#=y3-y1 dz2#=z3-z1 nx#=dy1*dz2-dz1*dy2 ny#=dz1*dx2-dx1*dz2 nz#=dx1*dy2-dy1*dx2 mag#=Sqr(nx*nx+ny*ny+nz*nz) nx#=nx/mag ny#=ny/mag nz#=nz/mag v1=AddVertex(s,x1,y1,z1,0,0) v2=AddVertex(s,x2,y2,z2,0,1) v3=AddVertex(s,x3,y3,z3,1,1) v4=AddVertex(s,x4,y4,z4,1,0) VertexNormal s,v1,nx,ny,nz VertexNormal s,v2,nx,ny,nz VertexNormal s,v3,nx,ny,nz VertexNormal s,v4,nx,ny,nz AddTriangle s,v1,v2,v3 AddTriangle s,v1,v3,v4 End Function |
| ||
Thanks for the help guys. And Peter, the code is VERY much appreciated! :-) |