1. 先上代码

头文件glCommon.h

#include <GL/glew.h>
#include <GL/GL.h>
#include <GL/GLU.h> #define MAX_LEN 2048 void GLLog(const char *pszFormat, ...);
void SetupPixelFomat( HWND hWnd, HDC &hDC );
bool InitGL(HDC hDC, HGLRC &hRC);
void DestroyGL(HDC hDC, HGLRC hRC);

源文件

#include <stdio.h>
#include "glCommon.h" void SetupPixelFomat( HWND hWnd, HDC &hDC )
{
hDC = GetDC(hWnd); PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size
1, // version
PFD_SUPPORT_OPENGL | // OpenGL window
PFD_DRAW_TO_WINDOW | // render to window
PFD_DOUBLEBUFFER, // support double-buffering
PFD_TYPE_RGBA, // color type
32, // preferred color depth
0, 0, 0, 0, 0, 0, // color bits (ignored)
0, // no alpha buffer
0, // alpha bits (ignored)
0, // no accumulation buffer
0, 0, 0, 0, // accum bits (ignored)
24, // depth buffer
8, // no stencil buffer
0, // no auxiliary buffers
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0, // no layer, visible, damage masks
}; int pixelFormat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, pixelFormat, &pfd);
} bool InitGL( HDC hDC, HGLRC &hRC )
{
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC); const GLubyte *glVersion = glGetString(GL_VERSION);
GLLog("OpenGL version = %s", glVersion); if (atof((const char *)glVersion) < 1.5)
{
char strComplain[256] = {0};
sprintf_s(strComplain, 256,"OpenGL 1.5 or higher is required (your version is %s). Please upgrade the driver of your video card.",
glVersion);
MessageBox(NULL,(LPCWSTR)strComplain, _T("OpenGL version too old"),MB_OK);
return false;
} GLenum GlewInitResult = glewInit();
if (GLEW_OK != GlewInitResult)
{
MessageBox(NULL,(LPCWSTR)glewGetErrorString(GlewInitResult), _T("OpenGL version too old"),MB_OK);
return false;
} return true;
} void GLLog( const char *pszFormat, ... )
{
char szBuf[MAX_LEN]; va_list ap;
va_start(ap, pszFormat);
vsnprintf_s(szBuf, MAX_LEN, MAX_LEN, pszFormat, ap);
va_end(ap); WCHAR wszBuf[MAX_LEN] = {0};
MultiByteToWideChar(CP_UTF8, 0, szBuf, -1, wszBuf, sizeof(wszBuf));
OutputDebugStringW(wszBuf);
OutputDebugStringA("\n"); WideCharToMultiByte(CP_ACP, 0, wszBuf, sizeof(wszBuf), szBuf, sizeof(szBuf), NULL, FALSE);
printf("%s\n", szBuf);
} void DestroyGL( HDC hDC, HGLRC hRC )
{
if (hDC != NULL && hRC != NULL)
{
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
}
}
#include "glCommon.h"

