Commit 79fb5c86 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Graphics assignment 10 - Texture mapping

Finished the complete assignment
parent 2c97965d
...@@ -103,8 +103,8 @@ setHemispherePoint(vec3 *p, vec3* n, vec3* t, int latitude, int longitude, ...@@ -103,8 +103,8 @@ setHemispherePoint(vec3 *p, vec3* n, vec3* t, int latitude, int longitude,
p->z = oz + cos(longitude * dToR) * cos(latitude * dToR) * s; p->z = oz + cos(longitude * dToR) * cos(latitude * dToR) * s;
// Set texture coordinate // Set texture coordinate
t->x = 0.0; t->x = longitude / 360.0;
t->y = 0.0; t->y = latitude / 90.0;
// calculate normal // calculate normal
n->x = p->x - ox; n->x = p->x - ox;
...@@ -165,6 +165,7 @@ createCylinder(polys * list, double radius, double height, ...@@ -165,6 +165,7 @@ createCylinder(polys * list, double radius, double height,
double len; double len;
poly p; poly p;
int i; int i;
GLfloat tex_height = (GLfloat)(height / (2 * M_PI * radius));
// prepare poly datastructure, as these values are the same for all generated polys // prepare poly datastructure, as these values are the same for all generated polys
p.points = 4; p.points = 4;
...@@ -197,8 +198,9 @@ createCylinder(polys * list, double radius, double height, ...@@ -197,8 +198,9 @@ createCylinder(polys * list, double radius, double height,
p.normal[i].x /= len; p.normal[i].x /= len;
p.normal[i].z /= len; p.normal[i].z /= len;
// Set texture coordinate // Set texture coordinate/
p.tcoord[i].x = p.tcoord[i].y = 0.0; p.tcoord[i].x = (i < 2 ? longitude : longitude+10) / 360.0;
p.tcoord[i].y = (i == 1 || i == 2) ? tex_height : 0.0;
} }
AddPolyToPolylist(list, p); AddPolyToPolylist(list, p);
...@@ -259,7 +261,6 @@ readPolyVertices(poly *p, FILE *f, const vec3 *vertex_list, int num_vertices_to_ ...@@ -259,7 +261,6 @@ readPolyVertices(poly *p, FILE *f, const vec3 *vertex_list, int num_vertices_to_
} }
} }
} }
// Read a polygonal object from a .OBJ file. // Read a polygonal object from a .OBJ file.
......
...@@ -7,6 +7,6 @@ v -100 0 50 ...@@ -7,6 +7,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7 0.7 0.7
11 0.0 0.0 11 0.0 1.0
10 0.0 0.0 10 3.0 1.0
14 0.0 0.0 14 3.0 0.0
15 0.0 0.0 15 0.0 0.0
...@@ -132,10 +132,9 @@ InitializePolygonlists(void) ...@@ -132,10 +132,9 @@ InitializePolygonlists(void)
object_positions[0].x, object_positions[0].y, object_positions[0].z); object_positions[0].x, object_positions[0].y, object_positions[0].z);
// A single tree object // A single tree object
polylistTreeLeafs = CreatePolylist(10); polylistTreeLeafs = CreatePolylist(4);
createSphere(polylistTreeLeafs, 0.7, 0.7, 0.7, 0, 1.7, 0, 0, 1, 0); loadPolygonalObject(polylistTreeLeafs, "banana_leaf.obj", texture_names,
for (i = 0; i < polylistTreeLeafs->length; i++) 1.0, 0, 1.8, 0);
polylistTreeLeafs->items[i].texture_id = texture_names[0];
polylistTreeStem = CreatePolylist(10); polylistTreeStem = CreatePolylist(10);
createCylinder(polylistTreeStem, 0.075, 1.8, 0, 0, 0, 0.5, 0.3, 0); createCylinder(polylistTreeStem, 0.075, 1.8, 0, 0, 0, 0.5, 0.3, 0);
...@@ -145,10 +144,10 @@ InitializePolygonlists(void) ...@@ -145,10 +144,10 @@ InitializePolygonlists(void)
// Done! // Done!
printf("%d polygons\n", printf("%d polygons\n",
polylistHouse->length + polylistTreeLeafs->length + polylistHouse->length + polylistTreeLeafs->length +
polylistTreeStem->length + polylistGround1->length + polylistGround2->length); polylistTreeStem->length + polylistGround1->length +
polylistGround2->length);
} }
void void
InitGL(void) InitGL(void)
{ {
...@@ -257,14 +256,16 @@ InitGL(void) ...@@ -257,14 +256,16 @@ InitGL(void)
glBindTexture(GL_TEXTURE_2D, texture_names[i]); glBindTexture(GL_TEXTURE_2D, texture_names[i]);
glCheckError("glBindTexture"); glCheckError("glBindTexture");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glCheckError("glTexParameteri"); glCheckError("glTexParameteri");
glTexImage2D(GL_TEXTURE_2D, 0, texture_internal_format, gluBuild2DMipmaps(GL_TEXTURE_2D, texture_internal_format,
width, height, 0, texture_format, texture_type, image_data); width, height, texture_format, texture_type, image_data);
//glTexImage2D(GL_TEXTURE_2D, 0, texture_internal_format,
// width, height, 0, texture_format, texture_type, image_data);
glCheckError("glTexImage2D"); glCheckError("glTexImage2D");
// Free the image data, as OpenGL will have made its internal copy by now // Free the image data, as OpenGL will have made its internal copy by now
...@@ -312,6 +313,7 @@ DrawPolylist(polys * list) ...@@ -312,6 +313,7 @@ DrawPolylist(polys * list)
glBegin(GL_POLYGON); glBegin(GL_POLYGON);
for (j = 0; j < p.points; j++) for (j = 0; j < p.points; j++)
{ {
glTexCoord2f(p.tcoord[j].x, p.tcoord[j].y);
glNormal3f(p.normal[j].x, p.normal[j].y, p.normal[j].z); glNormal3f(p.normal[j].x, p.normal[j].y, p.normal[j].z);
glVertex3f(p.pts[j].x, p.pts[j].y, p.pts[j].z); glVertex3f(p.pts[j].x, p.pts[j].y, p.pts[j].z);
} }
...@@ -420,7 +422,17 @@ DrawGLScene(void) ...@@ -420,7 +422,17 @@ DrawGLScene(void)
glScalef(1, 1 + (rand_float()-0.5)*0.6, 1); glScalef(1, 1 + (rand_float()-0.5)*0.6, 1);
DrawPolylist(polylistTreeStem); DrawPolylist(polylistTreeStem);
DrawPolylist(polylistTreeLeafs);
// Draw 5 to 10 leafs
for( int l = 0, step = 360 / (5+rand()%6); l < 360; l += step )
{
glPushMatrix();
glRotatef((float)l, 0, 1, 0);
DrawPolylist(polylistTreeLeafs);
glPopMatrix();
}
glPopMatrix(); glPopMatrix();
} }
...@@ -429,7 +441,7 @@ DrawGLScene(void) ...@@ -429,7 +441,7 @@ DrawGLScene(void)
glPushAttrib(GL_LIGHTING_BIT); glPushAttrib(GL_LIGHTING_BIT);
glDisable(GL_LIGHTING); glDisable(GL_LIGHTING);
//DrawPolylist(polylistSkydome); DrawPolylist(polylistSkydome);
glPopAttrib(); glPopAttrib();
glutSwapBuffers(); glutSwapBuffers();
...@@ -511,7 +523,6 @@ mouseFunc(int button, int state, int x, int y) ...@@ -511,7 +523,6 @@ mouseFunc(int button, int state, int x, int y)
// pressed button released // pressed button released
mouse_mode = 0; mouse_mode = 0;
} }
} }
static void static void
......
...@@ -7,6 +7,6 @@ v -100 0 5 ...@@ -7,6 +7,6 @@ v -100 0 5
p 4 5 p 4 5
0.85 0.85 0.85 0.85 0.85 0.85
0 0.0 0.0 0 0.0 0.0
1 0.0 0.0 1 0.0 1.0
2 0.0 0.0 2 20.0 1.0
3 0.0 0.0 3 20.0 0.0
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment