Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1091207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:29:23+00:00 2026-05-16T23:29:23+00:00

I am new to OpenGL. I am using JOGL. I downloaded this model .

  • 0

I am new to OpenGL. I am using JOGL.

I downloaded this model. It has a bunch of textures. I’m trying to apply “truck_color-blue.jpg” here, but I’m not sure I’m doing it right. This is what I’m seeing:

Side by side: by texture and the site's demo

Essentially, what I am doing is recording all the vt, v, and vn entries, then using the f lines to index into them. This is the code that makes the glTexCoord*() call:

           if (facePoint.textureIndex != null) {
                Vector2f texCoords = getTexture(facePoint.textureIndex);
                gl.glTexCoord2f(texCoords.x, texCoords.y);
            }

            // ...

            Point3f vertex = getVertex(facePoint.vertexIndex);
            gl.glVertex3f(vertex.x, vertex.y, vertex.z);

I suspect that I’m not correctly matching up the texture coords to the vertices. The model says that it is UV mapped. Does that mean I need to do textures differently?

My code to read .obj files:

public class ObjModel extends WorldEntity {

    // ...

    @Override
    protected void buildDrawList() {
        startDrawList();

        for (Polygon poly : polygons) {

            if (poly.getFaces().size() == 4) {
                gl.glBegin(GL2.GL_QUADS);
            } else {
                gl.glBegin(GL2.GL_POLYGON);
            }

            for (FacePoint facePoint : poly.getFaces()) {
                if (facePoint.vertexIndex == null) {
                    throw new RuntimeException("Why was there a face with no vertex?");
                }

                if (facePoint.textureIndex != null) {
                    Vector2f texCoords = getTexture(facePoint.textureIndex);
                    gl.glTexCoord2f(texCoords.x, texCoords.y);
                }

                if (facePoint.normalIndex != null) { 
                    // why use both Vector3f and Point3f?
                    Vector3f normal = getNormal(facePoint.normalIndex);
                    gl.glNormal3f(normal.x, normal.y, normal.z);
                }

                Point3f vertex = getVertex(facePoint.vertexIndex);
                gl.glVertex3f(vertex.x, vertex.y, vertex.z);
            }
            gl.glEnd();
        }

        endDrawList();
    }

    private Point3f getVertex(int index) {
        if (index >= 0) {
            // -1 is necessary b/c .obj is 1 indexed, but vertices is 0 indexed.
            return vertices.get(index - 1);
        } else {
            return vertices.get(vertices.size() + index);
        }
    }

    private Vector3f getNormal(int index) {
        if (index >= 0) {
            return normals.get(index - 1);
        } else {
            return normals.get(normals.size() + index);
        }
    }

    private Vector2f getTexture(int index) {
        if (index >= 0) {
            return textures.get(index - 1);
        } else {
            return textures.get(textures.size() + index);
        }
    }

    private void readFile(String fileName) {
        try {
            BufferedReader input = new BufferedReader(new FileReader(fileName));
            try {
                String currLine = null;
                while ((currLine = input.readLine()) != null) {

                    int indVn = currLine.indexOf("vn ");
                    if (indVn != -1) {
                        readNormal(currLine);
                        continue;
                    }

                    int indV = currLine.indexOf("v ");
                    if (indV != -1) {
                        readVertex(currLine);
                        continue;
                    }

                    int indF = currLine.indexOf("f ");
                    if (indF != -1) {
                        readPolygon(currLine);
                        continue;
                    }

                    int indVt = currLine.indexOf("vt ");
                    if (indVt != -1) {
                        readTexture(currLine);
                        continue;
                    }
                }
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void readVertex(String newLine) {
        String pieces[] = newLine.split("\\s+");
        Point3f vertex = new Point3f(Float.parseFloat(pieces[1]), Float.parseFloat(pieces[2]), Float.parseFloat(pieces[3]));
        vertices.add(vertex);
    }

    private void readNormal(String newLine) {
        String pieces[] = newLine.split("\\s+");
        Vector3f norm = new Vector3f(Float.parseFloat(pieces[1]), Float.parseFloat(pieces[2]), Float.parseFloat(pieces[3]));
        normals.add(norm);
    }

    private void readTexture(String newLine) {
        String pieces[] = newLine.split("\\s+");
        Vector2f tex = new Vector2f(Float.parseFloat(pieces[1]), Float.parseFloat(pieces[2]));
        textures.add(tex);
    }

    private void readPolygon(String newLine) {
        polygons.add(new Polygon(newLine));
    }
}

class Polygon represents a line like f 1/2/3 4/5/6 7/8/9.

public class Polygon {
private List faces = new ArrayList();

public Polygon(String line) {
    if (line.charAt(0) != 'f') {
        throw new IllegalArgumentException(String.format("Line must be a face definition, but was: '%s'", line));
    }

    String[] facePointDefs = line.split("\\s+");

    // ignore the first piece - it will be "f ". 
    for (int i = 1; i < facePointDefs.length; i++) {
        faces.add(new FacePoint(facePointDefs[i]));
    }
}

public List<FacePoint> getFaces() {
    return Collections.unmodifiableList(faces);
}

}

class FacePoint represents a face point, like 21/342/231.

public class FacePoint {

    private static final String FACE_POINT_REGEX = "^(\\d+)\\/(\\d+)?(\\/(\\d+))?$";

    public Integer vertexIndex;
    public Integer textureIndex;
    public Integer normalIndex;

    public FacePoint(String facePointDef) {
        Matcher matcher = Pattern.compile(FACE_POINT_REGEX).matcher(facePointDef);

        if (!matcher.find()) {
            throw new IllegalArgumentException(String.format("facePointDef is invalid: '%s'", facePointDef));
        }

        String vertexMatch = matcher.group(1);
        if (vertexMatch == null) {
            throw new IllegalArgumentException(String.format("The face point definition didn't contain a vertex: '%s'",
                    facePointDef));
        }
        vertexIndex = Integer.parseInt(vertexMatch);

        String texMatch = matcher.group(2);
        if (texMatch != null) {
            textureIndex = Integer.parseInt(texMatch);
        }

        String normalMatch = matcher.group(4);
        if (normalMatch != null) {
            normalIndex = Integer.parseInt(normalMatch);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:29:24+00:00Added an answer on May 16, 2026 at 11:29 pm

    In OpenGL, the texture origin is at the bottom left as opposed to the top left. This could be messing up your textures. If this is the issue, a simple fix is to subtract the the y texture coordinate from 1: glTexCoord2f(texCoords.x, 1.0f - texCoords.y). (This is the same as flipping the texture vertically.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a go board using opengl. To do this, I'm trying
I am new to using OpenGL and am experimenting with jogl. I am able
I'm new to OpenGL. I'm using JOGL. I would like to create a sky
I'm new to OpenGL. I'm using Java/JOGL. I'm having difficulty with polygon faces. I
I am new to the opengl so i am using guide of the android
I'm fairly new to LWJGL/OpenGL and I've come across this problem which I can't
I'm new at OpenGL and I can't find out how to do this: I
I am new to opengl and using C#, opentk for development. My Application is
I'm playing with shaders in openGL using Processing. I'm pretty noob at this and
I am new to OpenGL and was trying to create a simple maze that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.