HACCEL g_hAccelTable = NULL;
HGLRC g_hRC;
HDC g_hDC; static GLuint VAOs[1];
static GLuint Buffers[1]; LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
void DrawScene();
void SceneInit();
void ReSizeScene(int w, int h); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
bool bRet = false;
static TCHAR szAppName[] = TEXT ("triangles");
HWND hwnd;
MSG msg;
WNDCLASSEX wndclassex = {0};
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = WndProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = hInstance;
wndclassex.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclassex.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = szAppName;
wndclassex.hIconSm = wndclassex.hIcon; if (!RegisterClassEx (&wndclassex))
{
MessageBox (NULL, TEXT ("RegisterClassEx failed!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindowEx (WS_EX_OVERLAPPEDWINDOW,
szAppName,
TEXT ("WindowTitle"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
500,
NULL,
NULL,
hInstance,
NULL); SetupPixelFomat(hwnd, g_hDC);
bRet = InitGL(g_hDC, g_hRC);
if(!bRet)
DestroyGL(g_hDC, g_hRC);
ShowWindow (hwnd, iCmdShow); SceneInit(); while (true)
{
if(!PeekMessage(&msg, NULL, 0, 0, PM_REMOVE ))
{
DrawScene(); ::SwapBuffers(g_hDC);
continue;
} if (msg.message == WM_QUIT)
break; // Deal with windows message.
if (! g_hAccelTable || ! TranslateAccelerator(msg.hwnd, g_hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch (message)
{
case WM_CREATE:
return (0); case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
EndPaint (hwnd, &ps);
return (0);
case WM_SIZE:
{
int w = LOWORD(wParam);
int h = HIWORD(wParam);
//ReSizeScene(w, h);
}
return (0);
case WM_DESTROY:
DestroyGL(g_hDC, g_hRC);
ReleaseDC(hwnd,g_hDC);
PostQuitMessage (0);
return (0);
case WM_CLOSE:
PostQuitMessage(0);
return (0);
}
return DefWindowProc (hwnd, message, wParam, lParam);
} void DrawScene()
{
glClear(GL_COLOR_BUFFER_BIT); glBindVertexArray(VAOs[0]);
glDrawArrays(GL_TRIANGLES, 0, 6);
} void SceneInit()
{
glGenVertexArrays(1 ,VAOs);
glBindVertexArray(VAOs[0]); GLfloat vertices[6][2] = {
{-0.9f, -0.9f},
{0.85f, -0.9f},
{-0.9f, 0.85f},
{0.9f, -0.85f},
{0.9f, 0.9f},
{-0.85f, 0.9f},
}; glGenBuffers(1, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
} void ReSizeScene( int w, int h )
{
SceneInit();
}

2. OpenGL的初始化过程

2.1 初始化顶点数组对象 --> glGenVertexArrays, glBindVertexArrays,

2.2 分配顶点缓存对象 ---> glGenBuffers, glBindBuffer

2.3 将数据载入缓存对象 ---> glBufferData

2.4 初始化顶点与片段着色器(之前的代码已把此部分删除)

2.5 顶点着色器里面的数据关联到顶点属性数组 --> glVertexAttribPointer

2.6 启动顶点属性数组 ---> glEnableVertexAttribArray

3. OpenGL 渲染过程

3.1 清除屏幕 ---> glClear

3.2 激活顶点数组 ---> glBindVertexArrays

3.3 使用OpenGL绘制函数 ---> glDrawArrays etc

OpenGL编程指南第版本学习笔记 --- OpenGL程序实现过程(win32 + OpenGL)的更多相关文章

  1. OpenGL编程指南(第七版)

    OpenGL编程指南(第七版) 转自:http://blog.csdn.net/w540982016044/article/details/21287645 在接触OpenGL中,配置显得相当麻烦,特 ...

  2. VS15 openGL 编程指南 配置库 triangle例子

    最近去图书馆借了一本书<OpenGL编程指南(原书第八版)>,今天倒腾了一天才把第一个例子运行出来. 所以,给大家分享一下,希望能快速解决配置问题. 一.下载需要的库文件 首先,我们需要去 ...

  3. 编译opengl编程指南第八版示例代码通过

    最近在编译opengl编程指南第八版的示例代码,如下 #include <iostream> #include "vgl.h" #include "LoadS ...

  4. [转]OpenGL编程指南(第9版)环境搭建--使用VS2017

    1.使用CMake Configure中选择VS2017 Win64 , Finish: 点击Generate. 2.进入build目录 打开GLFW.sln , 生成解决方案. 打开vermilio ...

  5. 《C#并发编程经典实例》学习笔记—2.7 避免上下文延续

    避免上下文延续 在默认情况下,一个 async 方法在被 await 调用后恢复运行时,会在原来的上下文中运行. 为了避免在上下文中恢复运行,可让 await 调用 ConfigureAwait 方法 ...

  6. 《C#并发编程经典实例》学习笔记—3.1 数据的并行处理

    问题 有一批数据,需要对每个元素进行相同的操作.该操作是计算密集型的,需要耗费一定的时间. 解决方案 常见的操作可以粗略分为 计算密集型操作 和 IO密集型操作.计算密集型操作主要是依赖于CPU计算, ...

  7. openstack学习笔记一 虚拟机启动过程代码跟踪

    openstack学习笔记一 虚拟机启动过程代码跟踪 本文主要通过对虚拟机创建过程的代码跟踪.观察虚拟机启动任务状态的变化,来透彻理解openstack各组件之间的作用过程. 当从horizon界面发 ...

  8. coco2dx-2.2.2 win32启动过程(opengl 和 窗口大小初始化部分) - 学习笔记 1

    因为最近要做不同分辩率的适配,所于看了下引擎这方面的代码,记录一下当是学习笔记,cocos2d-x 版本 2.2.2 , 例子是samples\Cpp\TestCpp下的 TestCpp. 先看下ma ...

  9. OpenGL 编程指南 (5.1)

    1.OpenGL支持同时使用多个纹理单元,使用GL_TEXTUREi进行标识,使用前需要先激活对应的纹理单元,默认GL_TEXTURE0是激活绑定的. void glActiveTexture(GLe ...

随机推荐

  1. Java 集合 HashMap & HashSet 拾遗

    Java 集合 HashMap & HashSet 拾遗 @author ixenos 摘要:HashMap内部结构分析 Java HashMap采用的是冲突链表方式 从上图容易看出,如果选择 ...

  2. 1.javaOOP_Part1_抽象和封装

    javaOOP_Part1_抽象和封装 javaOOP_Part1_抽象和封装 1.1 面向对象 1.1.1 为什么使用面向对象 1.一切皆对象 2.现实世界就是"面向对象的" 3 ...

  3. bullet_01

    #include <btBulletDynamicsCommon.h> #include <osgViewer/Viewer> #include <map> #in ...

  4. [转]详细的mysql时间和日期函数

    这里是一个使用日期函数的例子.下面的查询选择了所有记录,其date_col的值是在最后30天以内: mysql> SELECT something FROM table WHERE TO_DAY ...

  5. 星语硬件检测专家 V4.3 官方版

    软件名称: 星语硬件检测专家 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 15.8MB 图片预览: 软件简介: 星语硬件检测专家是一款功能非常强大的硬件 ...

  6. Android 图片显示

    一.Android手机显示图片 若R.G.B每种颜色使用一个字节(8bit)表示,每幅图像可以有1670万种颜色:若R.G.B每种颜色使用两个字节(16bit)表示,每幅图像可以有10的12次方种颜色 ...

  7. cocoaPods的安装方法

    1.打开终端 2.先升级Gem sudo gem update --system 3.切换cocoapods的数据源 [先删除,再添加,查看] gem sources --remove https:/ ...

  8. ZOJ 649 Rescue(优先队列+bfs)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  9. 清除浮动clearfix

    css用clearfix清除浮动 更多2013/11/4 来源:css学习浏览量:11901 学习标签: css clearfix 本文导读:写css 时总为浮动而烦恼,如果用了浮动,浮动的父层不会跟 ...

  10. HDU 3410 Passing the Message

    可以先处理出每个a[i]最左和最右能到达的位置,L[i],和R[i].然后就只要询问区间[ L[i],i-1 ]和区间[ i+1,R[i] ]最大值位置即可. #include<cstdio&g ...