0

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(-getRotationAngle(), 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.

4

2 回答 2

5

这是您使用矩阵的地方(请耐心等待,我来自 OpenGL/C 编程背景):

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

……至少,我是这么认为的。

于 2010-09-20T21:23:00.050 回答
0

这些部门不太可能导致任何性能问题。rfw 为您提供了实现此功能的常用方法,但我猜您的“缓慢”渲染主要是由于您的 GPU 是瓶颈,并且使用矩阵堆栈不会提高性能。

当您增加绘制对象的大小时,必须处理更多像素,并且 GPU 必须更加努力地工作。你的 CPU 在这一点上做什么(分区)是无关紧要的。

为了证明我的观点,尝试保留缩放代码,但大小约为 1。

于 2010-09-21T08:32:12.273 回答