Very recently I had a school project to create a computer graphics library similar to gl library. I was not able to get the whole library to work (of course), but I did manage to get some of it to work. So i want to toss up what I manage to get done.
Matrix Class
int x =4; // matrix size int y =4; class gtMatrix { float[][] m; gtMatrix() {m = new float[x][y];} //multiply two matrices float[][] gtMatrixMulti(float[][] A, float[][] B){ m = new float[x][y]; for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { for(int k = 0; k < y; k++){ m[i][j] += A[i][k]*B[k][j]; }}} return m; } // multiply matrix with a vertex float[]gtMatrixMulti(float[][] A, float[] B){ gtVertex p = new gtVertex(); for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { for(int k = 0; k < y; k++){ p.v[i] += A[i][k]*B[k]; }}} return p.v; } // copy a matrix gtMatrix gtMatrixCopy(){ gtMatrix berry = new gtMatrix(); for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { berry.m[i][j] = this.m[i][j]; }} return berry; } // print out a matrix void gtMatrixPrint(){ m = this.m; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { print(this.m[i][j]); print( " "); if (j== y-1) println (" "); } } } fills a matrix with just one number, more for trouble shooting float[][] gtMatrixSolid(int A){ m = new float[x][y]; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { this.m[i][j] = A; }} return this.m;} }
Vertex Class
class gtVertex { float[] v; gtVertex() {v = new float[4];} gtVertex(int a) {v = new float[a];} // multiply a matrix with a vertex float[]gtVertexMulti(float[][] A, float[] B){ gtVertex p = new gtVertex(); for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { for(int k = 0; k < y; k++){ p.v[i] += A[i][k]*B[k]; }}} return p.v; } // get the cross product of a 2 vertices float[] gtVertexCross(float[] v1, float[] v2){ gtVertex out = new gtVertex(); int x = 0; int y =1; int z =2; out.v[x] = v1[y] * v2[z] - v1[z] * v2[y]; // Cross Product For Y - Z out.v[y] = v1[z] * v2[x] - v1[x] * v2[z]; // Cross Product For X - Z out.v[z] = v1[x] * v2[y] - v1[y] * v2[x]; // Cross Product For X - Y return out.v; } // print out vertex void gtVertexPrint(){ v = this.v; for (int i = 0; i < x; i++) { println(this.v[i]); }} // normalize a vector or make it unit length. small chance this could be wrong gtVertex gtVertexNormal(float[] A){ gtVertex unit = new gtVertex(); float num = sqrt(A[0]*A[0] + A[1]*A[1] + A[2]*A[2]); for(int i =0 ; i < 4; i++){ unit.v[i] = A[i]/num; } return unit; } }
CTM Stack or just a Matrix Stack
Ok this might get slightly confusing. The idea was to have an array of matrices. So every slot on the array (stack) would just hold one matrix, but if you need to scale or transform you would just multiply it by the matrix that it is currently pointing to. Errrr, this hard to explain ,or maybe its not. Here the code That I finished.
int inc = 0; // stack pointer gtMatrix stack[] = new gtMatrix[10]; // stack // creates the stack and places the identity matrix on the bottom void gtInitialize() { gtMatrix identity = new gtMatrix(); for (int i = 0; i < x; i++){ identity.m[i][i] = 1; } stack[0] = identity; } // copies the current matrix and places it up one while incrementing the pointer void gtPushMatrix() { stack[inc+1] = stack[inc].gtMatrixCopy(); if(inc < 5) inc++; } // pops off and erases the matrix there. just really makes it equal to zero and lowers the counter void gtPopMatrix() { gtMatrix zero = new gtMatrix(); stack[inc] = zero; if (inc != 0) inc --; else print("your already at the bottom"); } // translates points void gtTranslate(float tx, float ty, float tz) { gtMatrix apple = new gtMatrix(); for (int i = 0; i < x; i++){ apple.m[i][i] = 1;} apple.m[3][0] = tx; apple.m[3][1] = ty; apple.m[3][2] = tz; stack[inc].gtMatrixMulti(stack[inc].m, apple.m); } // scales a set up vertices or a figure void gtScale(float sx, float sy, float sz) { gtMatrix melon = new gtMatrix(); melon.m[0][0] = sx; melon.m[1][1] = sy; melon.m[2][2] = sz; melon.m[3][3] = 1; stack[inc].gtMatrixMulti(stack[inc].m, melon.m); }
Final Words
I am not posting this because it something great and I have to share it with the world. I instead wanted to maybe help someone to get started or to help them out with there homework (as a reference and not copy and paste). I hope this will at least help someone out there. Please send me an email if you have any question or to bash me on how bad the code is.