tags:

views:

38

answers:

2

Hello,

I have an application to show a 2D data MxN with the data value from 0-63. I am displaying it using a colormap which is 64x3. I would like to do it this way: Prepare the vertex points, prepare the index array which is the data values. I think this would be the best way which has both the space and performance efficiency. The code would be like this:

 p=colormap_matlab;
 glEnableClientState(GL_VERTEX_ARRAY);
 glEnableClientState(GL_COLOR_ARRAY);
 glEnableClientState(GL_INDEX_ARRAY);
 glVertexPointer(2, GL_INT, 0, vertices);
 glColorPointer(3, GL_FLOAT, 0, p);
 glIndexPointer(GL_UNSIGNED_BYTE,0,color_index);
 int iter = 0;
 int iterP = 0;
 for(i = 0; i < 127; i++)
 {
  iter = 0;
  iterP = 0;
  for(j = 0; j < 1000; j++)
  {
   id1 = (int) data[i*1000+j ];
   id2 = (int) data[(i+1)*1000 + j ];

   color_index[iter++]=id1;
   color_index[iter++]=id2;

   vertices[iterP++] = i;
   vertices[iterP++] = j;
   vertices[iterP++] = i+1;
   vertices[iterP++] = j;

  }

  //glDrawElements(GL_QUAD_STRIP, 999*2, GL_UNSIGNED_INT, indices);
  glDrawArrays(GL_QUAD_STRIP,0,1000*2);
  //glDrawArrays(GL_QUAD_STRIP,500*2,500*2);
 }
 glDisableClientState(GL_VERTEX_ARRAY);
 glDisableClientState(GL_COLOR_ARRAY);
 glDisableClientState(GL_INDEX_ARRAY);

However, it turns out the index pointer array does not work at all. It just draws the color from the colormap sequentially (and will cause data overflow since the colormap is only 64x3).

setting up the context would be like this:

  CSimple_drawView *pView  = (CSimple_drawView* ) pParam;
   HWND hWnd = (pView)->GetSafeHwnd();  
   HDC  hDC ;
   HGLRC hRC;
   hDC = ::GetDC(hWnd);
   SetupPixelFormat(hDC);
   hRC = wglCreateContext( hDC );
   wglMakeCurrent( hDC, hRC );
   readfile(0);
   init_index();
   init_mesh_index();
   int  i = 0;
   all_threads.SetEvent();
   // end added here 
   int startTime = GetTickCount();
    while(i < 200)
    {
        initialize(hWnd);
        //readfile(0);
        WaitForSingleObject(all_threads.m_hObject, INFINITE);
        glLoadIdentity();
        glClear(GL_COLOR_BUFFER_BIT);
        i++;
        Render4(0,count);
        ++count;
        SwapBuffers(hDC);

    }


BOOL SetupPixelFormat(HDC hDC)
{

    PIXELFORMATDESCRIPTOR pixelDesc=
    {
        sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|
        PFD_DOUBLEBUFFER|PFD_SUPPORT_GDI,
        PFD_TYPE_RGBA,
        24,
        0,0,0,0,0,0,
        0,
        0,
        0,
        0,0,0,0,
        32,
        0,
        0,
        PFD_MAIN_PLANE,
        0,
        0,0,0
    };

    int pixelformat;

    if ( (pixelformat = ChoosePixelFormat(hDC, &pixelDesc)) == 0 )
    {
        MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
        return FALSE;
    }

    if (SetPixelFormat(hDC, pixelformat, &pixelDesc) == FALSE)
    {
        MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
        return FALSE;
    }
    return TRUE;

}

Anyone can give me some hints on this?

A: 

Make sure this doesn't apply to you.

genpfault
I know what you mean,but that is not my case. I just want to use the color index so I do not need to update the color array.
shangping
I'm pretty sure that's just for indexed color modes (think classic 8-bit paletted VGA).
genpfault
so what is your point here? Do I use it incorrectly or what else?
shangping
If you requested a RGB/RGBA context from your windowing system I'd say you're using it incorrectly.
genpfault
I did not do this. I have the colormap_matlab from matlab (all values filled 64x3). I just use the color_index array to save some space so I do not need to specify the R,G,B for each vertex. I just wonder why I cannot use the index_pointer to achieve this? I checked each function and made sure everything is correct as to my understanding.
shangping
Can you edit your question to show us your context creation code?
genpfault
Please see the updated question. Maybe I am wrong, I used the windows context. So you have any suggestions how I do it correctly? Thanks
shangping
Yep, you're creating a `PFD_TYPE_RGBA` context instead of `PFD_TYPE_COLORINDEX`. [Here's](http://msdn.microsoft.com/en-us/library/dd318293%28VS.85%29.aspx) some color index info.
genpfault
Thank you. I changed and it showed seemingly right but not exactly right pictures. The picture is not stable and slow, picture color is very confusing compared to the RGBA mode. I think that maybe due to that there is no shading with the polygons in the color_index mode? Am I right? Another question is: In the context setup, anywhere to specify the palette or is necessary to do so?
shangping
Actually I found that my colormap is not used at all after I changed it from RGBA to COLORINDEX. How do I add my colormap into effect? Thanks
shangping
A: 

You should setup a pixel format able to display indexed colors (see the documentation of PIXELFORMATDESCRIPTOR )

Substitute PFD_TYPE_RGBA with PFD_TYPE_COLOR_INDEX. The window setup shall follow the rendering operations.

To specify a custom palette, you have to define it at the window manager level (since you create the window using it). The functions are SelectPalette and SetPaletteEntries.

Luca
Hello Luca, thank you for the answer. But it seems that it uses the system palette. How can I use my own colormap as in the program? Thanks
shangping
Added the information to the answer. Check it.
Luca
It seems that windows does not support the customized palette. I checked with this: int res=GetDeviceCaps(hDC,RASTERCAPS)
shangping