| 1 |
|
| 2 |
#include <GL/glew.h> |
| 3 |
|
| 4 |
#include "MyCanvas.h" |
| 5 |
#include "HardSettings.h" |
| 6 |
|
| 7 |
MyCanvas mainCanvas(MyCanvas::COLOR_AND_DEPTH); |
| 8 |
MyCanvas shadowmapCanvas(MyCanvas::DEPTH); |
| 9 |
MyCanvas shadowAOCanvas(MyCanvas::DEPTH); |
| 10 |
MyCanvas haloCanvas(MyCanvas::DEPTH); |
| 11 |
MyCanvas moltextureCanvas(MyCanvas::COLOR,hardSettings.TSIZE); |
| 12 |
|
| 13 |
|
| 14 |
#define INVALID_ID 666 |
| 15 |
|
| 16 |
typedef unsigned int uint; |
| 17 |
|
| 18 |
bool MyCanvas::SetAsOutput(){ |
| 19 |
if (onVideo) glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0); |
| 20 |
else { |
| 21 |
if (frameID[ currentRes ] == INVALID_ID ) { |
| 22 |
if ( !InitRes() ) return false; |
| 23 |
} |
| 24 |
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, frameID[ currentRes ] ); |
| 25 |
} |
| 26 |
return true; |
| 27 |
} |
| 28 |
|
| •29 |
void MyCanvas::SetAsTexture(){ |
| 30 |
glBindTexture( GL_TEXTURE_2D, textureID[ currentRes ] ); |
| 31 |
} |
| 32 |
|
| 33 |
MyCanvas::MyCanvas(Kind k, int size){ |
| 34 |
currentRes=0; |
| 35 |
onVideo=false; |
| 36 |
kind=k; |
| 37 |
for (int i=0; i<MAX_RES; i++) { |
| 38 |
frameID[i]=textureID[i]=INVALID_ID; |
| 39 |
} |
| 40 |
SetRes(size); |
| 41 |
} |
| 42 |
|
| 43 |
MyCanvas::MyCanvas(Kind k){ |
| 44 |
currentRes=0; |
| 45 |
onVideo=false; |
| 46 |
kind=k; |
| 47 |
for (int i=0; i<MAX_RES; i++) { |
| 48 |
frameID[i]=textureID[i]=INVALID_ID; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
void MyCanvas::SetRes( int res ) { |
| 54 |
int i=0; |
| 55 |
softRes=res; |
| 56 |
|
| 57 |
while ( (1<<i) < res) i++; |
| 58 |
|
| 59 |
if (i>=MAX_RES) { i=MAX_RES-1; softRes=1<<i; } |
| 60 |
currentRes=i; |
| 61 |
|
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
void MyCanvas::SetResPow2( int pow ){ |
| 66 |
currentRes=pow; |
| 67 |
softRes=1<<currentRes; |
| 68 |
} |
| 69 |
|
| 70 |
void MyCanvas::RedirectToVideo(){ |
| 71 |
onVideo=true; |
| 72 |
} |
| 73 |
|
| 74 |
void MyCanvas::RedirectToMemory(){ |
| 75 |
onVideo=false; |
| 76 |
} |
| 77 |
|
| 78 |
void MyCanvas::SetVideoSize(int v){ |
| 79 |
videoSize=v; |
| 80 |
} |
| 81 |
|
| 82 |
int MyCanvas::GetHardRes(){ |
| 83 |
if (onVideo) { |
| 84 |
return videoSize; |
| 85 |
} else |
| 86 |
return 1<<currentRes; |
| 87 |
} |
| 88 |
|
| 89 |
int MyCanvas::GetSoftRes(){ |
| 90 |
if (onVideo) { |
| 91 |
return videoSize; |
| 92 |
} else |
| 93 |
return softRes; |
| 94 |
} |
| 95 |
|
| 96 |
void MyCanvas::SetSameRes( const MyCanvas &c ){ |
| 97 |
if (c.onVideo) { |
| 98 |
SetRes( c.videoSize ); |
| 99 |
} else { |
| 100 |
currentRes=c.currentRes; |
| 101 |
softRes=c.softRes; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
bool CheckFrameBuffer() |
| 106 |
{ |
| 107 |
GLenum res = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
| 108 |
switch(res) { |
| 109 |
case GL_FRAMEBUFFER_COMPLETE_EXT: return true; |
| 110 |
case GL_FRAMEBUFFER_UNSUPPORTED_EXT: |
| 111 |
//printf("Unsupported FB!\n"); |
| 112 |
return false; |
| 113 |
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: |
| 114 |
//printf("Incompl: attachment !\n"); |
| 115 |
return false; |
| 116 |
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: |
| 117 |
//printf("Incompl: missing attach FB!\n"); |
| 118 |
return false; |
| 119 |
//case GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT: Note this enum was removed by the Revision 117 of the FBO spec. |
| 120 |
//printf("Incompl: dupicate attach FB!\n"); |
| 121 |
// return false; |
| 122 |
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: |
| 123 |
//printf("Incompl: dimensions!\n"); |
| 124 |
return false; |
| 125 |
case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: |
| 126 |
//printf("Incompl: formats!\n"); |
| 127 |
return false; |
| 128 |
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: |
| 129 |
//printf("Incompl: draw buffer!\n"); |
| 130 |
return false; |
| 131 |
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: |
| 132 |
//printf("Incompl: read buffer!\n"); |
| 133 |
return false; |
| 134 |
default: |
| 135 |
//printf("Unknow FB error!\n"); |
| 136 |
return false; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
bool MyCanvas::InitRes(){ |
| 142 |
//bool createOffsetFrame(uint &frameID, uint &textureID, int screensize, int flags){ |
| 143 |
|
| 144 |
bool depth= (kind==DEPTH); |
| 145 |
|
| 146 |
bool hide= (kind==DEPTH); |
| 147 |
|
| 148 |
bool use_depth= (kind==COLOR_AND_DEPTH ); |
| 149 |
|
| 150 |
|
| 151 |
int screensizex=GetHardRes(); |
| 152 |
int screensizey=GetHardRes(); |
| 153 |
|
| 154 |
if (ratio2x1) screensizex*=2; |
| 155 |
|
| 156 |
uint status=12345; |
| 157 |
if (glCheckFramebufferStatusEXT!=0) |
| 158 |
status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
| 159 |
switch(status) { |
| 160 |
case 12345: |
| 161 |
//printf("FrameBufferObject Extension not found! [hint: Update drivers] No shadows :(...\n"); |
| 162 |
return false; |
| 163 |
case GL_FRAMEBUFFER_COMPLETE_EXT: break; |
| 164 |
case GL_FRAMEBUFFER_UNSUPPORTED_EXT: |
| 165 |
//printf("FrameBufferObject not supported by your card! No shadows :(...\n"); |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
// creiamo: |
| 170 |
glGenFramebuffersEXT(1, (GLuint *)(& (frameID[ currentRes ])) ); // frame buffer |
| 171 |
|
| 172 |
//if (textureID==666) |
| 173 |
glGenTextures(1, (GLuint *)(&(textureID[ currentRes ]) )); |
| 174 |
|
| 175 |
// settimao l'offset Frame |
| 176 |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameID[ currentRes ]); |
| 177 |
|
| 178 |
const int NTRIES=2; |
| 179 |
unsigned int tryme[NTRIES]={GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT16}; |
| 180 |
static GLuint dummydepth=666; |
| 181 |
|
| 182 |
for (int i=0; i<((depth||use_depth)?NTRIES:1); i++) { |
| 183 |
|
| 184 |
/**/ // initialize texture |
| 185 |
/**/ //glActiveTextureARB(GL_TEXTURE1_ARB); |
| 186 |
/**/ glBindTexture(GL_TEXTURE_2D, textureID[currentRes] ); |
| 187 |
/**/ glTexImage2D(GL_TEXTURE_2D, 0, |
| 188 |
/**/ (depth)?tryme[i] :GL_RGBA8, |
| 189 |
/**/ screensizex, screensizey, 0, |
| 190 |
/**/ (depth)?GL_DEPTH_COMPONENT:GL_RGBA, |
| 191 |
/**/ (depth)?GL_UNSIGNED_INT:GL_UNSIGNED_BYTE, |
| 192 |
/**/ 0 |
| 193 |
/**/ ); |
| 194 |
/**/ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 195 |
/**/ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 196 |
/**/ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 197 |
/**/ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 198 |
|
| 199 |
|
| 200 |
// attach texture to framebuffer depth or color buffer |
| 201 |
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, |
| 202 |
(depth)?GL_DEPTH_ATTACHMENT_EXT:GL_COLOR_ATTACHMENT0_EXT, |
| 203 |
GL_TEXTURE_2D, textureID[currentRes], 0); |
| 204 |
|
| 205 |
if (depth) { |
| 206 |
/**/ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 207 |
/**/ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 208 |
/**/ glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE); |
| 209 |
GLuint renderID; |
| 210 |
if (hide) { |
| 211 |
// TODO: render to buffer NONE |
| 212 |
glGenRenderbuffersEXT(1, &renderID ); // render buffer |
| 213 |
|
| 214 |
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderID ); |
| 215 |
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA, screensizex, screensizey); |
| 216 |
|
| 217 |
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, |
| 218 |
GL_RENDERBUFFER_EXT, renderID ); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (use_depth) { |
| 223 |
|
| 224 |
#if 0 |
| 225 |
// create dummy depth TEXTURE |
| 226 |
if (dummydepth==666); glGenTextures(1, &dummydepth); |
| 227 |
|
| 228 |
glBindTexture(GL_TEXTURE_2D, dummydepth ); |
| 229 |
glTexImage2D(GL_TEXTURE_2D, 0, |
| 230 |
tryme[i], |
| 231 |
screensizex, screensizey, 0, |
| 232 |
GL_DEPTH_COMPONENT, |
| 233 |
GL_UNSIGNED_INT, 0 |
| 234 |
); |
| 235 |
// attach it to framebuffer |
| 236 |
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D,dummydepth, 0); |
| 237 |
|
| 238 |
#else |
| 239 |
|
| 240 |
// create dummy depth Renderbuffer (on card) |
| 241 |
if (dummydepth==666) glGenRenderbuffersEXT(1, &dummydepth); |
| 242 |
|
| 243 |
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, dummydepth ); |
| 244 |
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, tryme[i], screensizex, screensizey ); |
| 245 |
// attach it to framebuffer |
| 246 |
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, dummydepth); |
| 247 |
|
| 248 |
#endif |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
dummydepth=666; |
| 254 |
if (CheckFrameBuffer()) { |
| 255 |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0 ); |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0 ); |
| 262 |
return false; |
| 263 |
} |
| 264 |
|