PDA

View Full Version : Can't load .png image using opengl texture



openglfirst
05-01-2012, 11:37 PM
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>

#define KEY_ESCAPE 27

void display();
void keyboard(unsigned char,int,int);
GLuint LoadTextureRAW( const char * filename, int wrap );

int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPT H );
glutInitWindowSize(600,400);
glutCreateWindow("Opengl Test");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
void display() {
GLuint texture=LoadTextureRAW("ball.png",1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texture);
glBegin( GL_QUADS );
glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
glTexCoord2d(1.0,0.0); glVertex2d(1.0,0.0);
glTexCoord2d(1.0,1.0); glVertex2d(1.0,1.0);
glTexCoord2d(0.0,1.0); glVertex2d(0.0,1.0);
glEnd();
glutSwapBuffers();
}
// load a 256x256 RGB .RAW file as a texture
GLuint LoadTextureRAW( const char * filename, int wrap )
{
GLuint texture;
int width, height;
// BYTE * data;
int *data;
FILE * file;

// open texture data
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;

// allocate buffer
width = 256;
height = 256;
data = (int*)malloc( width * height * 3 );

// read texture data
fread( data, width * height * 3, 1, file );
fclose( file );

// allocate a texture name
glGenTextures( 1, &texture );

// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );

// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
wrap ? GL_REPEAT : GL_CLAMP );

// build our texture mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
GL_RGB, GL_UNSIGNED_BYTE, data );

// free buffer
free( data );

return texture;
}
void keyboard(unsigned char key, int mousePositionX, int mousePositionY) {
switch ( key ) {
case KEY_ESCAPE:
exit ( 0 );
break;
default:
break;
}
}

I followed this, http://www.nullterminator.net/gltexture.html

What should i do?

BionicBytes
05-02-2012, 01:50 AM
Rather than just posting code and saying "What's wrong with this?" - try and explain clearly what you are trying to achive, what steps you have taken (providing relevant code where appropriate) and then explain what you are observing.

Mearly asking "What should i do? " is not helping us help you - how do we know what's wrong unless you tell us?

openglfirst
05-02-2012, 01:59 AM
It's not loading that .png file, some say i need libpng to make the png to raw file. I can't understand how to do it.

BionicBytes
05-02-2012, 02:36 AM
Avoid PNG files? Use an art package and convert to something you can load.
I made a choice many years ago to not rely upon someone elses work (library functions ).

openglfirst
05-02-2012, 04:01 AM
What is an art package? Why don't you want to rely upon someone elses work? What do you do then?

BionicBytes
05-02-2012, 06:11 AM
An art package like PhotoShop or Paintshop Pro.

Why don't you want to rely upon someone elses work?
Because quite often these pojects end up as abandonware - either not updated or not updated quickly enough. I did not want my projects to rely upon a certain feature set and then find out that that feature is no longer available. I'd rather write my own.

openglfirst
05-02-2012, 04:29 PM
That code you wrote, is that available in internet?

BionicBytes
05-03-2012, 04:28 AM
No. It would not help you either as it's in Delphi.

Kopelrativ
05-03-2012, 06:04 AM
I would also recommend Gimp. Freely available on Internet, can convert to most file types, and can do fairly advanced image manipulations.

openglfirst
05-03-2012, 06:46 AM
No. It would not help you either as it's in Delphi.
If it's in Delphi, then how do you use that with opengl-C/C++ ?

BionicBytes
05-03-2012, 07:46 AM
I translate the headers from C into Delphi; a long and tedious task. Delphi, like other langauages, is able to pass parameters to functions calls in a variety of ways to make them compatible with other languages.

Rosario Leonardi
05-04-2012, 05:23 PM
Png is a compressed format, is like try to zip a text file and then try to open it with notepad.
If you want to use any format around (jpeg, png, gif, etc...) you need an algorithm to decompress it or otherwise the only compressed format that you can use with hardware acceleration is DDS.
You can find a lot of library to load picture with relative documentation on the web. Google is the way.
By the way, why are you trying to load the texture EVERY FRAME?
And to finish, openGL is not a C library or and header, openGL is an interface. Interface that you can implement in any language, there is an openGL implementation for every language that I know.