How to achieve an animation of a 3D model?

Have a look at the Ocean Water Demo by Florian Zitzelsberger “ZMaster” here on NeHe: http://nehe.gamedev.net/data/contests/contest.asp?contest=08

Direct link to download: http://nehe.gamedev.net/counter.asp?file=data/contests/2004_creative/demo/florian_zitzelsberger.zip

Look at the water, how do I achieve it moving like that? Is the water hard coded, because I cannot see any models in the ZIP file? In the Shaders folder, there are two files named “Water_vp.cg” and “Water_fp.cg”. What kind of files are these?

When someone makes a model in a 3D program, like Blender, SoftImage, 3D Studio Max etc and makes the model animating, is it possible to use that animation of the model in an OpenGL application where the animation is NOT set by OpenGL but by the third part 3D developing software?

The .cg files are shader programs used to replace parts of the rendering with a custom written program. He’s probably updating variables in that program so that it dynamically moves the vertices on a grid to appear as waves.

That being said, OpenGL comes with no support for animations. It is the programmer’s responsibility to write code that can read the mesh and animation files created in Blender, etc. and use them. For animating, this might involve repeatedly computing new vertex positions on the CPU each frame, or you can write custom shaders to move that calculation onto the GPU.

Are those shader programs run by the JOGL application? If they are updating variables, how does the logic look like? It cannot update variables in the running Java application (right?), so it has to update variables and write them to some kind of file that is later read by the Java application. Is that the way of how it works?

Which language is the best one to start with between “OpenGL Shading Language” and “C for Graphics”? Are the .cg files written in “C for Graphics”?

The cg files are “C for Graphics”. I would recommend the opengl shading language (or glsl) since it’s opengl specified instead of Nvidia specified.

Generally the way shaders work (with both languages), is that within the cg or glsl file the programmer declares certain types of variables, called uniforms, that can be modified at runtime. There are also attributes, which get configured at runtime and take their values from custom geometry.

When using a shader, you “bind” it with OpenGL commands and it replaces the current rendering program (either custom or normal). After that anything rendered goes through that. Uniform values can also be sent to the shader to use with other OpenGL calls.

Shaders are complicated, especially to learn all of the details but there are some pretty good tutorials out there.

Is the “current rendering program” the one providing by OpenGL itself? Which OpenGL commands are used to bind a shader program?
I have found a lot of tutorials of how to write code, but no one showing how to include / bind it in the JOGL code. Any good tutorial on that?

Yes, when no shader loaded before, the “current rendering program” is the fixed function transform and lightning pipeline provided by OpenGL.

Shader must be load, compile and link before they can be bind, so you must use glShaderSource, glCompileShader, glAttachShader, glLinkProgram and finally glUseProgram.

A simple jogl example (for glsl) which shows how to load and use a shader can be downloaded here: http://www.too-late.de/fancy/kskb/gfx/shader/Simple.java
(sorry, uncommented source and for jogl release build / not nightly build)

Thanks for answering and the example! I will study it when I start learning about shaders (now I am just looking at the subject).

Ah, I see, so the compilation of the OGSL code is done during runtime of the JOGL application by that OpenGL command, glCompileShader. What about compilation errors? How are they presented?

Is it possible to achieve everything about shading just by using OpenGL than using a shader language?

In your file simple.vert linked in your example code, what would be the OpenGL way to do that your vertex shader does:

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex * vec4 (0.7, 0.5, 0.5, 1.0);

?

No Problem :wink:

Exactly. The shader are compiled by the graphic card driver. But this happens in the background, so nothing special needs to be done.

Compile errors can be queried via glGetShaderInfoLog, in the Simple.java can you see how it works. If the shader can not compile OpenGL will use the fixed function pipeline.

With shaders like glsl you can control exactly what happens with every vertex and every fragment in any situation. With pure OpenGL you can not do this. (By the way, from OpenGL 3.1 the fixed function pipeline is gone, so you must do all with shaders)

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex results in exactly the same as the fixed function results. Multiplied with vec4 (0.7, 0.5, 0.5, 1.0) is doing something similar to glScalef(0.7f, 0.5f, 0.5f).

Thanks!

What kind of 3D models are easiest to work with? Are OBJ models easiest?

Is there any tutorial of how to load an OBJ model onto the OpenGL screen?

OBJ is suppose to be a pain, but it can support many things if done properly. The “support” part can be difficult to track. This works well for “bone chain” exporting. I dare say Collada is both experimental yet promising. Don’t delve too deep with it yet. 3DS is so proprietary that it almost makes me cross-eyed, but yet, it doesn’t. Results involve either thoughts of praise or horror. It can replace the need to visit scary movies if you’re into that activity. Questions usually involve, “Why isn’t my model in the software looking like the one in my legal copy of 3D Studio Max?!!” DAE can be a mixed bag of different tricks. It’s good to explore if you can already guess what software format it comes from.

…And then there’s ye ole MD5. It works for humanoids. No, not “checksum” unless you enjoy it.