OpenGL - Animate/cycle/rotate palette

佐手、 提交于 2019-12-25 08:29:51

问题


I was trying to create a OpenGL version of an old plasma 8bit effect animation in DOS but I am stuck. Since almost every OpenGL program has included something to generate a palette for Win32 I thought it would not be that hard to apply palette animation on my old program.

My purpose is to generate a texture with color indices that does not change and a palette that is rotating. After digging into the web this weekend I am still not able to fix it. I cannot even display a texture with one color index so in that stage something is wrong (if it would work I can create the palette cycling mechanism).

I can force into palette mode by using PFD_TYPE_COLORINDEX and draw some random pixels using glIndexi. I read that glDrawPixels and glReadPixels are slow and the latter is not that accurate when getting pixels back from the framebuffer (due to inaccurate positioning as a result from rounding errors or something).

I tried the GL_COLOR_INDEX keyword. I also tried: glPixelTransferi(GL_MAP_COLOR, true); glPixelMapfv( GL_PIXEL_MAP_I_TO_R, ...); glTexImage2D... Some of the code I tried so far (latest changes):

init part:

void* rawplasma;
GLuint  plastexture;

rawplasma = (void*) malloc(256*256);
 memset(rawplasma,rand()%256,256*156);
glEnable( GL_TEXTURE_2D );
glGenTextures(1, plastexture);
glBindTexture(GL_TEXTURE_2D, plastexture);
glTexImage2D( GL_TEXTURE_2D, 0, GL_COLOR_INDEX, 256, 256, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE,     rawplasma );

update/draw:

float Rmap[256];
float Gmap[256];
float Bmap[256];
float Amap[256];

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
memset(rawplasma,rand()%256,256*256); //check if it works
glTexImage2D( GL_TEXTURE_2D, 0, GL_COLOR_INDEX, 256, 256, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE,     rawplasma );
glBindTexture(GL_TEXTURE_2D, plastexture );
/*
glPixelTransferi( GL_MAP_COLOR, GL_TRUE );
    glPixelMapfv(GL_PIXEL_MAP_I_TO_R,mapSize,Rmap);
    glPixelMapfv(GL_PIXEL_MAP_I_TO_G,mapSize,Gmap);
    glPixelMapfv(GL_PIXEL_MAP_I_TO_B,mapSize,Bmap);
    glPixelMapfv(GL_PIXEL_MAP_I_TO_A,mapSize,Amap);
glPixelTransferi(GL_MAP_COLOR,GL_TRUE);
*/
glBegin(GL_QUADS);
    glTexCoord2f(1.0, 0.0);  glVertex2f(-1.0, -1.0);
    glTexCoord2f(0.0, 0.0);  glVertex2f( 1.0, -1.0);
    glTexCoord2f(0.0, 1.0);  glVertex2f( 1.0,  1.0);
    glTexCoord2f(1.0, 1.0);  glVertex2f(-1.0,  1.0);
glEnd();
glFlush();
SwapBuffers(hDC);

Or should I use glColorTableEXT in combination with GL_COLOR_INDEX8_EXT? I read somewhere that textured palettes are not supported? I found a link which mentions Paletted Texture Extension: http://www.cs.rit.edu/~ncs/Courses/570/UserGuide/OpenGLonWin-20.html

This is what I want (but then in OpenGL):

http://www.codeforge.com/read/174459/PalAnimDemo.h__html

I am not looking for ES/Shader implementations (I am just a beginner ;)) and DirectDraw might be easier I think but I want to try OpenGL.


回答1:


Since almost every OpenGL program has included something to generate a palette for Win32

No, definitely not. If you mean the PIXELFORMATDESCRIPTOR, that is not a palette definition. Color index mode was a major PITA to work with in OpenGL, and no current implementation actually support it.

I am not looking for ES/Shader implementations (I am just a beginner ;))

But that's exactly what you should use. Also shaders are mandatory in modern OpenGL. A simple fragment shader, that performs a lookup into a 1D texture turning an index into a color is exactly what you need. And if you aim for modern systems, you'll have to use a shader anyway.



来源:https://stackoverflow.com/questions/13221961/opengl-animate-cycle-rotate-palette

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!