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

  • Home
  • SEARCH
  • 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 8642895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:53:46+00:00 2026-06-12T11:53:46+00:00

I have been given the task of importing an obj file and loading it

  • 0

I have been given the task of importing an obj file and loading it up in C++.

Its loading the file correctly but displainy it incorrectly. Can anybody see anything wrong with my setup of opengl stuff? 🙂

Here is a picture of what the render looks like atm

#define _USE_MATH_DEFINES
#include<stdio.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include "InitShader.h"
#include <math.h>
#include "MatrixStack.h"
#include "Object.h"
#include <time.h>

#define BUFFER_OFFSET( offset )   ((GLvoid*) (offset))
float x = 1.0;
const int NumVertices = 144;
const int NumIndicies = 104;
GLfloat  vertices[NumVertices];

// RGBA olors
GLfloat vertexColours[NumVertices];

// each entry is an index into the vertices matrix
GLint vertexIndicies[NumIndicies];


//GLfloat vertices2[NumVertices];

// RGBA olors
//GLfloat vertexColours2[NumVertices];

// each entry is an index into the vertices matrix
//GLint vertexIndicies2[NumIndicies];

GLuint vao;
GLuint program;
GLuint buffers[2];
GLfloat radius = 1.0;

GLfloat theta = 0.0;
GLfloat phi = 0.0;

const GLfloat  dr = 5.0 * M_PI/180.0;

// Projection transformation parameters

GLfloat  left2 = -1.0, right2 = 1.0;
GLfloat  bottom = -1.0, top = 1.0;
GLfloat  zNear = 0.5, zFar = 3.0;

GLuint  modelView;  // model-view matrix uniform shader variable location
GLuint  projection; // projection matrix uniform shader variable location

MatrixStack modelViewStack(5);
MatrixStack projectionStack(5);
// OpenGL initialization
void
init()
{
        srand ( time(NULL) );
        Object myst("assignment1.obj");
        float *tempFloat = myst.verts();
        float *tempFloat2 = myst.indis();
        for (unsigned int i=0; i<NumVertices; vertices[i]=tempFloat[i],i++);
        for (unsigned int i=0; i<NumIndicies; vertexIndicies[i]=tempFloat2[i],i++);
        for (unsigned int i=0; i<NumVertices;i++){
            float a = (float)(rand()%10);
            a = a/10;
            cout << a;
            vertexColours[i]=a;
        }
        for (unsigned int i=0; i<NumVertices; cout << vertices[i] << endl ,i++);
        for (unsigned int i=0; i<NumIndicies; cout << vertexIndicies[i] << endl ,i++);
        glEnable( GL_DEPTH_TEST );
        //make background yerpul in colour
        glClearColor( 0.235,  0.194,  0.314, 1.0 );

        // Load shaders and use the resulting shader program
        program = InitShader( "vshader41.glsl", "fshader41.glsl" );
        glUseProgram( program );

        // Create a vertex array object
        glGenVertexArrays( 1, &vao );
        glBindVertexArray( vao );

        // Create and initialize two buffer objects
        glGenBuffers( 2, buffers);

        //one buffer for the vertices and colours
        glBindBuffer( GL_ARRAY_BUFFER, buffers[0]);
        glBufferData( GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(vertexColours),NULL, GL_STATIC_DRAW );
        glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices );
        glBufferSubData( GL_ARRAY_BUFFER, sizeof(vertices), sizeof(vertexColours), vertexColours);

        //one buffer for the indices
        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
        glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(vertexIndicies),vertexIndicies, GL_STATIC_DRAW );

        // set up vertex arrays
        GLuint vPosition = glGetAttribLocation( program, "vPosition" );
        glEnableVertexAttribArray( vPosition );
        glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );

        GLuint vColor = glGetAttribLocation( program, "vColor" );
        glEnableVertexAttribArray( vColor );
        glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(vertices)) );

        modelView = glGetUniformLocation( program, "model_view" );
        projection = glGetUniformLocation( program, "projection" );

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
        }

