summaryrefslogtreecommitdiffstats
path: root/sdl/pyramid.c
diff options
context:
space:
mode:
Diffstat (limited to 'sdl/pyramid.c')
-rw-r--r--sdl/pyramid.c52
1 files changed, 42 insertions, 10 deletions
diff --git a/sdl/pyramid.c b/sdl/pyramid.c
index d03b4ce..fc95fd6 100644
--- a/sdl/pyramid.c
+++ b/sdl/pyramid.c
@@ -33,6 +33,7 @@ static void process_events(void);
static void platform_init(void);
static void platform_destroy(void);
static void render(void);
+GLuint gltLoadTGATexture(const char *);
/* global */
int program_running = 1;
@@ -51,6 +52,9 @@ static struct
GLuint ground_list;
GLuint triangle_list;
+ /* pyramid texture handle */
+ GLuint textures[2];
+
GLfloat xrot;
GLfloat yrot;
} p;
@@ -150,44 +154,73 @@ static void process_events(void)
flag = !flag;
}
-static void platform_init(void)
+/* mipmapping enabled by default */
+GLuint gltLoadTGATexture(const char *fname)
{
+ GLuint handle;
+
/* variables used for texture loading */
GLbyte *pBytes;
GLint iWidth, iHeight, iComponents;
GLenum eFormat;
- /* set the camera to <0,0,0> */
- glframe_reset(&p.camera);
- p.xrot = 0;
- p.yrot = 0;
+ glGenTextures(1, &handle);
+ glBindTexture(GL_TEXTURE_2D, handle);
/* load texture */
+ glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- pBytes = gltLoadTGA("stone.tga", &iWidth, &iHeight, &iComponents, &eFormat);
+
+ pBytes = gltLoadTGA(fname, &iWidth, &iHeight, &iComponents, &eFormat);
if (!pBytes)
fprintf(stderr, "gltLoadTGA: failed to load texture!\n");
- /* load texture image */
glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat,
GL_UNSIGNED_BYTE, pBytes);
free(pBytes);
+ /* seems like mipmapping only applies to MIN_FILTER since mipmapping divides
+ * current texture into smaller and smaller pieces
+ */
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); /* GL_LINEAR to disable mipmapping */
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+ return handle;
+}
+
+static void platform_init(void)
+{
+ memset(&p, 0, sizeof(p));
+
+ /* set the camera to <0,0,0> */
+ glframe_reset(&p.camera);
+ p.textures[0] = gltLoadTGATexture("stone.tga");
+ p.textures[1] = gltLoadTGATexture("grass.tga");
+
/* display list, precompile commands */
p.ground_list = glGenLists(2);
p.triangle_list = p.ground_list + 1;
+ /* a ground with grass drawn using magneta lines */
glNewList(p.ground_list, GL_COMPILE);
+ //glBindTexture(GL_TEXTURE_2D, p.textures[1]);
+ glColor3ub(255, 0, 255);
gltDrawGround();
glEndList();
+ /* a triangle with a texture */
glNewList(p.triangle_list, GL_COMPILE);
+ glBindTexture(GL_TEXTURE_2D, p.textures[0]);
+ glColor3f(1.0f, 1.0f, 1.0f);
gltDrawTriangle();
glEndList();
}
static void platform_destroy(void)
{
+ glDeleteTextures(2, p.textures);
glDeleteLists(p.ground_list, 2);
}
@@ -199,8 +232,8 @@ static void render(void)
/* save the matrix state and do the rotations */
glPushMatrix();
/* apply camera transform, and draw the ground */
- glframe_apply_camera_transform(&p.camera, 0);
- glColor3ub(255, 0, 255);
+ glframe_apply_camera_transform(&p.camera, 1);
+
glCallList(p.ground_list);
glPushMatrix();
@@ -210,7 +243,6 @@ static void render(void)
glRotatef(p.yrot, 0.0f, 1.0f, 0.0f);
/* draw the pyramid */
- glColor3f(1.0f, 1.0f, 1.0f);
glCallList(p.triangle_list);
glPopMatrix();