Skip to main content
GameDev.net gamedev.net
🔒 Locked

need help with framebuffer concepts

Started by yamaki308 Sep 18, 2008 at 3:16 AM 1 replies 1k views
Original Post
yamaki308
yamaki308
I'm using SDL w/ OpenGL in C with Netbeans 6.0.1. I'm trying to fathom completely about the framebuffer such as color depth and palette size. I want to use a single 8-bit color byte for the 640 x 480 resolution as output. The 8-bit color depth is 2^8 = 256 colors, which means that it needs 3 bits for both red and green and 2 remaining bits for blue. These colors can fit in 1-byte. Here is a code snippet on how I setup the attributes in SDL/OpenGL

    // setup OpenGL attributes for SDL
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 3);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 3);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 2);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    // setup the video mode for OpenGL
    screen = SDL_SetVideoMode(SCR_WIDTH, SCR_HEIGHT, 8, SDL_OPENGL);

Here are some code snippets on how I setup my framebuffer

unsigned char *frameBuffer = 0;
int scr_w = 0;
int scr_h = 0;

void fbClear(const unsigned char _r,
	     const unsigned char _g,
             const unsigned char _b)
{
    int x, y;
    assert(frameBuffer);
    
    unsigned char ucColor = 0;
    ucColor = _r << 5;
    ucColor = _g | ucColor;
    ucColor = (_b >> 6) | ucColor;
    
    for(y = 0; y < scr_h; y++)
    {
	for(x = 0; x < scr_w; x++)
	    frameBuffer[x + y * scr_w] = ucColor;
    }
}

void fbInit(const int _w, const int _h)
{
    scr_w = _w;
    scr_h = _h;
    
    /*
       Since the 8-bit color depth is 1 byte, it needs 640 x 480 x 1 = 307200
       bytes(300 KiB) to accommodate 
       plus 256 x 3 = 768 additional bytes store in the
       palette map (assuming 24-bits) but I don't know if I'm 
       supposed to allocate this part
       as well.
     */
    frameBuffer = (unsigned char *)malloc(scr_w * scr_h * 
		    sizeof(unsigned char));
    assert(frameBuffer);
    
    memset(frameBuffer, 0x00, scr_w * scr_h * sizeof(unsigned char));
}

void fbFree(){ free(frameBuffer); }

void setPixel(const int x, const int y, const unsigned char r, 
	const unsigned char g, const unsigned char b)
{
    unsigned char ucColor = 0;
    
    assert(frameBuffer);
    
    if((x < 0 && x > scr_w) && (y < 0 && y > scr_h)) return;
    

    ucColor = r << 5;
    ucColor = g | ucColor;
    ucColor = (b >> 6) | ucColor;
    
    frameBuffer[x + y * scr_w] = ucColor;

}

void render()
{ 
   fbClear(255, 255, 255);
   
   glDrawPixels(SCR_WIDTH, SCR_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, frameBuffer);
   SDL_GL_SwapBuffers();
}

The problem is that the OpenGL function, glDrawPixels, doesn't work. Is there anyone that can help me understand how this works.
V-man
V-man
Does SDL_SetVideoMode succeed?
Because 8 bit framebuffer is not supported by any GPU. It would either have to be 16 bit or 32 bit.

You are calling
frameBuffer = (unsigned char *)malloc(scr_w * scr_h *
sizeof(unsigned char));

and that should be

frameBuffer = (unsigned char *)malloc(scr_w * scr_h *
sizeof(unsigned char)*3);

since you are using GL_RGB in glDrawPixels.

You are not calling glCear

http://www.opengl.org/wiki/index.php/Common_Mistakes#Swap_Buffers
http://www.opengl.org/wiki/index.php/Common_Mistakes#glDrawPixels
http://www.opengl.org/wiki/index.php/Common_Mistakes#Unsupported_formats_.233
yamaki308
yamaki308
SDL_SetVideoMode succeeds but the glDrawPixels doesn't. My GPU supports 8-bit, 16-bit, and 32-bit color depths.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.