//----------------------------------------------------------------------------

void
display( void )
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    modelViewStack.loadIdentity();
    modelViewStack.lookAt(radius*sin(theta)*cos(phi),
            radius*sin(theta)*sin(phi),
            radius*cos(theta),
            0.0,0.0,0.0,
            0.0,1.0,0.0);

    glUniformMatrix4fv(modelView, 1, GL_FALSE, modelViewStack.getMatrixf());

    projectionStack.loadIdentity();
    projectionStack.ortho(left2,right2,bottom,top,zNear,zFar);

    glUniformMatrix4fv(projection, 1, GL_FALSE, projectionStack.getMatrixf());

    glBindVertexArray(vao);
    glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
    //Indexing into vertices we need to use glDrawElements
    glDrawElements (GL_TRIANGLES, NumIndicies, GL_UNSIGNED_INT, 0);
    glutSwapBuffers();
}

//----------------------------------------------------------------------------

void keyboard( unsigned char key, int x, int y )
{
    switch( key ) {
        case 033: // Escape Key
        case 'q': case 'Q':
        exit( EXIT_SUCCESS );
        break;

        case 'x': left2 *= 1.1; right2 *= 1.1; break;
        case 'X': left2 *= 0.9; right2 *= 0.9; break;
        case 'y': bottom *= 1.1; top *= 1.1; break;
        case 'Y': bottom *= 0.9; top *= 0.9; break;
        case 'z': zNear  *= 0.9; zFar *= 1.1; break;
        case 'Z': if (zNear<zFar){zNear *= 1.1; zFar *= 0.9;} break;
        case 'r': radius *= 2.0; break;
        case 'R': radius *= 0.5; break;
        case 'o': theta += dr; break;
        case 'O': theta -= dr; break;
        case 'p': phi += dr; break;
        case 'P': phi -= dr; break;

        case ' ':  // reset values to their defaults
        left2 = -1.0;
        right2 = 1.0;
        bottom = -1.0;
        top = 1.0;
        zNear = -1.0;
        zFar = 1.0;

        radius = 1.0;
        theta  = 0.0;
        phi    = 0.0;
        break;
  }

  glutPostRedisplay();
}
//----------------------------------------------------------------------------
void idle(){
    theta += .001;

    left2 += .0001;

    glutPostRedisplay();
}
//----------------------------------------------------------------------------
void
reshape( int width, int height )
{
    glViewport( 0, 0, width, height );
}

//----------------------------------------------------------------------------


int
main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
    glutInitWindowSize( 512, 512 );
    glutCreateWindow( "Orbit the Color Cube - Orthographic" );

    glewInit();

    init();
    glutIdleFunc(idle);
    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );
    glutReshapeFunc( reshape );

    glutMainLoop();
    return 0;
}
  • 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-06-12T11:53:48+00:00Added an answer on June 12, 2026 at 11:53 am

    The vertex indexes of the faces in the .obj file starts at 1, not 0 as might be expected. Try to decrease each index with 1 when you load the file. If I read your code correctly, this line:

    for (unsigned int i=0; i<NumIndicies; vertexIndicies[i]=tempFloat2[i],i++);

    should be

    for (unsigned int i=0; i<NumIndicies; vertexIndicies[i]=tempFloat2[i] - 1,i++);

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

Sidebar

Related Questions

I have been given a task to create a Python file that will take
I have been given a task that can be simplified to this scenario: Customers
I have been given the task of adding functionality to an existing IIS 6.0
I have been given the task to design a database to store a lot
I have been given the task of re-writing some libraries written in C# so
I have been given the task of devising a custom forms manager that has
I have been given the task of converting a macromedia application to a web
I am a student on a work placement. I have been given the task
I work in the Systems & admin team and have been given the task
I have been given the unenviable task of cleaning up after a developer who

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.