void calculateCullingFrustum(mat4 camMatrix) { float cLeft = -0.05f; float cRight = 0.05f; float cBottom = -0.05f; float cTop = 0.05f; float cNear = 0.2f; //float cFar = 50.f; vec3 topLeft = {cLeft, cTop, -cNear}; vec3 bottomLeft = {cLeft, cBottom, -cNear}; vec3 topRight = {cRight, cTop, -cNear}; vec3 bottomRight = {cRight, cBottom, -cNear}; vec3 farNormal = {0.f, 0.f, -1.f}; mat4 invCam = InvertMat4(camMatrix); topLeft = MultVec3(invCam, topLeft); bottomLeft = MultVec3(invCam, topLeft); topRight = MultVec3(invCam, topLeft); bottomRight = MultVec3(invCam, topLeft); farNormal = MultVec3(invCam, farNormal); cullingFrustumNormals[0] = Normalize(CrossProduct(topLeft, bottomLeft)); // Left normal cullingFrustumNormals[1] = Normalize(CrossProduct(bottomRight, topRight)); // Right normal cullingFrustumNormals[2] = Normalize(CrossProduct(bottomLeft, bottomRight)); // Bottom normal cullingFrustumNormals[3] = Normalize(CrossProduct(topRight, topLeft)); // Top normal cullingFrustumNormals[4] = Normalize(farNormal); // Far normal }
static void updateCameraMatrix(mat4 *matrix) { if (matrix != NULL) { camera = MultVec3(*matrix, camera); lookAtPoint = MultVec3(*matrix, lookAtPoint); } *_cameraMatrix = lookAt(camera.x, camera.y, camera.z, lookAtPoint.x, lookAtPoint.y, lookAtPoint.y, up.x, up.y, up.z); right = SetVector(_cameraMatrix->m[0], _cameraMatrix->m[1], _cameraMatrix->m[2]); up = SetVector(_cameraMatrix->m[4], _cameraMatrix->m[5], _cameraMatrix->m[6]); fwd = SetVector(_cameraMatrix->m[8], _cameraMatrix->m[9], _cameraMatrix->m[10]); // glUniformMatrix4fv(glGetUniformLocation(shader, viewMatName), 1, GL_TRUE, _cameraMatrix.m); }
void mouse(int x, int y) { vec3 camRight = Normalize(CrossProduct(camDir, camUp)); mat4 rot1; mat4 rot2; int dx = x - halfWindowWidth; int dy = y - halfWindowHeight; rot1 = ArbRotate(camRight, - dy * CAM_ROT_SPEED); rot2 = Ry(-dx * CAM_ROT_SPEED); rot2 = Mult(rot2, rot1); camDir = Normalize(MultVec3(rot2, camDir)); glutWarpPointer(halfWindowWidth, halfWindowHeight); }
void keyboardControl(unsigned char key, int x, int y) { vec3 camRight = Normalize(CrossProduct(camDir, camUp)); mat4 rot; switch(key) { case GLUT_KEY_F: rot = ArbRotate(camRight, CAM_ROT_SPEED); camDir = Normalize(MultVec3(rot, camDir)); break; case GLUT_KEY_H: glutToggleFullScreen(); break; case GLUT_KEY_ESC: glutExit(); break; // default: //printf("Key code: %d\n", key); } }
static void zprKey(unsigned char key, int x, int y) { float speed = 0.3, rotSpeed = 0.3; GLfloat speedX = 0, speedY = 0, speedZ = 0, angle = 0.0, dX = 0, dY = 0, dZ = 0; bool turnedX = false, turnedY = false; // printf("%c\n", key); switch (key) { case 'i': speedY = speed; break; case 'k': speedY = -speed; break; case 'j': speedX = -speed; break; case 'l': speedX = speed; break; case 'd': angle = -rotSpeed; turnedY = true; break; case 'a': angle = rotSpeed; turnedY = true; break; case 'w': speedZ = -speed; break; case 's': speedZ = speed; break; case 'q': angle = -rotSpeed; turnedX = true; break; case 'e': angle = rotSpeed; turnedX = true; break; } dX = (GLfloat) _cameraMatrix->m[0] * speedX + _cameraMatrix->m[4] * speedY + _cameraMatrix->m[8] * speedZ; dY = (GLfloat) _cameraMatrix->m[1] * speedX + _cameraMatrix->m[5] * speedY + _cameraMatrix->m[9] * speedZ; dZ = (GLfloat) _cameraMatrix->m[2] * speedX + _cameraMatrix->m[6] * speedY + _cameraMatrix->m[10] * speedZ; camera.x += dX; camera.y += dY; camera.z += dZ; lookAtPoint.x += dX; lookAtPoint.y += dY; lookAtPoint.z += dZ; if(turnedY) { tMat = T(-camera.x, -camera.y, -camera.z); rotMat = ArbRotate(up, angle); lookAtPoint = MultVec3(tMat, lookAtPoint); lookAtPoint = MultVec3(rotMat, lookAtPoint); tMat = T(camera.x, camera.y, camera.z); lookAtPoint = MultVec3(tMat, lookAtPoint); } if(turnedX) { tMat = T(-lookAtPoint.x, -lookAtPoint.y, -lookAtPoint.z); rotMat = ArbRotate(right, angle); camera = MultVec3(tMat, camera); camera = MultVec3(rotMat, camera); tMat = T(lookAtPoint.x, lookAtPoint.y, lookAtPoint.z); camera = MultVec3(tMat, camera); } updateCameraMatrix(0); }
void display(void) { ballXPos = ballXPos + 0.03; ballZPos = ballZPos + 0.03; int d = floor(ballXPos * 5.0); int e = floor(ballZPos * 5.0); dx = ballXPos * 5.0 - d; dz = ballZPos * 5.0 - e; if(dx+dz<1){ dyx = tm->vertexArray[(d+1 + e * texWidth)*3+1] - tm->vertexArray[(d + e * texWidth)*3+1]; dyz = tm->vertexArray[(d + (e+1) * texWidth)*3+1] - tm->vertexArray[(d + e * texWidth)*3+1]; ballYPos = dyx*dx+dyz*dz + tm->vertexArray[(d + e * texWidth)*3+1]; } else{ dyx = tm->vertexArray[(d+1 + e * texWidth)*3+1] - tm->vertexArray[((d+1) + (e+1) * texWidth)*3+1]; dyz = tm->vertexArray[(d + (e+1) * texWidth)*3+1] - tm->vertexArray[((d+1) + (e+1) * texWidth)*3+1]; dx = (ceil(ballXPos * 5.0)-ballXPos * 5.0); dz = (ceil(ballZPos * 5.0)-ballZPos * 5.0); ballYPos = dyx*dx+dyz*dz + tm->vertexArray[((d+1) + (e+1) * texWidth)*3+1]; } //printf("%lf\n",ballYPos); vec3 lookingDir = Normalize(VectorSub(l, p)); vec3 walkSideways = Normalize(CrossProduct(lookingDir, v)); if (glutKeyIsDown('a')) { p = VectorSub(p, walkSideways); l = VectorSub(l, walkSideways); } if (glutKeyIsDown('d')) { p = VectorAdd(p, walkSideways); l = VectorAdd(l, walkSideways); } if (glutKeyIsDown('w')) { p = VectorAdd(p, lookingDir); l = VectorAdd(l, lookingDir); } if (glutKeyIsDown('s')) { p = VectorSub(p, lookingDir); l = VectorSub(l, lookingDir); } if (glutKeyIsDown('l')) { trans = T(-p.x, -p.y, -p.z); trans2 = T(p.x, p.y, p.z); rot = Ry(-0.05); l = MultVec3(trans2, MultVec3(rot, MultVec3(trans, l))); } if (glutKeyIsDown('j')) { trans = T(-p.x, -p.y, -p.z); trans2 = T(p.x, p.y, p.z); rot = Ry(0.05); l = MultVec3(trans2, MultVec3(rot, MultVec3(trans, l))); } if (glutKeyIsDown('i')) { trans = T(-p.x, -p.y, -p.z); trans2 = T(p.x, p.y, p.z); rotateUpDown = ArbRotate(walkSideways, 0.05); l = MultVec3(trans2, MultVec3(rotateUpDown, MultVec3(trans, l))); } if (glutKeyIsDown('k')) { trans = T(-p.x, -p.y, -p.z); trans2 = T(p.x, p.y, p.z); rotateUpDown = ArbRotate(walkSideways, -0.05); l = MultVec3(trans2, MultVec3(rotateUpDown, MultVec3(trans, l))); } // clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); printError("pre display"); glUseProgram(program); // Build matrix camMatrix = lookAtv(p,l,v); modelView = IdentityMatrix(); total = Mult(camMatrix, modelView); glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, modelView.m); glUniformMatrix4fv(glGetUniformLocation(program, "cameraMatrix"), 1, GL_TRUE, camMatrix.m); DrawModel(tm, program, "inPosition", "inNormal", "inTexCoord"); //Draw sphere modelToWorld = T(ballXPos, ballYPos, ballZPos); glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, modelToWorld.m); DrawModel(sphere, program, "inPosition", "inNormal", NULL); printError("display 2"); glutSwapBuffers(); }
void display(void) { // Clear the screen____________________________________ if((cam.x < goalx + 2 && cam.x > goalx - 2) && (cam.z < goalz + 2 && cam.z > goalz - 2)) { third_person = true; } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Variables mat4 total, modelView, camMatrix; GLfloat t =(GLfloat)glutGet(GLUT_ELAPSED_TIME)/1000; //Scale with constant to make animation slower. //Read input_____________________________________________ if (third_person) { goal_found = true; if (glutKeyIsDown('t')) { third_person = false; } vec3 pos = {200, 200, 150};// SetVector(keyx,keyy,keyz); //Mouse control_____________________________________________ //fmin and fmax doesn't really make sense but looks good. GLfloat mousexlocal = 0; GLfloat mouseylocal = 0; if (mousex < 350 || mousex > 250) { mousexlocal = 300-mousex; } else { mousexlocal = 0; } deltaphi = deltaphi + deltaphi*-mousexlocal; deltaphi = fmin(0.0001,deltaphi); deltaphi = fmax(deltaphi,2 * PI - 0.0001); forward = MultVec3(Ry(deltaphi*-mousexlocal), forward); vec3 crossvec = CrossProduct(forward,up); if (mousey < 350 || mousey > 250) { mouseylocal = 300-mousey; } else { mouseylocal = 0; } deltatheta = deltatheta + deltatheta* -mouseylocal; deltatheta = fmin(0.0001,deltatheta); deltatheta = fmax(deltatheta,2*PI - 0.0001); forward = MultVec3( ArbRotate(crossvec,deltatheta*-mouseylocal), forward); //Create camera matrix_____________________________________________ camMatrix = lookAtv(pos,VectorAdd(forward,pos),up); } // FIRST PERSON VIEW -------------------------------------------------------- else { vec3 moveVec; if (glutKeyIsDown('w')) // forwards, obviously { vec3 oldcam = cam; //Save old position to check if its legal. moveVec = ScalarMult(VectorSub(cam, SetVector(lookAtPoint.x, cam.y, lookAtPoint.z)), 0.1); // VectorSub makes a vector in the direction we're looking. cam = VectorSub(cam, moveVec); // The new camera position is the old camera position + the move vector cam.y = getHeight(&ttex,cam.x, cam.z)+2; // Get the correct height from the new position GLfloat maze_height = getHeight(&ttexm, cam.x, cam.z); // Get the height of the maze at the new (possible) camera position GLfloat ground_height = getHeight(&ttex, cam.x, cam.z); // Get the height of the ground at the new (possible) camera position //Check if the new position is legal if ((ground_height - maze_height) < ground_height) // If 0, shouldn't move ever: OK! // When does it start to move?? { printf("woot"); cam = oldcam; } } else if (glutKeyIsDown('t')) { third_person = true; } GLfloat mousexlocal = 0; GLfloat mouseylocal = 0; if (mousex < 350 || mousex > 250) { mousexlocal = 300-mousex; } else { mousexlocal = 0; } deltaphi = deltaphi + deltaphi*-mousexlocal; deltaphi = fmin(0.0001,deltaphi); deltaphi = fmax(deltaphi,2 * PI - 0.0001); forward = MultVec3(Ry(deltaphi*-mousexlocal), forward); vec3 crossvec = CrossProduct(forward,up); if (mousey < 350 || mousey > 250) { mouseylocal = 300-mousey; } else { mouseylocal = 0; } deltatheta = deltatheta + deltatheta* -mouseylocal; deltatheta = fmin(0.0001,deltatheta); deltatheta = fmax(deltatheta,2*PI - 0.0001); forward = MultVec3( ArbRotate(crossvec,deltatheta*-mouseylocal), forward); lookAtPoint = VectorAdd(forward,cam); //Create camera matrix_____________________________________________ camMatrix = lookAtv(cam,lookAtPoint,up); } // --------------------------------------------------------// // SKYBOX // --------------------------------------------------------// glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glUseProgram(programSky); glActiveTexture(GL_TEXTURE2); //glBindTexture(GL_TEXTURE_2D, tex2); trans = T(0.0, 0.0, 0.0); rot = Rx(0); total = Mult(trans, rot); glUniformMatrix4fv(glGetUniformLocation(programSky, "mdlMatrix"), 1, GL_TRUE, total.m); lookSky = camMatrix; lookSky.m[3] = 0; lookSky.m[7] = 0; lookSky.m[11] = 0; glUniformMatrix4fv(glGetUniformLocation(programSky, "lookMatrix"), 1, GL_TRUE, lookSky.m); glUniformMatrix4fv(glGetUniformLocation(programSky, "projectionMatrix"), 1, GL_TRUE, projectionMatrix.m); glUniform1i(glGetUniformLocation(programSky, "texUnit2"), 2); // Texture unit 2 DrawModel(skybox, programSky, "in_Position", NULL, "inTexCoord"); glUseProgram(program); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); //------------------------------------------------------// //------------------------------------------------------// modelView = IdentityMatrix(); total = Mult(camMatrix, modelView); glActiveTexture(GL_TEXTURE1); glUseProgram(program); glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m); glBindTexture(GL_TEXTURE_2D, tex2); // Bind Our Texture tex1 DrawModel(tm, program,"inPosition", "inNormal", "inTexCoord"); glUseProgram(program); glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 0, GL_TRUE, total.m); glBindTexture(GL_TEXTURE_2D, tex1); // Bind Our Texture tex1 DrawModel(mm, program,"inPosition", "inNormal", "inTexCoord"); GLfloat y = getHeight(&ttexm, 200, 147); mat4 trans = T(goalx, 2, goalz); mat4 scale = S(1, 1, 1); total = Mult(total, trans); total = Mult(total, scale); if (goal_found) { char* string = "GOOD JOB!! You won :D"; sfDrawString(100, 100, string); //while(true){}; } glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m); glBindTexture(GL_TEXTURE_2D, tex3); // Bind Our Texture tex1 DrawModel(goal, program,"inPosition", "inNormal", "inTexCoord"); printError("display 2"); glutSwapBuffers(); printf("x %f\n", cam.x); printf("y %f\n", cam.y); printf("z %f\n", cam.z); /* if ( (cam.x < 95) && (cam.x > 85) && */ /* (cam.z < 85) && (cam.z > 75)) */ }
/////////////////////////////////////////////////////// // D E F O R M C Y L I N D E R // // Desc: deformera cylinder meshen enligt skelettet void DeformCylinder() { //vec3 v[kMaxBones]; //float w[kMaxBones]; int row, corner, bonesnum, bonesnum2; //bonesnum = 0; // för samtliga vertexar for (row = 0; row < kMaxRow; row++) { for (corner = 0; corner < kMaxCorners; corner++) { // ---------========= UPG 4 ===========--------- // TODO: skinna meshen mot alla benen. // // data som du kan använda: // g_bonesRes[].rot //Rotation på benen // g_bones[].pos //Benens position // g_boneWeights //g_boneWeights[kMaxRow][kMaxCorners][kMaxBones] // g_vertsOrg //Originalet // g_vertsRes //Det som ska skickas //g_vertsRes[row][corner] = g_vertsOrg[row][corner]; vec3 sum = SetVector(0.0, 0.0, 0.0); for(bonesnum = 0; bonesnum < kMaxBones; bonesnum++) { //Beräknar T för Org och Res mat4 currOrgT = T(g_bones[bonesnum].pos.x, g_bones[bonesnum].pos.y, g_bones[bonesnum].pos.z); mat4 currResT = T(g_bonesRes[bonesnum].pos.x, g_bonesRes[bonesnum].pos.y, g_bonesRes[bonesnum].pos.z); //Beräknar M för Org och Res, och även inverterad M mat4 currOrgM = Mult(currOrgT, g_bones[bonesnum].rot); mat4 currResM = Mult(currResT, g_bonesRes[bonesnum].rot); mat4 currInvM = InvertMat4(currOrgM); mat4 sumOrgM = currOrgM; mat4 sumResM = currResM; mat4 sumInvM = currInvM; for(bonesnum2 = bonesnum - 1; bonesnum2 >= 0; bonesnum2--) { //Beräknar T för Org och Res för ben innan bonesnum mat4 preOrgT = T(g_bones[bonesnum2].pos.x, g_bones[bonesnum2].pos.y, g_bones[bonesnum2].pos.z); mat4 preResT = T(g_bonesRes[bonesnum2].pos.x, g_bonesRes[bonesnum2].pos.y, g_bonesRes[bonesnum2].pos.z); //Beräknar M för Org och Res, och även inverterad M för ben innan bonesnum mat4 preOrgM = Mult(preOrgT, g_bones[bonesnum2].rot); mat4 preResM = Mult(preResT, g_bonesRes[bonesnum2].rot); mat4 preInvM = InvertMat4(preOrgM); //Summerar sumOrgM = Mult(sumOrgM, preOrgM); sumResM = Mult(sumResM, preResM); sumInvM = Mult(preInvM, sumInvM); } vec3 resV = MultVec3(sumOrgM, g_vertsOrg[row][corner]); mat4 temp1 = Mult(sumResM, sumInvM); vec3 temp2 = MultVec3(temp1, resV); temp2 = ScalarMult(temp2, g_boneWeights[row][corner][bonesnum]); sum = VectorAdd(sum, temp2); } g_vertsRes[row][corner] = sum; } } }
void display(void) { //Read input_____________________________________________ GLfloat t =(GLfloat)glutGet(GLUT_ELAPSED_TIME)/1000; //Scale with constant to make animation slower. if (glutKeyIsDown('w')) { keyz = keyz +1; } else if (glutKeyIsDown('s')) { keyz = keyz -1; } else if (glutKeyIsDown('a')) { keyx = keyx +1; } else if (glutKeyIsDown('d')) { keyx = keyx -1; } else if (glutKeyIsDown('e')) { keyy = keyy +1; } else if (glutKeyIsDown('q')) { keyy = keyy -1; } else if (glutKeyIsDown('r')) { exit(0); } // Clear the screen____________________________________ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); mat4 total, modelView, camMatrix; printError("pre display"); vec3 pos = SetVector(keyx,keyy,keyz); vec3 up = SetVector(0,1,0); //Mouse control_____________________________________________ //fmin and fmax doesn't really make sense but looks good. GLfloat mousexlocal = 0; GLfloat mouseylocal = 0; if (mousex < 350 || mousex > 250) { mousexlocal = 300-mousex; } else { mousexlocal = 0; } deltaphi = deltaphi + deltaphi*-mousexlocal; deltaphi = fmin(0.0001,deltaphi); deltaphi = fmax(deltaphi,2 * PI - 0.0001); forward = MultVec3(Ry(deltaphi*-mousexlocal), forward); vec3 crossvec = CrossProduct(forward,up); if (mousey < 350 || mousey > 250) { mouseylocal = 300-mousey; } else { mouseylocal = 0; } deltatheta = deltatheta + deltatheta* -mouseylocal; deltatheta = fmin(0.0001,deltatheta); deltatheta = fmax(deltatheta,2*PI - 0.0001); forward = MultVec3( ArbRotate(crossvec,deltatheta*-mouseylocal), forward); //Create camera matrix_____________________________________________ GLfloat x = cos(t)*5 + 35;//Mult(Ry(2*5),x); GLfloat z = sin(t)*5 + 75;//Mult(Ry //GLfloat z = 65; GLfloat y = getHeight(&ttex,x,z) + 0.8; // pos.y = pos.yy; camMatrix = lookAtv(pos,VectorAdd(forward,pos),up); modelView = IdentityMatrix(); total = Mult(camMatrix, modelView); glUseProgram(program); glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m); glBindTexture(GL_TEXTURE_2D, tex1); // Bind Our Texture tex1 DrawModel(tm, program,"inPosition", "inNormal", "inTexCoord"); //total = Mult(Ry(2*t),T(10,0,0)); total = T(x,y,z); total = Mult(camMatrix,total); glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m); glBindTexture(GL_TEXTURE_2D, tex2); // Bind Our Texture tex1 DrawModel(bunny, program, "inPosition", "inNormal", "inTexCoord"); printError("display 2"); glutSwapBuffers(); }