Newer
Older
JumpingGame / src / main / java / entities / Image2D.java
k-fujii on 16 Nov 2021 3 KB 地面を仮描画しました.
package entities;

import static org.lwjgl.opengl.GL11.*;

//---------------------------------------------------------------
//
public class Image2D {

    private int id; // テクスチャのID
    private Pair<Double> wh; // スプライトの幅高
    private Pair<Double> position; // スプライトの座標(画面座標)
    private Color color; // スプライトの色
    private double rotation; // 回転(度)
    private double scale; // 拡大
    private double alpha; // 透明度

    //---------------------------------------------------------------
    //---------------------------------------------------------------
    //
    public Image2D(Texture tex) {
        if (tex != null)
            this.id = tex.getId();
        else
            this.id = 0;

        this.position = new Pair<>(0.0, 0.0);
        this.color = new Color(1, 1, 1, 1);

        if (tex != null)
            wh = new Pair<>((double) tex.getWidth(), (double) tex.getHeight());
        else
            wh = new Pair<>(32d, 20d);
        rotation = 0.0;
        scale = 1.0;
        alpha = 1.0;
    }

    //---------------------------------------------------------------
    //---------------------------------------------------------------
    // setter
    public void setRotation(double rotation) {
        this.rotation = rotation;
    }

    public void setPosition(Pair<Double> position) {
        this.position = position;
    }

    public void setScale(double scale) {
        this.scale = scale;
    }

    //---------------------------------------------------------------
    //---------------------------------------------------------------
    //
    public void draw() {
        // テクスチャの結合
        glBindTexture(GL_TEXTURE_2D, id);

        // 変換行列の追加
        glPushMatrix();

        // モデルビューモード
        glMatrixMode(GL_MODELVIEW);
        // 行列の設定
        glLoadIdentity(); // 単位行列化
        glTranslated(position.getFirst(), position.getSecond(), 0); // 移動
        glRotated(rotation, 0, 0, 1); // 回転
        glScaled(scale, scale, 1); // 拡縮

        // プロジェクションモード
        glMatrixMode(GL_PROJECTION);
        // 行列の設定
        glLoadIdentity(); // 単位行列化
        glOrtho(0, WindowConfig.WIDTH, 0, WindowConfig.HEIGHT, -WindowConfig.DEPTH, WindowConfig.DEPTH); // 正射影投影

        // ビューポートの範囲
        glViewport(0, 0, WindowConfig.WIDTH, WindowConfig.HEIGHT);

        // ポリゴンの色
        glColor4d(color.getR(), color.getG(), color.getB(), alpha);

        // ポリゴンの作成とテクスチャの適応
        glBegin(GL_TRIANGLE_STRIP);
        glTexCoord2d(0, 0);
        glVertex2d(-wh.getFirst() / 2, wh.getSecond() / 2);
        glTexCoord2d(0, 1);
        glVertex2d(-wh.getFirst() / 2, -wh.getSecond() / 2);
        glTexCoord2d(1, 0);
        glVertex2d(wh.getFirst() / 2, wh.getSecond() / 2);
        glTexCoord2d(1, 1);
        glVertex2d(wh.getFirst() / 2, -wh.getSecond() / 2);
        glEnd();

        // 行列の破棄
        glPopMatrix();

        // テクスチャの解除
        glBindTexture(GL_TEXTURE_2D, 0);
    }

    //---------------------------------------------------------------
}