PDA

View Full Version : 2D scene to texture



mvila
09-18-2008, 02:16 AM
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.

ZbuffeR
09-18-2008, 02:54 AM
// 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.

mvila
09-18-2008, 04:18 AM
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!!! :p

Thanks again in advance!
MV

yooyo
09-18-2008, 06:16 AM
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.

mvila
09-18-2008, 07:08 AM
Still the same!! :sorrow:

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