Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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 1294 times)
0 Members and 1 Guest are viewing this topic.
Offline pixel

Junior Newbie




And the thing


« Posted 2003-05-27 00:35:34 »

Hi,

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

Edit:

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?

Pixel
Offline Mojomonkey

Senior Member




ooh ooh eee eeee


« Reply #1 - Posted 2003-05-27 03: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 04: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

Pixel
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 04: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 Member


Projects: 1


Used to be bleb


« Reply #4 - Posted 2003-05-27 09: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 11: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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (25 views)
2014-04-15 18:08:23

BurntPizza (20 views)
2014-04-15 03:46:01

UprightPath (36 views)
2014-04-14 17:39:50

UprightPath (18 views)
2014-04-14 17:35:47

Porlus (34 views)
2014-04-14 15:48:38

tom_mai78101 (60 views)
2014-04-10 04:04:31

BurntPizza (118 views)
2014-04-08 23:06:04

tom_mai78101 (218 views)
2014-04-05 13:34:39

trollwarrior1 (185 views)
2014-04-04 12:06:45

CJLetsGame (192 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org 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‑gaming.org
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!