OpenGL Rotation

后端 未结 6 1410
-上瘾入骨i
-上瘾入骨i 2021-02-14 11:33

I\'m trying to do a simple rotation in OpenGL but must be missing the point. I\'m not looking for a specific fix so much as a quick explanation or link that explains OpenGL rota

相关标签:
6条回答
  • 2021-02-14 11:43

    Do you get a 1 unit straight line? It seems that 90deg rot. around Y is going to have you looking at the side of a triangle with no depth.

    You should try rotating around the Z axis instead and see if you get something that makes more sense.

    OpenGL has two matrices related to the display of geometry, the ModelView and the Projection. Both are applied to coordinates before the data becomes visible on the screen. First the ModelView matrix is applied, transforming the data from model space into view space. Then the Projection matrix is applied with transforms the data from view space for "projection" on your 2D monitor.

    ModelView is used to position multiple objects to their locations in the "world", Projection is used to position the objects onto the screen.

    Your code seems fine, so I assume from reading the documentation you know what the nature of functions like glPushMatrix() is. If rotating around Z still doesn't make sense, verify that you're editing the ModelView matrix by calling glMatrixMode.

    0 讨论(0)
  • 2021-02-14 11:49

    Ensure that you're modifying the modelview matrix by putting the following before the glRotatef call:

    glMatrixMode(GL_MODELVIEW);
    

    Otherwise, you may be modifying either the projection or a texture matrix instead.

    0 讨论(0)
  • 2021-02-14 11:50

    Regarding Projection matrix, you can find a good source to start with here:

    http://msdn.microsoft.com/en-us/library/bb147302(VS.85).aspx

    It explains a bit about how to construct one type of projection matrix. Orthographic projection is the very basic/primitive form of such a matrix and basically what is does is taking 2 of the 3 axes coordinates and project them to the screen (you can still flip axes and scale them but there is no warp or perspective effect).

    transformation of matrices is most likely one of the most important things when rendering in 3D and basically involves 3 matrix stages:

    • Transform1 = Object coordinates system to World (for example - object rotation and scale)
    • Transform2 = World coordinates system to Camera (placing the object in the right place)
    • Transform3 = Camera coordinates system to Screen space (projecting to screen)

    Usually the 3 matrix multiplication result is referred to as the WorldViewProjection matrix (if you ever bump into this term), since it transforms the coordinates from Model space through World, then to Camera and finally to the screen representation.

    Have fun

    0 讨论(0)
  • 2021-02-14 11:54

    When I had a first look at OpenGL, the NeHe tutorials (see the left menu) were invaluable.

    0 讨论(0)
  • 2021-02-14 11:59

    The "accepted answer" is not fully correct - rotating around the Z will not help you see this triangle unless you've done some strange things prior to this code. Removing a glOrtho(...) call might have corrected the problem in this case, but you still have a couple of other issues.

    Two major problems with the code as written:

    • Have you positioned the camera previously? In OpenGL, the camera is located at the origin, looking down the Z axis, with positive Y as up. In this case, the triangle is being drawn in the same plane as your eye, but up and to the right. Unless you have a very strange projection matrix, you won't see it. gluLookat() is the easiest command to do this, but any command that moves the current matrix (which should be MODELVIEW) can be made to work.

    • You are drawing the triangle in a left handed, or clockwise method, whereas the default for OpenGL is a right handed, or counterclockwise coordinate system. This means that, if you are culling backfaces (which you are probably not, but will likely move onto as you get more advanced), you would not see the triangle as expected. To see the problem, put your right hand in front of your face and, imagining it is in the X-Y plane, move your fingers in the order you draw the vertices (1,1) to (3,2) to (3,1). When you do this, your thumb is facing away from your face, meaning you are looking at the back side of the triangle. You need to get into the habit of drawing faces in a right handed method, since that is the common way it is done in OpenGL.

    The best thing I can recommend is to use the NeHe tutorials - http://nehe.gamedev.net/. They begin by showing you how to set up OpenGL in several systems, move onto drawing triangles, and continue slowly and surely to more advanced topics. They are very easy to follow.

    0 讨论(0)
  • I'd like to recommend a book:

    3D Computer Graphics : A Mathematical Introduction with OpenGL by Samuel R. Buss

    It provides very clear explanations, and the mathematics are widely applicable to non-graphics domains. You'll also find a thorough description of orthographic projections vs. perspective transformations.

    0 讨论(0)
提交回复
热议问题