Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 2nd, 2006, 10:14 AM   #1
Johnny Redburn
Newbie
 
Join Date: Jun 2006
Posts: 1
Rep Power: 0 Johnny Redburn is on a distinguished road
need help with texture mapping (C and openGL) :(

Hiya
Im writing a program in C using openGL (GLUT) and im having a little trouble with the function to process textures (RAW files). It compiles ok but when i bind the texture in my display function and apply it to a quad it does not display it at runtime (just has a simple RGB default colour i had assigned with glColor3f). It has to be because its not allocating the memory correctly with the malloc i think? but i cant think of a way to just get it working, can anyone please maybe help me.. Pretty please? Im getting really frustrated lol.

Below is the texture processing function:


// load a 256x256 RGB .RAW file as a texture
GLuint LoadTextureRAW( const char * filename, int wrap ) {

  GLuint texture;
  int width, height;
  BYTE * data;
  FILE * file;

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

  // allocate buffer
  width	 = 256;
  height = 256;
  data = (BYTE*)malloc( width * height * 3 );  // <---- is this right?

  // 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 MIP map
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
  // when texture area is large, bilinear filter the first MIP map
  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 MIP maps
  gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );

  // free buffer
  free( data );

  return texture;
}

Im using it like this (just showing the parts that matter):


GLuint texture;
texture = LoadTextureRAW("bush.raw", TRUE);

glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_QUADS);				
	glColor3f(0.0, 0.6, 0.0); //Just the default colour
	glTexCoord2f(0.0,0.0);
	glVertex3f(-1000.0, 0.0, 1000.0);
	glTexCoord2f(0.0,1.0);
	glVertex3f(1000.0, 0.0, 1000.0);
	glTexCoord2f(1.0,1.0); 
	glVertex3f(1000.0, 0.0, -1000.0);
	glTexCoord2f(1.0,0.0); 
	glVertex3f(-1000.0, 0.0, -1000.0);		
glEnd();

I really need to get this working

Thanks.
Johnny Redburn is offline   Reply With Quote
Old Jun 2nd, 2006, 11:11 AM   #2
volatile
Newbie
 
Join Date: Nov 2005
Posts: 18
Rep Power: 0 volatile is on a distinguished road
yes, your malloc is correct. However, you should always check if your memory allocation was successfull. ie check if data is NULL, and take action accordingly.

You should call glPixelStorei(GL_UNPACK_ALIGNMENT, 1); before you call gluBuild2DMipmaps. You need to tell openGL how it should unpack your image from memory. Look it up in the reference.

And then ofcourse, the most important thing, I assume you have this pice of code somewhere in the beginning of you app.
glEnable(GL_TEXTURE_2D);

Can's see anything else that needs fixing. Hope this helps.
volatile is offline   Reply With Quote
Old Jun 3rd, 2006, 7:55 PM   #3
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 58
Rep Power: 0 Adak is an unknown quantity at this point
I've always been told to use sizeof(whatever), rather than multiplying it out yourself, to avoid getting a mean bite from possible alignment adjustments of the compiler.

Adak
Adak is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC