开发者

Why does GLfloat require global scope?

开发者 https://www.devze.com 2023-02-15 18:50 出处:网络
When declared as a local variable, rotating a GLfloat object does not occur. When it is declared as global, it will rotate accordingly, what is the reason behind it requiring global? Does 开发者_C百科

When declared as a local variable, rotating a GLfloat object does not occur. When it is declared as global, it will rotate accordingly, what is the reason behind it requiring global? Does 开发者_C百科the display() function of an OpenGL program call something else?


What I am thinking is that you may be trying to do something like this (with rot in the function not global):

GLfloat rot = 0.1;
glRotatef(rot, 0.0f, 1.0f, 0.0f);

which will not rotate the objects drawn after.

This is because glRotatef works with absolute rotation. You are applying the constant rotation of 0.1 to every image you draw and thus they are not moving.

You dont have to store rot globally, you can store it with the model of the object you rotate and do it like so:

glRotatef(myObj->rotation, 0.0f, 1.0f, 0.0f);
... draw myObj->triangles ...
myObj->rotation += speed;   // speed can be e.g. 0.1

As a note: keep in mind that if you do myObj->rotation += speed; in the render-function your rotation-speed will be proportional to your render-speed (faster on faster computers) which is generally not desired. To avoid this you can check the elapsed time and only evaluate it if that time is say longer than 50ms.


In response to your comment

but how do I create a model of my object? I'm just using simple functions like glVertex3f()

Instead of hard-coding the glVertex, glColor... you can separate your models from your rendering (the calls to glVertex, glColor...) in many ways. Let's first talk about the easy-to-understand (but inefficent) version: A linked list of triangles:

#define triangleList struct _triangleList
struct _triangleList {
    float ax, ay, az, bx, by, bz, cx, cy, cz;
    triangleList* next; // this has to be a null-pointer if this is the last link
}

And a model-structure to use them:

#define model struct _model
struct _model {
    float rotationX;
    triangleList* firstTriangle;
}

You then can make a generic renderFunction:

void myRenderFunc(model* aModel) {
    triangleList* current = aModel->firstTriangle;
    glBegin(GL_TRIANGLES);
    glRotate(aModel->rotationX,1.0,0.0,0.0);
    while(current!=0) {
        glVertex3f(current->ax,current->ay,current->az);
        ...
        current = current -> next;
    }
    glEnd();
}

Now you have a generic (but slow) renderfunction: To render new models you dont have to write glBegins/glVertex/glColor... but just create a new model and give it to the renderFunc (which you have to extended to support color...). Now a quick mention of how to do it faster (IF that is needed): instead of calling glVertex... read about vertex-arrays or even vertex-buffer-objects. But the general idea of separating your models from the rendering-function works with those too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号