M
M
Makaleks2013-11-15 20:56:16
OpenGL
Makaleks, 2013-11-15 20:56:16

OpenGL color indexes or implementation of glDrawPixels with GL_BITMAP parameter. How?

Such a thing
Пишу простенький монохромный графический редактор, который сохранял бы мой чёрно-белый результат в текстовом файле в виде Bitmap'a, который я бы потом использовал при создании растровых шрифтов того же openGL. Просто интересно.
snapshot.png
( screenshot with glDrawPixels( 16, 16,GL_COLOR_INDEX, GL_BITMAP, map ) and glPixelZoom( 20, 20 ) )
The monochrome RGB interface is somehow unnecessary. So, it would be great to change the (screen) output, just by editing the array of the bitmap Bitmap (remember that everything is reduced to an area like 32x32 or 64x64 (of course, not life-size)) so as not to draw 32x32 squares. I wanted to use...
glBitmap( ... );
... which I am more or less familiar with, but allegedly it cannot be scaled (not to make an array for all window pixels)! But with glPixelZoom ( ... ) you can scale...
glDrawPixels( 16, 16,GL_COLOR_INDEX, GL_BITMAP, map );

As far as I understand, he does not need RGB, but color indices.
Not only are index colors specified differently ( glIndex( TYPE c ) in which I don't understand how to convey the color with a single number c ...). But, supposedly, the color display mode must be set even before the window is initialized ...
There is little information on index colors in the internet (I searched as best I could). The literature is limited to arguments about the differences between RGB and color indices and the existence of the glIndex( ... ) function.
So, the questions are:
1) How to implement glDrawPixels with the GL_BITMAP parameter?
1.1) How to work with color indices (preferably with examples)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Makaleks, 2015-02-06
@Makaleks

Thanks for the answers)
If anyone needs:
Setting the color index is a platform-dependent operation. GLUT
users have

glutSetColor(int cell,
GLfloat red, GLfloat green, GLfloat blue);

Another way is through GL_MAP_COLOR
Example:
unsigned char bitmap[] = { 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0xFF, 0xFF, 0x00,
  0x03, 0xFF, 0xFF, 0xC0,
  0x07, 0xFF, 0xFF, 0xE0,
  0x0F, 0x80, 0x01, 0xF0,
  0x1E, 0x00, 0x00, 0xF8,
  0x1C, 0x00, 0x00, 0x38,
  0x3C, 0x7E, 0x7E, 0x3C,
  0x38, 0x0E, 0x70, 0x1C,
  0x39, 0xF0, 0x0F, 0x9C,
  0x38, 0x00, 0x00, 0x1C,
  0x38, 0x00, 0x00, 0x1C,
  0x38, 0x38, 0x1C, 0x1C,
  0x38, 0xFE, 0x7F, 0x1C,
  0x38, 0xC6, 0x63, 0x1C,
  0x38, 0xD6, 0x6B, 0x1C,
  0x38, 0xD6, 0x6B, 0x1C,
  0x38, 0xC6, 0x63, 0x1C,
  0x38, 0xFE, 0x7F, 0x1C,
  0x38, 0x7C, 0x3E, 0x1C,
  0x3C, 0x00, 0x00, 0x3C,
  0x3C, 0x00, 0x00, 0x3C,
  0x7E, 0x00, 0x00, 0x7E,
  0x7F, 0x80, 0x01, 0xFE,
  0x77, 0xFF, 0xFF, 0xEE,
  0x63, 0xFF, 0xFF, 0xC7,
  0xE7, 0xFF, 0xFF, 0xE7,
  0xFF, 0xF0, 0x0F, 0xFF,
  0xFF, 0x00, 0x01, 0xFF,
  0xF0, 0x00, 0x00, 0x0F
};
void draw(){
  glClearColor(1, 1, 0, 1);//жёлтым
  glClear(GL_COLOR_BUFFER_BIT);//закрасить
  glPixelTransferi(GL_MAP_COLOR, 1);//использоваь карту
  float map[2] = { 1, 0 };//пусть...
    //...все единицы закрашивают в чёрный, а все нули - в белый цвета...
    glPixelMapfv(GL_PIXEL_MAP_I_TO_R, 2, map);
    glPixelMapfv(GL_PIXEL_MAP_I_TO_G, 2, map);
    glPixelMapfv(GL_PIXEL_MAP_I_TO_B, 2, map);
  //...и все нули будут прозрачными
  map[0] = 0, map[1] = 1;
  glPixelMapfv(GL_PIXEL_MAP_I_TO_A, 2, map);
  //Итого: для единиц действует цвет glColor4f(0,0,0,1), а для нулей будет glColor4f(1,1,1,0)

  glRasterPos2i(-160, -160);//позиция от центра экрана в пикселях ( не забыть вызвать glOrtho(...) )
  glPixelZoom(10, 10);//теперь работает

/*этот блок*/		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
/*лучше поставить*/	glEnable(GL_BLEND);					//прозрачность
/*в начале программы*/	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	//определяется компонентом ALPHA
  glDrawPixels(32, 32, GL_COLOR_INDEX, GL_BITMAP, bitmap);//рисовать 32x32 bitmap, где кадый бит определяет цветовой индекс "0" или "1"

  glPixelTransferi(GL_MAP_COLOR, 0);//не использовать карту
}

2b74cc08dc274662aebbbd40563885b8.PNG

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question