📄 dwtobj.cpp
字号:
// boundary extension
collookup[i][0] = pos - 4;
switch (mode)
{
case symper:
if (collookup[i][0] < 0)
collookup[i][0] = -collookup[i][0];
if (collookup[i][0] > fullcol - 1)
collookup[i][0] = fullcol - 1 - (collookup[i][0] - fullcol + 1);
break;
case per:
collookup[i][0] = fmod((collookup[i][0] + fullcol), fullcol);
break;
}
// High pass or low pass?
if (collookup[i][0] >= halfcol)
collookup[i][1] = 1; // for Highpass
else
collookup[i][1] = 0; // for Lowpass
// Base Position
// loffset determine even or odd samples to choose for dowsampling
if (loffset == 1)
collookup[i][2] = (collookup[i][0] - halfcol * collookup[i][1]) * 2.0 + 0.5 + 1.0 - collookup[i][1];
else
collookup[i][2] = (collookup[i][0] - halfcol * collookup[i][1]) * 2.0 + 0.5 + collookup[i][1];
collookup[i][0] += 0.5;
}
DELTEXTURES(1, &m_rowLookupTexID);
DELTEXTURES(1, &m_colLookupTexID);
glGenTextures(1, &m_rowLookupTexID);
glGenTextures(1, &m_colLookupTexID);
// Create the Horizontal Lookup Texture
glBindTexture(GL_TEXTURE_RECTANGLE_NV, m_rowLookupTexID);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGB32_NV, m_imageWidth + 8, maxrowlevels, 0, GL_RGB, GL_FLOAT,
rowlookup);
// Create the Vertical Lookup Texture
glBindTexture(GL_TEXTURE_RECTANGLE_NV, m_colLookupTexID);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGB32_NV, m_imageHeight + 8, maxcollevels, 0, GL_RGB, GL_FLOAT,
collookup);
delete[]rowlookup;
delete[]collookup;
return true;
}
//------------------------------------------------------------------------------
// Function : CDwtObj::createImgTex
// Description : render input image data to texture
//------------------------------------------------------------------------------
// [in] imgbuffer --the input image buffer
//
bool CDwtObj::createImgTex(float *imgbuffer)
{
//uninitialized image dimensions
if (m_imageWidth <= 0 || m_imageHeight <= 0)
return false;
glGenTextures(1, &m_imageTexID);
glBindTexture(GL_TEXTURE_RECTANGLE_NV, m_imageTexID);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGB32_NV, m_imageWidth, m_imageHeight, 0, GL_RGB, GL_FLOAT, imgbuffer);
PrintGLerror("createimgtex");
if ( m_busefbo ) {
// fill the 2nd color texture of FBO with the image texture
glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
} else {
m_rt[1]->BeginCapture();
}
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT);
cgGLBindProgram(m_fillProg);
cgGLEnableProfile(m_fpProfile);
cgGLSetTextureParameter(m_fill_Tex_Param, m_imageTexID);
cgGLEnableTextureParameter(m_fill_Tex_Param);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(m_imageWidth, 0);
glVertex2f(m_imageWidth, 0);
glTexCoord2f(m_imageWidth, m_imageHeight);
glVertex2f(m_imageWidth, m_imageHeight);
glTexCoord2f(0, m_imageHeight);
glVertex2f(0, m_imageHeight);
glEnd();
cgGLDisableTextureParameter(m_fill_Tex_Param);
cgGLDisableProfile(m_fpProfile);
glPopMatrix();
PrintGLerror(" create image texture");
if ( !m_busefbo ) {
m_rt[1]->EndCapture();
}
DELTEXTURES(1, &m_imageTexID);
return true;
}
//------------------------------------------------------------------------------
// Function : CDwtObj::setImageDimensions
// Description : set image dimension variables
//------------------------------------------------------------------------------
// [in] width, height -- the input image dimension
bool CDwtObj::setImageDimensions(int width, int height)
{
if (width <= 0 || height <= 0)
return false;
this->m_imageWidth = width;
this->m_imageHeight = height;
return true;
}
//------------------------------------------------------------------------------
// Function : CDwtObj::initOpenGL
// Description : initialize OpenGL
//------------------------------------------------------------------------------
//
bool CDwtObj::initOpenGL()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
if (0 == glutGetWindow())
glutCreateWindow("gpu dwt");
return true;
}
//------------------------------------------------------------------------------
// Function : CDwtObj::initglew()
// Description : initialize glew
//------------------------------------------------------------------------------
//
bool CDwtObj::initglew()
{
int err = glewInit();
if (GLEW_OK != err)
{
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return false;
}
if (!GLEW_NV_texture_rectangle)
{
fprintf(stderr, "Sorry, Non--NVIDIA card detected, This class needs NVIDIA extensions ");
return false;
}
return true;
}
//------------------------------------------------------------------------------
// Function : CDwtObj::initRenderTexture
// Description : initialize the renderTexture objects
//------------------------------------------------------------------------------
// [in] BPP--bits per pixel
//
bool CDwtObj::initRenderTexture(int BPP)
{
bool succ;
//only support 16-bit or 32-bit
if (BPP != 16 && BPP != 32)
return false;
m_rt[0] = new RenderTexture(this->m_imageWidth, this->m_imageHeight);
m_rt[1] = new RenderTexture(this->m_imageWidth, this->m_imageHeight);
succ = m_rt[0]->Initialize(true, false, false, false, false, BPP, BPP, BPP, 0, RenderTexture::RT_RENDER_TO_TEXTURE);
succ = succ && (m_rt[1]->Initialize(true, false, false, false, false, BPP, BPP, BPP, 0, RenderTexture::RT_RENDER_TO_TEXTURE));
if (!succ)
{
printf("can't create RenderTexture\n");
exit(-1);
}
// setup the rendering context for the RenderTexture
for (int i = 0; i < 2; i++)
{
m_rt[i]->BeginCapture();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, m_imageWidth, 0.0f, m_imageHeight);
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glClearColor(0.2, 0.2, 0.2, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
m_rt[i]->EndCapture();
}
return true;
}
//------------------------------------------------------------------------------
// Function : CDwtObj::initFBO
// Description : initialize the FBO objects
//------------------------------------------------------------------------------
// [in] BPP--bits per pixel
//
bool CDwtObj::initFBO(int BPP)
{
//only support 16-bit or 32-bit
bool suc = false;
if (BPP != 16 && BPP != 32)
return false;
// Initialize the FBO object
GLenum GL_TARGET = GL_TEXTURE_RECTANGLE_NV;
GLuint GL_FORMAT = (BPP == 16) ? GL_FLOAT_RGB16_NV : GL_FLOAT_RGB32_NV;
glGenFramebuffersEXT(1, &m_fbo);
// Enable FBO
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
// Initialize two textures
glGenTextures(1, &m_fbort[0]);
glBindTexture(GL_TARGET, m_fbort[0]);
glTexParameteri(GL_TARGET, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TARGET, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TARGET, 0, GL_FORMAT, this->m_imageWidth, this->m_imageHeight, 0, GL_RGB, GL_FLOAT, NULL);
glGenTextures(1, &m_fbort[1]);
glBindTexture(GL_TARGET, m_fbort[1]);
glTexParameteri(GL_TARGET, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TARGET, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TARGET, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TARGET, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TARGET, 0, GL_FORMAT, this->m_imageWidth, this->m_imageHeight, 0, GL_RGB, GL_FLOAT, NULL);
// Attach textures to two color attachements of the FBO
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TARGET, m_fbort[0], 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TARGET, m_fbort[1], 0);
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
// Initialize the OpenGL rendering context
glViewport(0, 0, m_imageWidth, m_imageHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, m_imageWidth, 0.0f, m_imageHeight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glClearColor(0.2, 0.2, 0.2, 1);
// Check the completeness of the FBO
GLenum status;
status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
switch(status)
{
case GL_FRAMEBUFFER_COMPLETE_EXT:
suc = true;
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
printf("[initFBO]: FBO misses a required image/buffer attachment!\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
printf("[initFBO]: FBO has no images/buffers attached!\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT:
printf("[initFBO]: FBO has an image/buffer attached in multiple locations!\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
printf("[initFBO]: FBO has mismatched image/buffer dimensions!\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
printf("[initFBO]: FBO colorbuffer attachments have different types!\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
printf("[initFBO]: FBO is trying to draw to non-attached color buffer!\n");
break;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
printf("[initFBO]: FBO is trying to read from a non-attached color buffer!\n");
break;
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
printf("[initFBO]: FBO format is not supported by current graphics card/driver!\n");
break;
case GL_FRAMEBUFFER_STATUS_ERROR_EXT:
printf("[initFBO]: Non-framebuffer passed to glCheckFramebufferStatusEXT()!\n");
break;
default:
printf("[initFBO]: *UNKNOWN ERROR* reported from glCheckFramebufferStatusEXT() for %s!\n");
break;
}
return suc;
}
//------------------------------------------------------------------------------
// Function : CDwtObj::Init_Cg
// Description : initialize Cg and Cg Shaders
//------------------------------------------------------------------------------
// [in] mode--dwt mode ( forward, backward ,both)
//
// Modification history
//
// 15 March 2006 (By Liang Wan)
// - Use the latest fragment profile avaible on the GPU
//
bool CDwtObj::Init_Cg(dwtmode mode)
{
// Get the latest CG profile
m_fpProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
cgSetErrorCallback(cgErrorCallback);
// Create cgContext.
if (!cgIsContext(cgContext))
cgContext = cgCreateContext();
m_fillProg = cgCreateProgram(cgContext,
CG_SOURCE,
fillsource, m_fpProfile, NULL, NULL);
if (m_fillProg != NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -