3DIuWFNg̕\

hLg̖ڎ


̂\AړvO Move.java łB g\̗̂2’uAL[ň̗̂ړ܂B Xy[XL[ƂňړΏۂ݂Ɏ_̂ɐ؂ւ邱Ƃo܂B eL[́{Ɓ|ŎpύXł܂B"q"L[ŏI܂B ܂Aʏɂꂼ̍W܂B

Move screen

ȉǂẴ\[XR[hĂ܂B


vO̍\

CNX Move ł̓IuWFNgAɕ\CĂ܂B

cameraIuWFNg͖ڕWcube1ɐݒ肵Ă܂B ̂߁AcameräʒuړĂAcube1ړĂ ɉʂ̐^cube1悤ɂȂ܂B

܂A߂onix.gfx.image.BufferedGLImagegĂ܂B Java2D̋@\pĕAʂ֓\t邽߂̃obt@ƂĎg܂B

Ci[NX Controller ł̓L[ɑ΂āA ̂J̈ړAp̊gkȂǂs킹Ă܂B CameraNXCubeNXA ʒuړ̂߂̃\bhMaterialNX hNXɂĂ܂B̂߁Aړ̂؂ւ邱Ƃ 悤Ɉړ邱Ƃł܂B

initGraphics \bh

initGraphics \bhł͂‚̐ݒsĂ܂B

    public void initGraphics(GLGraphics g) {
        g.glViewport(0, 0, width, height);
        g.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        
        g.glEnable(GLConstants.GL_BLEND);
        g.glBlendFunc(GLConstants.GL_SRC_ALPHA,
                      GLConstants.GL_ONE_MINUS_SRC_ALPHA);
    }

update \bh

update \bh NAĂAJݒ肵̂\āA\ (q printMessage\bh)Ă܂B

    public void update(GLGraphics g) {
        g.glClear(GLConstants.GL_COLOR_BUFFER_BIT);
        
        g.glPushMatrix();
        g.glMatrixMode(GLConstants.GL_PROJECTION);
        g.glLoadIdentity();
        camera.update(g);
        g.glMatrixMode(GLConstants.GL_MODELVIEW);
        g.glLoadIdentity();
        for (int i = 0; i < materials.length; i++) {
            g.glPushMatrix();
            materials[i].update(g);
            g.glPopMatrix();
        }
        g.glPopMatrix();
        
        printMessage(g, new String[] {
            camera.toString(),
            "Cube1: " + cube1.toString(),
            "Cube2: " + cube2.toString(),
        });
    }

OpenGLɂ͍W[h‚AglMatrixModeŐ؂ւȂ `s܂B _̐ݒA2D̕\sꍇAGL_PROJECTIONgA 3D`悷ƂGL_MODELVIEWg܂B glMatrixModeŕΐAglLoadIdentityŏ sĂ܂B

glPushMatrix sƁǍɍsꂽWnϊ _̑Ȃǂ glPopMatrix sƂ NA邱Ƃo܂B cubê悤ȕʃIuWFNgɕ`ϏꍇȂǁA OglPushMatrix glPopMatrix ň͂ނƂ Wnێ悤ɂĂ܂B

JIuWFNg

Camera NXł́A_̈ʒu(this)AڕẄʒu(target)A p(viewAngle)Aʂ̃AXyNg(aspectRatio)ێĂ܂B p͊px(digree)łA0xɋ߂ÂƋȂ̂ŖڕW͉ʂ̒ő傫A 180xɋ߂ÂƍLȂ邽ߖڕW͉ʂ̒ŏ悤ɂȂ܂B

p

ȉupdate\bhłB

    public void update(GLGraphics g) {
        g.gluPerspective(viewAngle, aspectRatio, 1.0d, 1600.0d);
        g.gluLookAt(
            getX(), getY(), getZ(),
            target.getX(), target.getY(), target.getZ(),
            0, 0, 1);
    }

gluPerspective ŎpƑOŌ͈͂ݒ肵Ă܂B gluLookAt Ŏ_ƖڕWAZɂ悤ɐݒ肵Ă܂B

̃IuWFNg

Cube NX͔̂łB ̂ɂ8‚̒_܂Bgĕ\ʂ6‚̖ʂA ̂ɂ܂B ȉupdate \bhłB

    private double width = 2d;
    
    private int[][] faceList = {
        {0, 3, 2, 1},
        {1, 2, 6, 5},
        {5, 6, 7, 4},
        {4, 7, 3, 0},
        {4, 0, 1, 5},
        {3, 7, 6, 2},
    };
    
    public void update(GLGraphics g) {
        double[][] vertexList = {
            {getX() - width, getY() - width, getZ() - width},
            {getX() + width, getY() - width, getZ() - width},
            {getX() + width, getY() + width, getZ() - width},
            {getX() - width, getY() + width, getZ() - width},
            {getX() - width, getY() - width, getZ() + width},
            {getX() + width, getY() - width, getZ() + width},
            {getX() + width, getY() + width, getZ() + width},
            {getX() - width, getY() + width, getZ() + width},
        };
        
        g.glColor3f(1.0f, 1.0f, 1.0f);
        for (int i = 0; i < faceList.length; i++) {
            int[] face = faceList[i];
            g.glBegin(GLConstants.GL_LINE_LOOP);
            for (int j = 0; j < face.length; j++) {
                int vertexIndex = face[j];
                g.glVertex3dv(vertexList[vertexIndex]);
            }
            g.glEnd();
        }
    }

vertexList ͗̂̒_(x, y z)̃XgłB faceList ͗̂̎lp\ʂ̃XgŁA l͒_vertexListł̃CfbNXɂȂĂ܂B GL_LINE_LOOP ͎XƓ_ԐĂ߂ŁA ŏƍŌ̓_Ԑ`܂B ł͗̂1ʂ̉萳``Ă܂B e_glVertex3dv ({x,y,z})őłĂ܂B

obt@g̕\

printMessage \bhł̓obt@ɕ ݁Ãobt@ʂɏł܂B

    private void printMessage(GLGraphics g, String[] messages) {
        Graphics2D g2d = image.getBufferedImage().createGraphics();
        g2d.setTransform(new AffineTransform(1, 0, 0, -1, 0, height));
        
        g2d.setBackground(new Color(0, 0, 0, 0));
        g2d.clearRect(0, 0, width, height);
        
        g2d.setFont(g2d.getFont().deriveFont(Font.PLAIN, (float) fontSize));
        g2d.setColor(Color.blue);
        int margin = fontSize / 2;
        int x = margin;
        int y = height - (fontSize + margin) * (messages.length - 1) - margin;
        for (int i = 0; i < messages.length; i++) {
            g2d.drawString(messages[i], x, y);
            y += fontSize + margin;
        }
        
        g.glPushMatrix();
        g.glMatrixMode(GLConstants.GL_PROJECTION);
        g.glLoadIdentity();
        image.draw(g);
        g.glPopMatrix();
    }

setTransformŐݒ肵Ă̂̓obt@̍WnOpenGL̍Wnƍ悤 ϊĂ܂B‚܂Ax = xAy = height - yɂϊłB setBackgroundŔwiFァ芳w閧オド フの硝のバbt@゜で ネ降ヘ別ava2Dナ描謔キフと捺うノ描謔オド

obt@ノ杖だ窟 image.draw(g) トぶとで、ハ硝バbt@ブ`謔オワ

SystemFontgフ表

Java2DフクXg墲クノ僻でま Move2.java ナはの鼻gド

onix.gfx.font.SystemFont gワ ナcode>Move2̃RXgN^̒ŁAAWT̃tHgIuWFNg nč쐬Ă܂B

        font = new SystemFont(new Font("System", Font.PLAIN, fontSize));

printMessage͈ȉ̂悤ɂȂ܂B ȊOinitGraphicsAupdate\bh MoveƑSR[hłB

    private void printMessage(GLGraphics g, String[] messages) {
        g.glPushMatrix();
        g.glMatrixMode(GLConstants.GL_PROJECTION);
        g.glLoadIdentity();
        g.gluOrtho2D(0, width, 0, height);
        g.glScaled(1, -1, 1);
        g.glTranslated(0, -height, 0);
        
        g.glColor3d(0.0, 0.0, 1.0);
        int margin = fontSize / 2;
        int x = margin;
        int y = height - (margin + fontSize) * (messages.length - 1) - margin;
        g.glEnable(GLConstants.GL_TEXTURE_2D);
        for (int i = 0; i < messages.length; i++) {
            font.draw(messages[i], x, y, g);
            y += fontSize + margin;
        }
        g.glDisable(GLConstants.GL_TEXTURE_2D);
        g.glPopMatrix();
    }

gluOrtho2DŕʓeA glScaledAglTranslated Wnオ(0,0)ɂȂ悤ɕϊĂ܂B

SystemFontdraw\bhŕ`悳܂B draw\bh̑OGL_TEXTURE_2DLɂA ŖɂKv܂B




Copyright © 2001-2002 CyberStep, Inc. All Rights Reserved.

Oni \tgEFA