# Unseen Machine

Member

5

100 Neutral

• Rank
Newbie
1. ## Calculating normals for terrain?

Turns out it was the lighting mode giving me themost porblems, i had glLightModelfv GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE when it needed to be turned off after i had drawn my sky sphere. Here's how i have it looking now : GL_terrain_2 which is a vast improvement on how it was but it's till not perfect. Here's how i am doing the normals for it currently, am i doing it right? '######################################################################################################################## SUB GDK_GL_MAP_DRAW (X!, Y!, Z!, MapVert() AS VertexF, ImgH&, ImgW&) DIM TmpTri AS Triangle, TmpTri2 AS Triangle glTranslatef X!, Y!, Z! FOR j& = 0 TO ImgH& - 1 glBegin GL_TRIANGLE_STRIP FOR i& = 0 TO ImgW& - 1 TmpTri.Vertex1.X = MapVert(i&, j&).X TmpTri.Vertex1.Y = MapVert(i&, j&).Y TmpTri.Vertex1.Z = MapVert(i&, j&).Z TmpTri.Vertex2.X = MapVert(i& + 1, j&).X TmpTri.Vertex2.Y = MapVert(i& + 1, j&).Y TmpTri.Vertex2.Z = MapVert(i& + 1, j&).Z TmpTri.Vertex3.X = MapVert(i&, j& + 1).X TmpTri.Vertex3.Y = MapVert(i&, j& + 1).Y TmpTri.Vertex3.Z = MapVert(i&, j& + 1).Z TmpTri2.Vertex1.X = MapVert(i& + 1, j&).X TmpTri2.Vertex1.Y = MapVert(i& + 1, j&).Y TmpTri2.Vertex1.Z = MapVert(i& + 1, j&).Z TmpTri2.Vertex2.X = MapVert(i&, j& + 1).X TmpTri2.Vertex2.Y = MapVert(i&, j& + 1).Y TmpTri2.Vertex2.Z = MapVert(i&, j& + 1).Z TmpTri2.Vertex3.X = MapVert(i& + 1, j& + 1).X TmpTri2.Vertex3.Y = MapVert(i& + 1, j& + 1).Y TmpTri2.Vertex3.Z = MapVert(i& + 1, j& + 1).Z TmpTri.Normal.X = (TmpTri.Normal.X + TmpTri2.Normal.X) / 2 TmpTri.Normal.Y = (TmpTri.Normal.Y + TmpTri2.Normal.Y) / 2 TmpTri.Normal.Z = (TmpTri.Normal.Z + TmpTri2.Normal.Z) / 2 GL_Calculate_Normal TmpTri glNormal3f TmpTri.Normal.X, TmpTri.Normal.Y, TmpTri.Normal.Z glTexCoord2f 0.0, 0.0 glVertex3f MapVert(i&, j&).X, MapVert(i&, j&).Y, MapVert(i&, j&).Z glTexCoord2f 1.0, 0.0 glVertex3f MapVert(i& + 1, j&).X, MapVert(i& + 1, j&).Y, MapVert(i& + 1, j&).Z glTexCoord2f 0.0, 1.0 glVertex3f MapVert(i&, j& + 1).X, MapVert(i&, j& + 1).Y, MapVert(i&, j& + 1).Z glTexCoord2f 1.0, 1.0 glVertex3f MapVert(i& + 1, j& + 1).X, MapVert(i& + 1, j& + 1).Y, MapVert(i& + 1, j& + 1).Z NEXT glEnd NEXT END SUB '######################################################################################################################## SUB GDK_GL_Calculate_Normal (Tri AS Triangle) DIM u AS VertexF, v AS VertexF u.X = Tri.Vertex2.X - Tri.Vertex1.X u.Y = Tri.Vertex2.Y - Tri.Vertex1.Y u.Z = Tri.Vertex2.Z - Tri.Vertex1.Z v.X = Tri.Vertex3.X - Tri.Vertex1.X v.Y = Tri.Vertex3.Y - Tri.Vertex1.Y v.Z = Tri.Vertex3.Z - Tri.Vertex1.Z Tri.Normal.X = (u.Y * v.Z) - (u.Z * v.Y) Tri.Normal.Y = (u.Z * v.X) - (u.X * v.Z) Tri.Normal.Z = (u.X * v.Y) - (u.Y * v.X) END SUB '######################################################################################################################## Thanks again folks, John
2. ## Calculating normals for terrain?

I'll look into the shader thing as i can't use quick fixes as this is for an engine rather than just a game. Many thanks for the pointers i will report back when i have it finished. John
3. ## Calculating normals for terrain?

First make your terrain use a standard passtrough vertex shader to get to know your shader api[/quote] Where do i find a shader API? Seeing you're combining the terms vertex shading and normals into seemingly 1 problem does this mean what you're trying to accomplish is actually vertex lighting?[/quote] I dunno, i just want the ground to look smooth. I am still a noob to most of OpenGL especially the lighting bits and my math is far from brilliant. It's a whole new world to me! Thanks, John
4. ## Calculating normals for terrain?

Thanks for the responses, How are you getting the height for the terrain? [/quote] The y values relate to the greyscale value of the pixel the terrain is taken from. 0 (black) = no height, 255 = heightest point, i have yet to implement a y scale option. This link may help : [color="#284b72"]http://www.flipcode....ight_Maps.shtml I didn't like how It went straight to the optimized version though[/quote] It seems to be just what i might need, but my C/C++ is weak at best and theres code there i dont understand (Question marks and such) so i got no hope of converting it . what does the underside look like?[/quote] The same as the top where there is an equal mix of light and dark triangles. Thanks again, John
5. ## Calculating normals for terrain?

Hi, I am sure it been answered before, and I have Googled a bit. The results of the google are I reckon i need to learn how to do per-vertex shading (might be worng though), i have no idea how it is implemented when using GL_TRIANGLE_STRIP and i can't find a decent demo. Here's a picture of how my terrain looks GL_Terrain , as you can see, something is not quite right! The code is written in QB64 (a modern QBasic) using SFML to create the GL windows (via a C++ wrapper), i am happy to post it but it's quite large and requires a whole myriad of files, libs and QB64 to run it. Any ideas, links, etc on how to do per vertex shading would be great. Many thanks, John