Package ponkOut.graphics

Source Code of ponkOut.graphics.ItemGO

/* Copyright 2013 Christian Matt
*
* This file is part of PonkOut.
*
* PonkOut is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PonkOut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PonkOut.  If not, see <http://www.gnu.org/licenses/>.
*/

package ponkOut.graphics;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;

import ponkOut.logic.Item;

public class ItemGO extends WorldObject {
  private Item item;
  private TexturedMaterial imageMaterial;
  private int imageDisplayList;
  private int frameDisplayList;

  public ItemGO(Item item, int id, float width, float height, GraphicsObjectsManager goManager) {
    super(new Material(0.1f, 0.2f, 1.0f, 0.3f, 0.3f, 0.4f, 0.2f, 0.2f, 0.2f, 0.5f, 10.0f), goManager);

    this.item = item;
    imageMaterial = new TexturedMaterial("items.tga", 1.0f, 1.0f, 1.0f, 0.2f, 0.2f, 0.2f, 0.0f, 0.0f, 0.0f, 0.9f,
        128.0f);

    float left = -width / 2.0f;
    float right = width / 2.0f;
    float top = height / 2.0f;
    float bottom = -height / 2.0f;
    float front = -Board.surfaceZPosition / 3.0f;
    float back = Board.surfaceZPosition / 3.0f;

    float texX0 = (id % 8) * 0.125f;
    float texX1 = texX0 + 0.125f;
    float texY0 = 1.0f - (id / 8 + 1) * 0.125f;
    float texY1 = texY0 + 0.125f;

    // create display list for image inside
    imageDisplayList = GL11.glGenLists(1);
    GL11.glNewList(imageDisplayList, GL11.GL_COMPILE);
    GL11.glBegin(GL11.GL_QUADS);

    // front
    GL11.glNormal3f(0.0f, 0.0f, 1.0f);

    // bottom left
    GL11.glTexCoord2f(texX0, texY0);
    GL11.glVertex3f(-width / 2.5f, -height / 2.5f, 0.0f);

    // bottom right
    GL11.glTexCoord2f(texX1, texY0);
    GL11.glVertex3f(width / 2.5f, -height / 2.5f, 0.0f);

    // top right
    GL11.glTexCoord2f(texX1, texY1);
    GL11.glVertex3f(width / 2.5f, height / 2.5f, 0.0f);

    // top left
    GL11.glTexCoord2f(texX0, texY1);
    GL11.glVertex3f(-width / 2.5f, height / 2.5f, 0.0f);

    // back
    GL11.glNormal3f(0.0f, 0.0f, -1.0f);

    // bottom left
    GL11.glTexCoord2f(texX0, texY0);
    GL11.glVertex3f(-width / 2.5f, -height / 2.5f, 0.0f);

    // top left
    GL11.glTexCoord2f(texX0, texY1);
    GL11.glVertex3f(-width / 2.5f, height / 2.5f, 0.0f);

    // top right
    GL11.glTexCoord2f(texX1, texY1);
    GL11.glVertex3f(width / 2.5f, height / 2.5f, 0.0f);

    // bottom right
    GL11.glTexCoord2f(texX1, texY0);
    GL11.glVertex3f(width / 2.5f, -height / 2.5f, 0.0f);

    GL11.glEnd(); // GL_QUADS
    GL11.glEndList();

    // create display list for frame
    frameDisplayList = GL11.glGenLists(1);
    GL11.glNewList(frameDisplayList, GL11.GL_COMPILE);
    GL11.glBegin(GL11.GL_QUADS);

    // front
    GL11.glNormal3f(0.0f, 0.0f, 1.0f);
    GL11.glVertex3f(left, bottom, front);
    GL11.glVertex3f(right, bottom, front);
    GL11.glVertex3f(right, top, front);
    GL11.glVertex3f(left, top, front);

    // back
    GL11.glNormal3f(0.0f, 0.0f, -1.0f);
    GL11.glVertex3f(left, bottom, back);
    GL11.glVertex3f(left, top, back);
    GL11.glVertex3f(right, top, back);
    GL11.glVertex3f(right, bottom, back);

    // right
    GL11.glNormal3f(1.0f, 0.0f, 0.0f);
    GL11.glVertex3f(right, bottom, back);
    GL11.glVertex3f(right, top, back);
    GL11.glVertex3f(right, top, front);
    GL11.glVertex3f(right, bottom, front);

    // left
    GL11.glNormal3f(-1.0f, 0.0f, 0.0f);
    GL11.glVertex3f(left, bottom, back);
    GL11.glVertex3f(left, bottom, front);
    GL11.glVertex3f(left, top, front);
    GL11.glVertex3f(left, top, back);

    // top
    GL11.glNormal3f(0.0f, 1.0f, 0.0f);
    GL11.glVertex3f(left, top, back);
    GL11.glVertex3f(left, top, front);
    GL11.glVertex3f(right, top, front);
    GL11.glVertex3f(right, top, back);

    // bottom
    GL11.glNormal3f(0.0f, -1.0f, 0.0f);
    GL11.glVertex3f(left, bottom, back);
    GL11.glVertex3f(right, bottom, back);
    GL11.glVertex3f(right, bottom, front);
    GL11.glVertex3f(left, bottom, front);

    GL11.glEnd(); // GL_QUADS

    GL11.glEndList();
  }

  @Override
  public void draw() {
    // store current transformation matrix
    GL11.glPushMatrix();

    // translate to item position
    Vector2f pos = item.getPosition();
    GL11.glTranslatef(pos.x, pos.y, 0.0f);

    // enable lighting with texture
    Lighting.enable(true);

    // draw image inside
    imageMaterial.use();
    GL11.glCallList(imageDisplayList);

    // disable lighting
    Lighting.disable();

    // enable lighting without texture
    Lighting.enable(false);

    // draw frame
    appearance.use();
    GL11.glCallList(frameDisplayList);

    // disable lighting
    Lighting.disable();

    // restore transformation matrix
    GL11.glPopMatrix();
  }

  @Override
  public Vector3f getPosition() {
    Vector2f pos = item.getPosition();
    return new Vector3f(pos.x, pos.y, 0.0f);
  }
}
TOP

Related Classes of ponkOut.graphics.ItemGO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.