3D数学库的简单实现(C语言)

头文件

vmath.h

#ifndef _VMATH_H
#define  _VMATH_H


#ifdef _cplusplus
extern "C" {
#endif

typedef 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 perspectiveFrustum(Matrix44f M, float left, float right, float top, float bottom, float near, float far); //透视投影
void orthoPerspect(Matrix44f M, float left, float right, float top, float bottom, float near, float far);  //正交投影

#ifdef _cplusplus
}
#endif
#endif

实现文件vmath.c

#include <stdio.h>
#include <math.h>
#include <malloc.h>
#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;
}

绕任意轴旋转的矩阵尚未写!

编译好的静态库可至我的资源下载,使用VC++2008.


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值