Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: 3DS Max OBJ file to OpenGL C++

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    1

    3DS Max OBJ file to OpenGL C++

    This is the code I have to read the file. It reads in the vertices, colour and polygons.

    Code :
    #include <Windows.h>
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include "glut.h"
    #include <fstream>
    #include <sstream>
    #include <iostream>
    #include <string>
    using namespace std;
     
    int NUM_VERTS;
    int NUM_COL;
    int NUM_POLY;
     
    struct point3D {
    	float x;
    	float y;
    	float z;
    };
     
    struct colour{
    	float r;
    	float g;
    	float b;
    };
     
    struct polygon {
    	int a;
    	int b;
    	int c;
    	int d;
    };
     
     
    struct camera{
    	point3D pos;
    	point3D lookAt;
    	point3D up;
    }; 
     
    camera cam = {0, 0, 5, 0, 0, 0, 0, 1, 0};
     
    point3D  * vertices;
    colour * colours;
    polygon  * indices;
    char * image_array;
     
    void key(unsigned char key, int x, int y) {
    	if (key == 'a')
    		cam.pos.x -= 0.5;
    	else if (key == 'd')
    		cam.pos.x += 0.5;
    	else if (key == 'w')
    		cam.pos.y += 0.5;
    	else if (key == 's')
    		cam.pos.y -= 0.5;
    	else if (key == 'z')
    		cam.pos.z += 0.5;
    	else if (key == 'x')
    		cam.pos.z -= 0.5;
     
    	glutPostRedisplay();
    }
     
    void outputText(float x, float y, string text)
    {
       glRasterPos2f(x, y);  // where to start drawing
       for (int i = 0; i < text.length(); i++) {
         glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, text[i]);
       }
    }
     
     
    void drawPolygon(int a, int b, int c, int d) {
       glBegin(GL_QUADS);
       glTexCoord2f(0.0f, 0.0f);
     
         glVertex3fv(&vertices[a].x);
    	 glTexCoord2f(0.0f, 1.0f);
     
         glVertex3fv(&vertices[b].x);
    	 glTexCoord2f(1.0f, 1.0f);
     
         glVertex3fv(&vertices[c].x);
    	 glTexCoord2f(1.0f, 0.0f);
     
         glVertex3fv(&vertices[d].x);
       glEnd();
    }
     
     
    void drawCube() {
       for (int i = 0; i < NUM_POLY; i++){
       glColor3f(colours[i].r, colours[i].g, colours[i].b);
       drawPolygon(indices[i].a, indices[i].b, indices[i].c, indices[i].d);
      }
    }
     
    void display() {
    	glClear(GL_COLOR_BUFFER_BIT );
    	glMatrixMode(GL_MODELVIEW); 
    	glLoadIdentity(); // reset the matrix
    	gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z, 
    		cam.lookAt.x, cam.lookAt.y, cam.lookAt.z, 
    		cam.up.x, cam.up.y, cam.up.z);
    	drawCube();
    	glColor3f(0.0, 0.0, 0.0);
    	stringstream ss;
    	ss << "Camera (" << cam.pos.x << ", " << cam.pos.y << ", " << cam.pos.z << ")";
    	outputText(-1.0, 0.5, ss.str());
    	glFlush();
    }
     
    int read_file (const string fileName)
    {
    	ifstream inFile;
    	inFile.open(fileName.c_str());
     
    	if (!inFile.good())
    	{
    		cerr  << "Can't open file" << endl;
    		NUM_POLY = 0;
    		return 1;
    	}
     
    	inFile >> NUM_VERTS;
    	vertices = new point3D[NUM_VERTS];
     
    	for (int i=0; i < NUM_VERTS; i++)
    	{	
    		inFile >> vertices[i].x;
    		inFile >> vertices[i].y;
    		inFile >> vertices[i].z;
    	}
     
    	inFile >> NUM_COL;
    	colours = new colour[NUM_COL];
     
    	for (int i=0; i < NUM_COL; i++)
    	{	
    		inFile >> colours[i].r;
    		inFile >> colours[i].g;
    		inFile >> colours[i].b;
    	}
     
    	inFile >> NUM_POLY;
    	indices = new polygon[NUM_POLY];
     
    	for (int i=0; i < NUM_POLY; i++)
    	{	
    		inFile >> indices[i].a;
    		inFile >> indices[i].b;
    		inFile >> indices[i].c;
    		inFile >> indices[i].d;
    	}
     
    	inFile.close();
    	return 0;
    }
     
    int loadcolTexture(const string fileName) {
      ifstream inFile;
      inFile.open(fileName.c_str(), ios::binary );
      if (!inFile.good())  {
        cerr  << "Can't open texture file " << fileName << endl;
        return 1;
      }
      inFile.seekg (0, ios::end);
      int size = inFile.tellg();
      image_array = new char [size];
      inFile.seekg (0, ios::beg);
      inFile.read (image_array, size);
      inFile.close();
      return 0;
    }
     
     
    void init() {
    	glClearColor(1.0, 1.0, 1.0, 1.0);
    	glColor3f(0.0, 0.0, 0.0);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	glEnable(GL_CULL_FACE);
    	glCullFace(GL_BACK);
        glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 50.0);
    	read_file("BoxCenter2.txt");
     
    	// read the texture file
         loadcolTexture("Penguins.raw");
         // enable texturing
         glEnable(GL_TEXTURE_2D);
         // specify the filtering method
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
         // associate the image read in to the texture to be applied
         glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, image_array);
     
    }
     
    int main(int argc, char** argv) {
    	glutInit(&argc, argv);
    	glutInitWindowSize(500, 500);
    	glutInitDisplayMode(GLUT_RGB);
    	glutCreateWindow("Cube");
    	glutDisplayFunc(display);
    	glutKeyboardFunc(key);
    	init();
    	glutMainLoop();
    }

    This code works with this file:

    8
    -1.0 -1.0 1.0
    -1.0 1.0 1.0
    1.0 1.0 1.0
    1.0 -1.0 1.0
    -1.0 -1.0 -1.0
    -1.0 1.0 -1.0
    1.0 1.0 -1.0
    1.0 -1.0 -1.0
    6
    1.0 0.0 0.0
    0.0 1.0 1.0
    1.0 1.0 0.0
    0.0 1.0 0.0
    0.0 0.0 1.0
    1.0 0.0 1.0
    6
    0 3 2 1
    2 3 7 6
    3 0 4 7
    1 2 6 5
    4 5 6 7
    5 4 0 1


    but I'm trying to create an object in 3DS max and put that in the program so to test I made a cube in 3ds max and exported as an OBJ file then edited it so my program could read it. It exported like this:

    # 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
    # File Created: 01.05.2012 03:22:45

    #
    # object Box012
    #

    v -12.0000 -12.0000 4.3227
    v -12.0000 12.0000 4.3227
    v 12.0000 12.0000 4.3227
    v 12.0000 -12.0000 4.3227
    v -12.0000 -12.0000 11.3227
    v 12.0000 -12.0000 11.3227
    v 12.0000 12.0000 11.3227
    v -12.0000 12.0000 11.3227
    # 8 vertices

    g Box012
    f 1 2 3 4
    f 5 6 7 8
    f 1 4 6 5
    f 4 3 7 6
    f 3 2 8 7
    f 2 1 5 8
    # 6 polygons

    so I changed it to this:

    8
    -12.0000 -12.0000 4.3227
    -12.0000 12.0000 4.3227
    12.0000 12.0000 4.3227
    12.0000 -12.0000 4.3227
    -12.0000 -12.0000 11.3227
    12.0000 -12.0000 11.3227
    12.0000 12.0000 11.3227
    -12.0000 12.0000 11.3227
    6
    1.0 0.0 0.0
    0.0 1.0 1.0
    1.0 1.0 0.0
    0.0 1.0 0.0
    0.0 0.0 1.0
    1.0 0.0 1.0
    6
    1 2 3 4
    5 6 7 8
    1 4 6 5
    4 3 7 6
    3 2 8 7
    2 1 5 8

    but I can't seem to get this to work
    Any help please.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    Your vertices rangle from -12,-12,4 to 12,12,11 but your

    glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 50.0);

    ranges from -1,-1,2 to 1,1,50 so the cube faces will be clipped (except for part of the rear)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •