PDA

View Full Version : ***URGENT***LOADING TEXTURE



kingabhi
05-10-2009, 02:21 PM
hello guyz..
i am working on a college project,i need to submit it tommorow plz help me as soon as possible.
i have made a spherical object with lighting effect the following is its code and it works very well....




#include <iostream>
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>

#else
#include <GL/glut.h>
#endif

using namespace std;

const float RADIUS = 7.0f; //The radius of the sphere

float _angle = -60;

bool _highShininess = false; //Whether the shininess parameter is high
bool _lowSpecularity = false; //Whether the specularity parameter is high
bool _emission = false; //Whether the emission parameter is turned on

void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
break;
case 's':
_highShininess = !_highShininess;
break;
case 'p':
_lowSpecularity = !_lowSpecularity;
break;
case 'e':
_emission = !_emission;
break;
}
}

void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
//Disable color materials, so that glMaterial calls work
glDisable(GL_COLOR_MATERIAL);
}

void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
}

void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0.0f, 0.0f, -20.0f);
glRotatef(_angle, 0, 1, 0);

GLfloat ambientLight[] = {1.2f, 1.2f, 0.2f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);

GLfloat lightColor[] = {0.6f, 0.6f, 0.6f, 1.0f};
GLfloat lightPos[] = {1.5f * RADIUS, 2 * RADIUS, 1.5 * RADIUS, 1.0f};
//Diffuse (non-shiny) light component
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
//Specular (shiny) light component
glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

//Determine the specularity, emissivity, and shininess parameters, based on
//variables that can be toggled with keystrokes
float specularity;
if (_lowSpecularity) {
specularity = 0.3f;
}
else {
specularity = 1;
}

float emissivity;
if (_emission) {
emissivity = 0.05f;
}
else {
emissivity = 0;
}

float shininess;
if (_highShininess) {
shininess = 25;
}
else {
shininess = 12;
}

//The color of the sphere
GLfloat materialColor[] = {0.2f, 0.2f, 1.0f, 1.0f};
//The specular (shiny) component of the material
GLfloat materialSpecular[] = {specularity, specularity, specularity, 1.0f};
//The color emitted by the material
GLfloat materialEmission[] = {emissivity, emissivity, emissivity, 1.0f};

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColor);
glMaterialfv(GL_FRONT, GL_SPECULAR, materialSpecular);
glMaterialfv(GL_FRONT, GL_EMISSION, materialEmission);
glMaterialf(GL_FRONT, GL_SHININESS, shininess); //The shininess parameter

//Draw the sphere

glutSolidSphere(RADIUS, 150, 80);


glutSwapBuffers();
}

//Called every 25 milliseconds
void update(int value) {
_angle += 1.5f;
if (_angle > 360) {
_angle -= 360;
}

glutPostRedisplay();
glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);

glutCreateWindow("ABHISHEK");
initRendering();

glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);

glutMainLoop();
return 0;
}
now by going through the lesson 6 of nehe i am trying to load a .bmp image file named as "abhi".i want to wrap up the image of the earth around the spherical object so that it looks as it its the earth..the following is the code i have written but its going wrong:-


#include <iostream>
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>

#else
#include <GL/glut.h>
#endif

using namespace std;
GLuint texture[1];
const float RADIUS = 7.0f; //The radius of the sphere

float _angle = -60;

bool _highShininess = false; //Whether the shininess parameter is high
bool _lowSpecularity = false; //Whether the specularity parameter is high
bool _emission = false; //Whether the emission parameter is turned on


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/abhi.bmp"))
{
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]);
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_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,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
}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

glViewport(0,0,width,height); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}

int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
{
return FALSE; // If Texture Didn't Load Return FALSE
}

glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return TRUE; // Initialization Went OK
}
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
break;
case 's':
_highShininess = !_highShininess;
break;
case 'p':
_lowSpecularity = !_lowSpecularity;
break;
case 'e':
_emission = !_emission;
break;
}
}

void initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
//Disable color materials, so that glMaterial calls work
glDisable(GL_COLOR_MATERIAL);
}

void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
}

void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTranslatef(0.0f, 0.0f, -20.0f);
glRotatef(_angle, 0, 1, 0);

GLfloat ambientLight[] = {1.2f, 1.2f, 0.2f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);

GLfloat lightColor[] = {0.6f, 0.6f, 0.6f, 1.0f};
GLfloat lightPos[] = {1.5f * RADIUS, 2 * RADIUS, 1.5 * RADIUS, 1.0f};
//Diffuse (non-shiny) light component
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
//Specular (shiny) light component
glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

//Determine the specularity, emissivity, and shininess parameters, based on
//variables that can be toggled with keystrokes
float specularity;
if (_lowSpecularity) {
specularity = 0.3f;
}
else {
specularity = 1;
}

float emissivity;
if (_emission) {
emissivity = 0.05f;
}
else {
emissivity = 0;
}

float shininess;
if (_highShininess) {
shininess = 25;
}
else {
shininess = 12;
}

//The color of the sphere
GLfloat materialColor[] = {0.2f, 0.2f, 1.0f, 1.0f};
//The specular (shiny) component of the material
GLfloat materialSpecular[] = {specularity, specularity, specularity, 1.0f};
//The color emitted by the material
GLfloat materialEmission[] = {emissivity, emissivity, emissivity, 1.0f};

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialColor);
glMaterialfv(GL_FRONT, GL_SPECULAR, materialSpecular);
glMaterialfv(GL_FRONT, GL_EMISSION, materialEmission);
glMaterialf(GL_FRONT, GL_SHININESS, shininess); //The shininess parameter

//Draw the sphere

glutSolidSphere(RADIUS, 150, 80);


glutSwapBuffers();
}

//Called every 25 milliseconds
void update(int value) {
_angle += 1.5f;
if (_angle > 360) {
_angle -= 360;
}

glutPostRedisplay();
glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);

glutCreateWindow("ABHISHEK");
initRendering();

glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);

glutMainLoop();
return 0;
}

i have to submitt the project very soon.So i request you guyz to go through the code and plz help me out.i would be thankful to you.