void Load() { /* Load shader */ char shaderName_vsh[] = "Shader.vsh"; char shaderName_fsh[] = "Shader.fsh"; CompileShader(&g_Shader, shaderName_vsh, shaderName_fsh); /* Load mesh */ CreateMesh(&g_Mesh[0], kCommonMesh_Cube); CreateMesh(&g_Mesh[1], kCommonMesh_Square); // Cube g_Cube.mesh = &g_Mesh[0]; DefaultTransform(&g_Cube.transform); g_Cube.transform.position.z = 40.0f; g_Cube.transform.axis = GLKVector3Make(0.0f, 1.0f, 0.0f); //g_Cube.transform.scale = GLKVector3Make(10.0f, 10.0f, 10.0f); g_Cube.transform.angle = 0.0f; g_Cube.transform.parent = &g_WorldTrans; g_Cube.color = GLKVector4Make(0.4f, 0.6f, 0.7f, 0.8f); // Mask g_Mask.mesh = &g_Mesh[1]; DefaultTransform(&g_Mask.transform); g_Mask.transform.position = GLKVector3Make(0.0f, 2.0f, 40.0f); g_Mask.transform.axis = GLKVector3Make(1.0f, 0.0f, 0.0f); g_Mask.transform.angle = 90.0f; g_Mask.transform.scale = GLKVector3Make(2.0f, 2.0f, 1.0f); g_Mask.transform.parent = &g_WorldTrans; g_Mask.color = GLKVector4Make(0.0f, 0.0f, 0.0f, 0.8f); /* Set gl states */ glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glEnable(GL_DEPTH_TEST); }
GLKMatrix4 CoordinateFrameForPolar( RAPolarCoordinate polar ) { // convert to radians polar.latitude *= DEG_TO_RAD; polar.longitude *= DEG_TO_RAD; // Compute up vector GLKVector3 up = GLKVector3Make( cos(polar.longitude) * cos(polar.latitude), sin(polar.longitude) * cos(polar.latitude), sin(polar.latitude)); // Compute east vector GLKVector3 east = GLKVector3Make( -sin(polar.longitude), cos(polar.longitude), 0 ); // Compute north vector = outer product up x east GLKVector3 north = GLKVector3CrossProduct( up, east ); // set transform basis GLKMatrix4 coordinateFrame = GLKMatrix4Identity; coordinateFrame.m00 = east.x; coordinateFrame.m01 = east.y; coordinateFrame.m02 = east.z; coordinateFrame.m10 = north.x; coordinateFrame.m11 = north.y; coordinateFrame.m12 = north.z; coordinateFrame.m20 = up.x; coordinateFrame.m21 = up.y; coordinateFrame.m22 = up.z; return coordinateFrame; }
GLint gluUnProject(GLdouble winX, GLdouble winY, GLdouble winZ, const GLdouble * model, const GLdouble * proj, const GLint * view, GLdouble* objX, GLdouble* objY, GLdouble* objZ) { int glkview[] = {view[0], view[1], view[2], view[3]}; bool success; GLKVector3 result = GLKMathUnproject(GLKVector3Make(winX, winY, winZ), GLKMatrix4Make(model[0],model[1],model[2],model[3], model[4],model[5],model[6],model[7], model[8],model[9],model[10],model[11], model[12],model[13],model[14],model[15]), GLKMatrix4Make(proj[0],proj[1],proj[2],proj[3], proj[4],proj[5],proj[6],proj[7], proj[8],proj[9],proj[10],proj[11], proj[12],proj[13],proj[14],proj[15]), glkview, &success ); *objX = result.x; *objY = result.y; *objZ = result.z; return success ? GLU_TRUE : GLU_FALSE; }
GLKVector3 Transform_GetLocalPosition(const Transform slf, GLKVector3 world_position) { bool inv_result; GLKMatrix4 transform_inv = GLKMatrix4Invert(Transform_GetMV(&slf), &inv_result); assert(inv_result); // the matrix should be invertible. GLKVector4 position4 = GLKMatrix4MultiplyVector4(transform_inv, GLKVector4Make(world_position.x, world_position.y, world_position.z, 1.0f)); return GLKVector3Make(position4.x, position4.y, position4.z); }
void Reshape(GLsizei width, GLsizei height) { // Set perpective DefaultPerspective(&g_Frustum); // g_Frustum.dimension.x = width; // g_Frustum.dimension.y = height; // Set viewport glViewport(0, 0, width, height); // Set world transform DefaultTransform(&g_WorldTrans); g_WorldTrans.position = GLKVector3Make(0.0f, 0.0f, -g_Frustum.dimension.z/2.0f); g_WorldTrans.axis = GLKVector3Make(1.0f, 0.0f, 0.0f); g_WorldTrans.angle = 3.0f; g_WorldTrans.scale.x = (float)height/(float)width; }
GLint gluProject(GLdouble objX, GLdouble objY, GLdouble objZ, const GLdouble * model, const GLdouble * proj, const GLint * view, GLdouble* winX, GLdouble* winY, GLdouble* winZ) { int glkview[] = {view[0], view[1], view[2], view[3]}; GLKVector3 result = GLKMathProject(GLKVector3Make(objX, objY, objZ), GLKMatrix4Make(model[0],model[1],model[2],model[3], model[4],model[5],model[6],model[7], model[8],model[9],model[10],model[11], model[12],model[13],model[14],model[15]), GLKMatrix4Make(proj[0],proj[1],proj[2],proj[3], proj[4],proj[5],proj[6],proj[7], proj[8],proj[9],proj[10],proj[11], proj[12],proj[13],proj[14],proj[15]), glkview ); *winX = result.x; *winY = result.y; *winZ = result.z; return GLU_TRUE; }