Beginner needs help

Started by
9 comments, last by the_edd 12 years, 10 months ago
I'm very new to openGL (ES) programming and 3D things, so I need some tips on how to do this: I want to show my model in the middle of the screen, with a fixed bitmap background and a revolving camera. Basically, imagine a fight game when you have to select your player. I really don't know where to start.

I read some tutorials and the red book online version and I quite understood them, but when it comes to code I'm lost. I mean, it's easy to draw a cube and make it rotating, everything seems obvious. But when you have a real model it becomes much harder. My questions (among the others) are
  • How do I place my character in the middle of the scene? The "center" of the model is not (0, 0, 0) -let's say it's (123.34, 26.56, 756.12): should I translate and scale the MODELVIEW matrix based on model min and max coords before drawing?
  • How should I deal with something like projection matrix and glFrustrum?
  • How can I draw the quad (actually, two triangles), textured it, and ensure it's the background?

I know you can't give me the code tongue.gif Just point me in the right direction, show me the ideal workflow or maybe link me some accurate and deep paper

Best regards
Advertisement
This isn't really a direct answer to the question, but have you checked out the NeHe tutorials: nehe.gamedev.net? I think they will answer most, if not all, of those questions

This isn't really a direct answer to the question, but have you checked out the NeHe tutorials: nehe.gamedev.net? I think they will answer most, if not all, of those questions

Of course, I read those tutorials. Well, not all of them because they are poorly organized and the graphic style makes them difficult to read sad.gif
If only you could state "you are in the right direction" or "this is the way we do it" it would help more biggrin.gif
For example, playing with my model min anx max values for x, y e z, I could translate and scale my MODELVIEW matrix so that the object is placed in the middle of the screen and all its coordinates are < 1. Am I wrong?
Drawing the quad background is simple a matter of how I parameterized the far frustrum's plane?

How do I place my character in the middle of the scene? The "center" of the model is not (0, 0, 0) -let's say it's (123.34, 26.56, 756.12): should I translate and scale the MODELVIEW matrix based on model min and max coords before drawing?

The Red book's chapter on camera positioning is quite comprehensive. Revisit it and make sure you understand the duality of camera/object positioning encoded in the modelview matrix.

I tend to do things like this, at least conceptually:


  1. define a world-space position and orientation for the camera
  2. define world-space positions and orientations of objects in the scene.
  3. multiply each object by the matrix (so to speak) that positions and orients it appropriately in world space. If your object's origin is funny, then this matrix will have an extra translation component, or you could adjust the vertices after loading the 3D data in order to sanitize it.
  4. then multiply each object by the matrix that positions the camera at the world-space origin looking down -z. This matrix is the inverse of that which positions and orients the camera in world-space.


Whether you do the matrix multiplications in a vertex shader or using glMultMatrix/glPushMatrix/glPopMatrix/etc is up to you.

But start small with this stuff. Get it working with a cube or a glutTeapot before muddying the waters with object loading code.


How should I deal with something like projection matrix and glFrustrum?
[/quote]
Define a projection matrix. Decide on the field of view and near and far clip distances. gluPerspective can be a little easier to use in some situations. Again, the red book covers these functions in quite some detail. If you can rephrase your question in to something more specific, a better answer might be forthcoming.


How can I draw the quad (actually, two triangles), textured it, and ensure it's the background?

[/quote]
There are many ways. Perhaps set up an orthographic projection and use the identity for the modelview matrix. In this case the ±0.5*projection_width and ±0.5*projection_height of the projection give you the x and y coordinates of the quad's corners. A negative z value (say, -1) would be used. Ensure your orthographic projection is setup appropriately so that vertices at z=-1 aren't clipped.

The texturing chapter(s) in the red book will show you how to set up texturing.

Once that quad is drawn you probably want to clear the depth buffer using glClear(GL_DEPTH_BUFFER_BIT). Alternatively, you could disable depth writes while drawing the quad. Either way ensures that anything you draw afterwards will appear on top of the quad, as the usual depth test will always succeed.


show me the ideal workflow or maybe link me some accurate and deep paper
[/quote]
Really, that would be the Red book.
Thanks for your reply smile.gif Now I'm playing with a code-generated tetrahegon (the simplest 3D model) and cannot setup lighting correctly. More precisely I don't understand the openGL API regarding glDrawElements and normal arrays.
I mean, I use a vertices array to hold the vertices, then I should use a normals array. A tetrahegon has four vertices, but what about normals? Do I need 4 normals (maybe averaging the planes) or 12 normals since each vertex is shared among 3 triangles?
I guess I cannot share vertices having different normals. So for a tetrahedron I'd need 4*3 = 12 vertices and 4 normals.
But drawing with glDrawElements forces me to specify 12 lenght arrays for both vertices and normals, so I cannot save memory (and typing).
Am I wrong?
That is correct. Shared vertices is, simply put, only for vertices on a smooth surface. Cubes and similar shapes with only flat surfaces and sharp edges are the worst case examples when it comes to shared vertices, because there are none.

A vertex in OpenGL is a collection of all attributes defining it, so "vertex" is not just a position, but a collection of position, normal, color, texture coordinate and wherever else you want to use. Since your shared positions have different normals, they are different vertices and cannot be shared.
Thanks for the deep eplanation.
So it is not a waste of memory to specify 12 length arrays for both vertices and normals
It is only a waste if you have an option for less memory, but you don't have that. Your object have 12 unique vertices, so you need arrays of 12 vertices.
Sorry guys, it's really driving me crazy sad.gif Can someone explain this redbook excerpt about projection matrix
With gluPerspective(), you need to pick appropriate values for the field of view, or the image may look distorted. For example, suppose you're drawing to the entire screen, which happens to be 11 inches high. If you choose a field of view of 90 degrees, your eye has to be about 7.8 inches from the screen for the image to appear undistorted. (This is the distance that makes the screen subtend 90 degrees.) If your eye is farther from the screen, as it usually is, the perspective doesn't look right. If your drawing area occupies less than the full screen, your eye has to be even closer. To get a perfect field of view, figure out how far your eye normally is from the screen and how big the window is, and calculate the angle the window subtends at that size and distance. It's probably smaller than you would guess. Another way to think about it is that a 94-degree field of view with a 35-millimeter camera requires a 20-millimeter lens, which is a very wide-angle lens. "Troubleshooting Transformations," gives more details on how to calculate the desired field of view[/quote]
What the hell are those 11 inches??? And why does he state 7.8???(I guess it comes from 11*sin(90°/2) or maybe cosine, but what does it mean? Why may I have to figure the distance between user and screen? Also, I can't understand how a screen can subtend 90° - maybe because I'm not a native speaker but hey isn't the screen a 3D object?

It's getting very confused... I understand the basic concept that projection matrix is used to transform vertices by projecting them to the screen, and -concerning gluPerspective- that the wider the angle becomes, the narrower my object is drawn. But all those terminology and examples are really adding frustr(um)ation :P

This topic is closed to new replies.

Advertisement