Jump to content
  • Advertisement
Sign in to follow this  
Skyzyx

Loading a Simple Cube from an .obj File (Opengl ES 2.0)

This topic is 930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello!

 

I am trying to load a simple cube from file of type .obj. Something is going wrong but I can't seem to figure out what really because my vertices and the faces/indices for my vertices seems to be fine.

 

Hopefully somebody here will find the mistake that I am making. I will be posting all the relevant code below. You can assume that I have uploaded the information from the file correctly. I can post that code as well if somebody want to take a look but otherwise I don't want to upload unnecessary code. Also you don't have to worry about anything else but the vertices and the faces for my vertices. Don't bother with the texture coordinates, color, normals and so on.

 

Here are some pictures of how it looks right now. imgur.com/a/rZ6ap

 

Regards!

public class Mesh
{
    private boolean vertexHasColor = false;
    private boolean vertexHasTexture = false;
    private boolean vertexHasNormal = false;
    private ShortBuffer xyzFBuffer;
    private FloatBuffer xyzBuffer;
    private ShortBuffer uvFBuffer;
    private FloatBuffer uvBuffer;
    private FloatBuffer colorBuffer;
    private int h_TextureData;
    private int NUMBER_OF_XYZF;
    private int NUMBER_OF_UVF;
    private int NUMBER_OF_XYZ;
    private int NUMBER_OF_UV;
    private float [] TextureCoordinates;

    public Mesh(MeshData meshData, int h_TextureData)
    {
        this(meshData.xyz, meshData.xyzF, meshData.uv, meshData.uvF, null, h_TextureData, null);
    }


    /**
     * If the mesh has no color/normal/texture then pass in any negative
     * number as Offset. xyz contain the vertices. xyzF contains the faces.
     */
    public Mesh(ArrayList<XMFLOAT3> xyz, ArrayList<Short> xyzF, ArrayList<XMFLOAT2> uv,
                ArrayList<Short> uvF, float[] color, int h_TextureData, float[] normal)
    {
        NUMBER_OF_XYZ = xyz.size() * 3;
        NUMBER_OF_XYZF = xyzF.size();
        if(color != null)
        {
            vertexHasColor = true;
        }
        if(uv != null)
        {
            vertexHasTexture = true;
            NUMBER_OF_UV = uv.size() * 2;
            NUMBER_OF_UVF = uvF.size();
        }
        if(normal != null) { vertexHasNormal = true; }

        this.h_TextureData = h_TextureData;

        ByteBuffer vbb = ByteBuffer.allocateDirect(NUMBER_OF_XYZ * 4);
        vbb.order(ByteOrder.nativeOrder());
        xyzBuffer = vbb.asFloatBuffer();
        xyzBuffer.put(XMFLOAT3.ConvertToArray(xyz));
        xyzBuffer.position(0);

        ByteBuffer ibb = ByteBuffer.allocateDirect(NUMBER_OF_XYZF * 2);
        ibb.order(ByteOrder.nativeOrder());
        xyzFBuffer = ibb.asShortBuffer();
        xyzFBuffer.put(ConvertToArray.getArray(xyzF));
        xyzFBuffer.position(0);

        if(color != null) {
            ByteBuffer colorbb = ByteBuffer.allocateDirect(color.length * 4);
            colorbb.order(ByteOrder.nativeOrder());
            colorBuffer = colorbb.asFloatBuffer();
            colorBuffer.put(color);
            colorBuffer.position(0);
        }

        if(uv != null)
        {
            StoreTextureCoordinates(uv, uvF);
            ByteBuffer texturebb = ByteBuffer.allocateDirect(NUMBER_OF_UV * 4);
            texturebb.order(ByteOrder.nativeOrder());
            uvBuffer = texturebb.asFloatBuffer();
            uvBuffer.put(TextureCoordinates);
            uvBuffer.position(0);
        }
    }

    public void StoreTextureCoordinates(ArrayList<XMFLOAT2> uv, ArrayList<Short> uvF)
    {
        TextureCoordinates = new float[NUMBER_OF_UVF * 2];
        int index = 0;
        for(int a = 0; a < NUMBER_OF_UVF; a++)
        {
            TextureCoordinates[index] = uv.get(uvF.get(a)).u;
            TextureCoordinates[index + 1] = uv.get(uvF.get(a)).v;
            index += 2;
        }
        NUMBER_OF_UV = TextureCoordinates.length;
    }

    public void ClearScreen()
    {
        GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    }

