PDA

View Full Version : MD2 comes out sideways



fuzzymoochicken
03-22-2008, 07:33 PM
So, I'm working with SDL and OpenGL, and I know SDL makes textures upside-down, but what's really strange is that for some reason I imported an MD2 model into my program, using some code from a tutorial that used Windows code (rather than SDL), and well.... the thing is rendering sideways. @_@

Is it just something that SDL does? If so, then does anyone know how to fix the problem? And if not, then that clues me that it's possibly something I did wrong...

sqrt[-1]
03-22-2008, 10:31 PM
This is typically caused by different orientation systems of what is "up".

In some applications "y" is up and in others, "z" is up. Try flipping the y and z values and see if your problem goes away.

(You may also have to flip face winding and fix normals)

fuzzymoochicken
03-25-2008, 06:59 PM
Okay, now I'm just having weird problems with rendering them in the SDL/OpenGL combo.

So I'm working with an OpenGL tutorial (actually, a book, but whatever) that explains how to open MD2 files in OpenGL with Win32 code, but I'm converting the project to work with SDL since I prefer it to that Win32 stuff. So I successfully converted everything, and everything seemed to be working correctly up close...

http://img176.imageshack.us/img176/1192/okayxz8.png

But then, as the camera gets further from the models, things start to mess up... some of the surfaces start to disappear. I don't know why, since the same OpenGL code works fine with win32 rather than SDL. So I'm wondering if there's something special I have to do in order to keep this from happening.

Here, you can see part of their faces and legs have appeared to turned black at this distance:
http://img172.imageshack.us/img172/7113/problem2za9.png

And in this picture, their faces and front of their bodies have disappeared for no real reason:
http://img248.imageshack.us/img248/8245/problem5ww4.png


I also noticed that at distances, there's a weird problem with anti-aliasing and jagged lines at a distance... surely that's not normal:
http://img252.imageshack.us/img252/4172/problem1pu4.png

I had the same problem when rendering a landscape a while back, too... check out the jaggies here (this with OpenGL anti-aliasing turned on):
http://img260.imageshack.us/img260/6979/problem3em2.png


I just know there's gotta be something I must not setting up correctly. All this stuff is really bizarre... remember, the Win32 version of the model program works correctly. So I don't get why it has problems in SDL...

ZbuffeR
03-26-2008, 05:51 AM
Looks like zfighting.
Maybe the SDL version does not query a 24bits zbuffer, and you end up with onlyt 16 bits of depth precision.
How do you init your GL context and window ?

sqrt[-1]
03-26-2008, 07:12 AM
I would also say the near plane is too close or the far plane is too far away. What are your settings to glFrustum() ?

fuzzymoochicken
03-26-2008, 10:35 PM
Looks like zfighting.
Maybe the SDL version does not query a 24bits zbuffer, and you end up with onlyt 16 bits of depth precision.
How do you init your GL context and window ?
That doesn't sound like it. But, here's the code anyway:

SDL_Init(SDL_INIT_EVERYTHING);

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE,8 );
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32 );


SDL_Surface* drawContext;
Uint32 flags;

int width = 800, height = 600;

flags = SDL_OPENGL | SDL_HWSURFACE | SDL_GL_ACCELERATED_VISUAL;
drawContext = SDL_SetVideoMode(width, height, 0, flags);


glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,500.0f);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);

glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(10.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glShadeModel(GL_SMOOTH);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);



]I would also say the near plane is too close or the far plane is too far away. What are your settings to glFrustum() ?
glFrustrum...? Actually, I've never used that before, but I have used gluPerspective though. That's similar I suppose.

Wait, I just looked at the tutorial and I notice that for their gluPerspective() they have set up, they set the first parameter "fovy" as 54.0f... whereas I'm using 45.0f... so I put that number in and sure enough, it's working better now.

Hmm... gosh, such a newbie at this. xP So anyway, would I need to change the fovy for a different window resolution then?

-NiCo-
03-27-2008, 01:01 AM
The last 2 parameters of gluPerspective will be of more interest to you in this case. If the models start disappearing when moving to close your near plane is probably too far away. If they start disappearing when zooming out, your far plane is probably too close. Best way would be too dynamically calculate the near and far plane values based on a bounding box of your scene.

ZbuffeR
03-27-2008, 03:12 AM
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);

Change this ugly 16bits zbuffer to 24bits batman, quick !
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

fuzzymoochicken
03-27-2008, 01:54 PM
The last 2 parameters of gluPerspective will be of more interest to you in this case. If the models start disappearing when moving to close your near plane is probably too far away. If they start disappearing when zooming out, your far plane is probably too close. Best way would be too dynamically calculate the near and far plane values based on a bounding box of your scene.
Well, I changed the first parameter, fovy, to "54.0f" as I said... and that problem's gone away. I don't know why, though.

Should I be calculating the fovy value based on the resolution of the window/screen? Like, if someone were to change the resolution or switch to fullscreen mode, does it have to be given a new value to still render correctly?


Change this ugly 16bits zbuffer to 24bits batman, quick !
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

How did you know my secret identity?! O_O

Alright, changed it. =P