Jump to content
  • Advertisement
Sign in to follow this  
Arifin

OpenGL Color update problem

This topic is 3737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am new with OpenGL, and trying to build a Windows application that displays, modifies, and re-displays (as the image gets modified) a grayscale image (in PGM format). Upto now, I could load/display an image, and also could modify it, eg, say, drawing a circle in it (I checked where I saved the modified image to verify that the modification actually worked). After the modification, the circle should appear in 'white' as I drew it in the maximum grayscale intensity. But the problem is, it is appearing in 'black', instead of white. Any suggestion how to resolve this? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Arifin
Hi,
I am new with OpenGL, and trying to build a Windows application that displays, modifies, and re-displays (as the image gets modified) a grayscale image (in PGM format). Upto now, I could load/display an image, and also could modify it, eg, say, drawing a circle in it (I checked where I saved the modified image to verify that the modification actually worked). After the modification, the circle should appear in 'white' as I drew it in the maximum grayscale intensity. But the problem is, it is appearing in 'black', instead of white. Any suggestion how to resolve this?
Thanks!

In order to have an idea of what's actually wrong, you're going to have to post some code. Ideally, only that which is relevant to your problem, and put it between [source] and [/source] tags to give it scrollability and syntax highlighting.

Also, there's no need to cross post, just pick the most relevant forum and stick to it [smile].

Share this post


Link to post
Share on other sites
Ok, the problem with that is, since I am modifying someone else's code, I don't know which is the relevant part. Still, with my best guess, here is some code fragment:


void cw_drawCT(void)
// Draw CT images in the viewport
{
if(cw_GlobalVar_ptr_CurrentCT != NULL){
cw_CT_SLICE *ptr_slice;
int irow, icol;
GLubyte *SliceImage;
ptr_slice = cw_GlobalVar_ptr_CurrentCT->ptr_Arr1D_ptr_CTSlice[cw_GlobalVar_CurrentSliceIndex];
SliceImage = (GLubyte *)malloc(sizeof(GLubyte)*ptr_slice->nCols*ptr_slice->nRows*3);
if(SliceImage==NULL){
MessageBox(hWndMain, "Fail to allocate memory", "Error", MB_OK | MB_ICONEXCLAMATION);
PostMessage(hWndMain, WM_CLOSE, 0, 0);
return;
}

for(irow=0;irow<ptr_slice->nRows; irow++){
for(icol=0; icol<ptr_slice->nCols; icol++){
int grayscale = (ptr_slice->data[irow*ptr_slice->nCols+icol]*256)/ptr_slice->MaxGrayScale;
SliceImage[(irow*ptr_slice->nCols+icol)*3] = (GLubyte) grayscale;
SliceImage[(irow*ptr_slice->nCols+icol)*3+1] = (GLubyte) grayscale;
SliceImage[(irow*ptr_slice->nCols+icol)*3+2] = (GLubyte) grayscale;
}
}
{ //1 : 1 only
glRasterPos2i(0, cw_GL_ViewPortHeight);
glPixelZoom(1.0, -1.0); //OPENGL line draw/scan from bottom to up.
glDrawPixels(ptr_slice->nCols, ptr_slice->nRows, GL_RGB, GL_UNSIGNED_BYTE, SliceImage);
glPixelZoom(1.0, 1.0);
}
free(SliceImage);
}
}


void cw_Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

//Setup GL viewport
if(cw_GlobalVar_ptr_CurrentCT != NULL){
cw_CT_SLICE *pslice;
RECT rcClient;

pslice = cw_GlobalVar_ptr_CurrentCT->ptr_Arr1D_ptr_CTSlice[cw_GlobalVar_CurrentSliceIndex];
cw_GL_ViewPortOrigin_X = 280;
cw_GL_ViewPortOrigin_Y = 30 + pslice->nRows; //in client area coordinates
cw_GL_ViewPortWidth = pslice->nCols;
cw_GL_ViewPortHeight = pslice->nRows;

GetClientRect(hWndMain, &rcClient);
glViewport(cw_GL_ViewPortOrigin_X, rcClient.bottom - cw_GL_ViewPortOrigin_Y, cw_GL_ViewPortWidth, cw_GL_ViewPortHeight);
//GL viewport has a coordinate system different client area coordinate
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (double)cw_GL_ViewPortWidth, 0.0, (double)cw_GL_ViewPortHeight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glShadeModel(GL_FLAT);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
}

//draw CT image
cw_drawCT();
SwapBuffers( hDCMain );
return;
}


Share this post


Link to post
Share on other sites
It's probably because the raster position is offscreen, so nothing gets rendered.
You can try to simplify the code to


glViewport(0, 0, cw_GL_ViewPortWidth, cw_GL_ViewPortHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (double)cw_GL_ViewPortWidth, 0.0, (double)cw_GL_ViewPortHeight, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


and also

glRasterPos2i(0, 0); //Bottom left of window
glDrawPixels(ptr_slice->nCols, ptr_slice->nRows, GL_RGB, GL_UNSIGNED_BYTE, SliceImage);



You might want to use GL_BGRA. See http://www.opengl.org/wiki/index.php/Common_Mistakes#glDrawPixels

Share this post


Link to post
Share on other sites
Thanks, but actually the problem was here:

int grayscale = (ptr_slice->data[irow*ptr_slice->nCols+icol]*256)/ptr_slice->MaxGrayScale;
SliceImage[(irow*ptr_slice->nCols+icol)*3] = (GLubyte) grayscale;
SliceImage[(irow*ptr_slice->nCols+icol)*3+1] = (GLubyte) grayscale;
SliceImage[(irow*ptr_slice->nCols+icol)*3+2] = (GLubyte) grayscale;


For any MaxGray pixel found, it was getting a zero.
I debugged, and luckily could solve it by 'printing' a MaxGray pixel using slightly less intensity.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!