Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (781)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (857)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Mesh loader and normal discontinuity  (Read 2956 times)
0 Members and 1 Guest are viewing this topic.
Offline pixel

Junior Newbie

And the thing

« Posted 2003-05-26 22:35:34 »


I have written a mesh loader for MS DirectX .x files (just the mesh components for now, no textures, animation) and can display the loaded mesh using lwjgl drawing triangles and setting normals etc.

The problem is that when I specify a gl.normal3f() for each triangle (at a right angle to the triangle), the faces are predictably heavily faceted under lighting (like a crystal).

Does anyone have any recommendations for how I can display a poly model consisting of triangles so that the surface is smooth rather than heavily faceted (like a crystal)

I am happy to share the mesh loader on this forum when it is finished

Pixel  Wink


Having a look around, it seems that I will need to specify per vertex normals which are averaged between normals for each face.

Additionally, the gl.enable(gl.NORMALIZE) call seems to be important in rendering with Gouraud shading.

Does anyone have any working examples or links to an applicable tutorial (C or C++ is fine) for per vertex normal calculation and OpenGL rendering of triangle meshes with Gouraud shading?

Offline Mojomonkey

Senior Devvie

ooh ooh eee eeee

« Reply #1 - Posted 2003-05-27 01:03:32 »

You need to do what's called normal averaging. Basically smoothly interpolating between the normals of a face. Instead of calculating the normal for just the face, calculate the normal for each vertex. Where the normal for a single vertex is the average of the normals of each face it touches. Hope that helps.

Don't send a man to do a monkey's work.
Offline pixel

Junior Newbie

And the thing

« Reply #2 - Posted 2003-05-27 02:30:50 »

Thanks Mojomonkey,

Does anyone have sample code to calculate per vertex normals?

Once I have them, do I just call gl.normal3f() just before gl.vertex3f()?

My Mesh object contains (amongst other things)

1) a Vertex [] of vertices
2) a Vertex [] of normals for each triangle
3) a Triangle [] where each triangle lists the index of each point and an index for the normal for the triangle

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pixel

Junior Newbie

And the thing

« Reply #3 - Posted 2003-05-27 02:38:39 »

I suppose I could:

1) walk the triangle array indexing each vertex according to the triangles that contain that point.

2) Accumulate the indexes to the normals for each attached triangle for every vertex.

3) Acquire an average normal by adding each x,y,z component from the adjacent triangle normals and dividing each component total by the number of normals.

avNormal.x = (adjNormal1.x + adjNormal2.x + adjNormal3.x) / 3.0f;

avNormal.y = (adjNormal1.y + adjNormal2.y + adjNormal3.y) / 3.0f;

avNormal.z = (adjNormal1.z + adjNormal2.z + adjNormal3.z) / 3.0f;

Is it that easy?  Or is there some funky vector math that I should use to average a set of normals?

Pixel  :-/
Offline ryanm

Senior Devvie

Projects: 1
Exp: 15 years

Used to be bleb

« Reply #4 - Posted 2003-05-27 07:45:26 »

That's basically it, but for smoother results you can take the size of each face into account, instead of giving each the same weighting in the average.

I believe that this is done in idx3d, which can be found here. It's a software renderer that does phong shading, environment mapping and other stuff, all in java 1.1.  Digging around the source may turn up some tips.
Offline pixel

Junior Newbie

And the thing

« Reply #5 - Posted 2003-05-27 09:30:50 »

Thanks Bleb,

I tried what I was suggesting and the results are very promising.  The loader just calculates the per vertex normal as it is loading the mesh.

The result is certainly smooth enough for the engine I am writing.

Now for some texturing....
Pages: [1]
  ignore  |  Print  

hadezbladez (1165 views)
2018-11-16 13:46:03

hadezbladez (525 views)
2018-11-16 13:41:33

hadezbladez (1187 views)
2018-11-16 13:35:35

hadezbladez (272 views)
2018-11-16 13:32:03

EgonOlsen (2567 views)
2018-06-10 19:43:48

EgonOlsen (2786 views)
2018-06-10 19:43:44

EgonOlsen (1564 views)
2018-06-10 19:43:20

DesertCoockie (2269 views)
2018-05-13 18:23:11

nelsongames (2121 views)
2018-04-24 18:15:36

nelsongames (2815 views)
2018-04-24 18:14:32
Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!