1 | #include <iostream>
|
---|
2 | #include "FrameBufferObject.h"
|
---|
3 | #include "glInterface.h"
|
---|
4 |
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 |
|
---|
8 | namespace CHCDemoEngine
|
---|
9 | {
|
---|
10 |
|
---|
11 |
|
---|
12 | void PrintFBOStatus(GLenum status)
|
---|
13 | {
|
---|
14 | switch(status)
|
---|
15 | {
|
---|
16 | case GL_FRAMEBUFFER_COMPLETE_EXT:
|
---|
17 | cout << "frame buffer object created successfully" << endl;
|
---|
18 | break;
|
---|
19 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
|
---|
20 | cerr << "incomplete attachment" << endl;
|
---|
21 | break;
|
---|
22 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
|
---|
23 | cerr << "missing attachment" << endl;
|
---|
24 | break;
|
---|
25 | case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
|
---|
26 | cerr << "incomplete dimensions" << endl;
|
---|
27 | break;
|
---|
28 | case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
|
---|
29 | cerr << "incomplete formats" << endl;
|
---|
30 | break;
|
---|
31 | case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
|
---|
32 | cerr << "incomplete draw buffer" << endl;
|
---|
33 | break;
|
---|
34 | case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
|
---|
35 | cerr << "incomplete read buffer" << endl;
|
---|
36 | break;
|
---|
37 | case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
|
---|
38 | cerr << "framebuffer unsupported" << endl;
|
---|
39 | break;
|
---|
40 | default:
|
---|
41 | cerr << "unknown status code " << status << endl;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | ColorBufferObject::ColorBufferObject(int w, int h,
|
---|
47 | FORMAT c,
|
---|
48 | WRAP_TYPE wrapType,
|
---|
49 | FILTER_TYPE filterType,
|
---|
50 | bool useMipMap,
|
---|
51 | bool useMultiSampling)
|
---|
52 | {
|
---|
53 | glGenRenderbuffersEXT(1, &mId);
|
---|
54 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
|
---|
55 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, w, h);
|
---|
56 |
|
---|
57 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_RENDERBUFFER_EXT, mId);
|
---|
58 |
|
---|
59 | glGenTextures(1, &mTexId);
|
---|
60 | glBindTexture(GL_TEXTURE_2D, mTexId);
|
---|
61 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
---|
62 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, mTexId, 0);
|
---|
63 |
|
---|
64 | GLuint filterParam;
|
---|
65 |
|
---|
66 | switch (filterType)
|
---|
67 | {
|
---|
68 | case FILTER_NEAREST:
|
---|
69 | filterParam = GL_NEAREST; break;
|
---|
70 | case FILTER_LINEAR:
|
---|
71 | filterParam = GL_LINEAR; break;
|
---|
72 | case FILTER_MIPMAP_LINEAR:
|
---|
73 | filterParam = GL_LINEAR_MIPMAP_LINEAR; break;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | GLuint wrapParam;
|
---|
78 |
|
---|
79 | switch (wrapType)
|
---|
80 | {
|
---|
81 | case WRAP_REPEAT:
|
---|
82 | wrapParam = GL_REPEAT; break;
|
---|
83 | case WRAP_CLAMP_TO_EDGE:
|
---|
84 | wrapParam = GL_CLAMP_TO_EDGE; break;
|
---|
85 | }
|
---|
86 |
|
---|
87 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterParam);
|
---|
88 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterParam);
|
---|
89 |
|
---|
90 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
|
---|
91 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
|
---|
92 |
|
---|
93 | // print status
|
---|
94 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | FrameBufferObject::FrameBufferObject(int w, int h, bool useDepth, DEPTH_FORMAT d)
|
---|
99 | : mWidth(w), mHeight(h)
|
---|
100 | {
|
---|
101 | glGenFramebuffersEXT(1, &mId);
|
---|
102 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
|
---|
103 |
|
---|
104 |
|
---|
105 | ///////////
|
---|
106 | //-- create depth buffer
|
---|
107 |
|
---|
108 | if (useDepth)
|
---|
109 | {
|
---|
110 | glGenRenderbuffersEXT(1, &mDepthId);
|
---|
111 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId);
|
---|
112 |
|
---|
113 | GLuint depthFormat;
|
---|
114 |
|
---|
115 | switch (d)
|
---|
116 | {
|
---|
117 | case DEPTH_16:
|
---|
118 | depthFormat = GL_DEPTH_COMPONENT16; break;
|
---|
119 | case DEPTH_24:
|
---|
120 | depthFormat = GL_DEPTH_COMPONENT24; break;
|
---|
121 | case DEPTH_32:
|
---|
122 | depthFormat = GL_DEPTH_COMPONENT32; break;
|
---|
123 | }
|
---|
124 |
|
---|
125 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, mWidth, mHeight);
|
---|
126 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
|
---|
127 | }
|
---|
128 |
|
---|
129 | // print status
|
---|
130 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | ColorBufferObject *FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
|
---|
135 | ColorBufferObject::WRAP_TYPE wrapType,
|
---|
136 | ColorBufferObject::FILTER_TYPE filterType,
|
---|
137 | bool useMipMap,
|
---|
138 | bool useMultiSampling)
|
---|
139 | {
|
---|
140 | ColorBufferObject *colorBuf =
|
---|
141 | new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, useMultiSampling);
|
---|
142 | mColorBuffers.push_back(colorBuf);
|
---|
143 |
|
---|
144 | return colorBuf;
|
---|
145 | }
|
---|
146 |
|
---|
147 | } // namespace
|
---|