开发者

OpenGL: How to undo scaling?

开发者 https://www.devze.com 2023-01-17 09:17 出处:网络
I\'m new to OpenGL. I\'m using JOGL. I have a WorldEntity class that represents a thing that can be rendered. It has attributes like position and size. To render, I\'ve been using this method:

I'm new to OpenGL. I'm using JOGL.

I have a WorldEntity class that represents a thing that can be rendered. It has attributes like position and size. To render, I've been using this method:

    /**
     * Renders the object in the world.
     */
    public void render() {
        gl.glTranslatef(getPosition().x, getPosition().y, getPosition().z);
        gl.glRotatef(getRotationAngle(), getRotation().x, getRotation().y, getRotation().z);
//        gl.glScalef(size, size, size);

        gl.glCallList(drawID);

//        gl.glScalef(1/size, 1/size, 1/size);
        gl.glRotatef(-getRotationAngl开发者_Python百科e(), getRotation().x, getRotation().y, getRotation().z);
        gl.glTranslatef(-getPosition().x, -getPosition().y, -getPosition().z);
    }

The pattern I've been using is applying each attribute of the entity (like position or rotation), then undoing it to avoid corrupting the state for the next entity to get rendered.

Uncommenting out the scaling lines causes the app to be much more sluggish as it renders a modest scene on my modest computer. I'm guessing that the float division is too much to handle thousands of operations per second. (?)

What is the correct way to go about this? Can I find a less computationally intensive way to undo a scaling transformation? Do I need to sort objects by scale and draw them in order to reduce scaling transformations required?

Thanks.


This is where you use matrices (bear with me, I come from a OpenGL/C programming background):

glMatrixMode(GL_MODELVIEW); // set the matrix mode to manipulate models

glPushMatrix(); // push the matrix onto the matrix stack

// apply transformations
glTranslatef(getPosition().x, getPosition().y, getPosition().z);
glRotatef(getRotationAngle(), getRotation().x, getRotation().y, getRotation().z);
glScalef(size, size, size);

glCallList(drawID); // drawing here

glPopMatrix(); // get your original matrix back

... at least, that's what I think it is.


It's very unlikely the divisions will cause any perf issue. rfw gave you the usual way of implementing this, but my guess is that your "slugish" rendering is mostly due to the fact that your GPU is the bottleneck, and using the matrix stacks will not improve perf.

When you increase the size of your drawn objects, more pixels have to be processed, and the GPU has to work significantly harder. What your CPU does at this point (the divisions) is irrelevant.

To prove my point, try to keep the scaling code in, but with sizes around 1.

0

精彩评论

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