Results 1 to 5 of 5

Thread: 2D scene to texture

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2008
    Posts
    4

    2D scene to texture

    Hi there,

    I am using openGL to basically display a 2D graphic at real time.

    In the background I want to display a grid and some other stuff (I will call it STATIC part).

    To avoid drawing all the time the DYNAMIC part (2D graphic) plus the static part (grid and other things), I want to use textures...(layer concept).

    Basically what I do is:



    // 1. init GL
    ...


    // 2. init texture
    GLuint texName;
    GLubyte checkImage[256][256][4];

    glGenTextures( 1, &texName );
    glBindTexture( GL_TEXTURE_2D, texName );

    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexImage2D( GL_TEXTURE_2D, 0, 4, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage );


    // 3. draw STATIC part
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDisable( GL_TEXTURE_2D);
    glLoadIdentity;

    // do grid drawing and some other stuff (STATIC drawing)
    ...

    glBindTexture(GL_TEXTURE_2D, texName);
    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 256, 256);

    SwapBuffers( dc->m_hDC );



    // 4. and when I want to update the 2D graphic (DYNAMIC part) I do:

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glEnable( GL_TEXTURE_2D );
    glBindTexture(GL_TEXTURE_2D, texName);

    // do dynamic stuff
    ...

    SwapBuffers( dc->m_hDC );



    The problem: This is not working!
    Am I doing something wrong?
    What I can see is just the window empty!

    If I do not perform step 4, I can see the "STATIC part", but when I try to use the generated texture...it looks like everyting is clear...

    Any help will be highly appreciated!
    Thanks in advance!

    MV.

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: 2D scene to texture

    // 4. and when I want to update the 2D graphic (DYNAMIC part) I do:
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glEnable( GL_TEXTURE_2D );
    glBindTexture(GL_TEXTURE_2D, texName);

    You forgot to actually draw a quad with the current texture
    glBindTexture only select the texture to use : to draw stuff you still have to create triangle/quads/etc.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2008
    Posts
    4

    Re: 2D scene to texture

    Hi ZbuffeR,

    First of all, thanks for your fast answer!
    I miss to type the code, but I actuallt draw the quad:

    // 4. and when I want to update the 2D graphic (DYNAMIC part) I do:
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glEnable( GL_TEXTURE_2D );
    glBindTexture(GL_TEXTURE_2D, texName);

    glBegin(GL_QUADS);
    glColor3f( ( GLfloat ) 0, ( GLfloat ) 0.0, ( GLfloat ) 1.0 );
    glTexCoord2d( 1, 0.0 );
    glVertex2d(20, 20 );

    glTexCoord2d( 0, 0 );
    glVertex2d(20, 800);

    glTexCoord2d( 0, 1 );
    glVertex2d(800, 800);

    glTexCoord2d( 1, 1 );
    glVertex2d(800, 20);
    glEnd();
    SwapBuffers( dc->m_hDC );


    However I just see an empty scene!! (the effect of glClear)
    Am i missing something?
    I am getting crazy!!!

    Thanks again in advance!
    MV

  4. #4
    Senior Member Frequent Contributor yooyo's Avatar
    Join Date
    Apr 2003
    Location
    Belgrade, Serbia
    Posts
    872

    Re: 2D scene to texture

    Polygon culling misuse? Add this at begin of your main render func
    1.
    glFrontFace(GL_CCW); // or try GL_CW
    glEnable(GL_CULL_FACE);


    or

    2.
    glDisable(GL_CULL_FACE);

    or

    3. Change winding of your quad and dont touch culling.
    20,20 -> 800,20 -> 800,800 -> 20,800


    Keep in mind if you change culling somwhere in your program return it to previous state.

  5. #5
    Junior Member Newbie
    Join Date
    Sep 2008
    Posts
    4

    Re: 2D scene to texture

    Still the same!!

    Let me show you exactly my initialization and my drawing code. May be I am doing something wrong in there...

    // 1. init GL

    static PIXELFORMATDESCRIPTOR pixelFormatDesc =
    {
    sizeof( PIXELFORMATDESCRIPTOR ),
    1,
    PFD_DRAW_TO_WINDOW |
    PFD_SUPPORT_OPENGL |
    PFD_DOUBLEBUFFER,
    PFD_TYPE_RGBA,
    resolution.dmBitsPerPel,
    0, 0, 0, 0, 0, 0,
    8,
    0,
    0,
    0, 0, 0, 0,
    24,
    stencil,
    0,
    0,
    0,
    0, 0, 0
    };

    ChoosePixelFormat (deviceContext, &pixelFormatDesc);
    SetPixelFormat(deviceContext, pixelFormat, &pixelFormatDesc);

    wglCreateContext(deviceContext);
    wglMakeCurrent(deviceContext, renderContext);


    glShadeModel( GL_SMOOTH );
    glColor3f( ( GLfloat ) 0.9, ( GLfloat ) 0.9, ( GLfloat ) 0.0 );
    glClearDepth( 1.0f );
    glDepthFunc( GL_LEQUAL );
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );


    glMatrixMode( GL_PROJECTION );
    glLoadIdentity( );
    gluOrtho2D( 0, 1000, 0, 1000 );
    glMatrixMode( GL_MODELVIEW );


    // 2. init texture
    ...


    // 3. draw STATIC part
    glViewport (0, 0, 256, 256);
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDisable( GL_TEXTURE_2D);
    glLoadIdentity;


    glBegin( GL_LINES );
    glColor3f( ( GLfloat ) 1, ( GLfloat ) 0.0, ( GLfloat ) 0.0 );
    glVertex2d( 0, 0 );
    glVertex2d( 1000, 1000 );
    glEnd( );

    glBindTexture(GL_TEXTURE_2D, texName);
    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 256, 256); // copy framebuffer to your texture

    SwapBuffers( dc->m_hDC );


    // 4. draw DYNAMIC part

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glEnable( GL_TEXTURE_2D );
    glBindTexture(GL_TEXTURE_2D, texName);

    glBegin(GL_QUADS);
    glColor3f( ( GLfloat ) 0, ( GLfloat ) 0.0, ( GLfloat ) 1.0 );

    glTexCoord2d( 0, 0.0 );
    glVertex2d(20, 20 );

    glTexCoord2d( 1, 0 );
    glVertex2d(800, 20);

    glTexCoord2d( 1, 1 );
    glVertex2d( 800, 800 );

    glTexCoord2d( 0, 1 );
    glVertex2d( 20, 800 );
    glEnd();

    SwapBuffers( dc->m_hDC );




    I should see a rectangle with a line inside....right???
    What i amb doing wrong?

    Thanks in advance!
    MV

Similar Threads

  1. Rendering voxelized scene into a 3D texture
    By imported_JoshKlint in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 05-21-2018, 12:42 AM
  2. how to render a scene to texture atlas
    By beginnerG in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-30-2018, 09:36 PM
  3. Need help rendering scene to a texture and reusing it in a shader
    By cardinal724 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-04-2013, 11:18 PM
  4. Render scene to texture: FBO and glCopyTexImage2D
    By BrickTamland in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 07-30-2011, 12:15 PM
  5. 2D scene to texture??
    By mvila in forum OpenGL: Windows
    Replies: 1
    Last Post: 09-18-2008, 03:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean