-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOffScreenRendering.cpp
More file actions
153 lines (137 loc) · 3.78 KB
/
OffScreenRendering.cpp
File metadata and controls
153 lines (137 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// GDISaveAsBMP.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <GL/glut.h>
#include <afx.h>
#include <afxwin.h>
#include <atlimage.h>
#include <iostream>
using namespace ATL;
using namespace std;
#define BMPWidth 640
#define BMPHeight 640
HBITMAP hBitmap,hOldBitmap;
HDC hScrDC, hMemDC,olddc;
HGLRC oldrc,newrc;
int _tmain(int argc, _TCHAR* argv[])
{
void InitializeDC();
void InitializeGL();
void DrawPolygon();
void SaveBMP(char*);
InitializeDC();
InitializeGL();
DrawPolygon();
SaveBMP("D://SaveAsBmp.bmp");
return 0;
}
void InitializeDC()
{
// 创建绘图DC及内存DC
hScrDC = CreateDC(_T("BITMAP"), NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hScrDC);
//设置位图信息
int iPixel = 32;
LPBITMAPINFO lpbmih = new BITMAPINFO;
lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
lpbmih->bmiHeader.biWidth = BMPWidth;
lpbmih->bmiHeader.biHeight = BMPHeight;
lpbmih->bmiHeader.biPlanes= 1;
lpbmih->bmiHeader.biBitCount = iPixel;
lpbmih->bmiHeader.biCompression = BI_RGB;
lpbmih->bmiHeader.biSizeImage = 0;
lpbmih->bmiHeader.biXPelsPerMeter = 0;
lpbmih->bmiHeader.biYPelsPerMeter = 0;
lpbmih->bmiHeader.biClrUsed= 0;
lpbmih->bmiHeader.biClrImportant= 0;
//创建HBITMAP
BYTE *pBits;
hBitmap = CreateDIBSection(hMemDC,lpbmih,DIB_PAL_COLORS,(void **)&pBits,NULL,0);
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
CRect rc(0,0,BMPWidth,BMPHeight);
// 由内存DC创建新的RC,将图形绘制到该内存DC中
// 创建新的像素格式,用来将图形绘制到位图
PIXELFORMATDESCRIPTOR pfd= {
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1,
// version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DRAW_TO_BITMAP | // *** ***此处与绘制到窗口的rc设置不同,无double buffered *** ***
PFD_SUPPORT_GDI,
PFD_TYPE_RGBA, // RGBA type
24,// 24-bit color depth
0, 0, 0, 0, 0, 0,// color bits ignored
0,// no alpha buffer
0,// shift bit ignored
0,// no accumulation buffer
0, 0, 0, 0,// accum bits ignored
32,// 32-bit z-buffer
0,// no stencil buffer
0,// no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0,// reserved
0, 0, 0// layer masks ignored
};
int pixelFormat=ChoosePixelFormat(hMemDC,&pfd);
SetPixelFormat(hMemDC,pixelFormat,&pfd);
// 保存之前绘图的RC及DC
oldrc = wglGetCurrentContext();
olddc = wglGetCurrentDC();
// 创建新的RC
newrc = wglCreateContext(hMemDC);
// 设置新的RC及DC
wglMakeCurrent(hMemDC,newrc);
}
void InitializeGL()
{
glViewport(0,0,BMPWidth,BMPHeight);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f,0.0f,0.0f,1.0f);
// 设置投影矩阵,模型视图矩阵,视口
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
gluPerspective(45.0,BMPWidth/BMPHeight,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,0.0, 0.0,0.0,-1.0, 0.0,1.0,0.0);
}
void DrawPolygon()
{
glTranslatef(0.0f,0.0f,-20.0f);
// 清除深度及颜色缓存
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// 开始绘制
glShadeModel(GL_SMOOTH);
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex3f(-1.0,1.0,0.0);
glColor3f(0.0,1.0,0.0);
glVertex3f(1.0,1.0,0.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(1.0,-1.0,0.0);
glColor3f(1.0,1.0,0.0);
glVertex3f(-1.0,-1.0,0.0);
glEnd();
//结束绘制
glFinish();
SwapBuffers(wglGetCurrentDC());
//恢复之前的RC及DC,释放新创建的RC
wglMakeCurrent(olddc,oldrc);
wglDeleteContext(newrc);
// 完成OPENGL绘制
hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
// 释放DC
DeleteDC(hScrDC);
DeleteDC(hMemDC);
}
//保存HBITMAP为文件
void SaveBMP(char* fileName)
{
CImage image;
image.Attach(hBitmap);
HRESULT hResult = image.Save(fileName);
DeleteObject(hBitmap);
}