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

📄 torus.cpp

📁 Torus3D.rar,BREW平台的3D游戏引擎的一个实例.喜欢的朋友可以下载
💻 CPP
字号:
// ==========================================================================================================
//
// BREW v2.0+ OPENGLES MICROENGINE
//
// ----------------------------------------
//
// Written by Vander Nunes
//
// ==========================================================================================================

#include "torus.h"


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
CEngine::CEngine(AEEApplet* pApplet)
{
	m_pApplet = pApplet;
	m_pDDBitmap = NULL;
	m_pIGL = NULL;
	m_pIEGL = NULL;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
CEngine::~CEngine()
{
	CleanUp();
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
boolean CEngine::Init()
{
	// get device frame buffer bitmap
	if (IDISPLAY_GetDeviceBitmap(m_pApplet->m_pIDisplay, &m_pDDBitmap) != SUCCESS)
	{
		CleanUp();
		return FALSE;
	}

	// get device frame buffer info
	if (IBITMAP_GetInfo(m_pDDBitmap, &m_DDBitmapInfo, sizeof(AEEBitmapInfo)) != SUCCESS)
	{
		CleanUp();
		return FALSE;
	}

	if (ScreenDepth() < 12)
	{
		#if 1
			char* szTitle = "CANNOT RUN";
			AECHAR* wTitle = new AECHAR[64];
			UTF8TOWSTR((const byte*)szTitle, STRLEN(szTitle)+1, wTitle, 64);

			char* szText = "\nA device with at least 4096 colors is required to run this program. Cannot continue.";
			AECHAR* wText = new AECHAR[256];
			UTF8TOWSTR((const byte*)szText, STRLEN(szText)+1, wText, 256);

			ISHELL_MessageBoxText(m_pApplet->m_pIShell, (const AECHAR*)wTitle, (const AECHAR*)wText);

			delete [] wTitle;
			delete [] wText;
		#endif

		return TRUE;
	}

	// Initialize sound engine
	ISHELL_CreateInstance(m_pApplet->m_pIShell, AEECLSID_SOUNDPLAYER, (void**)&m_pISoundPlayer);

	// Prepare IGL and IEGL
	if (ISHELL_CreateInstance(m_pApplet->m_pIShell, AEECLSID_GL, (void **)&m_pIGL) != SUCCESS)
		return FALSE;
	else
		IGL_Init(m_pIGL);

	if (ISHELL_CreateInstance(m_pApplet->m_pIShell, AEECLSID_EGL, (void **)&m_pIEGL) != SUCCESS)
		return FALSE;
	else
		IEGL_Init(m_pIEGL);

	// Setup GL && EGL
	SetupEGL();
	SetupGL();

	return TRUE;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CEngine::CleanUp()
{
	// release GL && EGL resources
	if (m_pIEGL)
	{
		if (eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) == EGL_FALSE || eglGetError() != EGL_SUCCESS)
		{}
		if (eglDestroyContext(m_eglDisplay, m_eglContext) == EGL_FALSE || eglGetError() != EGL_SUCCESS)
		{}
		if (eglDestroySurface(m_eglDisplay, m_eglSurface) == EGL_FALSE || eglGetError() != EGL_SUCCESS)
		{}
		if (eglTerminate(m_eglDisplay) == EGL_FALSE || eglGetError() != EGL_SUCCESS)
		{}
		IEGL_Release(m_pIEGL);
		m_pIEGL = NULL;
	}
	if (m_pIGL)
	{
		IGL_Release(m_pIGL);
		m_pIGL = NULL;
	}

	// release the device bitmap
	if (m_pDDBitmap)
	{
		IBITMAP_Release(m_pDDBitmap);
		m_pDDBitmap = NULL;
	}

	// release sound engine
	ISOUNDPLAYER_Release(m_pISoundPlayer);
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
boolean CEngine::SetupEGL()
{
	EGLConfig myConfig;
	EGLint ncfg = 1;
	EGLint params[5] = {EGL_NONE, EGL_NONE, EGL_NONE, EGL_NONE, EGL_NONE};

	// init State Data
	m_eglDisplay = EGL_NO_DISPLAY;
	m_eglSurface = EGL_NO_SURFACE;
	m_eglContext = EGL_NO_CONTEXT;

	// main Display
	m_eglDisplay = eglGetDisplay(m_pApplet->m_pIDisplay);
	if (m_eglDisplay == EGL_NO_DISPLAY || eglGetError() != EGL_SUCCESS)
		return FALSE;

	if (eglInitialize(m_eglDisplay, NULL, NULL) == EGL_FALSE || eglGetError() != EGL_SUCCESS)
		return FALSE;

	// get Configuration
	eglGetConfigs(m_eglDisplay, &myConfig, 1, &ncfg);

	// window Surface
	IDIB *pDIB;

	if (IBITMAP_QueryInterface(m_pDDBitmap, AEECLSID_DIB, (void**)&pDIB) != SUCCESS)
		return EFAILED;

	m_eglSurface = eglCreateWindowSurface(m_eglDisplay, myConfig, pDIB, params);

	IDIB_Release(pDIB);

	if (m_eglSurface == EGL_NO_SURFACE || eglGetError() != EGL_SUCCESS)
		return FALSE;

	// context
	m_eglContext = eglCreateContext(m_eglDisplay, myConfig, 0, 0);
	if (m_eglContext == EGL_NO_CONTEXT || eglGetError() != EGL_SUCCESS)
		return FALSE;

	if (eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext) == EGL_FALSE || eglGetError() != EGL_SUCCESS)
		return FALSE;

	return TRUE;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
boolean CEngine::SetupGL()
{
	// Shading type: GL_FLAT or GL_SMOOTH
	glShadeModel(GL_FLAT);

	// Depth Test
	glEnable(GL_DEPTH_TEST);

	// ### this is a hack because of the camera projection matrix inverting the zbuffer
	// so the depth clear is changed from 1 to 0, and the comparison is changed from GL_LESS to GL_GREATER
	glClearDepthx(0);
	glDepthFunc(GL_GEQUAL);
	// ----------------------------

	// Texture mapping
	glEnable(GL_TEXTURE_2D);

	// Face culling
	glEnable(GL_CULL_FACE);

	glDisable(GL_COLOR_MATERIAL);
	glDisable(GL_LIGHTING);
	glDisable(GL_BLEND);
	//glDisable(GL_NORMALIZE);

	// Perspective Correction
	#if 1
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	#else
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
	#endif


	// ------------------------------------------------------------------------------------
	//
	// Set a default projection matrix
	//
	// ------------------------------------------------------------------------------------

	// Setup Viewport
	glViewport(0, 0, ScreenWidth(), ScreenHeight());

	// Setup Frustum
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	float ratio = (float)ScreenWidth() / ScreenHeight();
	int s = 6;	// affects directly perspective
	int w = (int)(s * ratio);
	int h = s;
	glFrustumx(
							ITOX(-w), ITOX(w),
							ITOX(-h), ITOX(h),
							ITOX(10), ITOX(500)		// near and far planes (also affects perspective)
						);
	// ------------------------------------------------------------------------------------


	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	if (glGetError() != EGL_SUCCESS)
		return FALSE;

	return TRUE;
}


void CEngine::SwapBuffers()
{
	eglSwapBuffers(m_eglDisplay, m_eglSurface);
}

⌨️ 快捷键说明

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