No 2D texture in a MFC APP

No 2D texture in a MFC APP

Post by Sébastien GUYO » Sat, 02 Dec 2000 04:00:00



Hi,

I've got a problem in a MFC application. I use some OpenGl code. I link
with glu32, glut32 and opengl32.
I work on VC++6.0.
I can't visualize a declared 2D texture. The same code part works in a
console application.

I declare my 2D texture like that (the alpha component is 0) :

 buf=(unsigned*)malloc(size*size*sizeof(unsigned));
  for(int j=0;j<size;j++)
   for(int i=0;i<size;i++)
   {
    ind=i+j*size;
    buf[ind]= ((DWORD)(((BYTE)(img1[ind][0]) |
       ((WORD)(img1[ind][1]) << 8)) |
       (((DWORD)(BYTE)(img1[ind][2])) << 16)));

   }
  glEnable(GL_TEXTURE_2D);
  glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  glBindTexture(GL_TEXTURE_2D,np);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,size,size,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);

I draw my polygon like that

   glEnable(GL_TEXTURE_2D);
   glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
   glBindTexture(GL_TEXTURE_2D,np);
   glBegin(GL_QUADS);
   p=pyra->p1();
   glVertex3d(p.getx(),p.gety(),p.getz());
   glTexCoord2f(1.0f,1.0f);
   p=pyra->p2();
   glVertex3d(p.getx(),p.gety(),p.getz());
   glTexCoord2f(1.0f,0.0f);
   p=pyra->p3();
   glVertex3d(p.getx(),p.gety(),p.getz());
   glTexCoord2f(0.0f,0.0f);
   p=pyra->p4();
   glVertex3d(p.getx(),p.gety(),p.getz());
   glTexCoord2f(0.0f,1.0f);
   glEnd();

But I just see a white filled polygon.
OpenGl acts as if the GL_TEXTURE_2D was not activated.

I show you my PIXELFORMATDESCRIPTOR

int CSepMatterView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if(CView::OnCreate(lpCreateStruct)==-1) return -1;
    CClientDC dc(this) ;
    PIXELFORMATDESCRIPTOR pfd ;
 memset(&pfd,0, sizeof(PIXELFORMATDESCRIPTOR)) ;
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1 ;          // Version number
 pfd.dwFlags =  PFD_DOUBLEBUFFER |      // Use double buffer
                PFD_SUPPORT_OPENGL |      // Use OpenGL
                PFD_DRAW_TO_WINDOW ;      // Pixel format is for a
window.
 pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;         // 24-bit color
 pfd.cDepthBits = 32;            // 32-bit depth buffer
    pfd.iLayerType = PFD_MAIN_PLANE;      // Layer type
    int nPixelFormat = ChoosePixelFormat(dc.m_hDC, &pfd);
 if (nPixelFormat == 0) return -1 ;
    BOOL bResult = SetPixelFormat(dc.m_hDC, nPixelFormat, &pfd);
 if (!bResult) return -1;
    m_hrc = wglCreateContext(dc.m_hDC);
 if(!m_hrc) return -1;
 //CreateRGBPalette(dc.m_hDC) ;
 return 0;

Quote:}

I don't need any palette because I work with a GeForce 256 DDR/DVI 32Mo.

What's the problem ?

Thanks for any help.

Sep.

 
 
 

No 2D texture in a MFC APP

Post by Sébastien GUYO » Sat, 02 Dec 2000 04:00:00


Mhhhh...

It doesn't work...

sincerly,
Sep.

Ryan Harm a crit :

Quote:> >   glEnable(GL_TEXTURE_2D);
> >   glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
> >   glBindTexture(GL_TEXTURE_2D,np);
> >   glBegin(GL_QUADS);
> >   p=pyra->p1();
> >   glVertex3d(p.getx(),p.gety(),p.getz());
> >   glTexCoord2f(1.0f,1.0f);

> Try reversing the order of your glVertex and glTexCoord commands.

> regards
> ryan


 
 
 

No 2D texture in a MFC APP

Post by Andrew F. Vespe » Sat, 02 Dec 2000 04:00:00



> I've got a problem in a MFC application. I use some OpenGl code. I link with
> glu32, glut32 and opengl32.
> I work on VC++6.0.
> I can't visualize a declared 2D texture. The same code part works in a console
> application.

> I declare my 2D texture like that (the alpha component is 0) :

>  buf=(unsigned*)malloc(size*size*sizeof(unsigned));

Err -- unsigned *what*?
(Yes, I know it is an unsigned int, but still....)

Quote:>   for(int j=0;j<size;j++)
>    for(int i=0;i<size;i++)
>    {
>     ind=i+j*size;
>     buf[ind]= ((DWORD)(((BYTE)(img1[ind][0]) |
>        ((WORD)(img1[ind][1]) << 8)) |
>        (((DWORD)(BYTE)(img1[ind][2])) << 16)));

>    }

You are writing to "unsigned int" but expecting OpenGL to read it as 4
bytes. Consider writing bytes instead of unsigned ints. It'll save you headaches

if/when you port to a big endian machine.

Quote:>   glEnable(GL_TEXTURE_2D);
>   glPixelStorei(GL_UNPACK_ALIGNMENT,1);
>   glBindTexture(GL_TEXTURE_2D,np);
>   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
>   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
>   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
>   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

> glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,size,size,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);

Err -- you are sure that "size" is a power of two?

Quote:> I draw my polygon like that

>    glEnable(GL_TEXTURE_2D);
>    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
>    glBindTexture(GL_TEXTURE_2D,np);
>    glBegin(GL_QUADS);
>    p=pyra->p1();
>    glVertex3d(p.getx(),p.gety(),p.getz());
>    glTexCoord2f(1.0f,1.0f);

TexCoords (and all other per-vertex data) go before Vertex.

Quote:> But I just see a white filled polygon.
> OpenGl acts as if the GL_TEXTURE_2D was not activated.

Do you have the same OpenGL implementation for the "console" application and the

MFC application? Print out glGetString (GL_VENDOR) etc. to be sure.

--
Andy V (OpenGL Alpha Geek)
"In order to make progress, one must leave the door to the unknown ajar."
Richard P. Feynman, quoted by Jagdish Mehra in _The Beat of a Different Drum_.

Paul Martz's OpenGL FAQ: http://www.opengl.org/About/FAQ/Technical.html

 
 
 

No 2D texture in a MFC APP

Post by Ruud van Ga » Sat, 02 Dec 2000 04:00:00


On Fri, 1 Dec 2000 17:40:26 GMT, Sbastien GUYON


>Mhhhh...

>It doesn't work...
...
>> >   glEnable(GL_TEXTURE_2D);
>> >   glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
>> >   glBindTexture(GL_TEXTURE_2D,np);
>> >   glBegin(GL_QUADS);
>> >   p=pyra->p1();
>> >   glVertex3d(p.getx(),p.gety(),p.getz());
>> >   glTexCoord2f(1.0f,1.0f);

You are binding a texture AFTER setting texture settings for whatever
texture was previously bound. I've had this problem before, when I
thought that binding would bound the current settings to a texture id.
However, try:

   glBindTexture(GL_TEXTURE_2D,np);
   glEnable(GL_TEXTURE_2D);
   glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
   glBegin(GL_QUADS);
   p=pyra->p1();
   glVertex3d(p.getx(),p.gety(),p.getz());
   glTexCoord2f(1.0f,1.0f);

Ruud van Gaal, GPL Rank +53.25
MarketGraph   : http://www.marketgraph.nl
Pencil art    : http://www.marketgraph.nl/gallery/
Car simulation: http://www.marketgraph.nl/gallery/racer/

 
 
 

No 2D texture in a MFC APP

Post by Ryan Har » Sun, 03 Dec 2000 01:16:57


Quote:>   glEnable(GL_TEXTURE_2D);
>   glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
>   glBindTexture(GL_TEXTURE_2D,np);
>   glBegin(GL_QUADS);
>   p=pyra->p1();
>   glVertex3d(p.getx(),p.gety(),p.getz());
>   glTexCoord2f(1.0f,1.0f);

Try reversing the order of your glVertex and glTexCoord commands.

regards
ryan

 
 
 

No 2D texture in a MFC APP

Post by V-ma » Sun, 03 Dec 2000 04:00:00


glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,size,size,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);

So is everyone using  GL_RGBA or the other ogl defines for parameter 3?

I would put 4 instead of GL_RGBA or 3 if I have a GL_RGB image.

Doh!

V-man

 
 
 

No 2D texture in a MFC APP

Post by Charles E Hardwidg » Sun, 03 Dec 2000 04:00:00


Quote:>glTexImage2D
>GL_TEXTURE_2D,0,GL_RGBA,size,size,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);
>So is everyone using  GL_RGBA or the other ogl defines for parameter 3?

Er, no. Parameter 3 is the size in bytes of the pixel. A valid value is
1,2,3, or 4. Taking a look at the gl header file shows; #define GL_RGB
0x1907 and, #define GL_RGBA 0x1908. You most certainly don't want that in
there do you?

Quote:>I would put 4 instead of GL_RGBA or 3 if I have a GL_RGB image.

You are correct.

--
Contact details on website
http://www.hardwidge.org.uk

 
 
 

No 2D texture in a MFC APP

Post by Andrew F. Vespe » Sun, 03 Dec 2000 04:00:00



> >glTexImage2D
> >GL_TEXTURE_2D,0,GL_RGBA,size,size,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);
> >So is everyone using  GL_RGBA or the other ogl defines for parameter 3?

> Er, no. Parameter 3 is the size in bytes of the pixel.

Err, no. Parameter 3 *was* the number of components in OpenGL 1.0. It *became*
internalFormat in 1.1, and the following correspondents were made:
    1    GL_LUMINANCE
    2    GL_LUMINANCE_ALPHA
    3    GL_RGB
    4    GL_RGBA

Quote:> A valid value is
> 1,2,3, or 4. Taking a look at the gl header file shows; #define GL_RGB
> 0x1907 and, #define GL_RGBA 0x1908. You most certainly don't want that in
> there do you?

--
Andy V (OpenGL Alpha Geek)
"In order to make progress, one must leave the door to the unknown ajar."
Richard P. Feynman, quoted by Jagdish Mehra in _The Beat of a Different Drum_.

Paul Martz's OpenGL FAQ: http://www.opengl.org/About/FAQ/Technical.html

 
 
 

No 2D texture in a MFC APP

Post by Charles E Hardwidg » Mon, 04 Dec 2000 10:49:23


Whoops. Yes you are correct Andy. Thankyou for refreshing my memory. As you
say, the internalFormat parameter of glTexImage2D is 1-4 or one of 38
constants. For compatibility with OpenGL 1.0, GL_LUMINANCE,
GL_LUMINANCE_ALPHA, GL_RGB, and GL_RGBA are mapped to the values 1, 2, 3,
and 4.

Touche' ;).

--
Contact details on website
http://www.hardwidge.org.uk

 
 
 

No 2D texture in a MFC APP

Post by Sébastien GUYO » Tue, 05 Dec 2000 04:00:00


You're right.

But it doesn't solve the problem.
I always have a white colored polygon instead of my picture...

sincerely,
Sep.

Ruud van Gaal a crit :

> On Fri, 1 Dec 2000 17:40:26 GMT, Sbastien GUYON

> >Mhhhh...

> >It doesn't work...
> ...
> >> >   glEnable(GL_TEXTURE_2D);
> >> >   glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
> >> >   glBindTexture(GL_TEXTURE_2D,np);
> >> >   glBegin(GL_QUADS);
> >> >   p=pyra->p1();
> >> >   glVertex3d(p.getx(),p.gety(),p.getz());
> >> >   glTexCoord2f(1.0f,1.0f);

> You are binding a texture AFTER setting texture settings for whatever
> texture was previously bound. I've had this problem before, when I
> thought that binding would bound the current settings to a texture id.
> However, try:

>    glBindTexture(GL_TEXTURE_2D,np);
>    glEnable(GL_TEXTURE_2D);
>    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
>    glBegin(GL_QUADS);
>    p=pyra->p1();
>    glVertex3d(p.getx(),p.gety(),p.getz());
>    glTexCoord2f(1.0f,1.0f);

> Ruud van Gaal, GPL Rank +53.25
> MarketGraph   : http://www.marketgraph.nl
> Pencil art    : http://www.marketgraph.nl/gallery/
> Car simulation: http://www.marketgraph.nl/gallery/racer/

 
 
 

No 2D texture in a MFC APP

Post by Sébastien GUYO » Tue, 05 Dec 2000 04:00:00


"Andrew F. Vesper" a crit :


> > I've got a problem in a MFC application. I use some OpenGl code. I link with
> > glu32, glut32 and opengl32.
> > I work on VC++6.0.
> > I can't visualize a declared 2D texture. The same code part works in a console
> > application.

> > I declare my 2D texture like that (the alpha component is 0) :

> >  buf=(unsigned*)malloc(size*size*sizeof(unsigned));

> Err -- unsigned *what*?
> (Yes, I know it is an unsigned int, but still....)

Well, you're right. But the modification haven't any consequence...

Quote:

> >   for(int j=0;j<size;j++)
> >    for(int i=0;i<size;i++)
> >    {
> >     ind=i+j*size;
> >     buf[ind]= ((DWORD)(((BYTE)(img1[ind][0]) |
> >        ((WORD)(img1[ind][1]) << 8)) |
> >        (((DWORD)(BYTE)(img1[ind][2])) << 16)));

> >    }

> You are writing to "unsigned int" but expecting OpenGL to read it as 4
> bytes. Consider writing bytes instead of unsigned ints. It'll save you headaches

> if/when you port to a big endian machine.

> >   glEnable(GL_TEXTURE_2D);
> >   glPixelStorei(GL_UNPACK_ALIGNMENT,1);
> >   glBindTexture(GL_TEXTURE_2D,np);
> >   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
> >   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
> >   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
> >   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

> > glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,size,size,0,GL_RGBA,GL_UNSIGNED_BYTE,buf);

> Err -- you are sure that "size" is a power of two?

Yes sir ! (512)

Quote:

> > I draw my polygon like that

> >    glEnable(GL_TEXTURE_2D);
> >    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
> >    glBindTexture(GL_TEXTURE_2D,np);
> >    glBegin(GL_QUADS);
> >    p=pyra->p1();
> >    glVertex3d(p.getx(),p.gety(),p.getz());
> >    glTexCoord2f(1.0f,1.0f);

> TexCoords (and all other per-vertex data) go before Vertex.

Ever seen, but inefficient...

- Show quoted text -

Quote:> > But I just see a white filled polygon.
> > OpenGl acts as if the GL_TEXTURE_2D was not activated.

> Do you have the same OpenGL implementation for the "console" application and the

> MFC application? Print out glGetString (GL_VENDOR) etc. to be sure.
> --
> Andy V (OpenGL Alpha Geek)
> "In order to make progress, one must leave the door to the unknown ajar."
> Richard P. Feynman, quoted by Jagdish Mehra in _The Beat of a Different Drum_.

> Paul Martz's OpenGL FAQ: http://www.opengl.org/About/FAQ/Technical.html

 
 
 

No 2D texture in a MFC APP

Post by Sébastien GUYO » Tue, 05 Dec 2000 04:00:00


I've found the answer : It was a WglContext initialisation problem.

Thanks a lot to Herv Berriot.

Thanks for your help.

Sep.

 
 
 

No 2D texture in a MFC APP

Post by Charles E Hardwidg » Wed, 06 Dec 2000 04:00:00


Quote:>> Err -- unsigned *what*?
>> (Yes, I know it is an unsigned int, but still....)
>Well, you're right. But the modification haven't any consequence...

Try maintainability and ease of understanding for other developers.
Personally, I regard the "unsigned" as against "unsigned int" to be a flaw
in the C specification.

--
Contact details on website
http://www.hardwidge.org.uk

 
 
 

No 2D texture in a MFC APP

Post by Andrew F. Vespe » Fri, 08 Dec 2000 04:00:00



> "Andrew F. Vesper" a crit :

> > TexCoords (and all other per-vertex data) go before Vertex.

> Ever seen, but inefficient...

It isn't a question of efficiency or not, but rather what data goes
with what vertex. The current texture coordinates (set by a
call to glTexCoord) are associated with a vertex when you call
glVertex. Any future call to glTexCoord will not affect that
vertex, but only (potential) future vertices.
--
Andy V (OpenGL Alpha Geek)
"In order to make progress, one must leave the door to the unknown ajar."
Richard P. Feynman, quoted by Jagdish Mehra in _The Beat of a Different Drum_.

Paul Martz's OpenGL FAQ: http://www.opengl.org/About/FAQ/Technical.html