Sign in to follow this  
wabbz111

Texture Mapping Problem

Recommended Posts

Hi people I am working on solar system simulator project. The code compiles fine but the texture does not bind to the sphere(sun) In the code you see two binding functions i am using the first one(see comments)
/*******************************************************************************
********************************************************************************
*******************************************************************************
       
        REGISTRATION NUMBER::::::::::::008450
        PROJECT::::::::::::::::::::::::SOLAR SYSTEM SIMULATOR
        YEARS OF DEVELOPMENT:::::::::::2008 TO 2009                                          

                        
                                                                                             

                                                     

*******************************************************************************
*******************************************************************************
******************************************************************************/

#include <windows.h>
#include <stdio.h>
#include <gl/glut.h>
#include <math.h>
#include <gl\glaux.h>

static GLfloat year = 0, day = 0;
static GLfloat pitch = 0, rot = 90.0;
static GLfloat xdist = 0, ydist = 0, zdist = 10.0;
static GLfloat xlook = 0.0, ylook = 0.0, zlook = 0.0;
static GLfloat xup = 0.0, yup = 0.0, zup= 0.0;
static int lastx = 0, lasty = 0;

///////////************Texture variables**********/////////////
GLuint texture[1]; // Storage For One Texture ( NEW )
GLuint LoadTextureBMP( const char * filename );


/* Lighting values
GLfloat whiteLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat sourceLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat	lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };
//GLfloat emissionSun[] = { 0.5f, 0.5f, 0.0f, 0.0f};
GLfloat emissionNone[] = { 0.0f, 0.0f, 0.0f, 1.0f};
*/

/********.........FIRST TEXTURE MAPPING FUNCTION BEGINS.........***************/

 AUX_RGBImageRec *LoadBMP(char *Filename)				// Loads A Bitmap 

Image
   
   {
	FILE *File=NULL;									

// File Handle

	if (!Filename)										

// Make Sure A Filename Was Given
	{
		return NULL;									

// If Not Return NULL
	}

	File=fopen(Filename,"r");							// 

Check To See If The File Exists

	if (File)										

	// Does The File Exist?
	{
		fclose(File);									

// Close The Handle
		return auxDIBImageLoad(Filename);				// Load The 

Bitmap And Return A Pointer
	}

	return NULL;										

// If Load Failed Return NULL
}

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("Data/sun.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]);
		
	    	 // Generate The Texture
		     glTexImage2D( GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,
             TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE,
             TextureImage[0]->data);
		     // Linear Filtering
             glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
             // Linear Filtering
             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
           }

	   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
	
}
/********.........FIRST TEXTURE MAPPING FUNCTION ENDS.........***************/

/************.....SECOND TEXTURE MAPPING FUNCTION BEGINS HERE.......***********
GLuint LoadTextureBMP( const char * filename )
{
  GLuint texture;
  int width, height;
  unsigned char * data;
  FILE * file;

  file = fopen( filename, "rb" );
  if ( file == NULL ) return 0;

  width = 1024;
  height = 512;
  data = (unsigned char *)malloc( width * height * 3 );

  fread( data, width * height * 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_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );

  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

  gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );

  free( data );

  return texture;

} 
**********************SECOND TEXTURE MAPPING FUNCTION ENDS HERE****************/

void GetKey();

/////**********FUCTION TO ENABLE ROTATION OF PLANETS*****************////////
void Rotate()
{
	day += 2;
	if(day > 360)
		day = day - 360;
	year += .1;
	if(year > 360)
		year = year - 360;
	glutPostRedisplay();
}

//**********........FUNCTION TO DRAW PLANETARY ORBITS OF BEGINS......**********/
void DrawOrbit(GLfloat rad, GLfloat inc)
{
	glPushMatrix();

	glColor3f(0.5, 0.5, 0.5);
	glBegin(GL_POINTS);
	GLfloat y = 0;
	for(GLfloat angle = 0; angle <= 360; angle+=inc)
	{
		GLfloat x = rad*sin((angle+90)*3.1415926535/180);
		GLfloat z = rad*cos((angle+90)*3.1415926535/180);
		//GLfloat y = rad*tan((angle+90)*3.1415926535/180);

		glVertex3f(x, y, z);
	}
	glEnd();

	glPopMatrix();
}
///*******........FUNCTION TO DRAW PLANETARY ORBITS ENDS.........**************/


///***************FUNCTIONS TO DRAW PLANETS BEGIN........**********************/
/*void BluePlanet(void)
{
	DrawOrbit(30, 1);

	glRotatef((GLfloat) year*6, 0.0, 1.0, 0.0); //rotate for the planet
	glTranslatef(30, 0.0, 0.0);
	glColor3f(0.0, 0.3, 1.0);
	glutSolidSphere(0.2, 10, 8); //draw planet

	glRotatef((GLfloat) day*2, 0.0, 1.0, 0.0); //rotate for the moon
	glTranslatef(0.5, 0.0, 0.0);
	glColor3f(0.5, 0.3, 0.0);
	glutSolidSphere(0.05, 5, 4); //draw moon
}

void GreenPlanet(void)
{
	DrawOrbit(45, 1);

	glRotatef((GLfloat) year*5, 0.0, 1.0, 0.0); //rotate for the planet
	glTranslatef(45, 0.0, 0.0);
	glColor3f(0.0, 1.0, 0.0);
	glutSolidSphere(0.3, 20, 16); //draw planet

	glRotatef((GLfloat) day*2, 0.0, 1.0, 0.0); //rotate for the moon
	glTranslatef(0.5, 0.0, 0.0);
	glColor3f(0.5, 0.3, 0.0);
	glutSolidSphere(0.1, 5, 4); //draw moon
}

void JupiterPlanet(void)
{
	DrawOrbit(60, 1);

	glRotatef((GLfloat) year*6, 0.0, 1.0, 0.0); //rotate for the planet
	glTranslatef(60, 0.0, 0.0);
	glColor3f(0.0, 0.2, 0.5);
	glutSolidSphere(1.0, 10, 15); //draw planet

	glRotatef((GLfloat) day*2, 0.0, 1.0, 0.0); //rotate for the moon
	glTranslatef(0.5, 0.0, 0.0);
	glColor3f(0.5, 0.3, 0.0);
	glutSolidSphere(0.1, 5, 4); //draw moon
}



void PurplePlanet(void)
{
	DrawOrbit(10.5, 1);

	glRotatef((GLfloat) year*8, 0.0, 1.0, 0.0); //rotate for the planet
	glTranslatef(10.5, 0.0, 0.0);
	glColor3f(1.0, 0.01, 1.0);
	glutSolidSphere(3.37, 20, 16); //draw planet

	glPushMatrix();
	glRotatef((GLfloat) day*2, 0.0, 1.0, 0.0); //rotate for the moon
	glTranslatef(0.6, 0.0, 0.0);
	glColor3f(0.5, 0.3, 0.0);
	glutSolidSphere(0.1, 5, 4); //draw moon

	glPopMatrix();
	glRotatef((GLfloat) day*3, 0.0, 1.0, 0.0); //rotate for the moon
	glTranslatef(1.2, 0.0, 0.0);
	glColor3f(0.5, 0.3, 0.0);
	glutSolidSphere(0.1, 5, 4); //draw moon
}

void RedPlanet(void)
{
	DrawOrbit(20, .5);

	glRotatef((GLfloat) year*2, 0.0, 1.0, 0.0); //rotate for the planet
	glTranslatef(20, 0.0, 0.0);
	glColor3f(1.0, 0.01, 0.0);
	glutSolidSphere(4.7, 20, 16); //draw planet

	glPushMatrix();
	glRotatef((GLfloat) day*3, 0.0, 1.0, 0.0); //rotate for the moon
	glTranslatef(1.5, 0.0, 0.0);
	glColor3f(0.5, 0.3, 0.0);
	glutSolidSphere(0.15, 5, 4); //draw moon

	glPopMatrix();
	glPushMatrix();
	glRotatef((GLfloat) day*2, 0.0, 1.0, 0.0); //rotate for the moon
	glTranslatef(2.1, 0.0, 0.0);
	glColor3f(0.5, 0.3, 0.0);
	glutSolidSphere(0.15, 5, 4); //draw moon

	glPopMatrix();
	glRotatef((GLfloat) day, 0.0, 1.0, 0.0); //rotate for the moon
	glTranslatef(2.7, 0.0, 0.0);
	glColor3f(0.5, 0.3, 0.0);
	glutSolidSphere(0.2, 5, 4); //draw moon
}

void WhitePlanet(void)
{
	DrawOrbit(27, .3);
	
	glRotatef((GLfloat) year, 0.0, 1.0, 0.0); //rotate for the planet
	glTranslatef(27, 0.0, 0.0);
	glColor3f(0.9, 0.9, 0.9);
	glutSolidSphere(2.6, 20, 16); //draw planet

	glRotatef(15.0, 0.0, 0.0, 1.0);
	glRotatef((GLfloat) day*4, 0.0, 1.0, 0.0); //rotate for the moon
	glTranslatef(1.2, 0.0, 0.0);
	glColor3f(0.5, 0.3, 0.1);
	glutSolidSphere(0.1, 5, 4); //draw moon  
}

/************END DRAWING OF PLANETS AND MOONS**********************/

