1 | #include "object.h"
|
---|
2 | #include <GL/glext.h>
|
---|
3 | #include <glh/glh_extensions.h>
|
---|
4 |
|
---|
5 |
|
---|
6 | Object::Object()
|
---|
7 | {
|
---|
8 |
|
---|
9 | }
|
---|
10 |
|
---|
11 | Object::~Object()
|
---|
12 | {
|
---|
13 |
|
---|
14 | }
|
---|
15 |
|
---|
16 | void Object::Load(ifstream *file)
|
---|
17 | {
|
---|
18 | fprintf(stderr, "Loading Object: \n");
|
---|
19 |
|
---|
20 | char* tempchar=new char[500];
|
---|
21 | file->getline(tempchar,500,'\n');
|
---|
22 | sscanf(tempchar,"translate: %lf %lf %lf",&Translate.x,&Translate.y,&Translate.z);
|
---|
23 |
|
---|
24 | file->getline(tempchar,500,'\n');
|
---|
25 | sscanf(tempchar,"rotate: %lf %lf %lf",&Rotate.x,&Rotate.y,&Rotate.z);
|
---|
26 |
|
---|
27 | file->getline(tempchar,500,'\n');
|
---|
28 | sscanf(tempchar,"scale: %lf %lf %lf",&Scale.x,&Scale.y,&Scale.z);
|
---|
29 |
|
---|
30 | file->getline(tempchar,500,'\n');
|
---|
31 | sscanf(tempchar,"Vertexcount %ui",&vertexcount);
|
---|
32 |
|
---|
33 | Vertices=new float[vertexcount*3];
|
---|
34 | fprintf(stderr, "vertexcount: %i ",vertexcount);
|
---|
35 | for(unsigned int i=0;i<vertexcount;i++)
|
---|
36 | {
|
---|
37 | //unsigned int v;
|
---|
38 | file->getline(tempchar,500,'\n');
|
---|
39 | //sscanf(tempchar,"vertex %ui %lf %lf %lf",&v);
|
---|
40 | file->getline(tempchar,500,'\n');
|
---|
41 | sscanf(tempchar,"%f %f %f",&Vertices[3*i],&Vertices[3*i+1],&Vertices[3*i+2]);
|
---|
42 |
|
---|
43 | }
|
---|
44 |
|
---|
45 | file->getline(tempchar,500,'\n');
|
---|
46 | sscanf(tempchar,"Facecount %ui",&facecount);
|
---|
47 |
|
---|
48 | fprintf(stderr, "Facecount: %i\n",facecount);
|
---|
49 |
|
---|
50 | Normals=new float[9*facecount];
|
---|
51 | Indexes=new unsigned int[3*facecount];
|
---|
52 | TexCoords=new float[6*facecount];
|
---|
53 | AllVertices=new float[9*facecount];
|
---|
54 |
|
---|
55 | unsigned int f;
|
---|
56 |
|
---|
57 | for(int i=0;i<facecount;i++)
|
---|
58 | {
|
---|
59 | file->getline(tempchar,500,':');
|
---|
60 | //eatwhitespace??
|
---|
61 | bool more=true;
|
---|
62 | while (more)
|
---|
63 | {
|
---|
64 | file->getline(tempchar,500,' ');
|
---|
65 | if(tempchar[0]!=0)
|
---|
66 | more=false;
|
---|
67 | }
|
---|
68 | sscanf(tempchar,"%ui",&Indexes[3*i]);
|
---|
69 | more=true;
|
---|
70 | while (more)
|
---|
71 | {
|
---|
72 | file->getline(tempchar,500,' ');
|
---|
73 | if(tempchar[0]!=0)
|
---|
74 | more=false;
|
---|
75 | }
|
---|
76 | sscanf(tempchar,"%ui",&Indexes[3*i+1]);
|
---|
77 | more=true;
|
---|
78 | while (more)
|
---|
79 | {
|
---|
80 | file->getline(tempchar,500,' ');
|
---|
81 | if(tempchar[0]!=0)
|
---|
82 | more=false;
|
---|
83 | }
|
---|
84 | sscanf(tempchar,"%ui",&Indexes[3*i+2]);
|
---|
85 |
|
---|
86 | file->getline(tempchar,500,'\n');
|
---|
87 | file->getline(tempchar,500,'\n');//uvs
|
---|
88 | file->getline(tempchar,500,'\n');//normals
|
---|
89 |
|
---|
90 | file->getline(tempchar,500,'\n');
|
---|
91 | sscanf(tempchar,"%f %f",&TexCoords[6*i],&TexCoords[6*i+1]);
|
---|
92 | file->getline(tempchar,500,'\n');
|
---|
93 | sscanf(tempchar,"%f %f",&TexCoords[6*i+2],&TexCoords[6*i+3]);
|
---|
94 | file->getline(tempchar,500,'\n');
|
---|
95 | sscanf(tempchar,"%f %f",&TexCoords[6*i+4],&TexCoords[6*i+5]);
|
---|
96 |
|
---|
97 | file->getline(tempchar,500,'\n');
|
---|
98 | sscanf(tempchar,"%f %f %f",&Normals[9*i],&Normals[9*i+1],&Normals[9*i+2]);
|
---|
99 | file->getline(tempchar,500,'\n');
|
---|
100 | sscanf(tempchar,"%f %f %f",&Normals[9*i+3],&Normals[9*i+4],&Normals[9*i+5]);
|
---|
101 | file->getline(tempchar,500,'\n');
|
---|
102 | sscanf(tempchar,"%f %f %f",&Normals[9*i+6],&Normals[9*i+7],&Normals[9*i+8]);
|
---|
103 | }
|
---|
104 | fprintf(stderr, "Building vertex buffer...\n");
|
---|
105 |
|
---|
106 | for(int i=0;i<facecount;i++)
|
---|
107 | {
|
---|
108 | unsigned int index1=Indexes[i*3];
|
---|
109 | unsigned int index2=Indexes[i*3+1];
|
---|
110 | unsigned int index3=Indexes[i*3+2];
|
---|
111 |
|
---|
112 | AllVertices[i*9]=Vertices[3*index1];
|
---|
113 | AllVertices[i*9+1]=Vertices[3*index1+1];
|
---|
114 | AllVertices[i*9+2]=Vertices[3*index1+2];
|
---|
115 | AllVertices[i*9+3]=Vertices[3*index2];
|
---|
116 | AllVertices[i*9+4]=Vertices[3*index2+1];
|
---|
117 | AllVertices[i*9+5]=Vertices[3*index2+2];
|
---|
118 | AllVertices[i*9+6]=Vertices[3*index3];
|
---|
119 | AllVertices[i*9+7]=Vertices[3*index3+1];
|
---|
120 | AllVertices[i*9+8]=Vertices[3*index3+2];
|
---|
121 | }
|
---|
122 | delete []Vertices;
|
---|
123 | /*
|
---|
124 | glGenBuffersARB( 1, &VertexbufferID );
|
---|
125 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, VertexbufferID );
|
---|
126 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, vertexcount*sizeof(float), Vertices, GL_STATIC_DRAW_ARB );
|
---|
127 |
|
---|
128 | glGenBuffersARB( 1, &IndexBufferID );
|
---|
129 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, IndexBufferID );
|
---|
130 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, facecount*3*sizeof(float), Indexes, GL_STATIC_DRAW_ARB );
|
---|
131 |
|
---|
132 | glGenBuffersARB( 1, &NormalBufferID );
|
---|
133 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, NormalBufferID );
|
---|
134 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, facecount*9*sizeof(float), Normals, GL_STATIC_DRAW_ARB );
|
---|
135 |
|
---|
136 | glGenBuffersARB( 1, &TexCoordBufferID );
|
---|
137 | glBindBufferARB( GL_ARRAY_BUFFER_ARB, TexCoordBufferID );
|
---|
138 | glBufferDataARB( GL_ARRAY_BUFFER_ARB, facecount*6*sizeof(float), TexCoords, GL_STATIC_DRAW_ARB );
|
---|
139 | */
|
---|
140 | }
|
---|
141 |
|
---|
142 | void Object::Rendermodel()
|
---|
143 | {
|
---|
144 | //glShadeModel(GL_FLAT);
|
---|
145 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
146 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
147 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
148 |
|
---|
149 | glVertexPointer (3, GL_FLOAT, 0, AllVertices);
|
---|
150 | glNormalPointer(GL_FLOAT,0,Normals);
|
---|
151 | glTexCoordPointer(2, GL_FLOAT, 0, TexCoords);
|
---|
152 |
|
---|
153 | glDrawArrays(GL_TRIANGLES,0, 3*facecount);
|
---|
154 |
|
---|
155 | glDisableClientState( GL_VERTEX_ARRAY );
|
---|
156 | glDisableClientState( GL_NORMAL_ARRAY );
|
---|
157 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
158 |
|
---|
159 | //glutSolidTeapot(1.5);
|
---|
160 | } |
---|