//----------------------------------------------------------------------------------------
//Here is our initialization of shader in Cg language (Because I'm using Cg language also)
myCgContext = cgCreateContext();
checkForCgError("creating context");
cgGLSetDebugMode(CG_FALSE);
cgSetParameterSettingMode(myCgContext, CG_DEFERRED_PARAMETER_SETTING);
myCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
cgGLSetOptimalOptions(myCgVertexProfile);
checkForCgError("selecting vertex profile");
myCgVertexProgram =
cgCreateProgramFromFile(
myCgContext, /* Cg runtime context */
CG_SOURCE, /* Program in human-readable form */
myVertexProgramFileName, /* Name of file containing program */
myCgVertexProfile, /* Profile: OpenGL ARB vertex program */
myVertexProgramName, /* Entry function name */
NULL); /* No extra compiler options */
checkForCgError("creating vertex program from file");
cgGLLoadProgram(myCgVertexProgram);
checkForCgError("loading vertex program");
/* No uniform vertex program parameters expected. */
myCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
cgGLSetOptimalOptions(myCgFragmentProfile);
checkForCgError("selecting fragment profile");
myCgFragmentProgram =
cgCreateProgramFromFile(
myCgContext, /* Cg runtime context */
CG_SOURCE, /* Program in human-readable form */
myFragmentProgramFileName, /* Name of file containing program */
myCgFragmentProfile, /* Profile: OpenGL ARB vertex program */
myFragmentProgramName, /* Entry function name */
NULL); /* No extra compiler options */
checkForCgError("creating fragment program from file");
cgGLLoadProgram(myCgFragmentProgram);
checkForCgError("loading fragment program");
myCgFragmentParam_decal =
cgGetNamedParameter(myCgFragmentProgram, "decal");
checkForCgError("getting decal parameter");
//--------------------------------------------------------------------------------------------
GLuint toTexture;
glGenTextures(1,&toTexture);
glBindTexture(GL_TEXTURE_2D,toTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,FINAL_WIDTH,FINAL_HEIGHT,0,GL_RGB,GL_UNSIGNED_BYTE,NULL);
GLuint texFramebuffer;
glGenFramebuffers(1,&texFramebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,texFramebuffer);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,toTexture,0);
glEnable(GL_TEXTURE_2D);
glDrawPixels(FINAL_WIDTH,FINAL_HEIGHT,GL_RED,GL_UNSIGNED_BYTE,pboImageStore); /*Here pboImageStore is the image pixels.*/
glDisable(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* Tightly packed texture data. */
glBindTexture(GL_TEXTURE_2D,toTexture);
glGetTexImage(GL_TEXTURE_2D,0,GL_RED,GL_UNSIGNED_BYTE,pboImageRead); /*This line just for test to have found the texture is
really rendered.*/
glGenerateMipmap(toTexture);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);
//--------------------------------------------------------------------------------------
//Load shaders (I'm also using Cg language shader)
cgGLBindProgram(myCgVertexProgram);
checkForCgError("binding vertex program");
cgGLEnableProfile(myCgVertexProfile);
checkForCgError("enabling vertex profile");
cgGLBindProgram(myCgFragmentProgram);
checkForCgError("binding fragment program");
cgGLEnableProfile(myCgFragmentProfile);
checkForCgError("enabling fragment profile");
//---------------------------------------------------------------------------------------
//Bind the drew picture for shader processing
cgGLSetTextureParameter(myCgFragmentParam_decal, toTexture);
checkForCgError("setting decal 2D texture");
cgGLEnableTextureParameter(myCgFragmentParam_decal);
checkForCgError("enable decal texture");
//---------------------------------------------------------------------------------------
//Draw the image
glBegin(GL_QUADS);
glVertex2f(-1, -1);
glVertex2f(1, -1);
glVertex2f(1, 1);
glVertex2f(-1, 1);
glEnd();
//----------------------------------------------------------------------------------------
//Unload shader
cgGLDisableProfile(myCgVertexProfile);
checkForCgError("disabling vertex profile");
cgGLDisableProfile(myCgFragmentProfile);
checkForCgError("disabling fragment profile");
cgGLDisableTextureParameter(myCgFragmentParam_decal);
checkForCgError("disabling decal texture");
//----------------------------------------------------------------------------------------
glBindFramebuffer(GL_READ_FRAMEBUFFER,0);
//----------------------------------------------------------------------------------------
//Read red component out from the color buffer
glReadPixels(0,0,FINAL_WIDTH,FINAL_HEIGHT,GL_RED,GL_UNSIGNED_BYTE,pboImageReadBackRed); /*To find the pixel of texture was not
rendered.*/