/*
void SetupRC(void)
 {
     
	// Enable lighting
	//glEnable(GL_LIGHTING);

	// Setup and enable light0
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, whiteLight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, sourceLight);
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glEnable(GL_LIGHT0);

	// Enable color tracking
	glEnable(GL_COLOR_MATERIAL);
	
	// Set Material properties to follow glColor values
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
   
    glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Set The Blending Function For Translucency
    glEnable(GL_BLEND); // Enable Blending
	glClearColor(0.0, 0.0, 0.0, 0.0);

}*/
 
//*******FUNCTION THAT DRAWS EVERYTHING TO THE SCREEN BEGINS.......**********/// 
void RenderScene(void)
{
     /* Enable lighting
	glEnable(GL_LIGHTING);
	// Setup and enable light0
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, whiteLight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, sourceLight);
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glEnable(GL_LIGHT0);*/

	// Enable color tracking
	glEnable(GL_COLOR_MATERIAL);
	
	// Set Material properties to follow glColor values
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
   
    glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Set The Blending Function For Translucency
    glEnable(GL_BLEND); // Enable Blending
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glEnable(GL_DEPTH_TEST);

	Rotate();
	GetKey();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glColor3f(1.0, 1.0, 1.0);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
   	glShadeModel(GL_FLAT);  //this makes everything look blocky
 
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	// Set light position before viewing transformation
//	glLightfv(GL_LIGHT0,GL_POSITION,lightPos);

	//set the viewing transformation
	gluLookAt(xdist, ydist, zdist,  xlook, ylook, zlook,  xup, yup, zup);
    // This determines where the camera's position and view is
    gluLookAt(0, 0, 5,     0, 0, 0,     2, 10, 5);
    
    //glMaterialfv(GL_FRONT, GL_EMISSION, emissionNone);

	glPushMatrix(); //save state
	glTranslatef(0.0, 0.0, -5.0); //translate out to sun
    
   //glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
/*	glutSolidSphere(0.1, 5, 4); //draw light
     
    /*********PLANETARY MATRICES BEGINS............************** 
	glPushMatrix(); //save state
	BluePlanet();
	glPopMatrix(); //restore state

	glPushMatrix(); //save state
	GreenPlanet();
	glPopMatrix(); //restore state

	glPushMatrix(); //save state
	PurplePlanet();
	glPopMatrix(); //restore state

	glPushMatrix(); //save state
	RedPlanet();
	glPopMatrix(); //restore state
	
	glPushMatrix(); //save state
	WhitePlanet();
	glPopMatrix(); //restore state
	
	glPushMatrix(); //save state
    JupiterPlanet();
	glPopMatrix(); //restore state
	
	    /*********PLANETARY MATRICES ENDS ............************** 
*/
	
	glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glColor4f(1.0, 1.0, 0.0, .8);
	//glMaterialfv(GL_FRONT, GL_EMISSION, emissionSun);
	
	//******* Select Our Texture and bind to sun********* 
	 glBindTexture(GL_TEXTURE_2D, texture[0]); 
	 //draws the sun
     glutSolidSphere(5, 40, 32); 
	//glDisable(GL_BLEND);
	glPopMatrix(); //restore state

	glutSwapBuffers();
}
//*******FUNCTION THAT DRAWS EVERYTHING TO THE SCREEN ENDS.......**********///

