PDA

View Full Version : Help with putting a texture on a glusphere.



thealmightyone
06-02-2009, 04:06 PM
I've spent close to the past hour reading various tutorials, copying pretty much line for line. I've tried to only include the essentials, hopefully not leaving too much out.

Basically, the sphere is taking on colors set by previous material calls. If I move the sphere section to the start of drawScene, they become dark blue (darker than the blue set in glClearColor). Can anyone see why the texture is not being loaded?


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>

#ifdef WIN32
#include <windows.h>
#endif

////////////////////////////////

typedef struct Image
{
unsigned long sizeX;
unsigned long sizeY;
char *data;
} Image;

#include "BMPloader.c"

////////////////////////////////

GLuint texID[2];
GLint winX = 400, winY = 400, window;
float centreX, centreY, centreZ; // centre of the top of the volcano

////////////////////////////////

void initiate(void);
void display(void);
void reshape(int, int);
void lighting(void);

void loadTexture(void);

void timer(int);

void drawScene(void);

////////////////////////////////

int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);

initiate();

glutDisplayFunc(drawScene);
glutReshapeFunc(reshape);

glutMainLoop();

return 0;
}

void initiate(void)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(winX, winY);
window = glutCreateWindow("Volcano");

glClearColor(0.0, 0.0, 0.85, 0.0);
glShadeModel(GL_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthFunc(GL_LESS);
glPointSize(2.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_CULL_FACE);

loadTexture();

lighting();

reshape(winX, winY);
}

void reshape(int width, int height)
{
if (width == 0)
{
width = 1;
}
if (height == 0)
{
height = 1;
}

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)width/(float)height, 2, 2000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, width, height);
}

void loadTexture(void)
{
Image *textureImage;

textureImage = (Image*) malloc(sizeof(Image));

glEnable(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, texID);

glBindTexture(GL_TEXTURE_2D, texID[0]);

glTexImage2D(GL_TEXTURE_2D, 0, 3, textureImage->sizeX, textureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, textureImage->data);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

if (!ImageLoad("rock.bmp", textureImage))
{
printf("Error loading image\n");
}

free(textureImage->data);
free(textureImage);
}

void lighting(void)
{
GLfloat light_position[] = {1000.0, 1000.0, -1000.0, 1.0};

GLfloat diff[] = {1.0, 1.0, 1.0, 1.0};
GLfloat amb[] = {0.2, 0.2, 0.2, 1.0};
GLfloat global_amb[] = {0.2, 0.2, 0.2, 1.0};
GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};

glEnable(GL_LIGHTING);

glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diff);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);

glEnable(GL_LIGHT0);
}

void drawScene(void)
{
int i;

GLfloat smoke_amb_and_diff[] = {0.2, 0.2, 0.2, 1.0};
GLfloat smoke_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat smoke_emission[] = {0.0, 0.0, 0.0, 1.0};

GLfloat lava_amb_and_diff[] = {1.0, 0.0, 0.0, 1.0};
GLfloat lava_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat lava_emission[] = {0.0, 0.0, 0.0, 1.0};

GLUquadricObj *debris;
debris = gluNewQuadric();
gluQuadricDrawStyle(debris, GLU_FILL);
gluQuadricNormals(debris, GLU_SMOOTH);
gluQuadricOrientation(debris, GLU_OUTSIDE);
gluQuadricTexture(debris, GL_TRUE);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, smoke_amb_and_diff);
glMaterialfv(GL_FRONT, GL_EMISSION, smoke_emission);
for (i = 0; i < numOfSmokeParticles; i++)
{
// random stuff
}

for (i = 0; i < numOfDebris; i++)
{
glPushMatrix();
glTranslatef(debrisArray[i].xPos, debrisArray[i].yPos, debrisArray[i].zPos);
//glutSolidSphere(debrisArray[i].size, 20, 20);
gluSphere(debris, debrisArray[i].size, 16, 16);
glPopMatrix();
}

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, lava_amb_and_diff);
glMaterialfv(GL_FRONT, GL_EMISSION, lava_emission);
for (i = 0; i < numOfLava; i++)
{
//random stuff
}

glFlush();

glutSwapBuffers();
}

MaxH
06-02-2009, 06:02 PM
glTexImage2D(GL_TEXTURE_2D, 0, 3, textureImage->sizeX, textureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, textureImage->data);

If you print out sizeX and sizeY in the statement above, do you get the values you expect? Are these values powers of 2?

Alternatively, you can look at my posting of May 18. It is a self-contained demo which loads .bmp images and texture maps one of them onto a gluSphere.

Good luck.

Nicolai de Haan
06-02-2009, 09:51 PM
Call glTexImage2D after the image is loaded?