Opengl texture

Posted on 2011-05-08
Last Modified: 2012-05-11
I am trying to set a texture to a teapot using a bitmap loader. Is there anyway to use my texture on a glut teapot? Normally you assign texture coordinates, is there anyway to use a texture on a glut teapot. My class is below.
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <GL/gl.h>
#include <gl/glu.h>
#include <GL/glut.h>

#define CEMENT 0

/* structure for the storing the image data */
struct Image 
    unsigned long size_x;
    unsigned long size_y;
    char *data;

typedef struct Image Image;

const int textureCount = 1; // specifies # of textures

Image myTextureData[textureCount];  //array storing image texture info
GLuint theTexture[textureCount];  //array storing OpenGL texture info

/* texture filename list */
char* textureFilenames[textureCount] = {"cement.bmp"};

int window_width = 800; //sets the width of the window
int window_height = 600; //sets the hight of the window

-loads our bitmap data into the Image struct
int imageLoader(const char *filename, Image *image) 
    FILE *file;

    unsigned long size;
    unsigned long i;
    unsigned short int planes;
    unsigned short int bpp;

    char temp;
	char finalName[80];

	glTexCoord2f(1.0, 0.0);

	strcpy_s(finalName, "" );
	strcat_s(finalName, filename);

    if ((file = fopen(finalName, "rb"))==NULL) 
		printf("File Not Found : %s\n",finalName);
		return 0;

    fseek(file, 18, SEEK_CUR);

	glTexCoord2f(1.0, 0.0);
	if ((i = fread(&image->size_x, 4, 1, file)) != 1) 
		printf("Error reading width from %s.\n", finalName);
		return 0;

    if ((i = fread(&image->size_y, 4, 1, file)) != 1) 
		printf("Error reading height from %s.\n", finalName);
		return 0;

    size = image->size_x * image->size_y * 3;

    if ((fread(&planes, 2, 1, file)) != 1) 
		printf("Error reading planes from %s.\n", finalName);
		return 0;

    if (planes != 1) 
		printf("Planes from %s is not 1: %u\n", finalName, planes);
		return 0;

    if ((i = fread(&bpp, 2, 1, file)) != 1) 
		printf("Error reading bpp from %s.\n", finalName);
		return 0;

    if (bpp != 24) 
		printf("Bpp from %s is not 24: %u\n", finalName, bpp);
		return 0;

    fseek(file, 24, SEEK_CUR);

    image->data = (char *) malloc(size);

    if (image->data == NULL) 
		printf("Error allocating memory for color-corrected image data");
		return 0;

    if ((i = fread(image->data, size, 1, file)) != 1) 
		printf("Error reading image data from %s.\n", finalName);
		return 0;

    for (i=0;i<size;i+=3) 
		temp = image->data[i];
		image->data[i] = image->data[i+2];
		image->data[i+2] = temp;
    return 1;

void textureLoader() 
	/* set the pixel storage, GL_UNPACK_ALIGNMENT : specifies alignment requirements
	   for the start of each pixel row in memory 1 = byte-alignment*/
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	for(int k=0; k < textureCount; k++) 
		if(!imageLoader(textureFilenames[k], &myTextureData[k])) 
		/* generate texture names */
		glGenTextures(1, &theTexture[k]);
		/* create a named texture bound to a texture target */
	    glBindTexture(GL_TEXTURE_2D, theTexture[k]);

		/* set the texture parameters */
		/* builds and load a set of mipmaps */
	   	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, myTextureData[k].size_x, myTextureData[k].size_y, GL_RGB, GL_UNSIGNED_BYTE, myTextureData[k].data);

/*  planes for texture coordinate generation  */
static GLfloat xequalzero[] = {1.0, 0.0, 0.0, 0.0};
static GLfloat slanted[] = {1.0, 1.0, 1.0, 0.0};
static GLfloat *currentCoeff;
static GLenum currentPlane;
static GLint currentGenMode;

void init(void)
   glClearColor (0.0, 0.0, 0.0, 0.0);

   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

   glMaterialf (GL_FRONT, GL_SHININESS, 64.0);

void display(void)


   glPushMatrix ();
   glRotatef(45.0, 0.0, 0.0, 1.0);

   glBindTexture(GL_TEXTURE_2D, theTexture[CEMENT]);
   glPopMatrix ();

void reshape(int w, int h)
   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
   if (w <= h)
      glOrtho (-3.5, 3.5, -3.5*(GLfloat)h/(GLfloat)w, 
               3.5*(GLfloat)h/(GLfloat)w, -3.5, 3.5);
      glOrtho (-3.5*(GLfloat)w/(GLfloat)h, 
               3.5*(GLfloat)w/(GLfloat)h, -3.5, 3.5, -3.5, 3.5);

int main(int argc, char** argv)
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize(256, 256);
   glutInitWindowPosition(100, 100);
   glutCreateWindow (argv[0]);
   init ();
   return 0;

Open in new window

Question by:tango2009
    LVL 18

    Accepted Solution

    Hello tango2009,

    In a previous question, we had discussed about a texture applied to a model and I assume that as solved properly. In the present subject, you need:

    1) Load the texture (your loader seems OK)
    2) Map the bitmap. Look at the code in line 153:

    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, myTextureData[k].size_x, myTextureData[k].size_y, GL_RGB, GL_UNSIGNED_BYTE, myTextureData[k].data);

    where size_x and size_y determine how big or small is the texture to be applied on the model. You can try several numbers until the texture is applied as you need.

    3) apply the texture on the model. Take a look at the example in .
    If you want to try alternatives and do a research on this theme, the blog at
    is plain of codes to try in.

    LVL 53

    Expert Comment

    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    add text to end of existing text in file 16 54
    dividesSelf challange 15 60
    Python Encoding Problem \u2013 4 58
    strCount chalenge 3 36
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    This video teaches viewers about errors in exception handling.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now