I have a problem with drawing RGB 2d texture. ( luminance texture is ok );
I’m was trying to draw RED square, but I got luminance 1/3 of the square.
Here is a minimum code… Can you tell me what is the problem ?
//============================= main.cpp ( usage code )
void display()
{
glClear(GL_COLOR_BUFFER_BIT | PTH_BUFFER_BIT);
gl->DisplayBuffer(gl->GetDstBuffer());
glutSwapBuffers();
}
void idle()
{
glutPostRedisplay();
}
void resize(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1, 1);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(512, 512);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("gpgpu_dct");
init_opengl();
gl = new GlHelper(512, 512, mrt_available);
unsigned char* img_dst = new unsigned char[512*512*3];
memset(img_dst, 0 , 512*512*3);
memset(img_dst, 200 , 512*512);
gl->LoadDstImage(img_dst);
glutDisplayFunc(display);
glutIdleFunc(idle);
glutReshapeFunc(resize);
glutMainLoop();
return 0;
}
//==================================== helper.cpp
#include "dct_gpu.h"
#include "shared/read_text_file.h"
CGcontext g_context;
GlHelper::GlHelper(int w, int h, bool use_mrt) : width(w), height(h), mrt(use_mrt)
{
char *format = "float=16 rgba textureRECT";
// char *format = "float=32 rgba textureRECT";
dest_buffer = CreateBuffer(w, h, format);
InitCg();
}
GlHelper::~GlHelper()
{
delete dest_buffer;
}
RenderTexture *GlHelper::CreateBuffer(int w, int h, char *mode)
{
RenderTexture *buffer = new RenderTexture(mode, w, h, GL_TEXTURE_RECTANGLE_NV);
buffer->Activate();
glDisable(GL_DITHER);
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, w, 0.0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, w, h);
buffer->Deactivate();
return buffer;
}
void cgErrorCallback()
{
CGerror lastError = cgGetError();
if(lastError) {
printf("Cg error:
");
printf("%s
", cgGetErrorString(lastError));
printf("%s
", cgGetLastListing(g_context));
cgDestroyContext(g_context);
exit(-1);
}
}
void GlHelper::InitCg()
{
cgSetErrorCallback(cgErrorCallback);
g_context = cgCreateContext();
const char *code = read_text_file("gpgpu_dct/my.cg");
fprog_profile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
display_fprog = cgCreateProgram(g_context, CG_SOURCE, code, fprog_profile, "Display_PS", NULL);
brightness_param = cgGetNamedParameter(display_fprog, "brightness");
cgGLLoadProgram(display_fprog);
delete [] code;
}
void GlHelper::LoadDstImage(unsigned char* img)
{
glGenTextures(1, &img_tex_dst);
glBindTexture(GL_TEXTURE_RECTANGLE_NV, img_tex_dst);
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_RGBA, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
dest_buffer->Activate();
DisplayTexture(img_tex_dst);
dest_buffer->Deactivate();
}
void GlHelper::DrawQuad(int w, int h, int tw, int th)
{
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(0, 0);
glTexCoord2i(tw, 0); glVertex2i(w, 0);
glTexCoord2i(tw, th); glVertex2i(w, h);
glTexCoord2i(0, th); glVertex2i(0, h);
glEnd();
}
// display contents of buffer to screen
void GlHelper::DisplayBuffer(RenderTexture *buffer, float brightness)
{
cgGLBindProgram(display_fprog);
cgGLSetParameter1f(brightness_param, brightness);
cgGLEnableProfile(fprog_profile);
glActiveTextureARB(GL_TEXTURE0_ARB);
buffer->Bind();
DrawQuad(width, height, buffer->GetWidth(), buffer->GetHeight());
buffer->Release();
cgGLDisableProfile(fprog_profile);
}
void GlHelper::DisplayTexture(GLuint tex)
{
cgGLBindProgram(display_fprog);
cgGLEnableProfile(fprog_profile);
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_NV, tex);
DrawQuad(width, height, width, height);
cgGLDisableProfile(fprog_profile);
}
// perform a rendering pass with given fragment program and source and destination buffers
void
GlHelper::Pass(CGprogram prog, RenderTexture *src, RenderTexture *src2, RenderTexture *dest)
{
dest->Activate();
cgGLBindProgram(prog);
cgGLEnableProfile(fprog_profile);
glActiveTextureARB(GL_TEXTURE0_ARB);
src->Bind();
if (src2) {
glActiveTextureARB(GL_TEXTURE1_ARB);
src2->Bind();
}
DrawQuad(dest->GetWidth(), dest->GetHeight(), width, height);
src->Release();
if (src2) src2->Release();
cgGLDisableProfile(fprog_profile);
dest->Deactivate();
}
//===================================== my.cg code
float4 Display_PS(float2 texcoord : TEXCOORD0,
uniform samplerRECT image,
uniform float brightness = 1.0
) : COLOR
{
float4 c = texRECT(image, texcoord);
c *= brightness;
return c;
}