How can one load an OBJ file?

Started by elias4444, January 27, 2005, 00:13:05

Previous topic - Next topic

napier

I used code from this tutorial:  http://www.xmission.com/~nate/smooth.html
to smooth models.  Worked well, and gave me some ideas about how to handle smoothing.  

This code doesn't do groups, it smooths the entire model and preserves sharp edges (ie. 90 degree edges), but I suppose you could smooth sets of verts instead of the whole model.
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl

elias4444

Smoothing is really just a trick with the normals. I've noticed that with different objects, artists like to use different smoothing techniques for different parts of it. What I do now is just edit my objects in something like poseray first, and then use the object loader in this thread (well, ok, I actually use an updated one). That way, I can get different vertex normals for each vertice if I want to.
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

Achilleterzo

Hi all, i'm glad to talk with you.
I have some "cathodic" how-to:

1) Enabling textures into the above elia's class.
2) Using bump or more complex maps like normal.

Many and in advance, thanks.

the GLModel class (very ugly, uncommented code):

package Engine;

import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.lwjgl.opengl.GL11;

public class GLModel
{

    private ArrayList vertexsets;
    private ArrayList vertexsetsnorms;
    private ArrayList vertexsetstexs;
    private ArrayList faces;
    private ArrayList facestexs;
    private ArrayList facesnorms;
    private int objectlist;
    private int numpolys;
    public float toppoint;
    public float bottompoint;
    public float leftpoint;
    public float rightpoint;
    public float farpoint;
    public float nearpoint;

    public GLModel(BufferedReader ref, boolean centerit)
    {
        vertexsets = new ArrayList();
        vertexsetsnorms = new ArrayList();
        vertexsetstexs = new ArrayList();
        faces = new ArrayList();
        facestexs = new ArrayList();
        facesnorms = new ArrayList();
        numpolys = 0;
        toppoint = 0.0F;
        bottompoint = 0.0F;
        leftpoint = 0.0F;
        rightpoint = 0.0F;
        farpoint = 0.0F;
        nearpoint = 0.0F;
        loadobject(ref);
        if(centerit)
            centerit();
        opengldrawtolist();
        numpolys = faces.size();
        cleanup();
    }

    private void cleanup()
    {
        vertexsets.clear();
        vertexsetsnorms.clear();
        vertexsetstexs.clear();
        faces.clear();
        facestexs.clear();
        facesnorms.clear();
    }

    private void loadobject(BufferedReader br)
    {
        try
        {
            boolean firstpass = true;
            String newline;
            while((newline = br.readLine()) != null) 
                if(newline.length() > 0)
                {
                    newline = newline.trim();
                    if(newline.startsWith("v "))
                    {
                        float coords[] = new float[4];
                        String coordstext[] = new String[4];
                        newline = newline.substring(2, newline.length());
                        StringTokenizer st = new StringTokenizer(newline, " ");
                        for(int i = 0; st.hasMoreTokens(); i++)
                            coords[i] = Float.parseFloat(st.nextToken());

                        if(firstpass)
                        {
                            rightpoint = coords[0];
                            leftpoint = coords[0];
                            toppoint = coords[1];
                            bottompoint = coords[1];
                            nearpoint = coords[2];
                            farpoint = coords[2];
                            firstpass = false;
                        }
                        if(coords[0] > rightpoint)
                            rightpoint = coords[0];
                        if(coords[0] < leftpoint)
                            leftpoint = coords[0];
                        if(coords[1] > toppoint)
                            toppoint = coords[1];
                        if(coords[1] < bottompoint)
                            bottompoint = coords[1];
                        if(coords[2] > nearpoint)
                            nearpoint = coords[2];
                        if(coords[2] < farpoint)
                            farpoint = coords[2];
                        vertexsets.add(coords);
                    } else
                    if(newline.startsWith("vt"))
                    {
                        float coords[] = new float[4];
                        String coordstext[] = new String[4];
                        newline = newline.substring(3, newline.length());
                        StringTokenizer st = new StringTokenizer(newline, " ");
                        for(int i = 0; st.hasMoreTokens(); i++)
                            coords[i] = Float.parseFloat(st.nextToken());

                        vertexsetstexs.add(coords);
                    } else
                    if(newline.startsWith("vn"))
                    {
                        float coords[] = new float[4];
                        String coordstext[] = new String[4];
                        newline = newline.substring(3, newline.length());
                        StringTokenizer st = new StringTokenizer(newline, " ");
                        for(int i = 0; st.hasMoreTokens(); i++)
                            coords[i] = Float.parseFloat(st.nextToken());

                        vertexsetsnorms.add(coords);
                    } else
                    if(newline.startsWith("f "))
                    {
                        newline = newline.substring(2, newline.length());
                        StringTokenizer st = new StringTokenizer(newline, " ");
                        int count = st.countTokens();
                        int v[] = new int[count];
                        int vt[] = new int[count];
                        int vn[] = new int[count];
                        for(int i = 0; i < count; i++)
                        {
                            char chars[] = st.nextToken().toCharArray();
                            StringBuffer sb = new StringBuffer();
                            char lc = 'x';
                            for(int k = 0; k < chars.length; k++)
                            {
                                if(chars[k] == '/' && lc == '/')
                                    sb.append('0');
                                lc = chars[k];
                                sb.append(lc);
                            }

                            StringTokenizer st2 = new StringTokenizer(sb.toString(), "/");
                            int num = st2.countTokens();
                            v[i] = Integer.parseInt(st2.nextToken());
                            if(num > 1)
                                vt[i] = Integer.parseInt(st2.nextToken());
                            else
                                vt[i] = 0;
                            if(num > 2)
                                vn[i] = Integer.parseInt(st2.nextToken());
                            else
                                vn[i] = 0;
                        }

                        faces.add(v);
                        facestexs.add(vt);
                        facesnorms.add(vn);
                    }
                }
        }
        catch(IOException e)
        {
            System.out.println("Failed to read file: " + br.toString());
        }
        catch(NumberFormatException e)
        {
            System.out.println("Malformed OBJ file: " + br.toString() + "\r \r" + e.getMessage());
        }
    }

    private void centerit()
    {
        float xshift = (rightpoint - leftpoint) / 2.0F;
        float yshift = (toppoint - bottompoint) / 2.0F;
        float zshift = (nearpoint - farpoint) / 2.0F;
        for(int i = 0; i < vertexsets.size(); i++)
        {
            float coords[] = new float[4];
            coords[0] = ((float[])vertexsets.get(i))[0] - leftpoint - xshift;
            coords[1] = ((float[])vertexsets.get(i))[1] - bottompoint - yshift;
            coords[2] = ((float[])vertexsets.get(i))[2] - farpoint - zshift;
            vertexsets.set(i, coords);
        }

    }

    public float getXWidth()
    {
        float returnval = 0.0F;
        returnval = rightpoint - leftpoint;
        return returnval;
    }

    public float getYHeight()
    {
        float returnval = 0.0F;
        returnval = toppoint - bottompoint;
        return returnval;
    }

    public float getZDepth()
    {
        float returnval = 0.0F;
        returnval = nearpoint - farpoint;
        return returnval;
    }

    public int numpolygons()
    {
        return numpolys;
    }

    public void opengldrawtolist()
    {
        objectlist = GL11.glGenLists(1);
        GL11.glNewList(objectlist, 4864);
        for(int i = 0; i < faces.size(); i++)
        {
            int tempfaces[] = (int[])faces.get(i);
            int tempfacesnorms[] = (int[])facesnorms.get(i);
            int tempfacestexs[] = (int[])facestexs.get(i);
            int polytype;
            if(tempfaces.length == 3)
                polytype = 4;
            else
            if(tempfaces.length == 4)
                polytype = 7;
            else
                polytype = 9;
            GL11.glBegin(polytype);
            for(int w = 0; w < tempfaces.length; w++)
            {
                if(tempfacesnorms[w] != 0)
                {
                    float normtempx = ((float[])vertexsetsnorms.get(tempfacesnorms[w] - 1))[0];
                    float normtempy = ((float[])vertexsetsnorms.get(tempfacesnorms[w] - 1))[1];
                    float normtempz = ((float[])vertexsetsnorms.get(tempfacesnorms[w] - 1))[2];
                    GL11.glNormal3f(normtempx, normtempy, normtempz);
                }
                if(tempfacestexs[w] != 0)
                {
                    float textempx = ((float[])vertexsetstexs.get(tempfacestexs[w] - 1))[0];
                    float textempy = ((float[])vertexsetstexs.get(tempfacestexs[w] - 1))[1];
                    float textempz = ((float[])vertexsetstexs.get(tempfacestexs[w] - 1))[2];
                    GL11.glTexCoord3f(textempx, 1.0F - textempy, textempz);
                }
                float tempx = ((float[])vertexsets.get(tempfaces[w] - 1))[0];
                float tempy = ((float[])vertexsets.get(tempfaces[w] - 1))[1];
                float tempz = ((float[])vertexsets.get(tempfaces[w] - 1))[2];
                GL11.glVertex3f(tempx, tempy, tempz);
            }

            GL11.glEnd();
        }
        GL11.glEndList();
    }

    public void opengldraw()
    {
        GL11.glCallList(objectlist);
    }
}

Achilleterzo

Main class init and render section:

   private void init() throws Exception
    {
        display = new EngineDisplay();
        display.createWindow(false, "Game");
        display.initGL();
        IL.create();
        input = new EngineInput();
        input.initInput(display.displayMode);
        
        camera = new GLCamera();
        camera.setCameraPosition();
        
        String path = "models/p51_mustang.obj";
        try
        {
            FileInputStream r_path = new FileInputStream(path);
            BufferedReader b_read = new BufferedReader(new InputStreamReader(r_path));
            modello = new GLModel(b_read, true);
            r_path.close();
            b_read.close();
        }
        catch(IOException e)
        {
            System.out.println("Could not open file: " + path);
        }
        
        textures = new GLTextures();
        textures.loadTextures();
        
        light = new GLLight();
        light.setLight();
    }

    private boolean render()
    {
        GL11.glClear(16640);
        GL11.glEnable(3553);
        GL11.glShadeModel(7425);
        GL11.glClearColor(0.0F, 0.0F, 0.0F, 0.0F);
        GL11.glClearDepth(1.0D);
        GL11.glEnable(2929);
        GL11.glDepthFunc(515);
        GL11.glMatrixMode(5888);
        GL11.glLoadIdentity();
        GL11.glTranslatef(0.0F, 0.0F, z);
        GL11.glRotatef(xrot, 1.0F, 0.0F, 0.0F);
        GL11.glRotatef(yrot, 0.0F, 1.0F, 0.0F);
        GL11.glRotatef(360F - camera.cameraRotation, 0.0F, 1.0F, 0.0F);
        GLCamera _tmp = camera;
        GLCamera _tmp1 = camera;
        GLCamera _tmp2 = camera;
        GL11.glTranslatef(-GLCamera.cameraPos[0], -GLCamera.cameraPos[1], -GLCamera.cameraPos[2]);
        
        // Qui renderizzo il piano:
        GL11.glPushMatrix();
        GL11.glTranslatef(0.0F, -2.1F, 0.0F);
        GL11.glScalef(10F, 0.01F, 10F);
        GL11.glBindTexture(3553, textures.texture[filter]);
        renderCube();
        GL11.glPopMatrix();

        //Qui faccio il rendering dell'oggetto
        GL11.glPushMatrix();
        GL11.glBindTexture(3553, textures.texture[filter]);
        modello.opengldraw();
        GL11.glPopMatrix();
        
        xrot += xspeed;
        yrot += yspeed;
        return true;
    }

duodecimo

I came in just now, and only want to thank Elias for this very nice code.

guillaumesmo

Could someone here please reply to the topic I posted here :

http://lwjgl.org/forum/index.php/topic,2417.0.html

Thank you!

elias4444

For those interested, I went ahead and updated the sample jar with my newest incarnation of the OBJ loader and TrueType Font Renderer.

www.tommytwisters.com/texturetester.jar
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

tronador

I tried to load objects using the loader but i get the message:

"Your system is not capable of running this game. Please make sure your video drivers are current."

What's wrong?? How i can solve this problem??  :( :( :( :( :(

Any other alternative to load OBJ or any other kind of models in lwjgl ???


Best regards

Matzon

Quote from: tronador on September 24, 2007, 02:53:42
"Your system is not capable of running this game. Please make sure your video drivers are current."

What's wrong?? How i can solve this problem??  :( :( :( :( :(
I'm guessing that you could try and make your video drivers current  ::)

elias4444

What kind of video card do you have?
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

tronador

Is a  MSI  nVidia 6200 256MB, i download the last drivers but dont works :(

elias4444

Strange, I've never seen a problem with an Nvidia card before. What OS are you running on? (Please include version, etc..)

=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

tronador

Quote from: elias4444 on September 26, 2007, 15:44:24
Strange, I've never seen a problem with an Nvidia card before. What OS are you running on? (Please include version, etc..)



Windows XP SP2 [Versión 5.1.2600] (Spanish)
I Have last version of LWJGL

Matzon

do any of the other lwjgl demoes work ?

tronador