    public void SetupMesh()
    {
        xyzBuffer.position(0);
        GLES20.glVertexAttribPointer(Shaders.h_pos, 3, GLES20.GL_FLOAT, false, 0, xyzBuffer);
        GLES20.glEnableVertexAttribArray(Shaders.h_pos);

        if(vertexHasColor)
        {
            xyzBuffer.position(0);
            GLES20.glVertexAttribPointer(Shaders.h_col, 4, GLES20.GL_FLOAT, false, 0, colorBuffer);
            GLES20.glEnableVertexAttribArray(Shaders.h_col);
        }

        if(vertexHasTexture)
        {
            GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, h_TextureData);
            GLES20.glUniform1i(Shaders.h_uTexture, 0);
            uvBuffer.position(0);
            GLES20.glVertexAttribPointer(Shaders.h_tex, 2, GLES20.GL_FLOAT, false, 0, uvBuffer);
            GLES20.glEnableVertexAttribArray(Shaders.h_tex);
        }

        if(vertexHasNormal)
        {
        }
    }

    public void DrawMesh()
    {
        GLES20.glDrawElements(GLES20.GL_LINES, NUMBER_OF_XYZF, GLES20.GL_UNSIGNED_SHORT, xyzFBuffer);
    }
}
public class XMFLOAT3
{
    public float x;
    public float y;
    public float z;

    public static float[] ConvertToArray(ArrayList<XMFLOAT3> XM3ArrL)
    {
        float returnArray[] = new float[XM3ArrL.size() * 3];
        int XM3ArrLCounter = 0;
        for(int a = 0; a < returnArray.length; a += 3)
        {
            returnArray[a] = XM3ArrL.get(XM3ArrLCounter).x;
            returnArray[a + 1] = XM3ArrL.get(XM3ArrLCounter).y;
            returnArray[a + 2] = XM3ArrL.get(XM3ArrLCounter).z;
            XM3ArrLCounter++;
        }
        return returnArray;
    }
}
public class XMFLOAT2
{
    public float u;
    public float v;

    public static float[] ConvertToArray(ArrayList<XMFLOAT2> XM2ArrL)
    {
        float returnArray[] = new float[XM2ArrL.size() * 2];
        int XM2ArrLCounter = 0;
        for(int a = 0; a < returnArray.length; a += 2)
        {
            returnArray[a] = XM2ArrL.get(XM2ArrLCounter).u;
            returnArray[a + 1] = XM2ArrL.get(XM2ArrLCounter).v;
            XM2ArrLCounter++;
        }
        return returnArray;
    }
}
public class MeshData
{
    public ArrayList<XMFLOAT3> xyz;
    public ArrayList<XMFLOAT2> uv;
    public ArrayList<Short> uvF;
    public ArrayList<Short> xyzF;
    public MeshData()
    {
        xyz = new ArrayList<>();
        uv = new ArrayList<>();
        uvF = new ArrayList<>();
        xyzF = new ArrayList<>();
    }
}
//THE CUBE FILE

v 0.0 0.0 0.0
v 0.0 0.0 1.0
v 0.0 1.0 0.0
v 0.0 1.0 1.0
v 1.0 0.0 0.0
v 1.0 0.0 1.0
v 1.0 1.0 0.0
v 1.0 1.0 1.0
 
vt 0.0 0.0
vt 0.0 1.0
vt 1.0 0.0
vt 1.0 1.0

f 1/4/2 7/1/2 5/2/2
f 1/4/2 3/3/2 7/1/2
f 1/2/6 4/3/6 3/1/6
f 1/2/6 2/4/6 4/3/6
f 3/1/3 8/4/3 7/3/3
f 3/1/3 4/2/3 8/4/3
f 5/4/5 7/3/5 8/1/5
f 5/4/5 8/1/5 6/2/5
f 1/2/4 5/4/4 6/3/4
f 1/2/4 6/3/4 2/1/4
f 2/2/1 6/4/1 8/3/1
f 2/2/1 8/3/1 4/1/1
Edited by Skyzyx

Share this post


Link to post
Share on other sites
Advertisement

You store your data in an array. Did you also take into account that the indices in the obj file format starts at 1 and in arrays it starts at 0?

Share this post


Link to post
Share on other sites

You store your data in an array. Did you also take into account that the indices in the obj file format starts at 1 and in arrays it starts at 0?

Yes I did, I know that the faces need to be decremented by 1 because the indicies in the arrays start at 0.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!