summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-07-04 22:46:50 -0500
committerKamil Kaminski <kamilkss@gmail.com>2011-07-04 22:46:50 -0500
commitf65f7c583f763120bba3eeacd2af857d113d79ba (patch)
treeeb0c64de094184c79a39694f76c66ffbe1d28835
parent6e420fa48b415dcdf6353adec9298e53998e7dff (diff)
downloadGLPyramid-f65f7c583f763120bba3eeacd2af857d113d79ba.tar.gz
GLPyramid-f65f7c583f763120bba3eeacd2af857d113d79ba.tar.bz2
GLPyramid-f65f7c583f763120bba3eeacd2af857d113d79ba.zip
sync up
-rw-r--r--sdl/GL_notes.txt6
-rw-r--r--sdl/Makefile43
-rw-r--r--sdl/glframe.c22
-rw-r--r--sdl/gltools.c5
-rw-r--r--sdl/grass.tgabin0 -> 786476 bytes
-rw-r--r--sdl/math3d.c2
-rw-r--r--sdl/math3d.h2
-rw-r--r--sdl/platform.c49
-rw-r--r--sdl/pyramid.c52
9 files changed, 103 insertions, 78 deletions
diff --git a/sdl/GL_notes.txt b/sdl/GL_notes.txt
index 0a19bbb..23795e9 100644
--- a/sdl/GL_notes.txt
+++ b/sdl/GL_notes.txt
@@ -75,3 +75,9 @@ Vertex Shader--you can write code for tasks such as:
- Lighting per vertex or computing values for lighting per pixel
- Color computation
+Fixed Functionality Pipeline
+- glTranslatef: can be mimicked by building m3dTranslationMatrix44f
+- glRotatef: can be mimicked by building m3dRotationMatrix44f
+- Both of the matrices can be combined by multiplying them, and loaded by using glLoadMatrixf,
+ but more efficient way is to let OpenGL do the multiplication for you by using glMultMatrixf
+
diff --git a/sdl/Makefile b/sdl/Makefile
index 1ff2958..a2309bb 100644
--- a/sdl/Makefile
+++ b/sdl/Makefile
@@ -1,9 +1,3 @@
-#
-#
-# Note: object targets are not valid and get recompiled everytime simply
-# because they do not point to a valid location
-#
-
BIN = pyramid
SRC = pyramid.c math3d.c gltools.c glframe.c shader.c platform.c window.c
CC = gcc
@@ -21,32 +15,31 @@ SDL_image_CFLAGS := $(shell pkg-config --cflags SDL_image)
SDL_image_LDFLAGS := $(shell pkg-config --libs SDL_image)
OBJ_DIR = obj
-OBJ_REL = $(addsuffix .o, $(subst .c,,$(SRC)))
-OBJ_ABS = $(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(subst .c,,$(SRC))))
+OBJ_FILES = $(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(subst .c,,$(SRC))))
-$(BIN): $(OBJ_DIR) $(OBJ_REL)
- $(CC) $(LDFLAGS) $(SDL_LDFLAGS) $(SDL_image_LDFLAGS) $(OBJ_ABS) -o $@
+$(BIN): $(OBJ_DIR) $(OBJ_FILES)
+ $(CC) $(LDFLAGS) $(SDL_LDFLAGS) $(SDL_image_LDFLAGS) $(OBJ_FILES) -o $@
-pyramid.o: %.o: %.c math3d.h gltools.h glframe.h platform.h
- $(CC) -c $(CFLAGS) $(SDL_CFLAGS) $(SDL_image_CFLAGS) $< -o $(OBJ_DIR)/$@
+$(OBJ_DIR)/pyramid.o: $(OBJ_DIR)/%.o: %.c math3d.h gltools.h glframe.h platform.h
+ $(CC) -c $(CFLAGS) $(SDL_CFLAGS) $(SDL_image_CFLAGS) $< -o $@
-math3d.o: %.o: %.c %.h
- $(CC) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@
+$(OBJ_DIR)/math3d.o: $(OBJ_DIR)/%.o: %.c %.h
+ $(CC) -c $(CFLAGS) $< -o $@
-gltools.o: %.o: %.c %.h math3d.h
- $(CC) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@
+$(OBJ_DIR)/gltools.o: $(OBJ_DIR)/%.o: %.c %.h math3d.h
+ $(CC) -c $(CFLAGS) $< -o $@
-glframe.o: %.o: %.c %.h math3d.h
- $(CC) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@
+$(OBJ_DIR)/glframe.o: $(OBJ_DIR)/%.o: %.c %.h math3d.h
+ $(CC) -c $(CFLAGS) $< -o $@
-shader.o: %.o: %.c %.h
- $(CC) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@
+$(OBJ_DIR)/shader.o: $(OBJ_DIR)/%.o: %.c %.h
+ $(CC) -c $(CFLAGS) $< -o $@
-platform.o: %.o: %.c %.h
- $(CC) -c $(CFLAGS) $(SDL_CFLAGS) $(SDL_image_CFLAGS) $< -o $(OBJ_DIR)/$@
+$(OBJ_DIR)/platform.o: $(OBJ_DIR)/%.o: %.c %.h
+ $(CC) -c $(CFLAGS) $(SDL_CFLAGS) $(SDL_image_CFLAGS) $< -o $@
-window.o: %.o: %.c %.h
- $(CC) -c $(CFLAGS) $(SDL_CFLAGS) $(SDL_image_CFLAGS) $< -o $(OBJ_DIR)/$@
+$(OBJ_DIR)/window.o: $(OBJ_DIR)/%.o: %.c %.h
+ $(CC) -c $(CFLAGS) $(SDL_CFLAGS) $(SDL_image_CFLAGS) $< -o $@
$(OBJ_DIR):
mkdir -p $(OBJ_DIR)
@@ -58,4 +51,4 @@ clean:
@rm -f $(BIN)
info:
- @echo $(OBJ_REL)
+ @echo $(OBJ_FILES)
diff --git a/sdl/glframe.c b/sdl/glframe.c
index fbf8cd6..16a91cb 100644
--- a/sdl/glframe.c
+++ b/sdl/glframe.c
@@ -2,8 +2,6 @@
*
* Camera
*
- * notes: we could make these functions to be inlined, but they would need
- * to be moved into a header
*
*/
@@ -63,6 +61,18 @@ inline void glframe_get_camera_orientation(GLFrame *frame, M3DMatrix44f m)
/* perform viewing or modeling transformations */
inline void glframe_apply_camera_transform(GLFrame *frame, const int rot_only)
{
+ if (rot_only)
+ {
+ gluLookAt(frame->v_location[0], frame->v_location[1], frame->v_location[2],
+ frame->v_location[0] + frame->v_forward[0],
+ frame->v_location[1] + frame->v_forward[1],
+ frame->v_location[2] + frame->v_forward[2],
+ frame->v_up[0], frame->v_up[1], frame->v_up[2]);
+
+ return;
+ }
+
+ /* do it the hard way */
M3DMatrix44f m;
glframe_get_camera_orientation(frame, m);
glMultMatrixf(m);
@@ -70,14 +80,6 @@ inline void glframe_apply_camera_transform(GLFrame *frame, const int rot_only)
if (!rot_only)
glTranslatef(-frame->v_location[0], -frame->v_location[1],
-frame->v_location[2]);
-
-#if 0
- gluLookAt(frame->v_location[0], frame->v_location[1], frame->v_location[2],
- frame->v_location[0] + frame->v_forward[0],
- frame->v_location[1] + frame->v_forward[1],
- frame->v_location[2] + frame->v_forward[2],
- frame->v_up[0], frame->v_up[1], frame->v_up[2]);
-#endif
}
inline void glframe_move_forward(GLFrame *frame, const float delta)
diff --git a/sdl/gltools.c b/sdl/gltools.c
index 7db2410..2c2c0c4 100644
--- a/sdl/gltools.c
+++ b/sdl/gltools.c
@@ -2,7 +2,7 @@
*
* OpenGL Tools
*
- * notes: some function modify current color, just fyi
+ * notes: some function modify current color, just FYI
*
*/
@@ -37,7 +37,7 @@ GLint gltWriteTGA(const char *szFileName)
return 0;
}
- /* read bits from color buffer */
+ /* change how bits are stored */
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glPixelStorei(GL_PACK_SKIP_ROWS, 0);
@@ -427,7 +427,6 @@ void gltDrawUnitAxes(void)
}
/* draw a torus (doughnut), using the current 1d texture for light shading */
-/* this funct accepts 4x4 trans matrix to be applied to the vertices */
void gltDrawTorus(GLfloat majorRadius, GLfloat minorRadius, GLint numMajor,
GLint numMinor)
{
diff --git a/sdl/grass.tga b/sdl/grass.tga
new file mode 100644
index 0000000..81699a2
--- /dev/null
+++ b/sdl/grass.tga
Binary files differ
diff --git a/sdl/math3d.c b/sdl/math3d.c
index ca60233..b22c530 100644
--- a/sdl/math3d.c
+++ b/sdl/math3d.c
@@ -4,9 +4,7 @@
*
*/
-#include <math.h>
#include <stdio.h>
-#include <string.h>
#include "math3d.h"
void m3dFindNormalf(M3DVector3f result, const M3DVector3f point1,
diff --git a/sdl/math3d.h b/sdl/math3d.h
index 875757c..d9d9117 100644
--- a/sdl/math3d.h
+++ b/sdl/math3d.h
@@ -111,8 +111,6 @@ static inline void m3dSetMatrixColumn44f(M3DMatrix44f dst, const M3DVector4f src
memcpy(dst + (4 * col), src, sizeof(float) * 4);
}
-
-
static inline void m3dLoadVector2f(M3DVector2f v, const float x, const float y)
{
v[0] = x;
diff --git a/sdl/platform.c b/sdl/platform.c
index 55f6555..7542d11 100644
--- a/sdl/platform.c
+++ b/sdl/platform.c
@@ -13,6 +13,7 @@
/* few light arrays */
GLfloat fNoLight[] = { 0.0f, 0.0f, 0.0f, 0.0f };
GLfloat fLowLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
+GLfloat fMedLight[] = { 0.50f, 0.50f, 0.50f, 1.0f };
GLfloat fBrightLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
/* light values and coordinates */
@@ -36,26 +37,29 @@ void setup_opengl(void)
glFogf(GL_FOG_END, 20.0f);
glFogi(GL_FOG_MODE, GL_LINEAR); /* fog equation */
- glEnable(GL_DEPTH_TEST); /* hidden surface removal */
- glFrontFace(GL_CCW); /* counter clock-wise polygons face out */
- glEnable(GL_CULL_FACE); /* do not calculate inside of a pyramid */
+ glEnable(GL_DEPTH_TEST); /* hidden surface removal */
+ glFrontFace(GL_CCW); /* counter clock-wise polygons face out */
+ glEnable(GL_CULL_FACE); /* do not calculate inside of a pyramid */
+
+ /* enable lighting */
+ glEnable(GL_LIGHTING);
+
+ /* global illumination, ambient RGBA intensity of the entire scene */
+ glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fLowLight);
/* setup and enable light 0 */
- /* ambient RGBA intensity of the entire scene */
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, whiteLight);
- glLightfv(GL_LIGHT0, GL_AMBIENT, sourceLight);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight);
+ glLightfv(GL_LIGHT0, GL_AMBIENT, fMedLight);
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, fMedLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
-
- /* enable lighting */
- glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
/* enable color tracking */
glEnable(GL_COLOR_MATERIAL);
- /* set Material properties to follow glColor values */
+ /* set material properties to follow glColor values */
+ /* all primitives specified after the glMaterial call are affected by the
+ * last values set, until another call to glMaterial is made */
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, fBrightLight);
glMateriali(GL_FRONT, GL_SHININESS, 128);
@@ -67,34 +71,27 @@ void setup_opengl(void)
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
- /* screws up snowman */
+
+ /* screws up snowman, this has been replaced by superior multisampling */
//glEnable(GL_POLYGON_SMOOTH);
//glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
- /* gray background */
- glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
-
- /* original texture loading used to be here */
-
- /* texture filtering, we modify default values since we don't have mipmaps */
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- 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);
+ /* multisampling for polygons, conflicts with *polygon* anti-aliasing */
+ glEnable(GL_MULTISAMPLE);
/* how OpenGL combines the colors from texels with the color of the underlying
* geometry is controlled by the texture environment mode */
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
/* once texture is loaded and enabled, it will applied to every primitive
- * that specifies coordinates */
+ * that specifies coordinates, ORLY? */
glEnable(GL_TEXTURE_2D);
- /* multisampling for polygons, conflicts with anti-aliasing */
- //glEnable(GL_MULTISAMPLE);
-
/* don't know about this, but why not */
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
+
+ /* gray background */
+ glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
}
void setup_sdl(void)
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();