Skip to content

Instantly share code, notes, and snippets.

@dlivingstone
Created October 16, 2010 13:07
Show Gist options
  • Save dlivingstone/629763 to your computer and use it in GitHub Desktop.
Save dlivingstone/629763 to your computer and use it in GitHub Desktop.
An example model loader/viewer for OpenGL core profile. This code uses the GLTools library. Model details on the model format will be available at 3dgamedev.wordpress.com ...
uwsm0_2
378
48.4251968503937 0 0 0 -38.18897637795276 0 0 0 0 48.4251968503937 -38.18897637795276 0 0 0 11.81102362204724 0 -38.18897637795276 0 0 -38.18897637795276 11.81102362204724 0 0 0 0 0 11.81102362204724 48.4251968503937 0 0 0 0 0 48.4251968503937 0 11.81102362204724 48.4251968503937 0 0 48.4251968503937 -38.18897637795276 11.81102362204724 48.4251968503937 -38.18897637795276 0 48.4251968503937 -26.57597781267043 11.81102362204724 48.4251968503937 0 11.81102362204724 48.4251968503937 -38.18897637795276 11.81102362204724 0 -38.18897637795276 0 48.4251968503937 -38.18897637795276 0 0 -38.18897637795276 11.81102362204724 40.11978340741202 -38.18897637795276 11.81102362204724 31.92395467371481 -38.18897637795276 11.81102362204724 0 -38.18897637795276 11.81102362204724 27.25139183695274 -35.34645501628111 11.81102362204724 0 0 11.81102362204724 29.82020513564828 -37.31757478744946 11.81102362204724 31.92395467371481 -38.18897637795276 11.81102362204724 25.28027206578439 -32.77764171758558 11.81102362204724 24.04117437711892 -29.78619527250425 11.81102362204724 23.61854123073313 -26.57597781267043 11.81102362204724 27.25139183695273 -17.80550060905975 11.81102362204724 48.4251968503937 0 11.81102362204724 25.28027206578438 -20.37431390775529 11.81102362204724 24.04117437711891 -23.36576035283662 11.81102362204724 29.82020513564827 -15.8343808378914 11.81102362204724 32.81165158072959 -14.59528314922593 11.81102362204724 36.02186904056342 -14.17265000284014 11.81102362204724 39.23208650039722 -14.59528314922593 11.81102362204724 42.22353294547855 -15.8343808378914 11.81102362204724 44.7923462441741 -17.80550060905974 11.81102362204724 46.76346601534245 -20.37431390775528 11.81102362204724 48.00256370400791 -23.36576035283661 11.81102362204724 48.4251968503937 -26.57597781267043 11.81102362204724 48.4251968503937 -38.18897637795276 11.81102362204724 42.22353294547856 -37.31757478744946 11.81102362204724 40.11978340741202 -38.18897637795276 11.81102362204724 44.7923462441741 -35.34645501628111 11.81102362204724 46.76346601534245 -32.77764171758557 11.81102362204724 48.00256370400792 -29.78619527250424 11.81102362204724 32.8116515807296 -38.55667247611493 11.81102362204724 36.02186904056342 -38.97930562250072 11.81102362204724 39.23208650039724 -38.55667247611493 11.81102362204724 40.11978340741202 -38.18897637795276 31.88976377952757 31.92395467371481 -38.18897637795276 31.88976377952757 31.92395467371481 -38.18897637795276 31.88976377952757 29.82020513564828 -37.31757478744946 11.81102362204724 31.92395467371481 -38.18897637795276 11.81102362204724 29.82020513564828 -37.31757478744946 31.88976377952757 27.25139183695274 -35.34645501628111 11.81102362204724 27.25139183695274 -35.34645501628111 31.88976377952757 25.28027206578439 -32.77764171758558 31.88976377952757 25.28027206578439 -32.77764171758558 11.81102362204724 24.04117437711892 -29.78619527250425 31.88976377952757 24.04117437711892 -29.78619527250425 11.81102362204724 23.61854123073313 -26.57597781267043 31.88976377952757 23.61854123073313 -26.57597781267043 11.81102362204724 24.04117437711891 -23.36576035283662 31.88976377952757 24.04117437711891 -23.36576035283662 11.81102362204724 25.28027206578438 -20.37431390775529 31.88976377952757 25.28027206578438 -20.37431390775529 11.81102362204724 27.25139183695273 -17.80550060905975 31.88976377952757 27.25139183695273 -17.80550060905975 11.81102362204724 29.82020513564827 -15.8343808378914 11.81102362204724 29.82020513564827 -15.8343808378914 31.88976377952757 32.81165158072959 -14.59528314922593 11.81102362204724 32.81165158072959 -14.59528314922593 31.88976377952757 36.02186904056342 -14.17265000284014 11.81102362204724 36.02186904056342 -14.17265000284014 31.88976377952757 39.23208650039722 -14.59528314922593 11.81102362204724 39.23208650039722 -14.59528314922593 31.88976377952757 42.22353294547855 -15.8343808378914 11.81102362204724 42.22353294547855 -15.8343808378914 31.88976377952757 44.7923462441741 -17.80550060905974 11.81102362204724 44.7923462441741 -17.80550060905974 31.88976377952757 46.76346601534245 -20.37431390775528 31.88976377952757 46.76346601534245 -20.37431390775528 11.81102362204724 48.00256370400791 -23.36576035283661 31.88976377952757 48.00256370400791 -23.36576035283661 11.81102362204724 48.4251968503937 -26.57597781267043 31.88976377952757 48.4251968503937 -26.57597781267043 11.81102362204724 48.4251968503937 -26.57597781267043 11.81102362204724 48.00256370400792 -29.78619527250424 31.88976377952757 48.00256370400792 -29.78619527250424 11.81102362204724 48.4251968503937 -26.57597781267043 31.88976377952757 46.76346601534245 -32.77764171758557 31.88976377952757 46.76346601534245 -32.77764171758557 11.81102362204724 44.7923462441741 -35.34645501628111 31.88976377952757 44.7923462441741 -35.34645501628111 11.81102362204724 42.22353294547856 -37.31757478744946 11.81102362204724 42.22353294547856 -37.31757478744946 31.88976377952757 40.11978340741202 -38.18897637795276 11.81102362204724 40.11978340741202 -38.18897637795276 31.88976377952757 24.04117437711892 -29.78619527250425 31.88976377952757 24.04117437711891 -23.36576035283662 31.88976377952757 23.61854123073313 -26.57597781267043 31.88976377952757 25.28027206578438 -20.37431390775529 31.88976377952757 25.28027206578439 -32.77764171758558 31.88976377952757 27.25139183695273 -17.80550060905975 31.88976377952757 27.25139183695274 -35.34645501628111 31.88976377952757 29.82020513564827 -15.8343808378914 31.88976377952757 29.82020513564828 -37.31757478744946 31.88976377952757 32.81165158072959 -14.59528314922593 31.88976377952757 31.92395467371481 -38.18897637795276 31.88976377952757 40.11978340741202 -38.18897637795276 31.88976377952757 36.02186904056342 -14.17265000284014 31.88976377952757 39.23208650039722 -14.59528314922593 31.88976377952757 42.22353294547855 -15.8343808378914 31.88976377952757 42.22353294547856 -37.31757478744946 31.88976377952757 44.7923462441741 -17.80550060905974 31.88976377952757 44.7923462441741 -35.34645501628111 31.88976377952757 46.76346601534245 -32.77764171758557 31.88976377952757 46.76346601534245 -20.37431390775528 31.88976377952757 48.00256370400792 -29.78619527250424 31.88976377952757 48.00256370400791 -23.36576035283661 31.88976377952757 48.4251968503937 -26.57597781267043 31.88976377952757
378
0 0 -1 0 0 -1 0 0 -1 0 0 -1 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -0 1 0 -0 1 0 -0 1 0 -0 1 0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 0 -1 0 0 -1 0 -0.3826834323650915 -0.9238795325112862 -0 -0.5000000000000007 -0.8660254037844383 0 -0.3826834323650915 -0.9238795325112862 -0 -0.5000000000000007 -0.8660254037844383 0 -0.7071067811865469 -0.7071067811865482 0 -0.7071067811865469 -0.7071067811865482 0 -0.8660254037844383 -0.5000000000000008 0 -0.8660254037844383 -0.5000000000000008 0 -0.9659258262890682 -0.2588190451025208 0 -0.9659258262890682 -0.2588190451025208 0 -1 -3.920888354084638e-016 0 -1 -3.920888354084638e-016 0 -0.9659258262890684 0.2588190451025202 0 -0.9659258262890684 0.2588190451025202 0 -0.8660254037844388 0.4999999999999997 0 -0.8660254037844388 0.4999999999999997 0 -0.7071067811865477 0.7071067811865474 0 -0.7071067811865477 0.7071067811865474 0 -0.5000000000000006 0.8660254037844383 0 -0.5000000000000006 0.8660254037844383 0 -0.2588190451025213 0.9659258262890682 0 -0.2588190451025213 0.9659258262890682 0 -1.158444286434098e-016 1 0 -1.158444286434098e-016 1 0 0.2588190451025203 0.9659258262890684 0 0.2588190451025203 0.9659258262890684 0 0.4999999999999992 0.8660254037844392 0 0.4999999999999992 0.8660254037844392 0 0.7071067811865471 0.707106781186548 0 0.7071067811865471 0.707106781186548 0 0.8660254037844385 0.5000000000000002 0 0.8660254037844385 0.5000000000000002 0 0.9659258262890682 0.2588190451025211 0 0.9659258262890682 0.2588190451025211 0 0.9914448613738103 0.1305261922200525 0 0.9914448613738103 0.1305261922200525 0 0.9914448613738106 -0.1305261922200508 0 0.9659258262890681 -0.2588190451025211 0 0.9659258262890681 -0.2588190451025211 0 0.9914448613738106 -0.1305261922200508 0 0.866025403784438 -0.5000000000000009 0 0.866025403784438 -0.5000000000000009 0 0.7071067811865476 -0.7071067811865476 0 0.7071067811865476 -0.7071067811865476 0 0.5000000000000002 -0.8660254037844385 0 0.5000000000000002 -0.8660254037844385 0 0.3826834323650897 -0.9238795325112867 0 0.3826834323650897 -0.9238795325112867 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1
3
0 0 0
107
0 1 2 1 0 3 4 5 6 5 4 7 8 9 10 9 8 11 12 13 14 13 12 15 15 12 16 17 18 19 18 17 20 20 17 21 20 21 22 23 24 25 24 23 26 26 23 27 25 24 28 25 28 29 25 29 30 25 31 32 31 25 33 33 25 34 34 25 30 32 31 35 32 35 36 32 36 37 32 37 38 32 38 39 32 39 40 32 40 41 32 41 42 32 42 43 44 45 46 45 44 47 47 44 48 48 44 49 49 44 43 50 46 27 46 50 51 46 51 52 53 22 21 22 53 54 55 56 57 56 55 58 58 59 56 59 58 60 61 59 60 59 61 62 63 62 61 62 63 64 65 64 63 64 65 66 67 66 65 66 67 68 69 68 67 68 69 70 71 70 69 70 71 72 71 73 72 73 71 74 74 75 73 75 74 76 76 77 75 77 76 78 78 79 77 79 78 80 80 81 79 81 80 82 82 83 81 83 82 84 83 85 86 85 83 84 86 87 88 87 86 85 88 89 90 89 88 87 91 92 93 92 91 94 93 95 96 95 93 92 96 97 98 97 96 95 97 99 98 99 97 100 100 101 99 101 100 102 103 104 105 104 103 106 106 103 107 106 107 108 108 107 109 108 109 110 110 109 111 110 111 112 112 111 113 112 113 114 112 114 115 115 114 116 116 114 117 117 114 118 117 118 119 119 118 120 119 120 121 119 121 122 122 121 123 122 123 124 124 123 125
// loadModelDataTest-GLTools
// A simple app that loads model data from a custom data format
// the format is a (relatively) simple one based on how an individual
// mesh may be saved in a Collada file - currently the easiest way
// to make models, is with Google Sketchup, export to Collada, then
// manually hack the data and save.
//
// A version of this code which only requires GLEW and freeglut is to
// follow
// Daniel Livingstone, Oct 2010
//
// On Windows, the following define can remove some compiler problems,
// but remove the line if compiling for other platforms!
// - sometimes get problems compiling GLBatch.h otherwise :-(
#define WIN32
#include <GLTools.h> // OpenGL toolkit
#include <GLMatrixStack.h>
#include <GLFrustum.h>
#include <GLBatch.h>
#include <GLMatrixStack.h>
#include <GLGeometryTransform.h>
#include <math3d.h>
#include <iostream>
#include <fstream>
#include <sstream>
#define FREEGLUT_STATIC
#include <GL/glut.h>
using namespace std;
// * Global Variables *
GLMatrixStack projectionMatrix;
GLFrustum viewFrustum;
GLShaderManager shaderManager;
GLBatch myObject;
GLMatrixStack modelViewMatrix;
GLGeometryTransform transformPipeline;
float angle = 0.0f;
// Functions
void reshape(GLint w, GLint h)
{
glViewport(0, 0, w, h);
viewFrustum.SetPerspective(35.0f, float(w) / float(h), 1.0f, 500.0f);
projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
}
void keyboard(unsigned char key, GLint x, GLint y)
{ // could manually control the rotations using this function
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear window
glEnable(GL_BLEND);
glDisable(GL_CULL_FACE);
projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
GLfloat vColour[] = { 1.0f, 0.0f, 0.8f, 1.0f};
GLfloat vLightPos[] = {0.0f, 3.0f, -3.0f};
modelViewMatrix.PushMatrix();
modelViewMatrix.Translate(-2.0f,-2.0f,-8.0f); // move object towards centre
modelViewMatrix.Rotate(-90,1.0f,0.0f,0.0f); // turn upright
// next three transforms are to make the model rotate in place
// (more or less) - this model has origin at one corner, instead of in middle
modelViewMatrix.Translate(2.0f,-1.0f,0.0f); // translate
modelViewMatrix.Rotate(angle,0.0f,0.0f,1.0f); // ... rotate
modelViewMatrix.Translate(-2.0f,1.0f,0.0f); // ... inverse translate
angle += 0.2f;
modelViewMatrix.Scale(0.1f,0.1f,0.1f);
shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF,
modelViewMatrix.GetMatrix(), projectionMatrix.GetMatrix(), vLightPos, vColour);
myObject.Draw();
// could add more tranformations and draw our object again...
// or draw some other stuff!
modelViewMatrix.PopMatrix();
glutPostRedisplay();
glutSwapBuffers();
}
// loadData function
// this function *assumes* that the data matches the particular
// format expected, and will blow-up if it does not
void loadData(GLBatch & object, char* fname)
{
ifstream::pos_type size;
char * memblock;
string text;
// file read based on example in cplusplus.com tutorial
ifstream file (fname, ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
//fSize = (GLuint) size;
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << "file " << fname << " loaded" << endl;
text.assign(memblock);
}
else
{
cout << "Unable to open file " << fname << endl;
exit(1);
}
int count; //count is number of floats, ie verts x 3
stringstream strstream;
strstream << memblock;
string magicnumber; // Basic check: is this a uwsm file?
strstream >> magicnumber;
if (magicnumber.compare(0,4,"uwsm")!=0)
{ // could also use the version number, but not doing that just now
cerr << "File " << fname << " is not a uwsm file." << endl;
exit(1);
}
strstream >> count;
M3DVector3f *vertices = new M3DVector3f[count/3];
int i;
for (i=0;i<count/3;i++)
strstream >> vertices[i][0] >> vertices[i][1] >> vertices[i][2];
strstream >> count; // how many normals? need not be same as vertices
M3DVector3f *normals = new M3DVector3f[count/3];
for (i=0;i<count/3;i++)
strstream >> normals[i][0] >> normals[i][1] >> normals[i][2];
strstream >> count; // how many texture coords? need not be same as vertices
GLfloat nul;
for (i=0;i<count;i++)
strstream >> nul; // not using these just now!
int triangles;
strstream >> triangles; // how many *triangles* in mesh?
M3DVector3f *meshVerts = new M3DVector3f[triangles*3];
M3DVector3f *meshNormals = new M3DVector3f[triangles*3];
int n;
for (i=0;i<triangles*3;i++)
{
// commented out lines show how we *could* vary no. of normals & tex co-ords
// from number of vertices. Useful for e.g. when a vertex is shared
// by two faces with different textures on them
strstream >> n; // vertex index
m3dCopyVector3(meshVerts[i],vertices[n]);
//strstream >> n; // normal index
m3dCopyVector3(meshNormals[i],normals[n]);
//strstream >> n; // texture u-v index - ignore for now
}
object.Begin(GL_TRIANGLES, triangles*3, 0);
object.CopyVertexData3f(meshVerts);
object.CopyNormalDataf(meshNormals);
object.End();
delete [] memblock; // I sure did allocate a lot of arrays!
delete [] vertices; // could do more for less, I'm sure
delete [] normals;
delete [] meshVerts;
delete [] meshNormals;
}
void init(void)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f ); // white background
shaderManager.InitializeStockShaders();
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
modelViewMatrix.LoadIdentity();
modelViewMatrix.Translate(0.0f,0.0f,-5.0f);
loadData(myObject,"blocks.uwsm");
}
// Main function - program entry point
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("simple3Dapp");
GLenum err = glewInit();
if (GLEW_OK != err)
{
// Problem: glewInit failed, something is seriously wrong.
// cerr is the standard error output stream
cerr << "Error: " << glewGetErrorString(err) << endl;
return 1;
}
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
init();
glutPostRedisplay(); // draw the scene once
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment