⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testmain.cpp

📁 XMathLib是一个通用的3D图形数学库。 其中包含两个部分: XMathLib和XGeomLib。分别处理数学和几何运算。 数学部分包含向量、矩阵、四元数的运算。以及其它的运算。 几何部分
💻 CPP
字号:
#include <iostream>
using namespace std;


#include "XMathLib.h"
#include "XGeomLib.h"
#include "XMTrace.h"
using namespace XMathLib;
using namespace XGeomLib;

//#define _CONSOLE_TEST_

#ifdef  _CONSOLE_TEST_
int main()
{
	XVector v1(4,4,1,0);
    
	cout<<"V1: "<<v1<<endl;
    XVector v2(2,0,0);

	float mdata1[16] = 
	{
	  -1 ,  1 ,  0 , 0,
	  -1 ,  0 ,  1 , 0,
	  -1 ,  0 , -2 , 0,
	  0 ,  0 ,  0 , 1,
	};

	float mdata2[16] = 
	{
	  3,1,1,1,
	  0,4,0,2,
	  2,0,1,1,
	  0,1,0,2,
	};

	XMatrix m1(mdata1);
	XMatrix m2(mdata2);

	XMatrix m3(1);

	cout<<"M1"<<endl<<m1;
	cout<<"M2"<<endl<<m2;

	bool b = XMatrix_Inv(m2,m3);
	if(!b)
		cout<<"M2为奇异矩阵"<<endl;
	cout<<"M3 = Inv(M2) "<<endl<<m3;
    cout<<"M1 X M3 "<<endl<<m2*m3<<endl;

	v2 = v1 * m1;
	cout<<"Angle to RAD 90: "<<XM_Deg2Rad(90.f)/XM_PI <<" * pi "<<endl;
	cout<<"Arc Cos Degree  45: "<<XM_ACosD(0.707f)<<endl;

	XMatrix m4;
	XVector eye(1,1,1);
	XVector at (2,1,1);
	XVector up (0,1,0);
	//XM_Rotate(m4,v4,15);

	XM_LookAt(m4,eye,at,up);
	cout<<m4<<endl;

	float fp = 0;
	XPlan plan(1,0,0,2);
	//XVector vplan = plan;
	//cout<<endl<<"VPlan : "<<vplan<<endl;
	//cout<<vplan*plan<<endl;

    cout<<"测试四元数"<<endl;

    XMatrix mRotX;
    XMathLib::XM_RotateY(mRotX,40.0);
    cout<<mRotX<<endl;

	XVector mV(21.3,1.9,29.,1);
    cout<<mV*mRotX<<endl;

    XQuaternion r(XVector(0,1,0,1),40.0);

    cout<<"Matrix build by quaternion"<<endl;
    r.toMatrix(mRotX);
    cout<<mRotX<<endl;
    XVector mV2(21.3,1.9,29.,1);
    cout<<r.Rotate(mV2)<<endl;

    XRay ray;

	ray.m_Dir = XVector3D(0,-1,0.5);
	ray.m_Point = XVector3D(0,2,0);

	//XPlan plan;
	plan.A = 0;
	plan.B = 1;
	plan.C = 0;
	plan.D = 1;

    XPoint ptOut;
	float tinter ;
	if( false == InterSection(ray,plan,ptOut,tinter) )
	{
		cout<<"平面和射线不相交"<<endl;
        
	}
	else
	{
        cout<<"平面和射线相交"<<endl;
		cout<<tinter<<endl;
		cout<<plan * ptOut<<endl;
	}
	
	return 0;
}

#else

#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"glut32.lib")
#include <windows.h>
#include <GL\glut.h>
bool g_gamemode;       // GLUT GameMode ON/OFF
bool g_fullscreen;     // Fullscreen Mode ON/OFF (When g_gamemode Is OFF)


// Our GL Specific Initializations
bool init(void)
{
	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
	return true;
}

// Our Rendering Is Done Here
void render(void)   
{
	XRay ray;
	ray.m_Dir = XVector3D(0,0,1);
	ray.m_Point = XVector3D(0,0,-1);

	XTriangle tri;
	tri.m_points[0] = XVector3D(-1,0,0);
	tri.m_points[1] = XVector3D(1,-1,0);
	tri.m_points[2] = XVector3D(1,1,0);

	XPoint p;
	float t,u,v;
	bool isInter =  InterSection(ray,tri,p,t,u,v);

	p=XVector3D(1,1,1);

	isInter =  InterSection(ray,tri,p,t);

    


	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glLoadIdentity();									// Reset The Current Modelview Matrix


	// Swap The Buffers To Become Our Rendering Visible
	glutSwapBuffers ( );
}

// Our Reshaping Handler (Required Even In Fullscreen-Only Modes)
void reshape(int w, int h)
{
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);     // Select The Projection Matrix
	glLoadIdentity();                // Reset The Projection Matrix
	// Calculate The Aspect Ratio And Set The Clipping Volume
	if (h == 0) h = 1;
	gluPerspective(80, (float)w/(float)h, 1.0, 5000.0);
	glMatrixMode(GL_MODELVIEW);      // Select The Modelview Matrix
	glLoadIdentity();                // Reset The Modelview Matrix
}

// Our Keyboard Handler (Normal Keys)
void keyboard(unsigned char key, int x, int y)
{
	switch (key) {
		case 27:        // When Escape Is Pressed...
			exit(0);    // Exit The Program
			break;          // Ready For Next Case
		default:        // Now Wrap It Up
			break;
	}
}

// Our Keyboard Handler For Special Keys (Like Arrow Keys And Function Keys)
void special_keys(int a_keys, int x, int y)
{
	switch (a_keys) {
		case GLUT_KEY_F1:
			// We Can Switch Between Windowed Mode And Fullscreen Mode Only
			if (!g_gamemode) {
				g_fullscreen = !g_fullscreen;       // Toggle g_fullscreen Flag
				if (g_fullscreen) glutFullScreen(); // We Went In Fullscreen Mode
				else glutReshapeWindow(500, 500);   // We Went In Windowed Mode
			}
			break;
		default:
			break;
	}
}

// Ask The User If He Wish To Enter GameMode Or Not
void ask_gamemode()
{
	int answer;
	// Use Windows MessageBox To Ask The User For Game Or Windowed Mode
	answer = MessageBox(NULL, "Do you want to enter game mode?", "Question",
		MB_ICONQUESTION | MB_YESNO);
	g_gamemode = (answer == IDYES);
	// If Not Game Mode Selected, Use Windowed Mode (User Can Change That With F1)
	g_fullscreen = false; 
}

// Main Function For Bringing It All Together.
int main(int argc, char** argv)
{
	ask_gamemode();                                  // Ask For Fullscreen Mode
	glutInit(&argc, argv);                           // GLUT Initializtion
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);     // Display Mode (Rgb And Double Buffered)
	if (g_gamemode) {
		glutGameModeString("640x480:16");            // Select The 640x480 In 16bpp Mode
		if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE))
			glutEnterGameMode();                     // Enter Full Screen
		else g_gamemode = false;                     // Cannot Enter Game Mode, Switch To Windowed
	}
	if (!g_gamemode) {
		glutInitWindowSize(500, 500);                // Window Size If We Start In Windowed Mode
		glutCreateWindow("NeHe's OpenGL Framework"); // Window Title 
	}
	init();                                          // Our Initialization
	glutDisplayFunc(render);                         // Register The Display Function
	glutReshapeFunc(reshape);                        // Register The Reshape Handler
	glutKeyboardFunc(keyboard);                      // Register The Keyboard Handler
	glutSpecialFunc(special_keys);                   // Register Special Keys Handler

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	//glOrtho(100,200,100,200,100,200);
	gluPerspective(60,3/4.,-50,100);
	double mat[16];
	glGetDoublev(GL_MODELVIEW_MATRIX,mat);
	for(int i = 0 ; i < 4; i ++)
	{
		for(int j = 0 ; j < 4 ; j ++)
		{
			cout<<mat[i * 4 + j]<<"\t";
		}
		cout<<endl;
	}  

	XMatrix matOut;
	//XM_Ortho3D(matOut,100,200,200,100,100,200);
	XM_Perspective(matOut,60,3/4.,-50,100);
	cout<<matOut<<endl;
	glutMainLoop();                                  // Go To GLUT Main Loop
	return 0;
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -