Jump to content
  • Advertisement
Sign in to follow this  
yamaki308

OpenGL need help with framebuffer concepts

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!