Project 2 – openGL .obj viewer

teapot1

This project was more challenging for me than was project one, i accredit that to my temporary lack of skills at programming languages. (i just declared my second major in CSC department). I caught on with Processing a little faster than with C++ and OpenGL, but life isn’t always as easy as we would like it to be. No worries, i still have a project to show, just took me a little longer than usual to compile and run with Dev++.

     Using C file I/O streams, i have read in the coordinates for the vertices and faces of the object file into arrays from which i can pull those locations and display on screen. Using some components of the sphereworld.cpp example from the GL bible, my very own C++ file i/o routine, and a little help from Dr.P and Duncan’s camera moves, i was able to have a working scene where objects could be displayed and viewed using key commanded camera movements.

#include “C:/Users/David/Desktop/GLSource/SB-WinSrc/examples/src/shared/gltools.h”
#include <GL/glut.h>
#include <stdlib.h>
#include “C:/Users/David/Desktop/GLSource/SB-WinSrc/examples/src/shared/glframe.h”
#include <math.h> 
#define NUM_SPHERES      50

#include <iostream>
#include <fstream>

using namespace std;
GLFrame    spheres[NUM_SPHERES];
GLFrame    frameCamera;
       

int main(void)
{
    ifstream inFile;
    int x,y,z;
   
    inFile.open(“obj.txt”);
    cout<<;
   
    inFile >> x
    inFile >> y
    inFIle >> z
    cout<<;
   
void SetupRC()
    {
    int iSphere;
   
    // Bluish background
    glClearColor(0.0f, 0.0f, .50f, 1.0f );
        
    // Draw everything as wire frame
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
   
    // Randomly place the sphere inhabitants
    for(iSphere = 0; iSphere < NUM_SPHERES; iSphere++)
        {
        // Pick a random location between -20 and 20 at .1 increments
        float x = ((float)((rand() % 400) – 200) * 0.1f);
        float z = (float)((rand() % 400) – 200) * 0.1f;
        spheres[iSphere].SetOrigin(x, 0.0f, z);
        }
    }

///////////////////////////////////////////////////////////
// Draw a gridded ground
void DrawGround(void)
    {
    GLfloat fExtent = 20.0f;
    GLfloat fStep = 1.0f;
    GLfloat y = -0.4f;
    GLfloat iLine;
   
    glBegin(GL_LINES);
       for(iLine = -fExtent; iLine <= fExtent; iLine += fStep)
          {
          glVertex3f(iLine, y, fExtent);    // Draw Z lines
          glVertex3f(iLine, y, -fExtent);
   
          glVertex3f(fExtent, y, iLine);
          glVertex3f(-fExtent, y, iLine);
          }
   
    glEnd();
    }

       
// Called to draw scene
void RenderScene(void)
    {
    int i;
    static GLfloat yRot = 0.0f;         // Rotation angle for animation
    yRot += 0.5f;
       
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       
    glPushMatrix();
        frameCamera.ApplyCameraTransform();
       
        // Draw the ground
        DrawGround();
       
        // Draw the randomly located spheres
        for(i = 0; i < NUM_SPHERES; i++)
            {
            glPushMatrix();
            spheres[i].ApplyActorTransform();
            glutSolidSphere(0.1f, 13, 26);
            glPopMatrix();
            }

        glPushMatrix();
            glTranslatef(0.0f, 0.0f, -2.5f);
   
            glPushMatrix();
                glRotatef(-yRot * 2.0f, 0.0f, 1.0f, 0.0f);
                glTranslatef(1.0f, 0.0f, 0.0f);
                glutSolidSphere(0.1f, 13, 26);
            glPopMatrix();
   
            glRotatef(yRot, 0.0f, 1.0f, 0.0f);
            //glutWireSphere(0.35, 0.15, 40, 20);
        glPopMatrix();
    glPopMatrix();
       
    // Do the buffer Swap
    glutSwapBuffers();
    }

// Respond to arrow keys by moving the camera frame of reference
void SpecialKeys(int key, int x, int y)
    {
    if(key == GLUT_KEY_UP)
        frameCamera.MoveForward(0.1f);
       
    if(key == GLUT_KEY_DOWN)
        frameCamera.MoveForward(-0.1f);

    if(key == GLUT_KEY_LEFT)
        frameCamera.RotateLocalY(0.1f);
       
    if(key == GLUT_KEY_RIGHT)
        frameCamera.RotateLocalY(-0.1f);
                       
    // Refresh the Window
    glutPostRedisplay();
    }

///////////////////////////////////////////////////////////
// Called by GLUT library when idle (window not being
// resized or moved)
void TimerFunction(int value)
    {
    // Redraw the scene with new coordinates
    glutPostRedisplay();
    glutTimerFunc(3,TimerFunction, 1);
    }

void ChangeSize(int w, int h)
    {
    GLfloat fAspect;

    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width).
    if(h == 0)
        h = 1;

    glViewport(0, 0, w, h);
       
    fAspect = (GLfloat)w / (GLfloat)h;

    // Reset the coordinate system before modifying
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
       
    // Set the clipping volume
    gluPerspective(35.0f, fAspect, 1.0f, 50.0f);
       
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

int main(int argc, char* argv[])
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(800,600);
    glutCreateWindow(“OpenGL SphereWorld Demo”);
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);
    glutSpecialFunc(SpecialKeys);

    SetupRC();
    glutTimerFunc(33, TimerFunction, 1);

    glutMainLoop();

    return 0;
    }

About these ads

~ by redeyeproductions88 on April 10, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: