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 8831039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:13:33+00:00 2026-06-14T08:13:33+00:00

I cannot find any decent tutorials on how to load a PNG file and

  • 0

I cannot find any decent tutorials on how to load a PNG file and use it as a texture to bind it to a sphere. Are there any library functions to do this?
How would i go about binding it to a sphere?
I’ve had a go with this and it hasnt worked, there are no errors but the texture is not loaded onto the sphere.
I call the LoadTexture with the particular file, before glutMainLoop()

Here is my code for the loading of a file:

GLuint LoadTexture( const char * filename, int width, int height )
    {
GLuint texture;
unsigned char * data;
FILE * file;

//The following code will read in our PNG file
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );

glGenTextures( 1, &texture ); //generate the texture with 

glBindTexture( GL_TEXTURE_2D, texture ); //bind the texture

glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, 
GL_MODULATE ); //set texture environment parameters



//even better quality, but this will do for now.
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
 GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
 GL_LINEAR );

//Here we are setting the parameter to repeat the texture 
//to the edge of our shape. 
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, 
 GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 
 GL_REPEAT );

//Generate the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
 GL_RGB, GL_UNSIGNED_BYTE, data);
free( data ); //free the texture
return texture; //return whether it was successfull

}

and here is where i create the sphere

void renderScene(void) {

// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 glEnable( GL_TEXTURE_2D );
// Reset transformations
glLoadIdentity();
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glPushMatrix();
glTranslatef(0.0,2.0,-6);
glRotatef(angle, 0.0f, 2.0, -6.0f);
glutSolidSphere(1,50,50);
glPopMatrix();


angle+=0.4f;
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
 }

Have i done any of it right?

  • 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-14T08:13:34+00:00Added an answer on June 14, 2026 at 8:13 am

    PNGs are compressed files, you can’t just read them in and expect OpenGL to know how to decode them. The recommended way to load a PNG is with libpng.

    Here’s an example of using libpng which demonstrates synchronously reading in a PNG file into a 2D array. OpenGL expects a flat 1D array, so you’ll need to flatten it yourself, but that’s pretty straightforward.

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

Sidebar

Related Questions

I'm missing something, but I cannot find any hint online. When I use Javascript's
Can someone please explain how Read/Show works.. I cannot find any tutorials on it.
I cannot find any examples of how to lazy load the Infragistics UltraWebTree v6.3
I see there are many tutorials about nhibernate but I cannot find a descent
I cannot find any example on how to use the pattern= parameter in list.files
I cannot find any detailed apple documentation on how the NSZombie really functions. I
What is routes.IgnoreRoute({resource}.axd/{*pathInfo}) I cannot find any .axd file in my project, can I
I cannot find any documentation on the encoding of file names in zip files.
I cannot find any good documentation on how to use a range in conjunction
Cannot find any example on Google.

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.