Skip to content

Commit

Permalink
Final files that I’m turning in (with all tabs changed to spaces)
Browse files Browse the repository at this point in the history
  • Loading branch information
Martination committed Mar 1, 2017
1 parent 0d4c9c2 commit cfa557c
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 161 deletions.
3 changes: 1 addition & 2 deletions Part_2/58/540texture.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ void texDestroy(texTexture *tex) {
/* At the start of rendering a frame, the renderer calls this function, to hook
the texture into a certain texture unit. textureUnit is something like
GL_TEXTURE0. textureUnitIndex would then be 0. */
void texRender(texTexture *tex, GLenum textureUnit, GLint textureUnitIndex,
GLint textureLoc) {
void texRender(texTexture *tex, GLenum textureUnit, GLint textureUnitIndex, GLint textureLoc) {
glActiveTexture(textureUnit);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex->openGL);
Expand Down
3 changes: 1 addition & 2 deletions Part_2/59/580mesh.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ struct meshMesh {
Does not actually fill in those triangles or vertices with useful data. When
you are finished with the mesh, you must call meshDestroy to deallocate its
backing resources. */
int meshInitialize(meshMesh *mesh, GLuint triNum, GLuint vertNum,
GLuint attrDim) {
int meshInitialize(meshMesh *mesh, GLuint triNum, GLuint vertNum, GLuint attrDim) {
mesh->tri = (GLuint *)malloc(triNum * 3 * sizeof(GLuint) +
vertNum * attrDim * sizeof(GLdouble));
if (mesh->tri != NULL) {
Expand Down
10 changes: 3 additions & 7 deletions Part_2/59/580scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ void sceneRender(sceneNode *node, GLdouble parent[4][4], GLint modelingLoc,
GLuint unifNum, GLuint unifDims[], GLint unifLocs[],
GLuint vaoIndex, GLint textureLocs[]) {

if (node == NULL) {
return;
}
if (node == NULL) { return; }

/* Set the uniform modeling matrix. */
GLdouble isometry[4][4];
Expand Down Expand Up @@ -248,14 +246,12 @@ void sceneRender(sceneNode *node, GLdouble parent[4][4], GLint modelingLoc,

if (node->firstChild != NULL) {
sceneRender(node->firstChild, renIsometry, modelingLoc,
unifNum, unifDims, unifLocs,
vaoIndex, textureLocs);
unifNum, unifDims, unifLocs, vaoIndex, textureLocs);
}

if (node->nextSibling != NULL) {
sceneRender(node->nextSibling, parent, modelingLoc,
unifNum, unifDims, unifLocs,
vaoIndex, textureLocs);
unifNum, unifDims, unifLocs, vaoIndex, textureLocs);
}

}
48 changes: 15 additions & 33 deletions Part_2/59/590mainShadowing.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/* On macOS, compile with...
clang -o shadow 590mainShadowing.c /usr/local/gl3w/src/gl3w.o
-lglfw -framework OpenGL -framework CoreFoundation
-lglfw -framework OpenGL -framework CoreFoundation; ./shadow
*/

#include <stdio.h>
Expand All @@ -17,20 +17,6 @@
#include <GLFW/glfw3.h>
#include <sys/time.h>

double getTime(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
}

#define GLFW_KEY_ZOOM_IN 334
#define GLFW_KEY_ZOOM_OUT 333

#define GLFW_KEY_NUMPAD_4 324
#define GLFW_KEY_NUMPAD_6 326
#define GLFW_KEY_NUMPAD_8 328
#define GLFW_KEY_NUMPAD_5 325

#include "500shader.c"
#include "530vector.c"
#include "580mesh.c"
Expand All @@ -45,13 +31,10 @@ camCamera cam;
texTexture texH, texV, texW, texT, texL;
meshGLMesh meshH, meshV, meshW, meshT, meshL;
sceneNode nodeH, nodeV, nodeW, nodeT, nodeL;
/* We need just one shadow program, because all of our meshes have the same
attribute structure. */
shadowProgram sdwProg;
/* We need one shadow map per shadow-casting light. */
lightLight lightA, lightB;
shadowMap sdwMapA, sdwMapB;
/* The main shader program has extra hooks for shadowing. */

GLuint program;
GLint viewingLoc, modelingLoc;
GLint unifLocs[1], textureLocs[1];
Expand All @@ -62,6 +45,11 @@ GLint camPosLoc;
GLint viewingSdwALoc, textureSdwALoc;
GLint viewingSdwBLoc, textureSdwBLoc;

double getTime(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
}

void handleError(int error, const char *description) {
fprintf(stderr, "handleError: %d\n%s\n", error, description);
Expand All @@ -72,16 +60,17 @@ void handleResize(GLFWwindow *window, int width, int height) {
camSetWidthHeight(&cam, width, height);
}

void handleKey(GLFWwindow *window, int key, int scancode, int action,
int mods) {
void handleKey(GLFWwindow *window, int key, int scancode, int action, int mods) {
int shiftIsDown = mods & GLFW_MOD_SHIFT;
int controlIsDown = mods & GLFW_MOD_CONTROL;
int altOptionIsDown = mods & GLFW_MOD_ALT;
int superCommandIsDown = mods & GLFW_MOD_SUPER;

if (action == GLFW_PRESS && key == GLFW_KEY_SPACE) {
camSwitchProjectionType(&cam);
} else if (action == GLFW_PRESS || action == GLFW_REPEAT) {

// Camera Controls
if (key == GLFW_KEY_J)
camAddTheta(&cam, -0.1);
else if (key == GLFW_KEY_L)
Expand All @@ -95,6 +84,7 @@ void handleKey(GLFWwindow *window, int key, int scancode, int action,
else if (key == GLFW_KEY_O)
camAddDistance(&cam, 0.5);

// Light 1 Controls
else if (key == GLFW_KEY_S) {
GLdouble vec[3];
vecCopy(3, lightA.translation, vec);
Expand Down Expand Up @@ -127,6 +117,7 @@ void handleKey(GLFWwindow *window, int key, int scancode, int action,
lightSetTranslation(&lightA, vec);
}

// Light 2 Controls
else if (key == GLFW_KEY_G) {
GLdouble vec[3];
vecCopy(3, lightB.translation, vec);
Expand Down Expand Up @@ -494,7 +485,7 @@ void render(void) {
/* Finish preparing the shadow maps, restore the viewport, and begin to
render the scene. */
shadowMapUnrender();

glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
Expand All @@ -519,12 +510,6 @@ void render(void) {
}

int main(void) {
// printf("Camera Controls\n");
// printf("UP:Arrow Up\nDOWN:Arrow Down\nLEFT:Arrow Left\nRIGHT:Arrow Right\n");
// printf("Light Controls\n");
// printf("UP:8\nDOWN:5\nLEFT:4\nRIGHT:6\n");
// printf("Zoom Controls\n");
// printf("IN:+\nOUT:-\n");
double oldTime;
double newTime = getTime();
glfwSetErrorCallback(handleError);
Expand Down Expand Up @@ -554,14 +539,11 @@ int main(void) {
}
fprintf(stderr, "main: OpenGL %s, GLSL %s.\n",
glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION));
/* We no longer do glDepthRange(1.0, 0.0). Instead we have changed our
projection matrices. */
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
if (initializeShaderProgram() != 0)
return 3;
/* Initialize the shadow mapping before the meshes. Why? */
if (initializeCameraLight() != 0)
return 4;
if (initializeScene() != 0)
Expand All @@ -575,7 +557,7 @@ int main(void) {
glfwSwapBuffers(window);
glfwPollEvents();
}
/* Deallocate more resources than ever. */

shadowProgramDestroy(&sdwProg);
shadowMapDestroy(&sdwMapA);
shadowMapDestroy(&sdwMapB);
Expand All @@ -584,4 +566,4 @@ int main(void) {
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
}
12 changes: 6 additions & 6 deletions Part_2/59/590matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

/* Pretty-prints the given matrix, with one line of text per row of matrix. */
void mat22Print(GLdouble m[2][2]) {
int i;
for (i = 0; i < 2; i += 1)
printf("%lf\t%lf\n", m[i][0], m[i][1]);
int i;
for (i = 0; i < 2; i += 1)
printf("%lf\t%lf\n", m[i][0], m[i][1]);
}

/* Returns the determinant of the matrix m. If the determinant is 0.0, then the
matrix is not invertible, and mInv is untouched. If the determinant is not 0.0,
then the matrix is invertible, and its inverse is placed into mInv. */
GLdouble mat22Invert(GLdouble m[2][2], GLdouble mInv[2][2]) {
GLdouble determinant;
GLdouble determinant;

determinant = m[0][0]*m[1][1] - m[0][1]*m[1][0];
determinant = fabs(determinant);
Expand All @@ -36,13 +36,13 @@ GLdouble mat22Invert(GLdouble m[2][2], GLdouble mInv[2][2]) {
/* Multiplies a 2x2 matrix m by a 2-column v, storing the result in mTimesV.
The output should not */
void mat221Multiply(GLdouble m[2][2], GLdouble v[2], GLdouble mTimesV[2]) {
mTimesV[0] = v[0]*m[0][0]+v[1]*m[0][1];
mTimesV[0] = v[0]*m[0][0]+v[1]*m[0][1];
mTimesV[1] = v[0]*m[1][0]+v[1]*m[1][1];
}

/* Fills the matrix m from its two columns. */
void mat22Columns(GLdouble col0[2], GLdouble col1[2], GLdouble m[2][2]) {
m[0][0] = col0[0];
m[0][0] = col0[0];
m[0][1] = col1[0];
m[1][0] = col0[1];
m[1][1] = col1[1];
Expand Down
Loading

0 comments on commit cfa557c

Please sign in to comment.