////**********reshape function*****************************/////////////
void Reshape(int w, int h)
{
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	//a very interesting function, much easier than doing aspect ratios by hand
	gluPerspective(40.0, (GLfloat)w/(GLfloat)h, 1.0, 400.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

///***********KEYBOARD FUNCTION BEGINS............**********************///////
void GetKey(void)
{
	if(GetAsyncKeyState(VK_LEFT))
	{
		xdist += (cos((rot+90)*3.1415926535/180))/2;
		zdist += -(sin((rot+90)*3.1415926535/180))/2;
	}

	if(GetAsyncKeyState(VK_RIGHT))
	{
		xdist += (cos((rot-90)*3.1415926535/180))/2;
		zdist += -(sin((rot-90)*3.1415926535/180))/2;
	}

	if(GetAsyncKeyState(VK_UP))
	{
		xdist += (cos(rot*3.1415926535/180)* cos(pitch*3.1415926535/180))/2;
		zdist += -(sin(rot*3.1415926535/180) * cos(pitch*3.1415926535/180))/2;
		ydist += sin(pitch*3.1415926535/180);
	}

	if(GetAsyncKeyState(VK_DOWN))
	{
		xdist -= (cos(rot*3.1415926535/180)* cos(pitch*3.1415926535/180))/2;
		zdist -= -(sin(rot*3.1415926535/180) * cos(pitch*3.1415926535/180))/2;
		ydist -= sin(pitch*3.1415926535/180);
	}

	if(GetAsyncKeyState(0x41)) //a
	{
		xdist += (cos(rot*3.1415926535/180)* cos((pitch+90)*3.1415926535/180));
		zdist += -(sin(rot*3.1415926535/180) * cos((pitch+90)*3.1415926535/180));
		ydist += sin((pitch+90)*3.1415926535/180);
	}
	if(GetAsyncKeyState(0x5a)) //z
	{
		xdist -= (cos(rot*3.1415926535/180)* cos((pitch+90)*3.1415926535/180));
		zdist -= -(sin(rot*3.1415926535/180) * cos((pitch+90)*3.1415926535/180));
		ydist -= sin((pitch+90)*3.1415926535/180);
	}

	xlook = xdist + (cos(rot*3.1415926535/180) * cos(pitch*3.1415926535/180));
	zlook = zdist - (sin(rot*3.1415926535/180) * cos(pitch*3.1415926535/180));
	ylook = ydist + sin(pitch*3.1415926535/180);

	xup = cos(rot*3.1415926535/180) * cos((pitch+90)*3.1415926535/180);
	zup = -sin(rot*3.1415926535/180) * cos((pitch+90)*3.1415926535/180);
	yup = sin((pitch+90)*3.1415926535/180);

	glutPostRedisplay();
}

///**********KEYBOARD FUNCTION ENDS.................********************////////


/*void PassiveMouse(int x, int y)
{
	if( (lastx - x) >50 || (lastx - x) <-50 ||
		(lasty - y) >50 || (lasty - y) <-50 )
	{
		lastx = x;
		lasty = y;
	}

	rot += (((GLfloat)lastx - (GLfloat)x)/2);
	lastx = x;

	pitch += -(((GLfloat)lasty - (GLfloat)y))/2;
	lasty = y;

	if(pitch > 90)
		pitch = 90.0;
	if(pitch<-90)
		pitch = -90.0;

	if(rot>360)
		rot-=360;
	if(rot<0)
		rot+=360;

//cout<<"rot: "<<rot<<" pitch: "<<pitch<<" xup: "<<xup<<" yup: "<<yup<<" zup: "<<zup<<endl;

	glutPostRedisplay();
}*/

////*******MAIN METHOD********************////////////////////
int main(void)
{
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(800, 600);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("SAYARI 3D");
     
	glutDisplayFunc(RenderScene);
	glutReshapeFunc(Reshape);
	
	//glutPassiveMotionFunc(PassiveMouse);
	//init();
	//SetupRC();

	glutMainLoop();
}

[\source]


Please help me out

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this