How to get Button inside 3Dmodel?

Started by harish, February 18, 2016, 10:22:33

Previous topic - Next topic

harish

Hello..
   I am new to LWJGL.I have loaded a 3D model(house like structure) using the code given below.Now, I need to get a button inside the model so that the user can enter into the model and click the button.
   Please provide me some solution.Thank You.

Main.java

package Model;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;

import java.io.IOException;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.util.vector.Vector3f;

public class Main {

    private Model m;
    private Vector3f location, rotation;

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    private void run() throws IOException {
        try {
            Display.setDisplayMode(new DisplayMode(1400, 1000));
            Display.setTitle("3D interaction");
            Display.setResizable(true);
            Display.setInitialBackground(0f, 0f, 0f);
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            Display.destroy();
            System.exit(1);
        }

        location = new Vector3f(-1.0f, -5.0f, -70.0f);
        rotation = new Vector3f(0.0f, 0.0f, 0.0f);
        try {
            m = Model.getModel("res/Model.obj");
        } catch (Exception e){
            e.printStackTrace();
        }

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glEnable(GL_TEXTURE_2D);
        gluPerspective(30f, (float) (1200/800), 3.0f, 100);
        glMatrixMode(GL_MODELVIEW);
        //glEnable(GL_DEPTH_TEST);

        while(!Display.isCloseRequested()){
            input();
            render();
            Display.update();
            Display.sync(60);
        }

        Display.destroy();
        System.exit(0);
    }

    private void input() {
        boolean up = Keyboard.isKeyDown(Keyboard.KEY_W);
        boolean down = Keyboard.isKeyDown(Keyboard.KEY_S);
        boolean left = Keyboard.isKeyDown(Keyboard.KEY_A);
        boolean right = Keyboard.isKeyDown(Keyboard.KEY_D);
        boolean speedUp = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
        boolean slowDown = Keyboard.isKeyDown(Keyboard.KEY_LCONTROL);
        float walkspeed = 0.15f;

        float mx = Mouse.getDX();
        float my = Mouse.getDY();
        mx *= 0.15f;
        my *= 0.15f;

       if (Mouse.isButtonDown(0)){
            rotation.y += mx;
            if (rotation.y > 360){
                rotation.y -= 360;
            }
            rotation.y += my;
            if (rotation.x > 85){
                rotation.x = 85;
            }
            if (rotation.x < -85){
                rotation.x = -85;
            }           
        }

        if(speedUp && !slowDown){
            walkspeed = 0.25f;
        }
        if(slowDown && !speedUp){
            walkspeed = 0.15f;
        }
        if(up && !down){
            float cz = (float) (walkspeed * 2 * Math.cos(Math.toRadians(rotation.y)));
            float cx = (float) (walkspeed * Math.sin(Math.toRadians(rotation.y)));
            location.z += cz;
            location.x -= cx;
        }
        if(down && !up){
            float cz = (float) (walkspeed * 2 * Math.cos(Math.toRadians(rotation.y)));
            float cx = (float) (walkspeed * Math.sin(Math.toRadians(rotation.y)));
            location.z -= cz;
            location.x += cx;
        }
        if(right && !left){
            float cz = (float) (walkspeed * 4 * Math.cos(Math.toRadians(rotation.y)));
            float cx = (float) (walkspeed * Math.sin(Math.toRadians(rotation.y)));
            rotation.y += cz;
            rotation.x -= cx;
        }
        if(left && !right){
            float cz = (float) (walkspeed * 4 * Math.cos(Math.toRadians(rotation.y)));
            float cx = (float) (walkspeed * Math.sin(Math.toRadians(rotation.y)));
            rotation.y -= cz;
            rotation.x += cx;
        }
    }

    private void render() throws IOException {
        glPushMatrix();
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
       
        //glRotatef(rotation.x, 1f, 0f, 0f);
        glRotatef(rotation.y, 0f, 1f, 0f);
        //glRotatef(rotation.z, 0f, 0f, 1f);
        glTranslatef(location.x, location.y, location.z);
        m.render();
        glPopMatrix();
    }
}

Model.java
package Model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.Color;
import static org.lwjgl.opengl.GL11.*;

public class Model {
   public List<Vector3f> verts;
    public List<Vector3f> norms;
    public List<Face> faces;

    public static Model getModel(String s) throws IOException{
        return new Model(s);
    }

    private Model(String path) throws IOException{
        verts = new ArrayList<Vector3f>();
        norms = new ArrayList<Vector3f>();
        faces = new ArrayList<Face>();
        new ModelLoader(this, path);
    }

    public void render() throws IOException{
//      glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
       Color.gray.bind();
       //TextureLoader.getTexture("JPEG", ResourceLoader.getResourceAsStream("res/i.jpeg"));
        glBegin(GL_TRIANGLES);
        for (Face f : faces){
            Vector3f v1 = verts.get((int) f.verts.x -1);
            Vector3f n1 = verts.get((int) f.norms.x -1);
            Vector3f v2 = verts.get((int) f.verts.y -1);
            Vector3f n2 = verts.get((int) f.norms.y -1);
            Vector3f v3 = verts.get((int) f.verts.z -1);
            Vector3f n3 = verts.get((int) f.norms.z -1);
            glNormal3f(n1.x, n1.y, n1.z);
            glVertex3f(v1.x, v1.y, v1.z);
            glNormal3f(n2.x, n2.y, n2.z);
            glVertex3f(v2.x, v2.y, v2.z);
            glNormal3f(n3.x, n3.y, n3.z);
            glVertex3f(v3.x, v3.y, v3.z);
        }
        glEnd();
    }
}

ModelLoader.java
package Model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


import org.lwjgl.util.vector.Vector3f;

public class ModelLoader {

    public ModelLoader(Model m, String path) throws IOException {
        BufferedReader read = new BufferedReader(new FileReader(new File(path)));
        String line;
        while((line = read.readLine()) != null) {
            if (line.startsWith("v ")){
                float x = Float.valueOf(line.split(" ")[1]);
                float y = Float.valueOf(line.split(" ")[2]);
                float z = Float.valueOf(line.split(" ")[3]);
                Vector3f v = new Vector3f(x, y, z);
                m.verts.add(v);
            } else if (line.startsWith("vm ")) {
                float x = Float.valueOf(line.split(" ")[1]);
                float y = Float.valueOf(line.split(" ")[2]);
                float z = Float.valueOf(line.split(" ")[3]);
                Vector3f v = new Vector3f(x, y, z);
                m.norms.add(v);
            } else if (line.startsWith("f ")) {
                float x1 = Float.valueOf(line.split(" ")[1].split("/")[0]);
                float y1 = Float.valueOf(line.split(" ")[2].split("/")[0]);
                float z1 = Float.valueOf(line.split(" ")[3].split("/")[0]);
                float x2 = Float.valueOf(line.split(" ")[1].split("/")[0]);
                float y2 = Float.valueOf(line.split(" ")[2].split("/")[0]);
                float z2 = Float.valueOf(line.split(" ")[3].split("/")[0]);
                Face f = new Face(new Vector3f(x2, y2, z2), new Vector3f(x1, y1, z1));
                m.faces.add(f);
            }
        }
        read.close();
    }
}

Face.java
package Model;

import org.lwjgl.util.vector.Vector3f;

public class Face {

    public Vector3f norms, verts;

    public Face(Vector3f n, Vector3f v){
        this.norms = n;
        this.verts = v;
    }

}