Jump to content

  • Log In with Google      Sign In   
  • Create Account


need help with framebuffer concepts


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 yamaki308   Members   -  Reputation: 122

Like
0Likes
Like

Posted 17 September 2008 - 09:16 PM

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.

Sponsor:

#2 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 18 September 2008 - 02:48 AM

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

#3 yamaki308   Members   -  Reputation: 122

Like
0Likes
Like

Posted 18 September 2008 - 10:06 AM

SDL_SetVideoMode succeeds but the glDrawPixels doesn't. My GPU supports 8-bit, 16-bit, and 32-bit color depths.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS