View Full Version : glRotate or not?
01-31-2001, 06:30 AM
Lets say I want to create a labyrinth and place objects in it. these objects are able to move in this labyrinth. Should I call a glroatet / gltranslate for every object or should I directly modify the vertices of an object?
01-31-2001, 07:06 AM
You should use gltranslate and glrotate of course, coz it use less cpu-cycles than recalculate by hand all vertices ... especially if u've got a T&L card !
I would move the objects by changing the vertices because glTranslate uses matrix multiplication for each vetrex and that's much slower than just an addition or two you need when you move your objects. For the rotation on the other hand I'd let the driver do it for you.
01-31-2001, 09:37 AM
I disagree for this reason:
If you're making a game that uses and even remotely moderate number of models (or objects as you call them), and let's say you have ten of them onscreen, then you'd need ten copies of that same model to modify the vertices of each one. However, if you assign a matrix to each object, you only need a single instance of the model but can of MANY matrices each associated with a seperate object. This saves alot of RAM. Plus, all the matrix math is done on your video card if it has T&L so you can save your CPU for other things. Plus, if you do addition to your vertices to move them, when you try and rotate them, you're going to run into difficulty because you'll be rotate the things about their origins which will no longer be in the center of the model, but rather out away from that model. You'd have to do some extra trickery to get it to work correctly anyway which means more CPU usage. So I think you're better off using a matrix for each object and just multiplying that object's matrix with the modelview matrix before drawing it. This works fantastic for me and have no significant slowdowns even with 500 or more objects (and therefore matrix mults) and no T&L on a K62-380. And as for each vertex getting multiplied by the matrix using matrices: they get multiplied anyway when you render the thing whether you're using a matrix for the model's orientation or not. When you translate or rotate a model that has a matrix associated with it, you're not multiplying any of the object's vertices by that matrix at that time. But when you go to render the object, you're modifying the modelview matrix by which the vertices will be multiplied either way when rendered. Using this approach should also lead to cleaner and easier to read code. I encourage you to use this method. But allow me to qualify that statement with this:
Let's say you're doing something REALLY simple like drawing plasma beams flying in a particular direction using GL_LINES or something. Probably the fastest way to handle this is by takeing the plasma beam "model" and multiplying it's vertices by a rotation matrix to start with, but then doing addition to move it in that direction. This is better because the direction of the beam will always be the same. It will never need to rotate. Plus, the model of the beam is very simple and therefore does not require a large amount of RAM to store additional copies of it. If anyone else has a better idea of how to do such a "plasma beam" thing, please post a reply because I'm doing exactly this in a project I'm currently working on and that's the best way I know to do it.
01-31-2001, 10:19 AM
I see.. So let's say I have a VERY simple object, a vertex and function drawvertex(float x, float z); the Body of my function would look like this:
Ok so we have the translations.. but how to rotate a model with matrix multiply? I can't figure it out..
And, BTW, is this the right approach on creating this? I could e.g. define a class for a model with all the coords stored and the x,y, and z modifier and the do a "tank.draw". Am i right with this?
Thanks a lot, darkdreamer
in your function isn't going to work. To be able to draw anything, you need to call glBegin first. Then you can call glVertex3f. But you also call glTranslatef, which will be inside the glBegin/glEnd pair, and THAT is forbidden. Not forbidden as in "bad coding", but forbidden by the rules of OpenGL, the specifications. It WILL result in an error.
Once you called glBegin, you cannot call any matrix-related functions untill you call glEnd. Thats the way OpenGL works, and there is no way around it.
What we mean by using glTranslatef, is that you call it before glBegin, not before each vertex.
I think you should always stick to OpenGL's own matrix stuff. It's easy, and it's not going to be any slower that using your own code.
02-01-2001, 03:05 AM
So, in combination with GLDrawArrays(), it's ok and somewhat speedy to use glRotate, glTranslate?
Use glRotate/glTranslate ALWAYS. Most of the time, the far easiest way is to store the object in a local coordinate system, and use glRotate/glTranslate to place the object relative the world coordinate system.
Do so unless you want really optimized code of course, but i really don't think you are at that stage yet.
And also, don't forget that unless you have the identity matrix loaded (some drivers skip multiplication if the identity matrix is loaded), the matrix WILL ALWAYS be multiplied with each vertext you pass, so why not use it for translating the object aswell? There's no performace hit at all doing another transformation, because they are all stored in one matrix which will be used anyway.
If you do your own transformation, you will do OpenGL's transformation AND your own. Stick to OpenGL's only, and you don't have to do your own.
It's like coding your own low-level function for printing text on the screen, when you can use the pre-coded printf().
02-01-2001, 11:57 AM
Thanks a lot http://www.opengl.org/discussion_boards/ubb/smile.gif
Now I will try to make a object- class http://www.opengl.org/discussion_boards/ubb/smile.gif http://www.opengl.org/discussion_boards/ubb/smile.gif
02-06-2001, 06:04 AM
I have to disagree with Bob in the case of something like plasma beams. Basically because you might have THOUSANDS of beams flying through space at the same time and you definately don't want to do a translate and rotate for EACH and EVERY beam EACH and EVERY frame! If it's a simple model (only a handful of vertices) and it will never change orientation after fired or whatever, you're better off to just rotate it beforehand and and then just do like you did. As for how to multiply the vertices of a model by a rotateion matrix, I'd have to look up my stuff. It's VERY hard, I've found, to find this information out. But fourtunately, I found it and if you need it, e-mail me at email@example.com (remove the spamsucks. from the address). However, for fewer and larger objects (especially ones that rotate) I definately agree with Bob that you should use GL routines to rotate and translate your objects. Even better, you should keep a seperate matrix for each object and perform the glRotates and glTranslates on those matrices. Then, when you want to display your objects, do a glMultMatrix by your object's matrix and that should orient it properly.
02-06-2001, 07:08 AM
There are always cases where it is better to do it the other way around. But these effects don't rule the engine, do they? The majority of polys should come from solid models which aren't transparent and not used for special effects.
02-07-2001, 11:35 PM
No, they don't. What I want to do is a simple 3d-engine with some objects in it, These objects wont be that complexe I think. At least not at the beginning http://www.opengl.org/discussion_boards/ubb/wink.gif
And some simple animations would be nice, so let's say if I'd do a robot wandering around, it should wink while moving or so http://www.opengl.org/discussion_boards/ubb/smile.gif
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.