Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8981899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:26:59+00:00 2026-06-15T20:26:59+00:00

I’m attempting to render a .png image as a texture. However, all that is

  • 0

I’m attempting to render a .png image as a texture. However, all that is being rendered is a white square.

I give my texture a unique int ID called texID, read the pixeldata into a buffer ‘image’ (declared in the .h file). I load my pixelbuffer, do all of my OpenGL stuff and bind that pixelbuffer to a texture for OpenGL. I then draw it all using glDrawElements.

Also I initialize the texture with a size of 32×32 when its contructor is called, therefore i doubt it is related to a power of two size issue.

Can anybody see any mistakes in my OpenGL GL_TEXTURE_2D setup that might give me a block white square.

 #include "Texture.h"




Texture::Texture(int width, int height, string filename)
{

    const char* fnPtr = filename.c_str(); //our image loader accepts a ptr to a char, not a string
    printf(fnPtr);
    w = width; //give our texture a width and height, the reason that we need to pass in the width and height values manually
    h = height;//UPDATE, these MUST be P.O.T.

    unsigned error = lodepng::decode(image,w,h,fnPtr);//lodepng's decode function will load the pixel data into image vector
    //display any errors with the texture
    if(error)
    {
        cout << "\ndecoder error " << error << ": " << lodepng_error_text(error) <<endl;
    }

    for(int i = 0; i<image.size(); i++)
    {
        printf("%i,", image.at(i));

    }

    printf("\nImage size is %i", image.size());

    //image now contains our pixeldata. All ready for OpenGL to do its thing

    //let's get this texture up in the video memory
    texGLInit();
}

void Texture::texGLInit()
{
    //WHERE YOU LEFT OFF: glGenTextures isn't assigning an ID to textures. it stays at zero the whole time
    //i believe this is why it's been rendering white
    glGenTextures(1, &textures);
    printf("\ntexture = %u", textures);
    glBindTexture(GL_TEXTURE_2D, textures);//evrything we're about to do is about this texture
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    //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_MAG_FILTER, GL_NEAREST);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    //glDisable(GL_COLOR_MATERIAL);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,w,h,0, GL_RGBA, GL_UNSIGNED_BYTE, &image);
    //we COULD free the image vectors memory right about now.


}

void Texture::draw(point centerPoint, point dimensions)
{
    glEnable(GL_TEXTURE_2D);
    printf("\nDrawing block at (%f, %f)",centerPoint.x, centerPoint.y);
    glBindTexture(GL_TEXTURE_2D, textures);//bind the texture
    //create a quick vertex array for the primitive we're going to bind the texture to
    printf("TexID = %u",textures);
    GLfloat vArray[8] = 
    {
        centerPoint.x-(dimensions.x/2), centerPoint.y-(dimensions.y/2),//bottom left i0
        centerPoint.x-(dimensions.x/2), centerPoint.y+(dimensions.y/2),//top left i1
        centerPoint.x+(dimensions.x/2), centerPoint.y+(dimensions.y/2),//top right i2
        centerPoint.x+(dimensions.x/2), centerPoint.y-(dimensions.y/2)//bottom right i3
    };

    //create a quick texture array (we COULD create this on the heap rather than creating/destoying every cycle)
    GLfloat tArray[8] = 
    {
        0.0f,0.0f, //0
        0.0f,1.0f, //1
        1.0f,1.0f, //2
        1.0f,0.0f //3
    };

    //and finally.. the index array...remember, we draw in triangles....(and we'll go CW)
    GLubyte iArray[6] =
    {
        0,1,2,
        0,2,3
    };

    //Activate arrays
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    //Give openGL a pointer to our vArray and tArray
    glVertexPointer(2, GL_FLOAT, 0, &vArray[0]);
    glTexCoordPointer(2, GL_FLOAT, 0, &tArray[0]);

    //Draw it all
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, &iArray[0]);

    //glDrawArrays(GL_TRIANGLES,0,6);

    //Disable the vertex arrays
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisable(GL_TEXTURE_2D);
    //done!

    /*glBegin(GL_QUADS);
    glTexCoord2f(0.0f,0.0f);
        glVertex2f(centerPoint.x-(dimensions.x/2), centerPoint.y-(dimensions.y/2));
    glTexCoord2f(0.0f,1.0f);
        glVertex2f(centerPoint.x-(dimensions.x/2), centerPoint.y+(dimensions.y/2));
    glTexCoord2f(1.0f,1.0f);
        glVertex2f(centerPoint.x+(dimensions.x/2), centerPoint.y+(dimensions.y/2));
    glTexCoord2f(1.0f,0.0f);
        glVertex2f(centerPoint.x+(dimensions.x/2), centerPoint.y-(dimensions.y/2));
    glEnd();*/
}



Texture::Texture(void)
{
}
Texture::~Texture(void)
{
}

I’ll also include the main class’ init, where I do a bit more OGL setup before this.

void init(void)
{
    printf("\n......Hello Guy. \n....\nInitilising");
    glMatrixMode(GL_PROJECTION);    
    glLoadIdentity();
    gluOrtho2D(0,XSize,0,YSize);
    glEnable(GL_TEXTURE_2D);
    myBlock = new Block(0,0,offset);
    glClearColor(0,0.4,0.7,1);


    glLineWidth(2);         // Width of the drawing line
    glMatrixMode(GL_MODELVIEW); 
    glDisable(GL_DEPTH_TEST);
    printf("\nInitialisation Complete");

}

Update: adding in the main function where I first setup my OpenGL window.

int main(int argc, char** argv) 
{
  glutInit(&argc, argv);    // GLUT Initialization 
  glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE); // Initializing the Display mode
  glutInitWindowSize(800,600);  // Define the window size
  glutCreateWindow("Gem Miners");   // Create the window, with caption.
        printf("\n========== McLeanTech Systems =========\nBecoming Sentient\n...\n...\n....\nKILL\nHUMAN\nRACE \n");
  init();   // All OpenGL initialization


  //-- Callback functions ---------------------
  glutDisplayFunc(display);
  glutKeyboardFunc(mykey);
  glutSpecialFunc(processSpecialKeys);
  glutSpecialUpFunc(processSpecialUpKeys);
  //glutMouseFunc(mymouse);


  glutMainLoop();   // Loop waiting for event 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:27:00+00:00Added an answer on June 15, 2026 at 8:27 pm

    Here’s the usual checklist for whenever textures come out white:

    • OpenGL context created and being bound to current thread when attemting to load texture?

    • Allocated texture ID using glGenTextures?

    • Are the parameters format and internal format to glTex[Sub]Image… valid OpenGL tokens allowed as input for this function?

    • Is mipmapping being used?

      • YES: Supply all mipmap layers – optimally set glTexParameteri GL_TEXTURE_BASE_LEVEL and GL_TEXTURE_MAX_LEVEL, as well as GL_TEXTURE_MIN_LOD and GL_TEXTURE_MAX_LOG.

      • NO: Turn off mipmap filtering by setting glTexParameteri GL_TEXTURE_MIN_FILTER to GL_NEAREST or GL_LINEAR.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.