Model* billboardModel(void) { int vertexCount = 4; int triangleCount = 2; GLfloat vertexArray[] = { -1.0f,5.0f,0.0f, 1.0f,5.0f,0.0f, -1.0f,0.0f,0.0f, 1.0f,0.0f,0.0f}; GLfloat normalArray[] = { 0.0f,0.0f,1.0f, 0.0f,0.0f,1.0f, 0.0f,0.0f,1.0f, 0.0f,0.0f,1.0f,}; GLfloat texCoordArray[] = { 1.0f,0.0f, 0.0f,0.0f, 1.0f,1.0f, 0.0f,1.0f}; GLuint indexArray[] = { 0,2,1,1,2,3}; Model* m = LoadDataToModel( vertexArray, normalArray, texCoordArray, NULL, indexArray, vertexCount, triangleCount*3); return m; }
Model* GenerateTerrain(TextureData *tex) { int vertexCount = tex->width * tex->height; int triangleCount = (tex->width-1) * (tex->height-1) * 2; int x, z; GLfloat *vertexArray = malloc(sizeof(GLfloat) * 3 * vertexCount); GLfloat *normalArray = malloc(sizeof(GLfloat) * 3 * vertexCount); GLfloat *texCoordArray = malloc(sizeof(GLfloat) * 2 * vertexCount); GLuint *indexArray = malloc(sizeof(GLuint) * triangleCount*3); printf("bpp %d\n", tex->bpp); for (x = 0; x < tex->width; x++) for (z = 0; z < tex->height; z++) { // Vertex array. You need to scale this properly vertexArray[(x + z * tex->width)*3 + 0] = x / 1.0; vertexArray[(x + z * tex->width)*3 + 1] = tex->imageData[(x + z * tex->width) * (tex->bpp/8)] / 20.0; vertexArray[(x + z * tex->width)*3 + 2] = z / 1.0; // Normal vectors. You need to calculate these. normalArray[(x + z * tex->width)*3 + 0] = 0.0; normalArray[(x + z * tex->width)*3 + 1] = 1.0; normalArray[(x + z * tex->width)*3 + 2] = 0.0; // Texture coordinates. You may want to scale them. texCoordArray[(x + z * tex->width)*2 + 0] = x; // (float)x / tex->width; texCoordArray[(x + z * tex->width)*2 + 1] = z; // (float)z / tex->height; } for (x = 0; x < tex->width-1; x++) for (z = 0; z < tex->height-1; z++) { // Triangle 1 indexArray[(x + z * (tex->width-1))*6 + 0] = x + z * tex->width; indexArray[(x + z * (tex->width-1))*6 + 1] = x + (z+1) * tex->width; indexArray[(x + z * (tex->width-1))*6 + 2] = x+1 + z * tex->width; // Triangle 2 indexArray[(x + z * (tex->width-1))*6 + 3] = x+1 + z * tex->width; indexArray[(x + z * (tex->width-1))*6 + 4] = x + (z+1) * tex->width; indexArray[(x + z * (tex->width-1))*6 + 5] = x+1 + (z+1) * tex->width; } // End of terrain generation // Create Model and upload to GPU: Model* model = LoadDataToModel( vertexArray, normalArray, texCoordArray, NULL, indexArray, vertexCount, triangleCount*3); return model; }
Model* controlModel(void) { int vertexCount = 8; int triangleCount = 6; GLfloat vertexArray[] = {0.0f,0.0f,0.13f, 0.15f,0.0f,-0.13f, -0.15f,0.0f,-0.13f, 0.0f,0.3f,0.13f, 0.15f,0.3f,-0.13f, -0.15f,0.3f,-0.13f, 0.0f,0.0f,0.13f, 0.0f,0.3f,0.13f,}; GLfloat normalArray[] = {0.0f,0.0f,0.13f, 0.1f,0.0f,-0.13f, -0.1f,0.0f,-0.13f, 0.0f,0.0f,0.13f, 0.1f,0.0f,-0.13f, -0.1f,0.0f,-0.13f}; GLfloat texCoordArray[] = { 0.0f,0.0f, 0.6667f,0.0f, 0.3333f,0.0f, 0.0f,1.0f, 0.6667f,1.0f, 0.3333f,1.0f, 1.0f,0.0f, 1.0f,1.0f}; GLuint indexArray[] = { 0,3,2, 3,5,2, 2,5,1, 5,4,1, 1,4,6, 4,7,6}; Model* m = LoadDataToModel( vertexArray, normalArray, texCoordArray, NULL, indexArray, vertexCount, triangleCount*3); return m; }
Model* generateCanvas() { // Create canvas to draw on GLfloat square[] = { -1, -1, 0, -1, 1, 0, 1, 1, 0, 1, -1, 0 }; GLfloat squareTexCoord[] = { 0, 0, 0, 1, 1, 1, 1, 0 }; GLuint squareIndices[] = { 0, 1, 2, 0, 2, 3 }; return LoadDataToModel(square, NULL, squareTexCoord, NULL, squareIndices, 4, 6); free(squareIndices); free(square); free(squareTexCoord); }
Model* generateCube(GLfloat s) { GLfloat vertexArray[3 * 8] = { -s, -s, s, s, -s, s, s, s, s, -s, s, s, -s, -s, -s, s, -s, -s, s, s, -s, -s, s, -s}; GLuint indexArray[6 * 2 * 3] = {0, 1, 2, 2, 3, 0, 3, 2, 6, 6, 7, 3, 7, 6, 5, 5, 4, 7, 4, 0, 3, 3, 7, 4, 0, 1, 5, 5, 4, 0, 1, 5, 6, 6, 2, 1}; // Create Model and upload to GPU. return LoadDataToModel(vertexArray, NULL, NULL, NULL, indexArray, 8, 6 * 2 * 3); }
///////////////////////////////////////// // M A I N // int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(512, 512); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitContextVersion(3, 2); // Might not be needed in Linux glutCreateWindow("Them bones, them bones"); glutDisplayFunc(DisplayWindow); glutTimerFunc(20, &OnTimer, 0); glutKeyboardFunc( keyboardFunc ); glutReshapeFunc(reshape); // Set up depth buffer glEnable(GL_DEPTH_TEST); // initiering #ifdef WIN32 glewInit(); #endif BuildCylinder(); setupBones(); initBoneWeights(); // Build Model from cylinder data cylinderModel = LoadDataToModel( (GLfloat*) g_vertsRes, (GLfloat*) g_normalsRes, (GLfloat*) g_boneWeightVis, // texCoords NULL, // (GLfloat*) g_boneWeights, // colors (GLuint*) g_poly, // indices kMaxRow*kMaxCorners, kMaxg_poly * 3); g_shader = loadShaders("shader.vert" , "shader.frag"); glutMainLoop(); return 0; }
Model * generate_terrain(int size) { int i, j, x, z; int tri_count = (size-1)*(size-1)*2; int count = size*size; Point3D tmp_normal; GLfloat *vertices = malloc(sizeof(GLfloat) * 3 * size * size); GLfloat *normals = malloc(sizeof(GLfloat) * 3 * size * size); GLuint *indices = malloc(sizeof(GLuint) * 3 * tri_count); for(x=0;x < size; x++) { for(z = 0;z < size; z++) { vertices[(z + x*size)*3 + 0] = x*6; vertices[(z + x*size)*3 + 1] = 0*(random()-.5); vertices[(z + x*size)*3 + 2] = z*6; } } for(x=0;x < size; x++) { for(z = 0;z < size; z++) { calc_normal(vertices, x, z, size, &tmp_normal); normals[(z + x*size)*3 + 0] = tmp_normal.x; normals[(z + x*size)*3 + 1] = tmp_normal.y; normals[(z + x*size)*3 + 2] = tmp_normal.z; } } for(x=0;x < size-1; x++) { for(z = 0;z < size-1; z++) { indices[(x + z*(size-1))*6 + 0] = x + z*size; indices[(x + z*(size-1))*6 + 1] = x + (z+1)*size; indices[(x + z*(size-1))*6 + 2] = x + 1 + z*size; indices[(x + z*(size-1))*6 + 3] = x + 1 + z*size; indices[(x + z*(size-1))*6 + 4] = x + (z+1)*size; indices[(x + z*(size-1))*6 + 5] = x + 1 + (z+1)*size; } } //int j=0; //for(j=0;j < 30;j++) // spatial_smooth(vertices, size); GLfloat Xre[size*size*6*6]; GLfloat Xim[size*size*6*6]; //Xre = malloc(sizeof(GLfloat)*size*size); //Xim = malloc(sizeof(GLfloat)*size*size); fft(vertices, size, Xre, Xim); filter(Xre, Xim, size); ifft(vertices, size, Xre, Xim); //printf("%f\n", Xre[0][0]); Model * m = LoadDataToModel( vertices, normals, NULL, NULL, indices, size*size, (size-1)*(size-1)*2*3); return m; }
Model* GenerateTerrain(TextureData *tex) { int vertexCount = tex->width * tex->height; int triangleCount = (tex->width-1) * (tex->height-1) * 2; int x, z; GLfloat *vertexArray = malloc(sizeof(GLfloat) * 3 * vertexCount); GLfloat *normalArray = malloc(sizeof(GLfloat) * 3 * vertexCount); GLfloat *texCoordArray = malloc(sizeof(GLfloat) * 2 * vertexCount); GLuint *indexArray = malloc(sizeof(GLuint) * triangleCount*3); printf("bpp %d\n", tex->bpp); for (x = 0; x < tex->width; x++) for (z = 0; z < tex->height; z++) { // Vertex array. You need to scale this properly vertexArray[(x + z * tex->width)*3 + 0] = x / 1.0; vertexArray[(x + z * tex->width)*3 + 1] = tex->imageData[(x + z * tex->width) * (tex->bpp/8)] / 20.0; vertexArray[(x + z * tex->width)*3 + 2] = z / 1.0; // Normal vectors. You need to calculate these. normalArray[(x + z * tex->width)*3 + 0] = 0.0; normalArray[(x + z * tex->width)*3 + 1] = 1.0; normalArray[(x + z * tex->width)*3 + 2] = 0.0; // Texture coordinates. You may want to scale them. texCoordArray[(x + z * tex->width)*2 + 0] = x; // (float)x / tex->width; texCoordArray[(x + z * tex->width)*2 + 1] = z; // (float)z / tex->height; } // Normal vector calculation pass // p3 // p2 p1 p4 // p5 // T1: v(x,z) -> v(x,z-1) -> v(x-1,z) = p1 -> p3 -> p2 // T2: v(x,z) -> v(x+1,z) -> v(x,z-1) = p1 -> p4 -> p3 // T3: v(x,z) -> v(x,z+1) -> v(x+1,z) = p1 -> p5 -> p4 // T4: v(x,z) -> v(x-1,z) -> v(x,z+1) = p1 -> p2 -> p5 vec3 n1, n2, n3, n4, p1, p2, p3, p4, p5; for (x = 0; x < tex->width-1; x++) for (z = 0; z < tex->height-1; z++) { if (z == 0 || x == 0) continue; // leave edges untouched for now p1.x = vertexArray[(x + z * tex->width)*3 + 0]; p1.y = vertexArray[(x + z * tex->width)*3 + 1]; p1.z = vertexArray[(x + z * tex->width)*3 + 2]; p2.x = vertexArray[((x-1) + z * tex->width)*3 + 0]; p2.y = vertexArray[((x-1) + z * tex->width)*3 + 1]; p2.z = vertexArray[((x-1) + z * tex->width)*3 + 2]; p3.x = vertexArray[(x + (z-1) * tex->width)*3 + 0]; p3.y = vertexArray[(x + (z-1) * tex->width)*3 + 1]; p3.z = vertexArray[(x + (z-1) * tex->width)*3 + 2]; p4.x = vertexArray[((x+1) + z * tex->width)*3 + 0]; p4.y = vertexArray[((x+1) + z * tex->width)*3 + 1]; p4.z = vertexArray[((x+1) + z * tex->width)*3 + 2]; p5.x = vertexArray[(x + (z+1) * tex->width)*3 + 0]; p5.y = vertexArray[(x + (z+1) * tex->width)*3 + 1]; p5.z = vertexArray[(x + (z+1) * tex->width)*3 + 2]; /*printf("p1: "); printVec3(p1); printf("p2: "); printVec3(p2); printf("p3: "); printVec3(p3); printf("p4: "); printVec3(p4); printf("p5: "); printVec3(p5); */ n1 = Normalize(CalcNormalVector(p1,p3,p2)); n2 = Normalize(CalcNormalVector(p1,p4,p3)); n3 = Normalize(CalcNormalVector(p1,p5,p4)); n4 = Normalize(CalcNormalVector(p1,p2,p5)); /*printf("n1: "); printVec3(n1); printf("n2: "); printVec3(n2); printf("n3: "); printVec3(n3); printf("n4: "); printVec3(n4); */ // Average the 4 triangle normals n1 = VectorAdd(VectorAdd(n1,n2), VectorAdd(n3,n4)); n1 = Normalize(n1); normalArray[(x + z * tex->width)*3 + 0] = n1.x; normalArray[(x + z * tex->width)*3 + 1] = n1.y; normalArray[(x + z * tex->width)*3 + 2] = n1.z; //printf("result: "); printVec3(n1); } for (x = 0; x < tex->width-1; x++) for (z = 0; z < tex->height-1; z++) { // Triangle 1 indexArray[(x + z * (tex->width-1))*6 + 0] = x + z * tex->width; indexArray[(x + z * (tex->width-1))*6 + 1] = x + (z+1) * tex->width; indexArray[(x + z * (tex->width-1))*6 + 2] = x+1 + z * tex->width; // Triangle 2 indexArray[(x + z * (tex->width-1))*6 + 3] = x+1 + z * tex->width; indexArray[(x + z * (tex->width-1))*6 + 4] = x + (z+1) * tex->width; indexArray[(x + z * (tex->width-1))*6 + 5] = x+1 + (z+1) * tex->width; } // End of terrain generation // Create Model and upload to GPU: Model* model = LoadDataToModel( vertexArray, normalArray, texCoordArray, NULL, indexArray, vertexCount, triangleCount*3); return model; }
void GenerateTerrain(Maze* maze) { GLfloat b; GLfloat skirt = 0; int pit_start = 2000; if (maze->obstacle == 0) { pit_start = maze->obstacle_x_pos - 5; } int x, z; int height_g = height_; int width_g = maze->length; maze->width = height_g-1; int vertexCount = width_g * height_g; int triangleCount = (width_g-1) * (height_g-1) * 2; GLfloat *vertexArray = new GLfloat[sizeof(GLfloat) * 3 * vertexCount]; GLfloat *normalArray = new GLfloat[sizeof(GLfloat) * 3 * vertexCount]; GLfloat *texCoordArray = new GLfloat[sizeof(GLfloat) * 2 * vertexCount]; GLuint *indexArray = new GLuint[sizeof(GLuint) * triangleCount*3]; GLfloat last_b, pit = 0; for (x = 0; x < width_g; x++) for (z = 0; z < height_g; z++) { //if (z < 2 || z > height_g -2) { if (z == 0 || z == height_g -1) { skirt = 1.5; // } //else // skirt = 1; } else{ skirt = 0; } if (x > pit_start && x < pit_start + 10) { pit = 30; } else{ pit = 0; } // Vertex array. You need to scale this properly vertexArray[(x + z * width_g)*3 + 0] = x / 1.0; if (x < width_g - height_g) { vertexArray[(x + z * width_g)*3 + 1] = 1.0*b - skirt - pit; //+ x*0.1; last_b = vertexArray[(x + z * width_g)*3 + 1] + skirt + pit; } else{ vertexArray[(x + z * width_g)*3 + 1] = last_b - skirt - pit; } vertexArray[(x + z * width_g)*3 + 2] = z / 1.0; // Beräkna normalvektorer vec3 vector1 = vec3(0,0,0); vec3 vector2 = vec3(0,0,0); vec3 normalvektor = vec3(0, 1, 0); if(x > 0 && z > 0){ vector1.x = vertexArray[(x-1 + z * width_g)*3 + 0] - vertexArray[(x + z * width_g)*3 + 0]; vector1.y = vertexArray[(x-1 + z * width_g)*3 + 1] - vertexArray[(x + z * width_g)*3 + 1]; vector1.z = vertexArray[(x-1 + z * width_g)*3 + 2] - vertexArray[(x + z * width_g)*3 + 2]; vector2.x = vertexArray[(x + (z-1) * width_g)*3 + 0] - vertexArray[(x + z * width_g)*3 + 0]; vector2.y = vertexArray[(x + (z-1) * width_g)*3 + 1] - vertexArray[(x + z * width_g)*3 + 1]; vector2.z = vertexArray[(x + (z-1) * width_g)*3 + 2] - vertexArray[(x + z * width_g)*3 + 2]; normalvektor = CrossProduct(vector2, vector1); } // Normal vectors. You need to calculate these. normalArray[(x + z * width_g)*3 + 0] = normalvektor.x; normalArray[(x + z * width_g)*3 + 1] = normalvektor.y; normalArray[(x + z * width_g)*3 + 2] = normalvektor.z; // Texture coordinates. You may want to scale them. texCoordArray[(x + z * width_g)*2 + 0] = 2.0*(float)x / width_g; texCoordArray[(x + z * width_g)*2 + 1] = 2.0*(float)z / height_g; } for (x = 0; x < width_g-1; x++) for (z = 0; z < height_g-1; z++) { // Triangle 1 indexArray[(x + z * (width_g-1))*6 + 0] = x + z * width_g; indexArray[(x + z * (width_g-1))*6 + 1] = x + (z+1) * width_g; indexArray[(x + z * (width_g-1))*6 + 2] = x+1 + z * width_g; // Triangle 2 indexArray[(x + z * (width_g-1))*6 + 3] = x+1 + z * width_g; indexArray[(x + z * (width_g-1))*6 + 4] = x + (z+1) * width_g; indexArray[(x + z * (width_g-1))*6 + 5] = x+1 + (z+1) * width_g; } // End of terrain generation // Create Model and upload to GPU: maze->track = LoadDataToModel( vertexArray, normalArray, texCoordArray, NULL, indexArray, vertexCount, triangleCount*3); maze->length = width_g; maze->width = height_g; }
Model* GenerateTerrain(TextureData *tex) { int vertexCount = tex->width * tex->height; int triangleCount = (tex->width-1) * (tex->height-1) * 2; int x, z; //GLfloat *vertexArray = malloc(sizeof(GLfloat) * 3 * vertexCount); vertexArray = malloc(sizeof(GLfloat) * 3 * vertexCount); GLfloat *normalArray = malloc(sizeof(GLfloat) * 3 * vertexCount); for(int i = 0; i < 3*vertexCount;i++){ normalArray[i] = 0; } GLfloat *texCoordArray = malloc(sizeof(GLfloat) * 2 * vertexCount); GLuint *indexArray = malloc(sizeof(GLuint) * triangleCount*3); printf("bpp %d\n", tex->bpp); // Temporary normal vector Point3D normal; for (x = 0; x < tex->width; x++) for (z = 0; z < tex->height; z++) { // Vertex array. You need to scale this properly vertexArray[(x + z * tex->width)*3 + 0] = x / 1.0; vertexArray[(x + z * tex->width)*3 + 1] = tex->imageData[(x + z * tex->width) * (tex->bpp/8)] / 5.0; //vertexArray[(x + z * tex->width)*3 + 1] = 0; vertexArray[(x + z * tex->width)*3 + 2] = z / 1.0; // Normal vectors. You need to calculate these. if(x==0){ // Doing something to get rid of warnings int a = 0; a += 1; } else if(x>1 && x<(tex->width-1) && z > 0 && z < (tex->height-1)){ //printf("In general case: x=%d, z=%d\n", x, z); //Lower triangle make_vertice_normal(vertexArray, x, z, tex->width, false, &normal); //printf("Normal: (%f,%f,%f)\n", normal.x, normal.y, normal.z); ScalarMult(&normal, 1.0/6, &normal); put_vertex_normal(normalArray, x, z-1, tex->width, &normal); put_vertex_normal(normalArray, x-1, z, tex->width, &normal); put_vertex_normal(normalArray, x, z, tex->width, &normal); //Upper triangle make_vertice_normal(vertexArray, x, z, tex->width, true, &normal); ScalarMult(&normal, 1.0/6, &normal); put_vertex_normal(normalArray, x-1, z, tex->width, &normal); put_vertex_normal(normalArray, x-1, z+1, tex->width, &normal); put_vertex_normal(normalArray, x, z, tex->width, &normal); } else if(x==1 && z==0){ //printf("In bottom left corner: x=%d, z=%d\n", x, z); //Upper triangle make_vertice_normal(vertexArray, x, z, tex->width, true, &normal); //printf("Normal: (%f,%f,%f)\n", normal.x, normal.y, normal.z); put_vertex_normal(normalArray, 0, 0, tex->width, &normal); ScalarMult(&normal, 1.0/3, &normal); put_vertex_normal(normalArray, 0, 1, tex->width, &normal); put_vertex_normal(normalArray, 1, 1, tex->width, &normal); } else if(x==tex->width-1 && z == tex->height-1){ //printf("In top right corner: x=%d, z=%d\n", x, z); //Lower triangle make_vertice_normal(vertexArray, x, z, tex->width, false, &normal); //printf("Normal: (%f,%f,%f)\n", normal.x, normal.y, normal.z); put_vertex_normal(normalArray, x, z, tex->width, &normal); ScalarMult(&normal, 1.0/3, &normal); put_vertex_normal(normalArray, x, z-1, tex->width, &normal); put_vertex_normal(normalArray, x-1, z, tex->width, &normal); } else if(x==1 && z < tex->height-1){ //printf("In left edge: x=%d, z=%d\n", x, z); // Left edge // Lower triangle make_vertice_normal(vertexArray, x, z, tex->width, false, &normal); //printf("Normal: (%f,%f,%f)\n", normal.x, normal.y, normal.z); ScalarMult(&normal, 1.0/3, &normal); put_vertex_normal(normalArray, x, z-1, tex->width, &normal); put_vertex_normal(normalArray, x-1, z, tex->width, &normal); ScalarMult(&normal, 1.0/2, &normal); put_vertex_normal(normalArray, x, z, tex->width, &normal); // Upper Triangle make_vertice_normal(vertexArray, x, z, tex->width, true, &normal); //printf("Normal: (%f,%f,%f)\n", normal.x, normal.y, normal.z); ScalarMult(&normal, 1.0/3, &normal); put_vertex_normal(normalArray, x-1, z, tex->width, &normal); put_vertex_normal(normalArray, x-1, z+1, tex->width, &normal); ScalarMult(&normal, 1.0/2, &normal); put_vertex_normal(normalArray, x, z, tex->width, &normal); } else if(z==0){ //printf("In bottom edge: x=%d, z=%d\n", x, z); // Bottom edge // Upper Triangle make_vertice_normal(vertexArray, x, z, tex->width, true, &normal); //printf("Normal: (%f,%f,%f)\n", normal.x, normal.y, normal.z); ScalarMult(&normal, 1.0/3, &normal); put_vertex_normal(normalArray, x, z, tex->width, &normal); put_vertex_normal(normalArray, x-1, z, tex->width, &normal); ScalarMult(&normal, 1.0/2, &normal); put_vertex_normal(normalArray, x-1, z+1, tex->width, &normal); } else if(x==tex->width-1 && z < tex->height-1){ //printf("In right edge: x=%d, z=%d\n", x, z); // Right edge // Lower triangle make_vertice_normal(vertexArray, x, z, tex->width, false, &normal); //printf("Normal: (%f,%f,%f)\n", normal.x, normal.y, normal.z); ScalarMult(&normal, 1.0/3, &normal); put_vertex_normal(normalArray, x, z-1, tex->width, &normal); put_vertex_normal(normalArray, x, z, tex->width, &normal); ScalarMult(&normal, 1.0/2, &normal); put_vertex_normal(normalArray, x-1, z, tex->width, &normal); // Upper Triangle make_vertice_normal(vertexArray, x, z, tex->width, true, &normal); //printf("Normal: (%f,%f,%f)\n", normal.x, normal.y, normal.z); ScalarMult(&normal, 1.0/3, &normal); put_vertex_normal(normalArray, x, z, tex->width, &normal); ScalarMult(&normal, 1.0/2, &normal); put_vertex_normal(normalArray, x-1, z, tex->width, &normal); put_vertex_normal(normalArray, x-1, z+1, tex->width, &normal); } else if(z==tex->height-1){ //printf("In top edge: x=%d, z=%d\n", x, z); // Top edge // Lower triangle make_vertice_normal(vertexArray, x, z, tex->width, false, &normal); //printf("Normal: (%f,%f,%f)\n", normal.x, normal.y, normal.z); ScalarMult(&normal, 1.0/3, &normal); put_vertex_normal(normalArray, x-1, z, tex->width, &normal); put_vertex_normal(normalArray, x, z, tex->width, &normal); ScalarMult(&normal, 1.0/2, &normal); put_vertex_normal(normalArray, x, z-1, tex->width, &normal); } // Texture coordinates. You may want to scale them. texCoordArray[(x + z * tex->width)*2 + 0] = 5*(float)x / tex->width; texCoordArray[(x + z * tex->width)*2 + 1] = 5*(float)z / tex->height; } for (x = 0; x < tex->width-1; x++) for (z = 0; z < tex->height-1; z++) { // Triangle 1 indexArray[(x + z * (tex->width-1))*6 + 0] = x + z * tex->width; indexArray[(x + z * (tex->width-1))*6 + 1] = x + (z+1) * tex->width; indexArray[(x + z * (tex->width-1))*6 + 2] = x+1 + z * tex->width; // Triangle 2 indexArray[(x + z * (tex->width-1))*6 + 3] = x+1 + z * tex->width; indexArray[(x + z * (tex->width-1))*6 + 4] = x + (z+1) * tex->width; indexArray[(x + z * (tex->width-1))*6 + 5] = x+1 + (z+1) * tex->width; } // End of terrain generation // Create Model and upload to GPU: Model* model = LoadDataToModel( vertexArray, normalArray, texCoordArray, NULL, indexArray, vertexCount, triangleCount*3); return model; }
void init(void) { dumpInfo(); // shader info // GL inits glClearColor(0.1, 0.1, 0.3, 0); glClearDepth(1.0); glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); // Load shader shader = loadShaders("shaders/projekt.vert", "shaders/projekt.frag"); phongshader = loadShaders("shaders/phong.vert", "shaders/phong.frag"); passShader = loadShaders("shaders/plaintextureshader.vert", "shaders/plaintextureshader.frag"); joinshader = loadShaders("shaders/joinshader.vert", "shaders/joinshader.frag"); lightShader = loadShaders("shaders/illuminati.vert", "shaders/illuminati.frag"); cameraDepthShader = loadShaders("shaders/cameraDepth.vert", "shaders/cameraDepth.frag"); lightDepthShader = loadShaders("shaders/lightDepth.vert", "shaders/lightDepth.frag"); thickness = loadShaders("shaders/thickness.vert", "shaders/thickness.frag"); //shadow shaders projTexShaderId = loadShaders("shaders/depth1V.vert", "shaders/depth1F.frag"); projTexShaderId2 = loadShaders("shaders/depth2V.vert", "shaders/depth2F.frag"); projTexMapUniform = glGetUniformLocation(projTexShaderId,"textureUnit"); plainShaderId = loadShaders("shaders/plain.vert", "shaders/plain.frag"); // Init FBOs fbo_phong = initFBO(W, H, 0); //fbo2 = initFBO(W, H, 0); fbo_depth = initFBO2(W,H, 0, 1); fbo_depth2 = initFBO2(W,H, 0, 1); fbo3 = initFBO(W, H, 0); fbo_sub = initFBO(W,H,0); fbo_lightDepth = initFBO(W,H,0); squareModel = LoadDataToModel( square, NULL, squareTexCoord, NULL, squareIndices, 4, 6); cam = SetVector(0, 0, 0.01); point = SetVector(0, 0, -10); axis = SetVector(0, 1, 0); // load the model bunny = LoadModelPlus("objects/bunny2/bunny_unwrap_noextras_blender.obj"); LoadTGATextureSimple("textures/badBunny.tga", &thicknessBunny); modelMatrix = Mult( modelMatrix, ArbRotate(axis, -0.6)); statue = LoadModelPlus("good_objects/statue_unwrapped_blender.obj"); LoadTGATextureSimple("textures/statue.tga", &thicknessStatue); box = LoadModelPlus("good_objects/box_standard_blender.obj"); LoadTGATextureSimple("textures/box.tga", &thicknessBox); box_bulge = LoadModelPlus("good_objects/box_bulge.obj"); box_valley = LoadModelPlus("good_objects/box_valley.obj"); box_stretched = LoadModelPlus("good_objects/box_stretched.obj"); // load the scenemodels bottom = LoadModelPlus("objects/bottom.obj"); side1 = LoadModelPlus("objects/side1.obj"); side2 = LoadModelPlus("objects/side2.obj"); // load sphere sphere = LoadModelPlus("objects/sphere.obj"); printf("%d vertices\n", sphere->numVertices); printf("%d indices\n", sphere->numIndices); //Light stuff lightPosition = SetVector(-5.0,5.0,-4.0); sphereModelMatrix = Mult(T(lightPosition.x, lightPosition.y, lightPosition.z), sphereModelMatrix); lightColor = SetVector(1.0,1.0,1.0); //Colors bunnyColor = SetVector(1.0,0.4,1.0); sceneColor = SetVector(0.2,0.2,0.7); glutTimerFunc(5, &OnTimer, 0); moveValue = 0.002; moveX = moveValue; zprInit(&viewMatrix, cam, point); //modelMatrix = Mult( T(0.0, -2.4,0.0),modelMatrix); }
/************************* * Use the GPGPU Shader * *************************/ void calculateNextPos(vector<glm::vec3> &particle, FBOstruct *fboPos, FBOstruct *fboOldPos, FBOstruct *fboVel, FBOstruct *fboOldVel, GLuint velocityEulerShader, GLuint pass, GLuint PositionEulerShader) { // en enkel fyrkant att rita på GLfloat square[] = { -1, -1, 0, -1, 1, 0, 1, 1, 0, 1, -1, 0 }; GLfloat squareTexCoord[] = { 0, 0, 0, 1, 1, 1, 1, 0 }; GLuint squareIndices[] = { 0, 1, 2, 0, 2, 3 }; Model* squareModel = LoadDataToModel( square, NULL, squareTexCoord, NULL, squareIndices, 4, 6); glUseProgram(velocityEulerShader); use2FBO(fboVel, fboOldVel, fboOldPos, velocityEulerShader); glClearColor(0.0, 0.0, 0.0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //send shared varible to shader, only need to be done the first time glUniform1f(glGetUniformLocation(velocityEulerShader, "nrOfParticlesVertically"), nrOfParticlesVertically); glUniform1f(glGetUniformLocation(velocityEulerShader, "nrOfParticlesHorizontally"), nrOfParticlesHorizontally); glUniform1f(glGetUniformLocation(velocityEulerShader, "timestep"), timestep); glUniform1f(glGetUniformLocation(velocityEulerShader, "particleMass"), particleMass); glUniform3f(glGetUniformLocation(velocityEulerShader, "g"), g.x, g.y, g.z); glUniform1f(glGetUniformLocation(velocityEulerShader, "kSt"), kSt); glUniform1f(glGetUniformLocation(velocityEulerShader, "kSh"), kSh); glUniform1f(glGetUniformLocation(velocityEulerShader, "kB"), kB); glUniform1f(glGetUniformLocation(velocityEulerShader, "oaSt"), oaSt); glUniform1f(glGetUniformLocation(velocityEulerShader, "oaSh"), oaSh); glUniform1f(glGetUniformLocation(velocityEulerShader, "oaB"), oaB); glUniform1f(glGetUniformLocation(velocityEulerShader, "cSt"), cSt); glUniform1f(glGetUniformLocation(velocityEulerShader, "cSh"), cSh); glUniform1f(glGetUniformLocation(velocityEulerShader, "cB"), cB); DrawModel(squareModel, velocityEulerShader, "in_Position", NULL, "in_TexCoord"); glUseProgram(pass); useFBO(fboOldVel, fboVel, 0L); //MÅSTE VARA PÅ VÄNSTER SIDA!!!!! glClearColor(0.0, 0.0, 0.0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); DrawModel(squareModel, pass, "in_Position", NULL, "in_TexCoord"); //old position is updated to current position useFBO(fboOldPos, fboPos, 0L); glClearColor(0.0, 0.0, 0.0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); DrawModel(squareModel, pass, "in_Position", NULL, "in_TexCoord"); //current position is updated to new position, need currnt position therfor updated ond position first glUseProgram(PositionEulerShader); use2FBO(fboPos, fboVel, fboOldPos, PositionEulerShader); glClearColor(0.0, 0.0, 0.0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUniform1f(glGetUniformLocation(PositionEulerShader, "timestep"), timestep); DrawModel(squareModel, PositionEulerShader, "in_Position", NULL, "in_TexCoord"); const size_t SIZE = nrOfParticlesVertically*nrOfParticlesHorizontally * 4; float particlePixels[SIZE]; glReadPixels(0, 0, nrOfParticlesVertically*nrOfParticlesHorizontally, 1, GL_RGBA, GL_FLOAT, particlePixels); for (int i = 0, j = 0; i < particle.size(); i++, j += 4) { //cout << "Position vector: " << particlePixels[j] << " " << particlePixels[j + 1] << " " << particlePixels[j + 2] << endl; particle.at(i).x = particlePixels[j]; particle.at(i).y = particlePixels[j + 1]; particle.at(i).z = particlePixels[j + 2]; }; }
/************************************** * Create initial textures on the FBOs* **************************************/ void initGPGPU(FBOstruct *fboPos, FBOstruct *fboOldPos, FBOstruct *fboVel, FBOstruct *fboOldVel) { // en enkel fyrkant att rita på GLfloat square[] = {-1, -1, 0, -1, 1, 0, 1, 1, 0, 1, -1, 0 }; GLfloat squareTexCoord[] = {0, 0, 0, 1, 1, 1, 1, 0 }; GLuint squareIndices[] = { 0, 1, 2, 0, 2, 3 }; Model* squareModel = LoadDataToModel(square, NULL, squareTexCoord, NULL,squareIndices, 4, 6); GLuint initVelosity = loadShaders("Shaders/initVelocityVertexShader.glsl", "Shaders/initVelocityFragmentShader.glsl"); GLuint passOverValues = loadShaders("Shaders/passVertexShader.glsl", "Shaders/passFragmentShader.glsl"); GLuint initPosition = loadShaders("Shaders/initPositionVertexShader.glsl", "Shaders/initPositionFragmentShader.glsl"); /**************** * Velosity FBO * ****************/ useFBO(fboVel, 0L, 0L); glClearColor(0.0, 0.0, 0.0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(initVelosity); GLint locHeight = glGetUniformLocation(initVelosity, "height"); GLint locWidth = glGetUniformLocation(initVelosity, "width"); if (locHeight != -1 && locWidth != -1) { glUniform1f(locWidth, nrOfParticlesHorizontally); glUniform1f(locHeight, nrOfParticlesVertically); } DrawModel(squareModel, initVelosity, "in_Position", NULL, "in_TexCoord"); /****************************************************** * Old velosity * * The same sa velosity, so just pass over the values * ******************************************************/ useFBO(fboOldVel, fboVel, 0L); glClearColor(0.0, 0.0, 0.0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(passOverValues); DrawModel(squareModel, passOverValues, "in_Position", NULL, "in_TexCoord"); //test so everything whent fine //const size_t SIZE = nrOfParticlesVertically*nrOfParticlesHorizontally * 4; //float particlePixels[SIZE]; /*glReadPixels(0, 0, nrOfParticlesVertically*nrOfParticlesHorizontally, 1, GL_RGBA, GL_FLOAT, particlePixels); for (int j = 0; j < SIZE; j += 4) { cout << " Init velosity: " << particlePixels[j] << " " << particlePixels[j + 1] << " " << particlePixels[j + 2] << " " << particlePixels[j + 3] << endl; }*/ /*************** * Position FBO * ****************/ useFBO(fboPos, 0L, 0L); glClearColor(0.0, 0.0, 0.0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(initPosition); locHeight = glGetUniformLocation(initPosition, "height"); locWidth = glGetUniformLocation(initPosition, "width"); GLint locSpringLenght = glGetUniformLocation(initPosition, "springRestLenght"); if (locHeight != -1 && locWidth != -1 && locSpringLenght != -1) { glUniform1f(locWidth, nrOfParticlesHorizontally); glUniform1f(locHeight, nrOfParticlesVertically); glUniform1f(locSpringLenght, springRestLenght); } DrawModel(squareModel, initPosition, "in_Position", NULL, "in_TexCoord"); /****************************************************** * Old Position * * The same sa position, so just pass over the values * ******************************************************/ useFBO(fboOldPos, fboPos, 0L); glClearColor(0.0, 0.0, 0.0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(passOverValues); DrawModel(squareModel, passOverValues, "in_Position", NULL, "in_TexCoord"); }
Model* GenerateTerrain(TextureData *tex) { int vertexCount = tex->width * tex->height; int triangleCount = (tex->width-1) * (tex->height-1) * 2; int x, z; vertexArray = malloc(sizeof(GLfloat) * 3 * vertexCount); GLfloat *normalArray = malloc(sizeof(GLfloat) * 3 * vertexCount); GLfloat *texCoordArray = malloc(sizeof(GLfloat) * 2 * vertexCount); GLuint *indexArray = malloc(sizeof(GLuint) * triangleCount*3); printf("bpp %d\n", tex->bpp); for (x = 0; x < tex->width; x++) for (z = 0; z < tex->height; z++) { GLfloat y = tex->imageData[(x + z * tex->width) * (tex->bpp/8)] / 10.0; // Vertex array. You need to scale this properly vertexArray[(x + z * tex->width)*3 + 0] = x / 1.0; vertexArray[(x + z * tex->width)*3 + 1] = y; vertexArray[(x + z * tex->width)*3 + 2] = z / 1.0; // CalcNormalVector // Normal vectors. You need to calculate these. vec3 NV; //Can't access elements outside the array if ((x==0) || (x==tex->width) || (z==0) || (z==tex->width)) { NV.x = 0.0; NV.y = 1.0; NV.z = 0.0; } else { vec3 p1,p2,p3; GLfloat xmx1 = vertexArray[( (x-1) + z * tex->width)*3 + 0]; GLfloat ymx1 = vertexArray[( (x-1) + z * tex->width)*3 + 1]; GLfloat zmx1 = vertexArray[( (x-1) + z * tex->width)*3 + 2]; GLfloat xpx1 = vertexArray[( x + (z+1) * tex->width)*3 + 0]; GLfloat ypx1 = vertexArray[( x + (z+1) * tex->width)*3 + 1]; GLfloat zpx1 = vertexArray[( x + (z+1) * tex->width)*3 + 2]; p1 = SetVector(x,y,z); //Consider using x+1 x-1 instead p2 = SetVector(xmx1,ymx1,zmx1); p3 = SetVector(xpx1,ypx1,zpx1); NV = Normalize(CalcNormalVector(p1,p2,p3)); } normalArray[(x + z * tex->width)*3 + 0] = NV.x; normalArray[(x + z * tex->width)*3 + 1] = NV.y; normalArray[(x + z * tex->width)*3 + 2] = NV.z; // Texture coordinates. You may want to scale them. texCoordArray[(x + z * tex->width)*2 + 0] = x; // (float)x / tex->width; texCoordArray[(x + z * tex->width)*2 + 1] = z; // (float)z / tex->height; } for (x = 0; x < tex->width-1; x++) for (z = 0; z < tex->height-1; z++) { // Triangle 1 indexArray[(x + z * (tex->width-1))*6 + 0] = x + z * tex->width; indexArray[(x + z * (tex->width-1))*6 + 1] = x + (z+1) * tex->width; indexArray[(x + z * (tex->width-1))*6 + 2] = x+1 + z * tex->width; // Triangle 2 indexArray[(x + z * (tex->width-1))*6 + 3] = x+1 + z * tex->width; indexArray[(x + z * (tex->width-1))*6 + 4] = x + (z+1) * tex->width; indexArray[(x + z * (tex->width-1))*6 + 5] = x+1 + (z+1) * tex->width; } // End of terrain generation // Create Model and upload to GPU: Model* model = LoadDataToModel( vertexArray, normalArray, texCoordArray, NULL, indexArray, vertexCount, triangleCount*3); return model; }