Results 1 to 3 of 3

Thread: Help with putting a texture on a glusphere.

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    9

    Help with putting a texture on a glusphere.

    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?

    Code :
    #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();
    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: Help with putting a texture on a glusphere.

    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.
    Am I doing your homework for you?

  3. #3
    Senior Member Regular Contributor
    Join Date
    Aug 2007
    Posts
    107

    Re: Help with putting a texture on a glusphere.

    Call glTexImage2D after the image is loaded?

Similar Threads

  1. help -> adding texture on gluSphere
    By imported_miroslav_karpis in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-17-2009, 01:09 AM
  2. gluSphere texture poles
    By Manojo in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-07-2007, 06:43 AM
  3. Texture map on a gluSphere
    By starman in forum OpenGL: Basic Coding
    Replies: 16
    Last Post: 02-25-2003, 02:39 PM
  4. putting a texture on a nurbs surface
    By slorac in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 11-16-2001, 11:13 PM
  5. Texture flipped on gluSphere
    By Iceman in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 08-31-2001, 08:32 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