Friday, December 19, 2008

Wavefront OBJ Loader - Normals Done...

Normals have been a pain in my backside. There are a couple of things I didn't know before, that I definitely know now. For example, calls to glNormalPointer() have pass a pointer to completely different data based on the type of shading you're using. If you're using GL_FLAT smoothing, then you have to pass an array of surface normals, one per triangle (really, it's per polygon, but since OpenGL ES only supports triangles, it amounts to the same thing). On the other hand, if you're using GL_SMOOTH, then you have to pass in vertex normals.

The OBJ file can provide vertex normals, but is not required to. I originally coded this so that it read the vertex normals from the file if they were there, but since calculating surface normals is easier to do from a polygon then it is to back it out of the vertex normals, I decided to basically ignore the normals provided in the file and just calculated them each time.

If you use a 3D program that exports the vertices with clockwise vertex ordering rather than the default counter-clockwise, you may have to flip the calculated normals. There is an inline function provided to do that, but I think most programs should export correctly.

Right now, you have to manually switch from using vertex to surface normals and back depending on what type of shading you're using, though they are both calculated when the model loads. I'm going to look at using the correct one based on the shading.

Anyway, here you can see a screenshot with plane model using snazzy lighting and smooth shading.



So, that means that handling texture coordinates is the only major bit of functionality that's missing. Not sure when I'll get to that, though - I've got a bunch of paying work that I need to get done before the end of next week. Plus, I have to find or make some models that have texture maps.

Here is the project with the current version of the OBJ loader



6 comments:

Eric said...

Wow, it's coming together very well!

This has been extremely useful for me.

Sam Carr said...

This is probably a very silly question, but I'm new to OpenGL: why do you have to supply these normals at all? Why doesn't the graphics card figure them out for itself? It is after all formulaic and would save bandwidth on sending all this extra redundant data.

It seems bonkers to me that you have to do this.

Alex said...

Jeff, I've just loaded the project and tried running it under 3.0 simulator. I get a white screen. If I terminate the app, then while it's unloading I see the plane for a split second. Could you take a look please?

Thank you!

Alex said...

Looks like there is an error, GLView.m line184:

if(err)
NSLog(@"%x error", err);

The error is 500 (GL_INVALID_ENUM).

Osagie said...

Hi Alex,
I'm not sure if you still need the answer to this question...just in case what you need to do is open Window in MainWindow.xib, change the Alpha value to 0 (default is 100).

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels