Results 1 to 8 of 8

Thread: How can i load image?

  1. #1
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    11

    How can i load image?

    I take this example in NeHe tutorial.But I can not load and write this image:
    Code :
    int LoadGLTextures()									// Load Bitmaps And Convert To Textures
    {
    	int Status=FALSE;									// Status Indicator
     
    	AUX_RGBImageRec *TextureImage[1];					// Create Storage Space For The Texture
     
    	memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL
     
    	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    	if (TextureImage[0]=LoadBMP("C:\\NeHe.bmp"))
    	{
    		Status=TRUE;									// Set The Status To TRUE
     
    		glGenTextures( 1,&texture[0]);					// Create The Texture
     
    		// Typical Texture Generation Using Data From The Bitmap
    		glBindTexture(GL_TEXTURE_2D, texture[0]);
    		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    	}
     
    	if (TextureImage[0])									// If Texture Exists
    	{
    		if (TextureImage[0]->data)							// If Texture Image Exists
    		{
    ///////////////////////////////////////////////////////////	////////////////////////////////	
       	free(TextureImage[0]->data);                            // Free The Texture Image Memory 
    		}
     
    		free(TextureImage[0]);								// Free The Image Structure
    	}
     
    	return Status;										// Return The Status
    }
    Thanks for all ur help.My OS is Windows and my ide Dev-C++

  2. #2
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    11

    Re: How can i load image?

    Can i do it with fread and fwrite binary read and write?

  3. #3
    V-man
    Guest

    Re: How can i load image?

    AUX is old. Don't use it.

    If you don't know how to read a bmp file, then use a library
    http://www.opengl.org/wiki/Related_t...ture_Libraries

  4. #4
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    11

    Re: How can i load image?

    Thank u for ur exegesis.I elected to use SOIL.And i write these code:
    Code :
    #include <GL/glut.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <SOIL.h>
     
    GLfloat angle = 0.0;       // Current turn angle.
    GLfloat angle_step = 0.1;  // How much the angle changes between frames.
    GLfloat angle_step2 = 0.1;
    GLfloat change = 0.1;      // How much angle_step changes when arrows pressed.
    GLuint texture[1];
     
    GLfloat vertices[][3] =
      {  {1.0, 1.0, 0.0},
         {1.5, 1.0, 0.0},
         {1.5, -1.0, 0.0},
         {1.0, -1.0, 0.0},
         {1.5, 0.5, 0.0},
         {-1.5, 0.5, 0.0},
         {-1.5, 1.0, 0.0},
         {-1.0, 1.0, 0.0},
         {-1.0, -1.0, 0.0},
         {-1.5, -1.0, 0.0},
         {-1.5, -0.5, 0.0},
         {1.5, -0.5, 0.0},
         {-1.5, 1.0, -0.25},
         {1.5, 1.0, -0.25},
         {-0.75, 0.25, -2.0},
         {0.75, 0.25, -2.0},
         {-0.75, -1.0, -2.0},
         {0.75, -1.0, -2.0},
         {1.5, 1.0, -0.25},
         {1.5, -1.0, -0.25},
         {-1.5, -1.0, -0.25},
         {-1.5, 1.0, -0.25},
         {-1.0, 0.5, -0.2},
         {-1.0, -0.5, -0.2},
         {1.0, -0.5, -0.2},
         {1.0, 0.5, -0.2},
         {-1.25, -1.0, 0.5},
         {1.25, -1.0, 0.5},
         {1.25, -1.0, 1.5},
         {-1.25, -1.0, 1.5}
     
         };
     
    GLfloat colors[][3] =
      {  {1.0, 1.0, 1.0},
         {0.0, 0.0, 0.0},
      };
     
      FILE *File=NULL;                           // File Handle
     
     
     
     
     
    int LoadGLTextures()                           // Load Bitmaps And Convert To Textures
    {
      int Status=FALSE;                           // Status Indicator
     
                   // Create Storage Space For The Texture
     
                    // Set The Pointer To NULL
    texture[1] = SOIL_load_OGL_texture
    	(
    		"C:\\NeHe.png",
    		SOIL_LOAD_AUTO,
    		SOIL_CREATE_NEW_ID,
    		SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
    	);
     
    	if( 0 == texture[1] )
    {
    	printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
    }
     
      // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
      if ( texture[1] = SOIL_load_OGL_texture
    	(
    		"C:\\NeHe.png",
    		SOIL_LOAD_AUTO,
    		SOIL_CREATE_NEW_ID,
    		SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
    	))
      {
         Status=TRUE;                           // Set The Status To TRUE
     
         glGenTextures(1, &amp;texture[0]);               // Create The Texture
     
         // Typical Texture Generation Using Data From The Bitmap
         glBindTexture(GL_TEXTURE_2D, texture[0]);
     
         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
      }
     
     
     
                               // Return The Status
     
     
     
    void init(void)
    {
      glClearColor(0.0, 0.0, 0.5, 1.0);
     
      /*if (!LoadGLTextures())                        // Jump To Texture Loading Routine ( NEW )
      {
         return FALSE;                           // If Texture Didn't Load Return FALSE
      }
     
      glEnable(GL_TEXTURE_2D);*/
    }
     
    void face4(int v0, int v1, int v2, int v3, int c)
      /* Render face with 4 vertices v1,v2,v3,v4 and colour c. */
    {
      glColor3fv(colors[c]);
      glBegin(GL_POLYGON);
         glVertex3fv(vertices[v0]);
         glVertex3fv(vertices[v1]);
         glVertex3fv(vertices[v2]);
         glVertex3fv(vertices[v3]);
      glEnd();
    }
     
    void cable(int x1, int y1, int z1, int x2, int y2, int z2)
    {
    glColor3f(0.0, 0.0, 0.0);
    glBegin(GL_LINES);
    glVertex3f((x1),(y1),(z1));
    glVertex3f((x2),(y2),(z2));
    glEnd();
    }
     
     
     
    void comp(int v0, int v1, int v2)
    /*render a computer at location v0,v1,v2 */
    { glTranslatef(v0, v1, v2);
     
      face4(0,1,2,3,0);
      face4(1,4,5,6,0);
      face4(6,7,8,9,0);
      face4(9,10,11,2,0);
      face4(6,12,13,1,0);
      face4(13,12,14,15,0);
      face4(15,14,16,17,0);
      face4(1,18,19,2,0);
      face4(6,9,20,21,0);
      face4(21,14,16,20,0);
      face4(18,15,17,19,0);
      face4(20,19,17,16,0);
      face4(22,23,24,25,1);
      face4(26,27,28,29,0);
     
       }
     
     
    void network(int x, int y, int z)
    {
        glTranslatef(x, y , z);
     
        glPushMatrix();
      glScalef(0.25, 0.25, 0.25);
     
      glPushMatrix();
      glTranslatef(-7.0, 0.0, -7.0);
      glRotatef(45.0, 0.0, 1.0, 0.0);
      comp(0,0,0);
      glPopMatrix();
     
      glPushMatrix();
      glTranslatef(-7.0, 0.0, 7.0);
      glRotatef(135.0, 0.0, 1.0, 0.0);
      comp(0,0,0);
      glPopMatrix();
     
     glPushMatrix();
      glTranslatef(7.0, 0.0, 7.0);
      glRotatef(-135.0, 0.0, 1.0, 0.0);
      comp(0,0,0);
      glPopMatrix();
     
      glPushMatrix();
      glTranslatef(7.0, 0.0, -7.0);
      glRotatef(-45.0, 0.0, 1.0, 0.0);
      comp(0,0,0);
      glPopMatrix();
     
      glPushMatrix();
      glLineWidth(5.0);
      cable(8,-0.1,-8,8,-0.1,8);
      cable(-8,-0.1,-8,-8,-0.1,8);
      cable(8,-0.1,-8,-8,-0.1,-8);
      cable(8,-0.1,8,-8,-0.1,8);
      glPopMatrix();
     
      glPopMatrix();
     
    }void lan(void)
    {  glPushMatrix();
     
      glTranslatef(-10.0, 0.0, 0.0);
      glRotatef(angle, 0.0, 1.0, 0.0);
      glScalef(1.5, 1.5, 1.5);
      network(0,0,0);
      glPopMatrix();
    }
     
    void wan(void)
    {
      glTranslatef(-12.5, 0.0, 0.0);
      glPushMatrix();
      glScalef(0.5, 0.5, 0.5);
     
      glPushMatrix();
      network(10,0,-5);
      glPopMatrix();
     
      glPushMatrix();
      network(10,0,-10);
      glPopMatrix();
     
      glPushMatrix();
      network(15,0,-5);
      glPopMatrix();
     
      glPushMatrix();
      network(15,0,-10);
      glPopMatrix();
     
      glPushMatrix();
      network(15,0,10);
      glPopMatrix();
     
      glPushMatrix();
      network(15,0,15);
      glPopMatrix();
     
      glPushMatrix();
      network(10,0,10);
      glPopMatrix();
     
      glPushMatrix();
      network(10,0,15);
      glPopMatrix();
     
      glPushMatrix();
      network(30,0,10);
      glPopMatrix();
     
      glPushMatrix();
      network(30,0,15);
      glPopMatrix();
     
      glPushMatrix();
      network(35,0,10);
      glPopMatrix();
     
      glPushMatrix();
      network(35,0,15);
      glPopMatrix();
     
       glPushMatrix();
      network(30,0,-5);
      glPopMatrix();
     
      glPushMatrix();
      network(30,0,-10);
      glPopMatrix();
     
      glPushMatrix();
      network(35,0,-5);
      glPopMatrix();
     
      glPushMatrix();
      network(35,0,-10);
      glPopMatrix();
     
      glPushMatrix();
      glLineWidth(5.0);
      cable(10,-0.1,-3,10,-0.1,8);
      cable(17,-0.1,-10,28,-0.1,-10);
      cable(17,-0.1,15,28,-0.1,15);
      glPopMatrix();
     
      glPopMatrix();
    }
    void display(void)
    {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(100.0, 1.4, 5.0, 50.0);
     
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
     
      glPushMatrix();
      glTranslatef(0.0, 0.0, -5.0);
    //Need to combine bmp texture to this polygon!
      /*glBindTexture(GL_TEXTURE_2D, texture[0]);*/
      glBegin(GL_POLYGON);
         glVertex2f(8.0, -3.0);
         glVertex2f(-8.0, -3.0);
         glVertex2f(-8.0, -6.0);
         glVertex2f(8.0, -6.0);
      glEnd();
      glPopMatrix();
     
      gluLookAt(0.0, 5.0, 15.0, 3.0, 0.0, 0.0, 0.0, 1.0, 0.0);
      /*glRotatef(angle, 0.0, 1.0, 0.0);*/
     
      glPushMatrix();
      lan();
      glPopMatrix();
     
      glPushMatrix();
      glTranslatef(12.5, 0.0, 0.0);
      glRotatef(angle, 0.0, 1.0, 0.0);
      wan();
      glPopMatrix();
     
     
      glutSwapBuffers();
     
     
     
    }
     
    void specialKeys(int key, int x, int y)
    {
      if (key == GLUT_KEY_LEFT)
         angle_step = angle_step + change;
      else if (key == GLUT_KEY_RIGHT)
         angle_step = angle_step - change;
    }
     
    void keyboard(unsigned char code, int x, int y)
    {
      if (code == ' ')
         angle_step = 0.0;
      else if (code == 27)
         exit(0);
    }
     
    void update(void)
    {
      angle = angle + angle_step2;
      if (angle >= 360.0)
         angle = angle - 360.0;
      else if (angle < 0.0)
         angle = angle + 360.0;
      glutPostRedisplay();
    }
    void reshape(int w, int h)
    {
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(40.0, (GLdouble)w/(GLdouble)h, 0.5, 20.0);
      glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    }
     
    int main(int argc, char** argv)
    {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
      glutInitWindowSize(1000,700);
      glutInitWindowPosition(12.5,10);
      glutCreateWindow("Square");
      init();
      glutDisplayFunc(display);
      glutReshapeFunc(reshape);
      glutIdleFunc(update);
      glutSpecialFunc(specialKeys);
      glutKeyboardFunc(keyboard);
      glutMainLoop();
      return 0;
    }

    But i take this error:
    Code :
     C:\Dev-Cpp\example.c In function `LoadGLTextures': 
    380 C:\Dev-Cpp\example.c syntax error at end of input 
     C:\Dev-Cpp\Makefile.win [Build Error]  [opengldenemeresim2.o] Error 1

  5. #5
    Senior Member Regular Contributor
    Join Date
    Jan 2011
    Location
    Paris, France
    Posts
    281

    Re: How can i load image?

    You can too use the DevIL library

    Here an simple example of using this library into a GLUT program that include a picture into another :

    Code :
     
    #include <GL/glut.h>    // Header File For The GLUT Library 
    #include <GL/gl.h>	// Header File For The OpenGL32 Library
    #include <GL/glu.h>	// Header File For The GLu32 Library
    #include <unistd.h>     // Header file for sleeping.
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <IL/il.h>
    #include <string.h>
     
    /* ascii code for the escape key */
    #define ESCAPE 27
     
    #define FALSE 0
    #define TRUE 1
     
    /* The number of our GLUT window */
    int window; 
     
     
    ILuint ilTexture, ilSubTexture;													
    GLuint Textures[2];
    int texture=0;
    char *dataTexture, *dataSubTexture, *initialTexture;
    int w,h, f,bpp;
    int wsub,hsub, fsub, bppsub;
     
    int subheight=100;
     
    int  i , j, k;
     
     
    void IncludeSubPicture()
    {
    // Include the subpicture into the original picture
      ilBindImage(ilTexture);
      ilSetPixels(0,0,0,w,h,1,fsub,IL_UNSIGNED_BYTE, initialTexture);
      ilSetPixels(0,0,0,wsub,subheight,1,fsub,IL_UNSIGNED_BYTE, dataSubTexture);
      printf("subheight=%d \n", subheight);
     
      glBindTexture(GL_TEXTURE_2D, Textures[0]);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, dataTexture);
    }
     
    void InitDevIL()
    {
     
      glEnable(GL_TEXTURE_2D);
      glGenTextures(2,Textures);
      glBindTexture(GL_TEXTURE_2D, Textures[0]);
      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	
      glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
      glBindTexture(GL_TEXTURE_2D,Textures[1]);
      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	
      glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
     
      ilInit();
     
      // Load the big picture with DevIL 
      ilGenImages(1, &amp;ilTexture);
      ilBindImage(ilTexture);
      if( ilLoadImage("test1.jpg") != 0)
         printf("ilLoadImage %s OK \n", "test1.jpg");
      else
         printf("ilLoadImage %s KO \n", "test1.jpg");
       dataTexture = ilGetData();
       w = ilGetInteger(IL_IMAGE_WIDTH);
       h = ilGetInteger(IL_IMAGE_HEIGHT);
       f = ilGetInteger(IL_IMAGE_FORMAT);
       bpp = ilGetInteger(IL_IMAGE_BPP);
       printf("ilGetData = %x w=%d, h=%d bpp=%d \n", dataTexture,w,h,bpp);
       initialTexture=malloc(w*h*bpp);
       memcpy(initialTexture,dataTexture,w*h*bpp);
     
      // Load the subpicture
      ilGenImages(1, &amp;ilSubTexture);
      ilBindImage(ilSubTexture);
      if( ilLoadImage("test2.jpg") != 0)
         printf("ilLoadImage %s OK \n", "test2.jpg");
      else
         printf("ilLoadImage %s KO \n", "test2.jpg");  
      dataSubTexture = ilGetData();
      wsub = ilGetInteger(IL_IMAGE_WIDTH);  
      hsub = ilGetInteger(IL_IMAGE_HEIGHT);
      fsub = ilGetInteger(IL_IMAGE_FORMAT);
      bppsub = ilGetInteger(IL_IMAGE_BPP);
      printf("ilGetData = %x w=%d, h=%d bpp=%d \n", dataSubTexture,wsub,hsub,bppsub);
     
      IncludeSubPicture();
    }   
     
    void DrawTest()
    {
     
      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Buffers
      glLoadIdentity ();											// Reset The Matrix
     
      glPushMatrix();
      glBindTexture(GL_TEXTURE_2D, Textures[texture]);
     
        glBegin(GL_QUADS);
        	//glColor3f(0,1,0); 
    	glTexCoord2i(1,0); 
    	glVertex3f(-1,-1,-1);
     
        	//glColor3f(0,0,1); 
    	glTexCoord2i(0,0); 
    	glVertex3f(1,-1,-1);
     
        	//glColor3f(1,0,0); 
    	glTexCoord2i(0,1); 
    	glVertex3f(1,1,-1);
     
    	//glColor3f(1,0,0); 
    	glTexCoord2i(1,1); 
    	glVertex3f(-1,1,-1);
      glEnd();
     
      glPopMatrix();
    	glutSwapBuffers();
     
    }
     
     
    /* A general OpenGL initialization function.  Sets all of the initial parameters. */
    void InitGL(int Width, int Height)	        // We call this right after our OpenGL window is created.
    {
     
      // Start Of User Initialization
      glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);			// Realy Nice perspective calculations
     
      glClearColor (0.7f, 0.7f, 0.7f, 0.0f);						// Light Grey Background
      glClearDepth (1.0f);										// Depth Buffer Setup
     
      InitDevIL();
     
    }
     
    /* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */
    void ReSizeGLScene(int Width, int Height)
    {
      if (Height==0)				// Prevent A Divide By Zero If The Window Is Too Small
        Height=1;
     
      glViewport(0, 0, Width, Height);		// Reset The Current Viewport And Perspective Transformation
     
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
     
      gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
      glMatrixMode(GL_MODELVIEW);
    }
     
     
    void Deinitialize (void)										// Any User DeInitialization Goes Here
    {
    	glDeleteTextures (2, Textures);					// Delete The Shader Texture ( NEW )
    }
     
    /* The function called whenever a key is pressed. */
    void keyPressed(unsigned char key, int x, int y) 
    {
        /* avoid thrashing this procedure */
      usleep(100);
     
        /* If escape is pressed, kill everything. */
     
      switch (key) {    
     
        case ESCAPE: // kill everything.
    	/* shut down our window */
    	Deinitialize();
    	glutDestroyWindow(window); 
     
    	/* exit the program...normal termination. */
    	exit(1);                   	
    	break; // redundant.
     
       case 56 : subheight++; printf("suhbeight=%d \n",subheight); IncludeSubPicture(); break;
       case 50 : subheight--; printf("suhbeight=%d \n",subheight); IncludeSubPicture(); break;
     
        default:
          printf ("Key %d pressed. No action there yet.\n", key);
          break;
        }
     
        glutPostRedisplay();
    }
     
    void specialKeyPressed(int key, int x, int y) 
    {
        /* avoid thrashing this procedure */
        usleep(100);
     
        switch (key) {    
     
        case GLUT_KEY_UP: // decrease x rotation speed;
    	subheight++;	
    	break;
     
        case GLUT_KEY_DOWN: // increase x rotation speed;
    	subheight--;	
    	break;
     
        default:
    	break;
        }	
    }
     
    int main(int argc, char **argv) 
    {  
      /* Initialize GLUT state - glut will take any command line arguments that pertain to it or 
         X Windows - look at its documentation at [url]http://reality.sgi.com/mjk/spec3/spec3.html[/url] */  
      glutInit(&amp;argc, argv);  
     
      /* Select type of Display mode:   
         Double buffer 
         RGBA color
         Alpha components supported 
         Depth buffer */  
      glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);  
     
      /* get a 640 x 480 window */
      glutInitWindowSize(640, 480);  
     
      /* the window starts at the upper left corner of the screen */
      glutInitWindowPosition(0, 0);  
     
      /* Open a window */  
      window = glutCreateWindow("YLP : first test with DevIL");  
     
      /* Register the function to do all our OpenGL drawing. */
      glutDisplayFunc(&amp;DrawTest);  
     
     
      /* Register the function called when our window is resized. */
      glutReshapeFunc(&amp;ReSizeGLScene);
     
      /* Register the function called when the keyboard is pressed. */
      glutKeyboardFunc(&amp;keyPressed);
     
      glutSpecialFunc(&amp;specialKeyPressed);
     
    /* Initialize our window. */
      InitGL(640, 480);
     
      /* Start Event Processing Engine */  
      glutMainLoop();  
     
      return 1;
    }
    (this load two files named test1.jpg and test2.jpg but you can very easily modify the InitDevIL func for to load anothers pictures files, use the 8 and 2 numerics keys for to make the subpicture more or less big)

    On linux, you can compile it like this

    Code :
      gcc testil.c -o testil -L/usr/X11R6/lib -lGL -lGLU -lglut -lIL
    @+
    Yannoo

  6. #6
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    11

    Re: How can i load image?

    I tried this.I take these error:
    Code :
    68 C:\Dev-Cpp\ilexam.cpp invalid conversion from `ILubyte*' to `char*' 
    74 C:\Dev-Cpp\ilexam.cpp invalid conversion from `void*' to `char*' 
    84 C:\Dev-Cpp\ilexam.cpp invalid conversion from `ILubyte*' to `char*'

    And my OS is Windows.

  7. #7
    Senior Member Regular Contributor
    Join Date
    Jan 2011
    Location
    Paris, France
    Posts
    281

    Re: How can i load image?

    Certainly a cast problem or something like this

    line 68 dataTexture = ilGetData();
    => dataTexture = (char *) ilGetData();

    line 74 initialTexture=malloc(w*h*bpp);
    => initialTexture = (char *) malloc(w*h*bpp);

    line 84 dataSubTexture = ilGetData();
    => dataSubTexture = (char *) ilGetData();
    @+
    Yannoo

  8. #8
    Junior Member Newbie
    Join Date
    May 2011
    Posts
    11

    Re: How can i load image?

    I take il linker errors.
    Can i solve this problem?
    :
    Code :
    #include <GL/glut.h>    // Header File For The GLUT Library 
    #include <GL/gl.h>	// Header File For The OpenGL32 Library
    #include <GL/glu.h>	// Header File For The GLu32 Library
    #include <unistd.h>     // Header file for sleeping.
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <fstream>
     
     
    #include <string.h>
     
    /* ascii code for the escape key */
    #define ESCAPE 27
     
    #define FALSE 0
    #define TRUE 1
     
    /* The number of our GLUT window */
    int window; 
     
     
     
    GLuint Textures[2];
    int texture=0;
    char *dataTexture, *dataSubTexture, *initialTexture;
    int w,h, f,bpp;
    int wsub,hsub, fsub, bppsub;
     
    int subheight=100;
     
    int  i , j, k;
     
    FILE *dosya;
    void IncludeSubPicture()
    {  
    // Include the subpicture into the original picture
     
      printf("subheight=%d \n", subheight);
     
      glBindTexture(GL_TEXTURE_2D, Textures[0]);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, dataTexture);
    }
    char *a;
    void InitDevIL()
    {
     
      glEnable(GL_TEXTURE_2D);
      glGenTextures(2,Textures);
      glBindTexture(GL_TEXTURE_2D, Textures[0]);
      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	
      glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
      glBindTexture(GL_TEXTURE_2D,Textures[1]);
      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	
      glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
     
     
    dosya=fopen("C:\\NeHe.bmp","rb");
    Textures[2]=fread(a,sizeof(unsigned char),1,dosya);
     
      // Load the big picture with DevIL 
     
      if(Textures[2]==0)
         printf("ilLoadImage %s OK \n", "Nehe.jpg");
      else
         printf("ilLoadImage %s KO \n", "Nehe.jpg");
     
     
     
       initialTexture=(char*)malloc(w*h*bpp);
       memcpy(initialTexture,dataTexture,w*h*bpp);
     
      // Load the subpicture
     
      if( Textures[2] != 0)
         printf("ilLoadImage %s OK \n", "Nehe.jpg");
      else
         printf("ilLoadImage %s KO \n", "Nehe.jpg");  
     
     
      printf("ilGetData = %x w=%d, h=%d bpp=%d \n", dataSubTexture,wsub,hsub,bppsub);
     
      IncludeSubPicture();
    }   
     
    void DrawTest()
    {
     
      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Buffers
      glLoadIdentity ();											// Reset The Matrix
     
      glPushMatrix();
      glBindTexture(GL_TEXTURE_2D, Textures[texture]);
     
        glBegin(GL_QUADS);
        	//glColor3f(0,1,0); 
    	glTexCoord2i(1,0); 
    	glVertex3f(-1,-1,-1);
     
        	//glColor3f(0,0,1); 
    	glTexCoord2i(0,0); 
    	glVertex3f(1,-1,-1);
     
        	//glColor3f(1,0,0); 
    	glTexCoord2i(0,1); 
    	glVertex3f(1,1,-1);
     
    	//glColor3f(1,0,0); 
    	glTexCoord2i(1,1); 
    	glVertex3f(-1,1,-1);
      glEnd();
     
      glPopMatrix();
    	glutSwapBuffers();
     
    }
     
     
    /* A general OpenGL initialization function.  Sets all of the initial parameters. */
    void InitGL(int Width, int Height)	        // We call this right after our OpenGL window is created.
    {
     
      // Start Of User Initialization
      glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);			// Realy Nice perspective calculations
     
      glClearColor (0.7f, 0.7f, 0.7f, 0.0f);						// Light Grey Background
      glClearDepth (1.0f);										// Depth Buffer Setup
     
      InitDevIL();
     
    }
     
    /* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */
    void ReSizeGLScene(int Width, int Height)
    {
      if (Height==0)				// Prevent A Divide By Zero If The Window Is Too Small
        Height=1;
     
      glViewport(0, 0, Width, Height);		// Reset The Current Viewport And Perspective Transformation
     
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
     
      gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
      glMatrixMode(GL_MODELVIEW);
    }
     
     
    void Deinitialize (void)										// Any User DeInitialization Goes Here
    {
    	glDeleteTextures (2, Textures);					// Delete The Shader Texture ( NEW )
    }
     
    /* The function called whenever a key is pressed. */
    void keyPressed(unsigned char key, int x, int y) 
    {
        /* avoid thrashing this procedure */
     
     
        /* If escape is pressed, kill everything. */
     
      switch (key) {    
     
        case ESCAPE: // kill everything.
    	/* shut down our window */
    	Deinitialize();
    	glutDestroyWindow(window); 
     
    	/* exit the program...normal termination. */
    	exit(1);                   	
    	break; // redundant.
     
       case 56 : subheight++; printf("suhbeight=%d \n",subheight); IncludeSubPicture(); break;
       case 50 : subheight--; printf("suhbeight=%d \n",subheight); IncludeSubPicture(); break;
     
        default:
          printf ("Key %d pressed. No action there yet.\n", key);
          break;
        }
     
        glutPostRedisplay();
    }
     
    void specialKeyPressed(int key, int x, int y) 
    {
        /* avoid thrashing this procedure */
     
     
        switch (key) {    
     
        case GLUT_KEY_UP: // decrease x rotation speed;
    	subheight++;	
    	break;
     
        case GLUT_KEY_DOWN: // increase x rotation speed;
    	subheight--;	
    	break;
     
        default:
    	break;
        }	
    }
     
    int main(int argc, char **argv) 
    {  
      /* Initialize GLUT state - glut will take any command line arguments that pertain to it or 
         X Windows - look at its documentation at [url]http://reality.sgi.com/mjk/spec3/spec3.html[/url] */  
      glutInit(&amp;argc, argv);  
     
      /* Select type of Display mode:   
         Double buffer 
         RGBA color
         Alpha components supported 
         Depth buffer */  
      glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);  
     
      /* get a 640 x 480 window */
      glutInitWindowSize(640, 480);  
     
      /* the window starts at the upper left corner of the screen */
      glutInitWindowPosition(0, 0);  
     
      /* Open a window */  
      window = glutCreateWindow("YLP : first test with DevIL");  
     
      /* Register the function to do all our OpenGL drawing. */
      glutDisplayFunc(&amp;DrawTest);  
     
     
      /* Register the function called when our window is resized. */
      glutReshapeFunc(&amp;ReSizeGLScene);
     
      /* Register the function called when the keyboard is pressed. */
      glutKeyboardFunc(&amp;keyPressed);
     
      glutSpecialFunc(&amp;specialKeyPressed);
     
    /* Initialize our window. */
      InitGL(640, 480);
     
      /* Start Event Processing Engine */  
      glutMainLoop();  
     
      return 1;
    }

Similar Threads

  1. Replies: 1
    Last Post: 05-07-2017, 03:36 AM
  2. Load png Image
    By tajouri in forum OpenGL ES
    Replies: 1
    Last Post: 07-23-2012, 07:02 AM
  3. could add some functions to load image?
    By beginnertom in forum OpenGL: General
    Replies: 2
    Last Post: 03-30-2010, 09:41 PM
  4. load tga image
    By trackin in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 09-03-2009, 04:17 PM
  5. Replies: 5
    Last Post: 08-02-2006, 01:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean