Simple implementation of 3D Math Library (C language)

Source: Internet
Author: User

Header file

Vmath. h

# Ifndef _ VMATH_H # define _ VMATH_H # ifdef _ fill "C" {# endiftypedef float Matrix44f [16]; void translate (Matrix44f M, float x, float y, float z ); void scale (Matrix44f M, float x, float y, float z); void rotateX (Matrix44f M, float radian); void rotateY (Matrix44f M, float radian ); void rotateZ (Matrix44f M, float radian); void rotateXYZ (Matrix44f M, float radian, float x, float y, float z); void Merge (Matrix44f M, float left, float right, float top, float bottom, float near, float far); // Perspective Projection void orthoPerspect (Matrix44f M, float left, float right, float top, float bottom, float near, float far); // orthogonal projection # ifdef _ cplusplus} # endif

Implementation file vmath. c

#include 
 
  #include 
  
   #include 
   
    #include "vmath.h"///////////////////////////////////////////////////////////////////////void translate(Matrix44f M, float x, float y, float z){int i;for(i= 0; i <16; i++)M[i] =0;M[3]  =x;M[7]  =y;M[11] =z;M[15] =1.0;}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void scale(Matrix44f M, float x, float y, float z){int i;for(i= 0; i <16; i++)M[i] =0;M[0]  =x;M[5]  =y;M[10] =z;M[15] =1.0;}///////////////////////////////////////////////////////////////////////////////void rotateX(Matrix44f M, float radian){int i;for(i= 0; i <16; i++)M[i] =0;M[0]  =1.0;M[5]  =cos(radian);M[6]  =sin(radian);M[9]  =-sin(radian);M[10] =cos(radian);M[15] =1.0;}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void rotateY(Matrix44f M, float radian){int i;for(i= 0; i <16; i++)M[i] =0;M[0]  =cos(radian);M[2]  =-sin(radian);M[5]  =1.0;M[8]  =sin(radian);M[10] =cos(radian);M[15] =1.0;}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void rotateZ(Matrix44f M, float radian){int i;for(i= 0; i <16; i++)M[i] =0;M[0]  =cos(radian);M[1]  =-sin(radian);M[4]  =sin(radian);M[5]  =cos(radian);M[10] =1.0;M[15] =1.0;}///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void rotateXYZ(Matrix44f M, float radian, float x, float y, float z){int i;for(i= 0; i <16; i++)M[i] =0;}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void perspectiveFrustum(Matrix44f M, float left, float right, float top, float bottom, float near, float far){int i;for(i= 0; i <16; i++)M[i] =0;M[0]  =2*near/(right -left);M[2]  =(right +left)/(right -left);M[5]  =2*near/(top -bottom);M[6]  =(top +bottom)/(top -bottom);M[10] =(near +far)/(near -far);M[11] =2*near*far/(near -far);M[14] =-1.0;}void orthoPerspect(Matrix44f M,  float left, float right, float top, float bottom, float near, float far){int i;for(i= 0; i <16; i++)M[i] =0;M[0]  =2/(right -left);M[3]  =(left +right)/(left -right);M[5]  =2/(top -bottom);M[7]  =(bottom +top)/(bottom -top);M[10] =2/(near -far);M[11] =(near +far)/(far -near);M[15] =1.0;}
   
  
 

The matrix that rotates around any axis has not been written yet!

Compiled static libraries can be downloaded to my resources, using VC ++ 2008.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.