Permalink
Browse files

load model with texture

  • Loading branch information...
1 parent fe2087d commit b1bc5d59d55c060cfb8481254dc9a86276ba934f @dayweek committed Jun 12, 2011
Showing with 57 additions and 76 deletions.
  1. +0 −32 build/objTester/CMakeFiles/objLoader.dir/CXX.includecache
  2. +11 −7 build/untitled.obj
  3. +46 −37 main.cpp
View
32 build/objTester/CMakeFiles/objLoader.dir/CXX.includecache
@@ -6,16 +6,6 @@
#IncludeRegexTransform:
-/home/david/skola/opengl/karel/objTester/list.cpp
-list.h
-/home/david/skola/opengl/karel/objTester/list.h
-string.h
--
-stdlib.h
--
-stdio.h
--
-
/home/david/skola/opengl/karel/objTester/list.h
/home/david/skola/opengl/karel/objTester/objLoader.cpp
@@ -34,29 +24,7 @@ stdio.h
objLoader.h
/home/david/skola/opengl/karel/objTester/objLoader.h
-/home/david/skola/opengl/karel/objTester/obj_parser.cpp
-stdio.h
--
-string.h
--
-stdlib.h
--
-obj_parser.h
/home/david/skola/opengl/karel/objTester/obj_parser.h
list.h
/home/david/skola/opengl/karel/objTester/list.h
-string_extra.h
-/home/david/skola/opengl/karel/objTester/string_extra.h
-
-/home/david/skola/opengl/karel/objTester/obj_parser.h
-list.h
-/home/david/skola/opengl/karel/objTester/list.h
-
-/home/david/skola/opengl/karel/objTester/string_extra.cpp
-string_extra.h
-/home/david/skola/opengl/karel/objTester/string_extra.h
-string.h
--
-
-/home/david/skola/opengl/karel/objTester/string_extra.h
View
18 build/untitled.obj
@@ -10,17 +10,21 @@ v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
+vt 0.000000 0.000000 0
+vt 1.000000 0.000000 0
+vt 1.000000 1.000000 0
+vt 0.000000 1.000000 0
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -0.000000 -0.000000 1.000000
vn -1.000000 -0.000000 -0.000000
vn 0.000000 0.000000 -1.000000
-usemtl Test
+usemtl Material
s off
-f 1//1 2//1 3//1 4//1
-f 5//2 8//2 7//2 6//2
-f 1//3 5//3 6//3 2//3
-f 2//4 6//4 7//4 3//4
-f 3//5 7//5 8//5 4//5
-f 5//6 1//6 4//6 8//6
+f 1/1/1 2/2/1 3/3/1 4/4/1
+f 5/1/2 8/2/2 7/3/2 6/4/2
+f 1/1/3 5/2/3 6/3/3 2/4/3
+f 2/1/4 6/2/4 7/3/4 3/4/4
+f 3/1/5 7/2/5 8/3/5 4/4/5
+f 5/1/6 1/2/6 4/3/6 8/4/6
View
83 main.cpp
@@ -48,20 +48,50 @@ Uint32 globalTime = 0;
float frame = 0;
Uint32 timeMile = 0;
float framesToPrint = 0.0;
-GLuint texture = 0;
GLuint crate_texture = 0;
const int size = 20;
typedef pair<int, int> pos;
pos closest;
vector<pair<int, int> > orders;
+//TODO mipmap
+void load_texture(string filename, GLuint* texture) {
+
+ int x,y,n;
+ GLint tmp;
+
+ unsigned char *data = stbi_load ( filename.c_str(), &x, &y, &n, 0 );
+ if (!data)
+ cerr << "cannot load " << filename << endl;
+ else {
+ glGenTextures ( 1, texture );
+ glGetIntegerv(GL_TEXTURE_BINDING_2D, &tmp);
+ // ... process data if not NULL ...
+ // ... x = width, y = height, n = # 8-bit components per pixel ...
+ // ... replace '0' with '1'..'4' to force that many components per pixel
+
+
+//gen texture
+
+ glBindTexture ( GL_TEXTURE_2D, *texture );
+ glTexImage2D ( GL_TEXTURE_2D, 0, 3, x, y, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
+// when texture area is small, bilinear filter the closest mipmap
+ glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
+ GL_LINEAR );
+// when texture area is large, bilinear filter the original
+ glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+ stbi_image_free ( data );
+ glBindTexture ( GL_TEXTURE_2D, tmp);
+ }
+}
class Model {
public:
int vertex_index;
int vertex_count;
GLuint list;
+ GLuint texture;
Model() {}
Model(string filename) {
@@ -70,11 +100,22 @@ class Model {
objData->load (const_cast<char*>(filename.c_str()));
list = lists;
+ if(objData->materialCount > 0) {
+ string s(objData->materialList[0]->texture_filename);
+ s.erase(s.end()-1);
+ load_texture(s.c_str(),&texture);
+ }
glNewList(lists++, GL_COMPILE);
+
glBegin(GL_QUADS);
+
for ( int i = 0; i < objData->faceCount; i++ ) {
for ( int ii = 0; ii < 4; ii++ ) {
obj_vector *v = objData->vertexList[objData->faceList[i]->vertex_index[ii]];
+ obj_vector *t = objData->textureList[objData->faceList[i]->texture_index[ii]];
+ obj_vector *n = objData->normalList[objData->faceList[i]->normal_index[ii]];
+ glNormal3f(n->e[0], n->e[1], n->e[2]);
+ glTexCoord2f(t->e[0], t->e[1]);
glVertex3f ( ( GLfloat ) v->e[0], ( GLfloat ) v->e[1], ( GLfloat ) v->e[2] );
}
}
@@ -85,10 +126,12 @@ class Model {
objData = 0;
}
void render() {
+ glBindTexture(GL_TEXTURE_2D, texture);
+ glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glCallList(list);
}
~Model() {
-
+// glDeleteTextures(1, &texture);
}
};
@@ -603,42 +646,11 @@ void load_font() {
}
}
-//TODO mipmap
-void load_texture(string filename, GLuint* texture) {
-
- int x,y,n;
- GLint tmp;
-
- unsigned char *data = stbi_load ( filename.c_str(), &x, &y, &n, 0 );
- if (!data)
- cerr << "cannot load " << filename << endl;
- else {
- glGenTextures ( 1, texture );
- glGetIntegerv(GL_TEXTURE_BINDING_2D, &tmp);
- // ... process data if not NULL ...
- // ... x = width, y = height, n = # 8-bit components per pixel ...
- // ... replace '0' with '1'..'4' to force that many components per pixel
-
-
-
-//gen texture
- glBindTexture ( GL_TEXTURE_2D, *texture );
- glTexImage2D ( GL_TEXTURE_2D, 0, 3, x, y, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
-// when texture area is small, bilinear filter the closest mipmap
- glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
- GL_LINEAR );
-// when texture area is large, bilinear filter the original
- glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
- stbi_image_free ( data );
- glBindTexture ( GL_TEXTURE_2D, tmp);
- }
-}
void load_textures() {
load_texture("crate.png", &crate_texture);
- load_texture(string("test.png"), &texture);
}
@@ -660,7 +672,7 @@ void renderImage() {
//draw quad
glColor3f ( 0.5, 0.6, 0.8 );
glPolygonMode ( GL_FRONT_AND_BACK, GL_FILL );
- glBindTexture ( GL_TEXTURE_2D, texture );
+// glBindTexture ( GL_TEXTURE_2D, texture );
glBegin ( GL_QUADS );
glTexCoord2f ( 0.0f, 0.0f );
glVertex3f ( 0.0f,0,-1 );
@@ -711,8 +723,6 @@ static void quit ( int code ) {
}
if(objData)
delete ( objData );
- if (texture)
- glDeleteTextures ( 1,&texture );
if (crate_texture)
glDeleteTextures ( 1,&crate_texture );
/* Exit program. */
@@ -1049,7 +1059,6 @@ static void draw_screen ( void ) {
void setup_texturing() {
glEnable ( GL_TEXTURE_2D );
- glGenTextures ( 1, &texture );
}
void setup_shading() {

0 comments on commit b1bc5d5

Please sign in to comment.