void GFX_load_identity( void ) { switch( gfx.matrix_mode ) { case MODELVIEW_MATRIX: { mat4_identity( GFX_get_modelview_matrix() ); break; } case PROJECTION_MATRIX: { mat4_identity( GFX_get_projection_matrix() ); break; } case TEXTURE_MATRIX: { mat4_identity( GFX_get_texture_matrix() ); break; } } }
void reshape(int width, int height) { if (height == 0) { height = 1; } glViewport(0, 0, width, height); mat4_identity(projection); projection = mat4_perspective(45.0f, (GLfloat) width / (GLfloat) height, 0.01f, 100000.0f , projection); mat4_identity(view); view = mat4_lookAt(eye, center, up, view); }
void mesh_draw_ts (struct Mesh *mesh, GLBTexture *tex, struct Armature *arm, int frame, float *mMat, float *vMat, float *pMat) { glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); mat4 t_matrix; mat4_identity(t_matrix); mat4_mult(mMat, t_matrix, t_matrix); mat4_mult(vMat, t_matrix, t_matrix); mat4_mult(pMat, t_matrix, t_matrix); mat4 *bmat = alloca(sizeof(mat4) * arm->nbones); armature_matrices(arm, frame, bmat); static bool TRUE = 1; glbProgramTexture(glbdrawmodel, GLB_FRAGMENT_SHADER, 0, tex); glbProgramUniform(glbdrawmodel, GLB_VERTEX_SHADER, 0, sizeof(bool), &TRUE); glbProgramUniformMatrix(glbdrawmodel, GLB_VERTEX_SHADER, 1, sizeof(float[16]), true, t_matrix); glbProgramUniformMatrix(glbdrawmodel, GLB_VERTEX_SHADER, 2, sizeof(float[16]) * arm->nbones, true, bmat); glbProgramOutput(glbdrawmodel, glbframebuffer); glbProgramDrawIndexed(glbdrawmodel, mesh->vbuffer, mesh->ibuffer); }
void armature_matrices(Armature *a, int frame, mat4 *matrices) { int i; for(i = 0; i < a->nbones; i++) { Bone *bone = &a->bones[i]; Pose *bpose = &bone->poses[frame]; mat4 tmp; mat4_identity(matrices[i]); quat q; memcpy(&q, bpose->rotation, sizeof(float) * 4); quaternion_to_mat4(q, tmp); mat4_translate(matrices[i], -bone->head[0], -bone->head[1], -bone->head[2]); mat4_mult(matrices[i], tmp, matrices[i]); mat4_translate(matrices[i], bone->head[0], bone->head[1], bone->head[2]); mat4_translate(matrices[i], bpose->position[0], bpose->position[1], bpose->position[2]); if(bone_hasparent(bone)) { mat4_mult(matrices[bone->parent->id], matrices[i], matrices[i]); } } }
void add_rigid_body(OBJMESH * objmesh, float mass) { btCollisionShape *btcollisionshape = new btBoxShape(btVector3(objmesh->dimension.x * 0.5f, objmesh->dimension.y * 0.5f, objmesh->dimension.z * 0.5f)); btTransform bttransform; mat4 mat; vec4 rotx = { 1.0f, 0.0f, 0.0f, objmesh->rotation.x }, roty = { 0.0f, 1.0f, 0.0f, objmesh->rotation.y}, rotz = { 0.0f, 0.0f, 1.0f, objmesh->rotation.z}; mat4_identity(&mat); mat4_translate(&mat, &mat, &objmesh->location); mat4_rotate(&mat, &mat, &rotz); mat4_rotate(&mat, &mat, &roty); mat4_rotate(&mat, &mat, &rotx); bttransform.setFromOpenGLMatrix((float *)&mat); btDefaultMotionState *btdefaultmotionstate = NULL; btdefaultmotionstate = new btDefaultMotionState(bttransform); btVector3 localinertia(0.0f, 0.0f, 0.0f); if (mass) btcollisionshape->calculateLocalInertia(mass, localinertia); objmesh->btrigidbody = new btRigidBody(mass, btdefaultmotionstate, btcollisionshape, localinertia); objmesh->btrigidbody->setUserPointer(objmesh); dynamicsworld->addRigidBody(objmesh->btrigidbody); }
static void window_draw(struct window *window) { struct mat4 matrix, modelview, projection, transform, result; GLfloat aspect; aspect = window->width / (GLfloat)window->height; mat4_perspective(&projection, 35.0f, aspect, 1.0f, 1024.0f); mat4_identity(&modelview); mat4_rotate_x(&transform, x); mat4_multiply(&result, &modelview, &transform); mat4_rotate_y(&transform, y); mat4_multiply(&modelview, &result, &transform); mat4_rotate_z(&transform, z); mat4_multiply(&result, &modelview, &transform); mat4_translate(&transform, 0.0f, 0.0f, -2.0f); mat4_multiply(&modelview, &transform, &result); mat4_multiply(&matrix, &projection, &modelview); glUniformMatrix4fv(mvp, 1, GL_FALSE, (GLfloat *)&matrix); glViewport(0, 0, window->width, window->height); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawElements(GL_TRIANGLES, ARRAY_SIZE(indices), GL_UNSIGNED_SHORT, indices); eglSwapBuffers(window->egl.display, window->egl.surface); x += 0.3f; y += 0.2f; z += 0.4f; }
void model_draw(struct Model *model, float *mMat, float *vMat, float *pMat) { glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); mat4 t_matrix; mat4_identity(t_matrix); mat4_mult(mMat, t_matrix, t_matrix); mat4_mult(vMat, t_matrix, t_matrix); mat4_mult(pMat, t_matrix, t_matrix); //mat4_transpose(t_matrix); int i; for(i = 0; i < varray_length(&model->features); i++) { static int FALSE = 0; const ModelFeature *feature = varray_get(&model->features, i); glbProgramTexture(glbdrawmodel, GLB_FRAGMENT_SHADER, 0, feature->color); glbProgramUniformMatrix(glbdrawmodel, GLB_VERTEX_SHADER, 0, sizeof(int), true, &FALSE); glbProgramUniformMatrix(glbdrawmodel, GLB_VERTEX_SHADER, 1, sizeof(float[16]), true, t_matrix); glbProgramDrawIndexed(glbdrawmodel, feature->mesh->vbuffer, feature->mesh->ibuffer); } }
/*! Create a normalized direction vector for an arbitraty set of rotation angles. \param[in,out] dst The resulting direction vector will be stored in this variable. \param[in] up_axis The up axis. \param[in] rotx The X angle in degree. \param[in] roty The Y angle in degree. \param[in] rotz The Z angle in degree. */ void create_direction_vector( vec3 *dst, vec3 *up_axis, float rotx, float roty, float rotz ) { vec4 rotation; mat4 m; mat4_identity( &m ); rotation.z = 1.0f; rotation.x = rotation.y = 0.0f; rotation.w = rotz; mat4_rotate( &m, &m, &rotation ); rotation.y = 1.0f; rotation.x = rotation.z = 0.0f; rotation.w = roty; mat4_rotate( &m, &m, &rotation ); rotation.x = 1.0f; rotation.y = rotation.z = 0.0f; rotation.w = rotx; mat4_rotate( &m, &m, &rotation ); vec3_invert( up_axis, up_axis ); vec3_multiply_mat4( dst, up_axis, &m ); }
/* draw3D {{{*/ void gl_drawbones(Armature *arm, int frame, float *mMat, float *vMat, float *pMat) { static Shader *drawbones; static GLuint tmatloc; static GLuint bone_enloc; static GLuint boneloc; static int once = 1; if(once) { drawbones = malloc(sizeof(Shader)); shader_init(drawbones, "clockwork/glsl/drawbones"); shader_add_attrib(drawbones, "position", 3, GL_FLOAT, false, 32, (void*) 0); shader_add_fragment_output(drawbones, "fragColor"); tmatloc = glGetUniformLocation(drawbones->program, "t_matrix"); bone_enloc = glGetUniformLocation(drawbones->program, "bones_enable"); boneloc = glGetUniformLocation(drawbones->program, "bones"); once = 0; } glDisable(GL_DEPTH_TEST); static GLuint bonetmp = 0; if(!bonetmp) { glGenBuffers(1, &bonetmp); } glBindBuffer(GL_ARRAY_BUFFER, bonetmp); glBufferData(GL_ARRAY_BUFFER, arm->nbones * 64, NULL, GL_STREAM_DRAW); struct Mesh_vert *bones = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); int i; for(i = 0; i < arm->nbones; i++) { //TODO: remove memcpy(&bones[i * 2].position, &arm->bones[i].head, sizeof(float[3])); memcpy(&bones[i * 2 + 1].position, &arm->bones[i].tail, sizeof(float[3])); } glUnmapBuffer(GL_ARRAY_BUFFER); mat4 t_matrix; mat4_identity(t_matrix); mat4_mult(mMat, t_matrix, t_matrix); mat4_mult(vMat, t_matrix, t_matrix); mat4_mult(pMat, t_matrix, t_matrix); gl_bindshader(drawbones); gl_bindshaderattributes(); //TODO: remove glUniformMatrix4fv(tmatloc, 1, true, t_matrix); glUniform1i(bone_enloc, true); mat4 *m = alloca(arm->nbones * sizeof(mat4)); armature_matrices(arm, frame, m); glUniformMatrix4fv(boneloc, arm->nbones, true, (float*)m); glDrawArrays(GL_LINES, 0, arm->nbones * 2); glBindBuffer(GL_ARRAY_BUFFER, 0); gl_unbindshader(); glEnable(GL_DEPTH_TEST); }
void mat4_translate(struct mat4 *m, GLfloat x, GLfloat y, GLfloat z) { mat4_identity(m); m->xw = x; m->yw = y; m->zw = z; }
void mat4_ortho(mat4_t mat, float w, float h, float n, float f) { mat4_identity(mat); mat[0] = 1.0f / w; mat[5] = 1.0f / h; mat[10] = 1.0f / (f - n); mat[11] = (-n) * mat[10]; }
mat4_t mat4_translate2(v2_t v) { mat4_t res = mat4_identity(); res.m14 = v.x; res.m24 = v.y; res.m34 = 0.f; res.m44 = 1.f; return res; }
mat4_t mat4_rotate_z(float angle) { mat4_t res = mat4_identity(); res.m11 = cos(angle); res.m12 = -sin(angle); res.m21 = sin(angle); res.m22 = cos(angle); return res; }
mat4_t mat4_rotate_y(float angle) { mat4_t res = mat4_identity(); res.m11 = cos(angle); res.m13 = sin(angle); res.m31 = -sin(angle); res.m33 = cos(angle); return res; }
mat4_t mat4_transalte3(v3_t v) { mat4_t res = mat4_identity(); res.m14 = v.x; res.m24 = v.y; res.m34 = v.z; res.m44 = 1.f; return res; }
mat4_t mat4_rotate_x(float angle) { mat4_t res = mat4_identity(); res.m22 = cos(angle); res.m23 = -sin(angle); res.m32 = sin(angle); res.m33 = cos(angle); return res; }
void mat4_rotate_y(struct mat4 *m, GLfloat angle) { GLfloat c = cos(angle * M_PI / 180.0); GLfloat s = sin(angle * M_PI / 180.0); mat4_identity(m); m->xx = c; m->xz = s; m->zx = -s; m->zz = c; }
void mat4_rotate_x(struct mat4 *m, GLfloat angle) { GLfloat c = cos(angle * M_PI / 180.0); GLfloat s = sin(angle * M_PI / 180.0); mat4_identity(m); m->yy = c; m->yz = -s; m->zy = s; m->zz = c; }
mat4_t mat4_ortho_projection(float left, float right, float bot, float top, float zNear, float zFar) { mat4_t mat = mat4_identity(); mat.m11 = 2.f / (right - left); mat.m22 = 2.f / (top - bot); mat.m33 = -2.f / (zFar - zNear); mat.m14 = -(right + left) / (right - left); mat.m24 = -(top + bot) / (top - bot); mat.m34 = -(zFar + zNear) / (zFar - zNear); return mat; }
void mat4_rotate_z(struct mat4 *m, GLfloat angle) { GLfloat c = cos(angle * M_PI / 180.0); GLfloat s = sin(angle * M_PI / 180.0); mat4_identity(m); m->xx = c; m->xy = -s; m->yx = s; m->yy = c; }
void test_matrix(void) { SECTION_BEGIN("Matrix Math"); TEST_BEGIN("Quaternion Conversion"); mat4 m; quaternion q; mat4_identity(m); mat4_to_quaternion(m, q); vec4_print(q); //TODO: assert q = (0,0,0,1); TEST_END("Quaternion Conversion"); SECTION_END("Matrix Math"); }
int main(int argc, char **argv) { for (int i = 1; i < argc; i += 1) { if (!strcmp(argv[i], "-help")) { printf("option: -help -list\n"); printf("selection: all || test_name || skip_test_name\n"); return 0; } } uint num_test_performed = 0; m_scope_exit(printf("Performed %d tests\n", num_test_performed)); m_test(math) { m_case(matrix) { mat4 m = mat4{} * mat4_identity(); m_assert(m == mat4{}); m = mat4_identity(); mat4 m_inv = mat4_inverse(m); m_assert(m == m_inv); } } }
void mat4_rotationZ(mat4_t mat, float deg) { float rad, cosRad, sinRad; rad = deg * PI / 180.0f; cosRad = cos(rad); sinRad = sin(rad); mat4_identity(mat); mat[0] = cosRad; mat[4] = sinRad; mat[1] = -sinRad; mat[5] = cosRad; }
void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); mat4_identity(view); view = mat4_lookAt(eye, center, up, view); render_geometry(&g, model, view, projection); if (debug) { render_debug(&g, model, view, projection); } glutSwapBuffers(); }
void mat4_rotate( mat4 *dst, mat4 *m, vec4 *v ) { float s = sinf( v->w * DEG_TO_RAD ), c = cosf( v->w * DEG_TO_RAD ), xx, yy, zz, xy, yz, zx, xs, ys, zs, c1; mat4 mat; vec3 t = { v->x, v->y, v->z }; mat4_identity( &mat ); if( !v->w || !vec3_normalize( &t, &t ) ) return; xx = t.x * t.x; yy = t.y * t.y; zz = t.z * t.z; xy = t.x * t.y; yz = t.y * t.z; zx = t.z * t.x; xs = t.x * s; ys = t.y * s; zs = t.z * s; c1 = 1.0f - c; mat.m[ 0 ].x = ( c1 * xx ) + c; mat.m[ 1 ].x = ( c1 * xy ) - zs; mat.m[ 2 ].x = ( c1 * zx ) + ys; mat.m[ 0 ].y = ( c1 * xy ) + zs; mat.m[ 1 ].y = ( c1 * yy ) + c; mat.m[ 2 ].y = ( c1 * yz ) - xs; mat.m[ 0 ].z = ( c1 * zx ) - ys; mat.m[ 1 ].z = ( c1 * yz ) + xs; mat.m[ 2 ].z = ( c1 * zz ) + c; mat4_multiply_mat4( m, m, &mat ); }
// Set a perspective frustum struct mat4 proj_frustum( scalar l, scalar r, // Left, Right scalar b, scalar t, // Bottom, Top scalar n, scalar f) // Near, Far { struct mat4 m; m = mat4_identity(); m.a.x = 2 * n / (r - l); m.a.z = (r + l) / (r - l); m.b.y = 2 * n / (t - b); m.b.z = (t + b) / (t - b); m.c.z = -(f + n) / (f - n); m.c.w = -(2 * f * n) / (f - n); m.d.z = -1; m.d.w = 0; return m; }
void mat4_perspective(mat4_t mat, float fov, float aspect, float n, float f) { float tanFov = tan(fov * PI / 180.0f / 2.0f); mat4_identity(mat); mat[0] = 1.0f / tanFov; mat[5] = 1.0f / (tanFov * aspect); mat[10] = 1.0f / (f - n); mat[11] = (-n) * mat[10]; //mat[0] = 1 / (ar * tanFov); //mat[5] = 1 / tanFov; //mat[10] = far / (far - near); //mat[11] = 1.0f; //mat[14] = -far * near / (far - near); //mat[15] = 0.0f; }
void InitGL() { glEnable(GL_DEPTH_TEST); glClearColor(0.2, 0.2 , 0.6, 0.0); projection = mat4_create(projection); view = mat4_create(view); model = mat4_create(model); model = mat4_identity(model); scan_data data = load_scan_data(filename, data_width, data_height, data_length, bit_rate); marching_cubes(threshold, &data, debug, &g); model = mat4_translate(model, g.center, model); free(data.data); }
void GFX_set_perspective( float fovy, float aspect_ratio, float clip_start, float clip_end, float screen_orientation ) { mat4 mat; float d = clip_end - clip_start, r = ( fovy * 0.5f ) * DEG_TO_RAD, s = sinf( r ), c = cosf( r ) / s; mat4_identity( &mat ); mat.m[ 0 ].x = c / aspect_ratio; mat.m[ 1 ].y = c; mat.m[ 2 ].z = -( clip_end + clip_start ) / d; mat.m[ 2 ].w = -1.0f; mat.m[ 3 ].z = -2.0f * clip_start * clip_end / d; mat.m[ 3 ].w = 0.0f; GFX_multiply_matrix( &mat ); if( screen_orientation ) GFX_rotate( screen_orientation, 0.0f, 0.0f, 1.0f ); }
void keyboard(unsigned char key, int x, int y) { float direction[3]; float rotation[16]; mat4_identity(rotation); switch (key) { case 'q': vec3_subtract(center, eye, direction); vec3_normalize(direction, direction); vec3_add(eye, direction, eye); break; case 'e': vec3_subtract(center, eye, direction); vec3_normalize(direction, direction); vec3_subtract(eye, direction, eye); break; case 'a': mat4_rotateY(rotation, 0.1f, rotation); mat4_multiply(rotation, model, model); break; case 'd': mat4_rotateY(rotation, -0.1f, rotation); mat4_multiply(rotation, model, model); break; case 'w': mat4_rotateX(rotation, 0.1f, rotation); mat4_multiply(rotation, model, model); break; case 's': mat4_rotateX(rotation, -0.1f, rotation); mat4_multiply(rotation, model, model); break; case 27: exit(0); default: break; } }