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

📄 wgl.cpp

📁 The goal of this project is to explore the idea of point-based radiosity, which is a shooting radio
💻 CPP
字号:
/*+-------------------------------------------------------------------
  Ben Landon
  CSCI E235 
  Final Project

*/

#include "wgl.h"
#include "gl_common.h"

/*+-------------------------------------------------------------------
  Place to set OpenGL specific state such as enabling lighting,
  enabling textures, etc.

*/
static void set_opengl_state (void)
{
    // Set up some initial state for OpenGL
    glClearColor(0.0, 0.0, 0.0, 1.0);    
    glClearDepth(1.0f);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();    

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_DEPTH_TEST);
}

/*+-------------------------------------------------------------------
  setup_opengl

  Creates and returns an OpenGL rendering context.  setup_opengl
  also makes this OpenGL rendering context the current context.

*/
HGLRC setup_opengl(HWND hwnd)
{
    HGLRC hglrc = NULL;
    HDC hdc = GetDC(hwnd);

    do 
    {
	PIXELFORMATDESCRIPTOR pfd;
	
	DWORD err = NO_ERROR;
	
	ZeroMemory(&pfd, sizeof(pfd));
	pfd.nSize = sizeof(pfd);
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	
	//pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;    
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cDepthBits = 32;
	pfd.cStencilBits = 8;        
        pfd.cColorBits = 64;
        pfd.cAccumBits = 64;

	int pixel_format = ChoosePixelFormat(hdc, &pfd);
	
	if (!pixel_format)
	{
	    break;
	}
	
        int max = DescribePixelFormat(hdc, pixel_format,sizeof(pfd),  &pfd);
                
	if (!SetPixelFormat(hdc, pixel_format, &pfd))
	{
	    break;
	}
        	
	bool double_buffered = pfd.dwFlags && PFD_DOUBLEBUFFER;
	
	hglrc = wglCreateContext(hdc);
	
	if (!hglrc)
	{
	    break;
	}
    
	wglMakeCurrent(hdc, hglrc);
	
	set_opengl_state();

    } while(false);
    
    if (hdc)
    {
	ReleaseDC(hwnd, hdc);
    }   
    
    return hglrc;
}


/*+-------------------------------------------------------------------
  cleanup_opengl

  Cleans up Win32 specific pieces of OpenGL.  This deletes
  the OpenGL rendering context.

*/
void cleanup_opengl (HGLRC hglrc)
{
    if (hglrc)
    {               
        wglMakeCurrent(NULL, NULL);
        wglDeleteContext(hglrc);
        hglrc = NULL;
    }
}

void set_viewport (unsigned int width, 
                   unsigned int height, 
                   float scene_aspect_ratio)
{
    if (scene_aspect_ratio <= 0.0f) 
        return;

    if ((width == 0) || (height == 0))
        return;

    float viewport_aspect_ratio = float(width) / float(height);
    
    float H;
    float W;

    if (scene_aspect_ratio < viewport_aspect_ratio)
    {
        H = float(height); 
        W = scene_aspect_ratio * height;      
    }
    else
    {
        H = width / scene_aspect_ratio;
        W = float(width);
    }
    
    glViewport(0, 0, (GLsizei)W, (GLsizei)H);
}

⌨️ 快捷键说明

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