当前位置: 首页 > 教程 > JOGL >

JOGL 缩放效果

精华
小牛编辑
105浏览
2023-03-14

在 JOGL 中,缩放用于增加或减少对象的大小。它操纵对象的物理尺寸。因此,通过缩放对象可以从其任何轴拉伸或收缩。

要缩放对象,请使用以下语法:

glScalef(float a, float b, float c)  

此处,glScalef() 方法由 GLMatrixFunc 接口提供,其参数表示维度。

JOGL 缩放 示例

在这个例子中,我们将减少多边形的尺寸。

缩放前代码:

package cn.xnip;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.*;

public class JScaling implements GLEventListener {

    @Override
    public void display(GLAutoDrawable drawable) {

        final GL2 gl = drawable.getGL().getGL2();

        //gl.glScalef(0.40f, 0.30f, 0.40f);

        gl.glBegin(GL2.GL_QUADS);

        gl.glVertex3f(0.0f, 0.5f, 0);
        gl.glVertex3f(-0.5f, 0f, 0);
        gl.glVertex3f(0f, -0.5f, 0);
        gl.glVertex3f(0.5f, 0f, 0);

        gl.glEnd();

    }

    @Override
    public void dispose(GLAutoDrawable arg0) {
    }

    @Override
    public void init(GLAutoDrawable arg0) {
    }

    @Override
    public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
    }

    public static void main(String[] args) {

        final GLProfile gp = GLProfile.get(GLProfile.GL2);
        GLCapabilities cap = new GLCapabilities(gp);

        final GLCanvas gc = new GLCanvas(cap);
        JScaling js = new JScaling();
        gc.addGLEventListener(js);
        gc.setSize(400, 400);

        final JFrame frame = new JFrame("xnip.cn:Befire Scaling");
        frame.add(gc);
        frame.setSize(500, 400);
        frame.setVisible(true);
    }

}

缩放前输出结果如下:

缩放后代码:

package cn.xnip;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.*;

public class JScaling implements GLEventListener {

    @Override
    public void display(GLAutoDrawable drawable) {

        final GL2 gl = drawable.getGL().getGL2();

        gl.glScalef(0.40f, 0.30f, 0.40f);

        gl.glBegin(GL2.GL_QUADS);

        gl.glVertex3f(0.0f, 0.5f, 0);
        gl.glVertex3f(-0.5f, 0f, 0);
        gl.glVertex3f(0f, -0.5f, 0);
        gl.glVertex3f(0.5f, 0f, 0);

        gl.glEnd();

    }

    @Override
    public void dispose(GLAutoDrawable arg0) {
    }

    @Override
    public void init(GLAutoDrawable arg0) {
    }

    @Override
    public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
    }

    public static void main(String[] args) {

        final GLProfile gp = GLProfile.get(GLProfile.GL2);
        GLCapabilities cap = new GLCapabilities(gp);

        final GLCanvas gc = new GLCanvas(cap);
        JScaling js = new JScaling();
        gc.addGLEventListener(js);
        gc.setSize(400, 400);

        final JFrame frame = new JFrame("xnip.cn:After Scaling");
        frame.add(gc);
        frame.setSize(500, 400);
        frame.setVisible(true);
    }

}

缩放后输入结果如下: