Jump to content
  • Advertisement
Sign in to follow this  
shinypixel

OpenGL Rendering Win32 font only works when image isn't loaded?

This topic is 1683 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 a little confused. On initialization, I have a texture object and a font object.

 

When I load both, only the texture shows.

When I comment out txtTree, only the font shows up.

 

I want both to show up.

 

 

    txtTree.init("art/tree.png", 50, 50);

    // Setup the Font characteristics
    HFONT hFont;
    LOGFONT logfont;
    logfont.lfHeight = -20;
    logfont.lfWidth = 0;
    logfont.lfEscapement = 0;
    logfont.lfOrientation = 0;
    logfont.lfWeight = FW_BOLD;
    logfont.lfItalic = FALSE;
    logfont.lfUnderline = FALSE;
    logfont.lfStrikeOut = FALSE;
    logfont.lfCharSet = ANSI_CHARSET;
    logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
    logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
    logfont.lfQuality = DEFAULT_QUALITY;
    logfont.lfPitchAndFamily = DEFAULT_PITCH;
    strcpy(logfont.lfFaceName,"Arial");
    // Create the font and display list
    hFont = CreateFontIndirect(&logfont);
    SelectObject (win_getDC(), hFont);
    //Create display lists for glyphs 0 through 128
    nFontList = glGenLists(128);
    wglUseFontBitmaps(win_getDC(), 0, 128, nFontList);
    DeleteObject(hFont); 

 

I've narrowed it down by finding it's due to the texture's init function.

 

bool Texture::init(const char *strFileName, const GLfloat x, const GLfloat y)
{
    GLuint imageSize;
    lodepng::State state;

    // set position
    m_x = x;
    m_y = y;

    // Get the next texture
    m_intTextureID = intTextureRefCount;
    intTextureRefCount++;
    glBindTexture(GL_TEXTURE_2D, m_intTextureID);

    // Load in the file
    lodepng::load_file(m_rawImage, strFileName);
    lodepng::decode(m_image, m_width, m_height, state, m_rawImage);

    // Get texture states
    imageSize = m_image.size();
    m_colorDepth = state.info_raw.bitdepth;
    m_format = state.info_raw.colortype;
    m_type = GL_UNSIGNED_BYTE;
    m_texture = new (std::nothrow) GLubyte[imageSize];
    if (m_texture == NULL)
        return false;
    
    // Flip and invert the PNG image since OpenGL likes to load everything
    // backwards from what is considered normal!
    unsigned char *imagePtr = &m_image[0];
    int halfTheHeightInPixels = m_height / 2;
    int heightInPixels = m_height;
    
    // Assuming RGBA for 4 components per pixel.
    int numColorComponents = 4;

    // Assuming each color component is an unsigned char.
    int widthInChars = m_width * numColorComponents;
    unsigned char *top = NULL;

    unsigned char *bottom = NULL;
    unsigned char temp = 0;
    for( int h = 0; h < halfTheHeightInPixels; ++h )
    {
        top = imagePtr + h * widthInChars;
        bottom = imagePtr + (heightInPixels - h - 1) * widthInChars;
        for( int w = 0; w < widthInChars; ++w )
        {
            // Swap the chars around.
            temp = *top;
            *top = *bottom;
            *bottom = temp;
            ++top;
            ++bottom;
        }
    }
    
    unsigned int c=0;
    for (unsigned int x = 0; x < m_width; x++)
    for (unsigned int y=0; y < m_height; y++)
        for (unsigned int co=0; co < numColorComponents; co++)
            ((GLubyte*)m_texture)[c++] = m_image[c];
      

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width,
        m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &m_image[0]);
        
    // delete texture memory
    if (m_texture)
    {
        delete[] m_texture;
        m_texture = NULL;
    }

    // set parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);  

    glEnable(GL_TEXTURE_2D);
    

    return true;
}

 

If I remove glEnable(GL_TEXTURE_2D), I get a white texture square, but also the text shows up.

Share this post


Link to post
Share on other sites
Advertisement

I found a solution.

 

Once I'm done rendering the current binded texture, I tried 'unbinding' it with glBindTexture(GL_TEXTURE_2D, -1);. That did the trick. Both texture and text are rendering now. I don't know if that's the standard way of unbinding, but it worked for now.

Share this post


Link to post
Share on other sites

You don’t unbind with glBindTexture(GL_TEXTURE_2D, -1), you unbind with glBindTexture(GL_TEXTURE_2D, 0). Texture ID -1 doesn’t exist and you are generating internal errors.
The fact that you did not catch this means you are also not calling glGetError() after every OpenGL call, which is also erroneous practice.


L. Spiro

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!