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

📄 openglcom.cpp

📁 涉及windows游戏编程中的一些源码
💻 CPP
字号:
/* 
This file is generated by AppWizard
			              Stanly Lee Xpert - HeartBlue
                          2002-7-25

        2002 7    增加对纹理的支持
		2002 9    增加对 矢量数学运算的支持(GLMath.cpp分离出去)
		2002 9 26 增加对摄影机的支持(加入Camera.cpp)
*/


#include <fstream>
using namespace std;
#include <windows.h>
#include <GL\gl.h>
#include <GL\glu.h>
#include "resource.h"
#include "OpenGLCom.h"
#include "GameApp.h"



COpenGL g_GL;
//=========================================
//Initialize the OpenGL and Release It
//
//=========================================
BOOL COpenGL::InitOpenGL(HWND hWnd)
{
	m_hWnd = hWnd;
	HDC hdc = GetDC(m_hWnd);
	PIXELFORMATDESCRIPTOR pfd=
	{
          sizeof(PIXELFORMATDESCRIPTOR),
		  1,
		  PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
		  PFD_TYPE_RGBA,
		  32,
		  0,0,0,0,0,0,0,0,
		  0,0,0,0,0,
		  16,
		  1,0,0,0,0,0,0
	};
    int n=ChoosePixelFormat(hdc,&pfd); 
    SetPixelFormat(hdc,n,&pfd);
    m_hrc=wglCreateContext(hdc); 
    wglMakeCurrent(hdc,m_hrc); 
	glColor3f(0.0,0.0,0.0);
	glClearColor(0.0,0.0,0.0,.50);
	glClearDepth(1);
	glClearStencil(0);
	glEnable(GL_DEPTH_TEST);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	CreateFont();
	ReleaseDC(m_hWnd,hdc);
	return 1;
}

void COpenGL::ReleaseGL()
{


	HDC hdc= GetDC(m_hWnd);
	wglMakeCurrent(hdc,NULL);
	wglDeleteContext(m_hrc); 
	ReleaseDC(m_hWnd,hdc);
}
void COpenGL::ReleaseFont()
{
	glDeleteLists(m_font_list,196);
}
//================================================================
//Function Name :   LoadBmpFromFile
//Description   :   Load a Bitmap bits from a dot bmp file
//filename:  the file contait the bitmap data
//pData   :  the buffer for keep the datas
//info    :  Infomation of the bitmap 
//=================================================================


void COpenGL::SetForceFps(int fps)
{
	if(fps==0)
	{
	  g_app.m_bForceFPS=0;
	  return ;
	}
	g_app.m_forceFPS=1000/fps;
	g_app.m_bForceFPS=1;
	return;
}
GLuint COpenGL::CreateFont()
{

	HDC hdc = GetDC(m_hWnd);
	m_font_list=glGenLists(96);
	BOOL hr=wglUseFontBitmaps(hdc,32,96,m_font_list);
	ReleaseDC(m_hWnd,hdc);
	return m_font_list;
}

void COpenGL::BeginRenderFont()
{
//Save the Attrib state
	glPushAttrib(GL_LIST_BIT);
	glPushAttrib(GL_ENABLE_BIT);

	glDisable(GL_DEPTH_TEST);
	glDisable(GL_TEXTURE_2D);
    glDisable(GL_LIGHTING);
	glDisable(GL_STENCIL_TEST);
//Save and set the View and Model matrix
    glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
//Save and set the Projection matrix
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
    glOrtho(-100,100,-100,100,-500.0,500.0);

    glPixelStorei(GL_UNPACK_ALIGNMENT,4);
}
void COpenGL::EndRenderFont()
{
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();
    glFinish();
	glPopAttrib();
	glPopAttrib();
}
void COpenGL::RenderFont(char* s,int x,int y)
{

	glRasterPos2i(x,y);
    glListBase(m_font_list-32);
    glCallLists(strlen(s),GL_UNSIGNED_BYTE,s);

}

extern float g_view_dist;
void COpenGL::Resize()
{
    RECT rct;
	GetClientRect(m_hWnd,&rct);
	m_view_width  = rct.right  - rct.left;
	m_view_height  = rct.bottom - rct.top ;
    glViewport(0,0,m_view_width,m_view_height);
	m_view_factor = float(m_view_width)/m_view_height;
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
    gluPerspective(m_eye_angle,float(m_view_width)/m_view_height,0.1,g_view_dist);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
void COpenGL::SwapBuffer()
{
	  HDC hdc = GetDC(m_hWnd);
	  SwapBuffers(hdc); 
	  ReleaseDC(m_hWnd,hdc);
	  glFlush();
}

//++++++++++++++++++++++++++++++++++++++++++++

⌨️ 快捷键说明

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