troubleshooting Question

Opengl texture problem

Avatar of tango2009
tango2009 asked on
Programming Languages-OtherGame ProgrammingProgramming
10 Comments1 Solution1404 ViewsLast Modified:
I am trying to use a texture on a vertex shape and when I run the program it displays a multi colored texture instead of the texture that I want to use. The code I am using is below.

This is the part where I am using the texture

void DrawFloor(void) {

        
       glBindTexture(GL_TEXTURE_2D,texture[0]);
       glTranslatef(0.0f,-3.0f,0.0f);

 glBegin(GL_QUADS);
       
        glTexCoord2f(100,0);
        //glColor3f(0.2f, 0.2f, 0.2f);
        glVertex3f(-100.0,-0.5, -100.0);
        glTexCoord2f(100,100);
       // glColor3f(0.4f, 0.4f, 0.4f);
        glVertex3f(-100.0,-0.5, 100.0);
        glTexCoord2f(0,100);
       // glColor3f(0.6f, 0.6f, 0.6f);
        glVertex3f(100.0, -0.5, 100.0);
        glTexCoord2f(0,0);
       // glColor3f(0.8f, 0.8f, 0.8f);
        glVertex3f(100.0, -0.5, -100.0);        
    glEnd();

}

I not sure what is going wrong here. When I run the program is just displays multicolored spots.
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glut.h>

#include <math.h>
#include "Enemy.h"
//angle of rotation
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle=0.0;
float cRadius = 10.0f; // our radius distance from our character
float lastx, lasty;
void DrawFloor();
void DrawSnowMan();
void DrawWeapon();


//positions of the cubes
float positionz[10];
float positionx[10];
// Load in textures 

GLuint	texture[3];			// Storage For 3 Textures


int loadtextures (const char *filename, float width, float height) {

  GLuint texture;

  unsigned char *data;

  FILE *file;

  file = fopen( filename, "rb" );


  if ( file == NULL ) return 0;
  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 );
  gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, 
GL_RGB, GL_UNSIGNED_BYTE, data );
    
  data = NULL;
  return texture;
}


void DrawWeapon(void)
{
   // Draw the Handle
   glPushMatrix();
   glRotatef(90.0f,5.0f,10.0f,5.0f);
   glScalef (2.0, 0.4, 1.0);
   glTranslatef(0.5f,-1.0f,0.6f);
   glutSolidCube (0.6);
   glPopMatrix();


   //Draw the hammer
   glPushMatrix();
   glTranslatef(0.6f,0.6f,-1.0f);
   glutSolidCube(0.9);
   glPopMatrix();



}


void DrawSnowMan(void)
{

  // Draw Body	
	glTranslatef(0.0f ,0.1f, 0.0f);
	glutSolidSphere(0.75f,20,20);

  

  // Draw Head
	glTranslatef(0.0f, 1.0f, 0.0f);
	glutSolidSphere(0.4f,20,20);

   // Draw Hand 
	glPushMatrix();
    glTranslatef(0.5f ,-0.5f, 0.0f);
	glutSolidSphere(0.4f,20,20);
    glPopMatrix();

	//Draw Second hand
	glPushMatrix();
    glTranslatef(-0.5f ,-0.5f, 0.0f);
	glutSolidSphere(0.4f,20,20);
	
    glPopMatrix();


// Draw Eyes
	glPushMatrix();
	glColor3f(0.0f,0.0f,0.0f);
	glTranslatef(0.05f, 0.10f, -0.4f);
	glutSolidSphere(0.05f,10,10);
	glTranslatef(-0.1f, 0.0f, 0.0f);
	glutSolidSphere(0.05f,10,10);
	glPopMatrix();


// Draw Nose
	glPushMatrix();
	glColor3f(1.0f, 0.5f , 0.5f);
	
	glTranslatef(0.0f,0.0f,-0.2f);
	glRotatef(180.0f,0.0f,0.0f,0.0f);
	glutSolidCone(0.08f,0.5f,10,2);
	
    glPopMatrix();
	
}



void cubepositions (void) { //set the positions of the cubes

    for (int i=0;i<10;i++)
    {
    positionz[i] = rand()%10 + 3;
    positionx[i] = rand()%10 + 3;
    }
}
//draw the cube
void cube (void) {
    for (int i=0;i<10 - 1;i++)
    {
     
    glPushMatrix();
    
    glTranslated(-positionx[i + 1] * 10, 0, -positionz[i + 1] * 10); //translate the cube
	
    // house
    glPushMatrix();
	glTranslatef(0,5,0);
    glBindTexture(GL_TEXTURE_2D,texture[0]);
    glutSolidCube(12);                 // building
    
    glTranslatef(0,5,0);
    glPushMatrix();                   // roof
    glRotatef(-90,1,0,0);
    glutSolidCone(10.5,10,16,19);
    glPopMatrix();

    glTranslatef(.75,.20,-.75);         
    glPushMatrix();                   // chimney
    glScalef(2,8,2);
    glutSolidCube(.25);
    glPopMatrix();
    glPopMatrix();

    glTranslatef(0,-.65,2);
   // glutSolidCube(2); //draw the cube
    glPopMatrix();
    }
}
void init (void) {

    glEnable (GL_LIGHTING); //enable the lighting
    glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
    cubepositions();
    glEnable(GL_TEXTURE_2D);
    texture[0] = loadtextures("Wood.bmp", 256,256);
    texture[1] = loadtextures("Snow.bmp", 256,256);
   
	
}


void enable (void) {
    glEnable (GL_DEPTH_TEST); //enable the depth testing
    
    glEnable (GL_COLOR_MATERIAL);
    glShadeModel (GL_SMOOTH); //set the shader to smooth shader
    glEnable(GL_TEXTURE_2D);
}



void display (void) {
    glClearColor (0.0,0.0,0.0,1.0); //clear the screen to 
//black
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
//clear the color buffer and the depth buffer
    enable();
    
    GLfloat light_ambient[] = {200.0, 200.0, 200.0, 200.0}; /* Red diffuse light. */

	GLfloat light_position[] = {0.0, 0.0, 10.0, 0.0};  /* Infinite light location. */

     glBindTexture(GL_TEXTURE_2D,texture[0]);

    glLoadIdentity(); 
    
   // glutSolidCube(12);
   
    glTranslatef(0.0f, 0.0f, -cRadius);
    glRotatef(xrot,1.0,0.0,0.0);
    glColor3f(1.0f, 0.0f, 0.0f);
	glColor3f(1.0f, 1.0f, 1.0f);

    
    //glutSolidCube(2); //Our character to follow*/
	glTranslatef(0.4f,0.0f,0.0f);
    DrawSnowMan();
	DrawWeapon();
    glRotatef(yrot,0.0,1.0,0.0);  
    glTranslated(-xpos,0.0f,-zpos); 
    glColor3f(1.0f, 1.0f, 1.0f);
    cube(); //call the cube drawing function
    //Weapon();`
    glPushMatrix();
   glScalef (2.0, 0.4, 1.0);
   DrawFloor();
   new Robot;
   
  // glutWireCube (5.0);
   glPopMatrix();
    glutSwapBuffers(); //swap the buffers
	
    angle++; //increase the angle
}



void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h); //set the viewport
 //to the current window specifications
    glMatrixMode (GL_PROJECTION); //set the matrix to projection

    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0); //set the perspective (angle of sight, width, height, , depth

    glMatrixMode (GL_MODELVIEW); //set the matrix back to model

}
void keyboard (unsigned char key, int x, int y) {
    if (key=='q')
    {
    xrot += 1;
    if (xrot >360) xrot -= 360;
    }
    if (key=='z')
    {
    xrot -= 1;
    if (xrot < -360) xrot += 360;
    }
    if (key=='w')
    {
    float xrotrad, yrotrad;
    yrotrad = (yrot / 180 * 3.141592654f);
    xrotrad = (xrot / 180 * 3.141592654f); 
    xpos += float(sin(yrotrad));
    zpos -= float(cos(yrotrad));
    ypos -= float(sin(xrotrad));
    }
    if (key=='s')
    {
    float xrotrad, yrotrad;
    yrotrad = (yrot / 180 * 3.141592654f);
    xrotrad = (xrot / 180 * 3.141592654f); 
    xpos -= float(sin(yrotrad));
    zpos += float(cos(yrotrad));
    ypos += float(sin(xrotrad));
    }
    if (key=='d')
    {
    float yrotrad;
    yrotrad = (yrot / 180 * 3.141592654f);
    xpos += float(cos(yrotrad)) * 0.2;
    zpos += float(sin(yrotrad)) * 0.2;
    }
    if (key=='a')
    {
    float yrotrad;
    yrotrad = (yrot / 180 * 3.141592654f);
    xpos -= float(cos(yrotrad)) * 0.2;
    zpos -= float(sin(yrotrad)) * 0.2;
    }
    if (key==27)
    {
    exit(0);
    }
}

void DrawFloor(void) { 

	   
       glBindTexture(GL_TEXTURE_2D,texture[0]);
       glTranslatef(0.0f,-3.0f,0.0f);

 glBegin(GL_QUADS);
       
        glTexCoord2f(100,0);
        //glColor3f(0.2f, 0.2f, 0.2f);
        glVertex3f(-100.0,-0.5, -100.0);
        glTexCoord2f(100,100);
       // glColor3f(0.4f, 0.4f, 0.4f);
        glVertex3f(-100.0,-0.5, 100.0);
        glTexCoord2f(0,100);
       // glColor3f(0.6f, 0.6f, 0.6f);
        glVertex3f(100.0, -0.5, 100.0);
        glTexCoord2f(0,0);
       // glColor3f(0.8f, 0.8f, 0.8f);
        glVertex3f(100.0, -0.5, -100.0);        
    glEnd();

}

void mouseMovement(int x, int y) {
    int diffx=x-lastx; //check the difference between the 
//current x and the last x position
    int diffy=y-lasty; //check the difference between the 
//current y and the last y position
    lastx=x; //set lastx to the current x position
    lasty=y; //set lasty to the current y position
    xrot += (float) diffy; //set the xrot to xrot with the addition
 //of the difference in the y position
    yrot += (float) diffx;    //set the xrot to yrot with the addition
 //of the difference in the x position
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA); 
    glutInitWindowSize (500, 500); 
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window"); 
    init (); 
	
    
	
	//drawSnowMan();
    glutDisplayFunc (display); 
    glutIdleFunc (display); 
    glutReshapeFunc (reshape);
    glutPassiveMotionFunc(mouseMovement); //check for mouse
// movement
    glutKeyboardFunc (keyboard); 
    glutMainLoop (); 
    return 0;
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros