Results 1 to 4 of 4

Thread: Texturing Problem ( Basic )

  1. #1
    Member Newbie
    Join Date
    Aug 2009
    Location
    Iran, Mashhad
    Posts
    41

    Texturing Problem ( Basic )

    Hi everyone

    I tried to code my very first texture code thing. But I don't know
    Why always I just get a distorted image! here is my code:
    Code :
     
    GLuint loadTexture()
    {
    	glClearColor(1,1,1,1);
    	glClear(GL_COLOR_BUFFER_BIT);
     
    	int w, h;
    	GLbyte * data;
    	FILE * file;
     
    	w = 750 ;
    	h = 704 ;
     
    	GLuint texture;
     
    	data = (GLbyte*) malloc ( h * w * 3 * 2 );
    	file = fopen("image.raw", "r");
    	if (file == NULL) return 0;
    	fread(data , h * w * 3 ,1 ,file);
    	fclose(file);
     
    	glGenTextures(1, &texture);
    	glBindTexture(GL_TEXTURE_2D, texture);
    	glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
    	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    	gluBuild2DMipmaps(GL_TEXTURE_2D,3,w,h, GL_RGB ,GL_UNSIGNED_BYTE ,data);
     
    	free(data);		
    	glBegin(GL_QUADS);
    		glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
    		glTexCoord2d(1.0,0.0); glVertex2d(100.0,0.0);
    		glTexCoord2d(1.0,1.0); glVertex2d(100.0,100.0);
    		glTexCoord2d(0.0,1.0); glVertex2d(0.0,100.0);
    	glEnd();
     
    	return texture;
    }
    And one more thing, there were no 2 in the tutorial I was following but It didn't work without 2 in the line below. why is that 2 for?
    Code :
    data = (GLbyte*) malloc ( h * w * 3 * 2 );

  2. #2
    Member Newbie
    Join Date
    Aug 2009
    Location
    Iran, Mashhad
    Posts
    41

    Re: Texturing Problem ( Basic )

    Well, I would be appreciated for your help guys ...

  3. #3
    Senior Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: Texturing Problem ( Basic )

    key to understand this is to understand this rather then copy n paste. basically any image has dimension, pixelsize (or bitdepth) and an special order of the channels (RGBA/ABGR). the later two components can be described as the pixel format (RGBA_8888/RGBA_5551). when you malloc you need to ensure the correct pixel data size of the image thus width * height * pixelsize f.e. 512 * 512 * 4 (RGBA ala 8bit per channel) equals 1048576 bytes. without knowing how the data is stored you will get wrong channels order (swapped colors) or distorted data what you have now. advice you to forget about .raw and use some image formats like TGA or BMP since they are easy to read. all the image data you listed above is stored in the header of the image files so you can easily retrieve them, malloc, read and upload to OpenGL.

  4. #4
    Member Newbie
    Join Date
    Aug 2009
    Location
    Iran, Mashhad
    Posts
    41

    Re: Texturing Problem ( Basic )

    Yeah, Your right. It's better to figuring out the concepts first.

Similar Threads

  1. basic texturing
    By sajis997 in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 03-30-2014, 05:46 AM
  2. Basic texturing w/ core (GLSL) problem
    By ElFuego in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 05-05-2011, 06:46 AM
  3. Basic texturing problem
    By Ottaz in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 03-14-2010, 03:22 PM
  4. A very basic question about the texturing pipeline
    By brian mcgann in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-14-2006, 09:49 AM
  5. Basic Texturing...
    By Sephiroth in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 12-28-2001, 07:26 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