PDA

View Full Version : Ahh! Texture rendering not working!



31337
05-05-2002, 04:22 PM
For some reason my program compiles, however it doesn't actually texture the rectangle I want it to. You see I'm writing this sailboat simulation and wanto texture the rectangle the boat is on to look like actual water, not a blue rectangle. I have a file in the same directory named suprise.bmp which is my texture.

So here's the code to my sailboat simulation so far (only 1 of the 4 files)

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

bool status;
#ifdef WIN32
#define M_PI 3.14159265
#include <windows.h>
#else
#include <sys/time.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glui.h>
#include <gl\glaux.h>

GLuint texture[1];

/** ------------------------------------------------------------------------
**
** Spanish Galleon Wind Symmulation
**
** Last Modified: 29 March 2002
** Author: Curtis Jensen and Jeff Jensen
** curtis@the-jensens.org
** jjensen@hightechhigh.org
** http://www.the-jensens.org/curtis/Development/wind_sym/
** \\|//
** (o o)
** ----------------------------------------------oOOo-(_)-oOOo-----------
**/


/**
** constants:
**/

/* title of these windows: */

const char *WINDOWTITLE = "Spanish Galleon Wind Symmulation";
const char *GLUITITLE = "Controlles";


/* what the glui package defines as true and false: */

const int GLUITRUE = true;
const int GLUIFALSE = false;


/* the escape key: */

const char ESCAPE = 0x1b;


/* initial window size: */

const int INIT_WINDOW_SIZE = 600;

/* multiplication factors for input interaction: */
/* (these are known from previous experience) */

const float ANGFACT = 1.;
const float SCLFACT = 0.005;


/* minimum allowable scale factor: */

const float MINSCALE = 0.05;


/* active mouse buttons (or them together): */

const int LEFT = 4;
const int MIDDLE = 2;
const int RIGHT = 1;


/* which projection: */

const int ORTHO = GLUIFALSE;
const int PERSP = GLUITRUE;


/* which button: */

const int RESET = 0;
const int QUIT = 1;


/* window background color (rgba): */

const float BACKCOLOR[] = { 0., 0., 0., 0. };


/* color and line width for the axes: */
const float AXES_COLOR[] = { 1., .5, 0. };
const float AXES_WIDTH = 3.;

/* the objects: */
/* this order must match the radio button order */
const int WATER = 0;
const int SHIP = 1;
const int FSAILS = 2;
const int BSAILS = 3;
const int TORUS = 4;
const int OCTAHEADRON = 5;
const int TETRAHEDRON = 6;
const int ICOSAHEDRON = 7;
const int AXIS = 8;


/* max # of objects: */
const int MAXOBJECTS = 9;


/* handy to have around: */
const int OFF = 0;
const int ON = 1;


/* convenient way to square and cube a number: */
inline int SQR( int x )
{
return x * x;
}

inline float SQR( float x )
{
return x * x;
}


inline int CUBE( int x )
{
return x * x * x;
}

inline float CUBE( float x )
{
return x * x * x;
}

/**
** non-constant global variables:
**/

int ActiveButton; /* current button that is down */
int AxesOn; /* ON or OFF */
int Debug; /* ON means print debug info */
GLUI * Glui; /* instance of glui window */
int GluiWindow; /* the glut id for the glui window */
int GrWindow; /* window id for graphics window */
int ObjectLists[MAXOBJECTS]; /* object display lists */
float RotMatrix[4][4]; /* set by glui rotation widget */
float Scale, Scale2; /* scaling factors */
float sailsAngle; /* sails rotation anlge */
float windAngle =180; /* wind angle */
int windSpeed = 5; /* wind speed */
int howFarz = 0; /* How far the ship has gone on the z axis */
float rudderAngle; /* rudder rotation anlge */
int WhichProjection; /* ORTHO or PERSP */
int Xmouse, Ymouse; /* mouse values */
float Xrot, Yrot; /* rotation angles in degrees */
float TransXYZ[3]; /* set by glui translation widgets */

/* Lighting Parameters */
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 45.0 };
GLfloat light_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat white_light[] = { 2.0, 2.0, 2.0, 1.0 };
GLfloat green_light[] = { 0.0, 1.0, 0.0, 1.0 };
GLfloat blue_light[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat red_light[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat red_g_light[] = { 0.3, 1.0, 0.3, 1.0 };


/**
** function prototypes:
**/

void Animate( void );
void Axes( float length );
void Buttons( int );
void Display( void );
void DoRasterString( float, float, float, char * );
void DoStrokeString( float, float, float, float, char * );
void InitGraphics( void );
void InitLists( void );
void InitGlui( void );
void Keyboard( unsigned char, int, int );
void MouseButton( int, int, int, int );
void MouseMotion( int, int );
void Reset( void );
void Resize( int, int );
void Sliders( int );
void Visibility( int );
float SValue( float x, float y, float z );
extern void HsvRgb( float hsv[3], float rgb[3] );
void GuCross( float [3], float [3], float [3] );
float GuUnit( float [3], float [3] );
extern GLint ship();
extern GLint frontSails();
extern GLint backSails();

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");
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;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);
if (TextureImage[0]=LoadBMP("suprise.bmp"))
{
Status=TRUE;
glGenTextures(1, &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); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR); // Linear Filtering
}

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


}


/**
** main program:
**/

int
main( int argc, char *argv[] )
{
/* turn on the glut package: */
/* (do this before checking argc and argv since it might */
/* pull some command line arguments out) */

glutInit( &argc, argv );


/* setup all the graphics stuff: */

InitGraphics();


/* create the display structures that will not change: */

InitLists();


/* init all the global variables used by Display(): */
/* this will also post a redisplay */
/* it is important to call this before InitGlui(): */
/* so that the variables that glui will control are correct */
/* when each glui widget is created */

Reset();


/* setup all the user interface stuff: */

InitGlui();


/* draw the scene once and wait for some interaction: */
/* (will never return) */

glutMainLoop();


/* this is here to make the compiler happy: */

return 0;
}

/**
** this is where one would put code that is to be called
** everytime the glut main loop has nothing to do
**
** this is typically where animation parameters are set
**
** do not call Display() from here -- let glutMainLoop() do it
**/

void
Animate( void )
{

}

/**
** glui buttons callback:
**/

void
Buttons( int id )
{
switch( id )
{
case RESET:
Reset();
break;

case QUIT:
/* gracefully close the glui window: */
/* gracefully close out the graphics: */
/* gracefully close the graphics window: */
/* gracefully exit the program: */

Glui->close();
glFinish();
glutDestroyWindow( GrWindow );
exit( 0 );
}

Glui->sync_live();
}

/**
** draw the complete scene:
**/

void
Display( void )
{
int dx, dy, d; /* viewport dimensions */
int xl, yb; /* lower-left corner of viewport */
float scale2; /* real glui scale factor */
char tmpStr[32];

if( Debug )
{
fprintf( stderr, "Display\n" );
}


/* set which window we want to do the graphics into: */

glutSetWindow( GrWindow );

/* erase the background: */
glDrawBuffer( GL_BACK );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glEnable( GL_DEPTH_TEST );

/* specify shading to be flat: */
glShadeModel( GL_SMOOTH );


/* set the viewport to a square centered in the window: */
dx = glutGet( GLUT_WINDOW_WIDTH );
dy = glutGet( GLUT_WINDOW_HEIGHT );
d = dx < dy ? dx : dy; /* minimum dimension */
xl = ( dx - d ) / 2;
yb = ( dy - d ) / 2;
glViewport( xl, yb, d, d );


/* set the viewing volume: */
/* remember that the eye is at the origin looking in -Z */
/* remember that the Z values are actually */
/* given as DISTANCES IN FRONT OF THE EYE */

glMatrixMode( GL_PROJECTION );
glLoadIdentity();

if( WhichProjection == ORTHO )
glOrtho( -3., 3., -3., 3., 0.1, 1000. );
else
gluPerspective( 90., 1., 0.1, 1000. );


/* place the object into the viewing volume: */

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( 6.1, 7.1, 9.4, 0., 0., 0., 0., 1., 0. );


/* rotate the scene: */

glRotatef( Yrot, 0., 1., 0. );
glRotatef( Xrot, 1., 0., 0. );
glMultMatrixf( (const GLfloat *) RotMatrix );


/* possibly draw the axes: */

if( AxesOn ) {
glPushMatrix();
glScalef( 3., 3., 3. );
glCallList( ObjectLists[AXIS] );
glPopMatrix();
}

/* translate the object: */
/* note the minus sign on the z value */
/* this is to make the appearance of the glui z translate */
/* widget more intuitively match the translate behavior */
howFarz+=windSpeed;
glTranslatef(0, 0, howFarz);


/* scale the scene: */

glScalef( Scale, Scale, Scale );
scale2 = 1. + Scale2; /* because glui translation starts at 0. */
if( scale2 < MINSCALE )
scale2 = MINSCALE;
glScalef( scale2, scale2, scale2 );
glEnable( GL_TEXTURE_2D );
// Draw water
glCallList( ObjectLists[WATER] );
glDisable( GL_TEXTURE_2D );
// Turn on the lights
GLfloat light_position[] = { 0.0, 10.0, 0.0, 1.0 };
GLfloat light_position2[] = { 0.0 -10.0, 0.0, 1.0 };
//GLfloat spot_direction[] = { 0.0, -1.0, 0.0 };

// blue light from bottom
glLightfv( GL_LIGHT2, GL_POSITION, light_position2 );
glLightfv( GL_LIGHT2, GL_AMBIENT, light_ambient );
glLightfv( GL_LIGHT2, GL_DIFFUSE, white_light );
// glLightf( GL_LIGHT3, GL_SPOT_CUTOFF, 30.0 );
//glLightfv( GL_LIGHT3, GL_SPOT_DIRECTION, spot_direction );
glLightfv( GL_LIGHT2, GL_SPECULAR, blue_light );

// White light from top
glLightfv( GL_LIGHT3, GL_POSITION, light_position );
glLightfv( GL_LIGHT3, GL_AMBIENT, light_ambient );
glLightfv( GL_LIGHT3, GL_DIFFUSE, white_light );
// glLightf( GL_LIGHT3, GL_SPOT_CUTOFF, 30.0 );
//glLightfv( GL_LIGHT3, GL_SPOT_DIRECTION, spot_direction );
glLightfv( GL_LIGHT3, GL_SPECULAR, white_light );

glEnable( GL_NORMALIZE );

glEnable( GL_LIGHTING );
glEnable( GL_LIGHT2 );
glEnable( GL_LIGHT3 );

// Draw ship
glPushMatrix();
// put any ship translations here


glScalef( 5.0, 5.0, 5.0 );

// put any ship rotations here

glCallList( ObjectLists[SHIP] );

glPushMatrix();
glTranslatef( 0.025, 0.18, 0.0 );
glScalef( 0.5, 0.5, 0.5 );
glRotatef( sailsAngle, 0.0, 1.0, 0.0);
glCallList( ObjectLists[BSAILS] );
glPopMatrix();

glPushMatrix();
glTranslatef( 0.21, 0.13, 0.0 );
glScalef( 0.45, 0.45, 0.45 );
glRotatef( sailsAngle, 0.0, 1.0, 0.0);
glCallList( ObjectLists[FSAILS] );
glPopMatrix();

glPopMatrix();

glDisable( GL_LIGHT3 );
glDisable( GL_LIGHT2 );
glDisable( GL_LIGHTING );


/* the projection matrix is reset to define a scene whose */
/* world coordinate system goes from 0-100 in each axis */
/* this is called "percent units", and is just a convenience */
/* the modelview matrix is reset to identity as we don't */
/* want to transform these coordinates */

glDisable( GL_DEPTH_TEST );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0., 100., 0., 100. );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f( 1., 1., 1. );
//sprintf( tmpStr, "Time: %f Frame: %d", deltat, Frame );
sprintf( tmpStr, "Wind Direction: %i deg. Wind Speed: 0MPH", windAngle );
DoRasterString( 5., 5., 0., tmpStr );


/* swap the double-buffered framebuffers: */
glutSwapBuffers();

/* be sure the graphics buffer has been sent: */
glFlush();
}

/**
** use glut to display a string of characters using a raster font:
**/

void
DoRasterString( float x, float y, float z, char *s )
{
char c; /* one character to print */

glRasterPos3f( x, y, z );
for( ; ( c = *s ) != '\0'; s++ )
{
glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_24, c );
}
}

/**
** use glut to display a string of characters using a stroke font:
**/

void
DoStrokeString( float x, float y, float z, float ht, char *s )
{
char c; /* one character to print */
float sf; /* the scale factor */

glPushMatrix();
glTranslatef( x, y, z );
sf = ht / ( 119.05 + 33.33 );
glScalef( sf, sf, sf );
for( ; ( c = *s ) != '\0'; s++ )
{
glutStrokeCharacter( GLUT_STROKE_ROMAN, c );
}
glPopMatrix();
}

/**
** initialize the glut and OpenGL libraries:
** also setup display lists and callback functions
**/

void
InitGraphics( void )
{
if( Debug )
fprintf( stderr, "InitGraphics\n" );


/* setup the display mode: */
/* ( *must* be done before call to glutCreateWindow() ) */
/* ask for color, double-buffering, and z-buffering: */

glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );


/* set the initial window configuration: */

glutInitWindowSize( INIT_WINDOW_SIZE, INIT_WINDOW_SIZE );


/* open the window and set its title: */

GrWindow = glutCreateWindow( WINDOWTITLE );
glutSetWindowTitle( WINDOWTITLE );


/* setup the clear values: */

glClearColor( BACKCOLOR[0], BACKCOLOR[1], BACKCOLOR[2], BACKCOLOR[3] );


/* setup the callback routines: */


/* DisplayFunc -- redraw the window */
/* ReshapeFunc -- handle the user resizing the window */
/* KeyboardFunc -- handle a keyboard input */
/* MouseFunc -- handle the mouse button going down or up */
/* MotionFunc -- handle the mouse moving with a button down */
/* PassiveMotionFunc -- handle the mouse moving with a button up*/
/* VisibilityFunc -- handle a change in window visibility */
/* EntryFunc -- handle the cursor entering or leaving the window */
/* SpecialFunc -- handle special keys on the keyboard */
/* SpaceballMotionFunc -- handle spaceball translation */
/* SpaceballRotateFunc -- handle spaceball rotation */
/* SpaceballButtonFunc -- handle spaceball button hits */
/* ButtonBoxFunc -- handle button box hits */
/* DialsFunc -- handle dial rotations */
/* TabletMotionFunc -- handle digitizing tablet motion */
/* TabletButtonFunc -- handle digitizing tablet button hits */
/* MenuStateFunc -- declare when a pop-up menu is in use */
/* IdleFunc -- what to do when nothing else is going on */
/* TimerFunc -- trigger something to happen every so often */

glutSetWindow( GrWindow );
glutDisplayFunc( Display );
glutReshapeFunc( Resize );
glutKeyboardFunc( Keyboard );
glutMouseFunc( MouseButton );
glutMotionFunc( MouseMotion );
glutPassiveMotionFunc( NULL );
glutVisibilityFunc( Visibility );
glutEntryFunc( NULL );
glutSpecialFunc( NULL );
glutSpaceballMotionFunc( NULL );
glutSpaceballRotateFunc( NULL );
glutSpaceballButtonFunc( NULL );
glutButtonBoxFunc( NULL );
glutDialsFunc( NULL );
glutTabletMotionFunc( NULL );
glutTabletButtonFunc( NULL );
glutMenuStateFunc( NULL );

// DO NOT SET THE GLUT IDLE FUNCTION HERE !!
// glutIdleFunc( NULL );
// let glui take care of it in InitGlui()

glutTimerFunc( 0, NULL, 0 );
}


/**
** initialize the display lists that will not change:
**/

void
InitLists( void )
{
if( Debug )
fprintf( stderr, "InitLists\n" );


/* create the objects: */
ObjectLists[ WATER ] = glGenLists( 1 );
glNewList( ObjectLists[WATER], GL_COMPILE );
glColor3f( 0.0, 0.0, 0.5 );
glPushMatrix();
glTranslatef( 0.0, -2.5, 0.0 );
//glScalef( 2000.0, 1.0, 2000.0 );
//glutSolidCube( 1.0 );
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTranslatef(0, 1, 0);
glBegin( GL_QUADS );
glTexCoord2f(0.0f, 0.0f); glVertex3f( 2000, 0, 2000 );
glTexCoord2f(1.0f, 0.0f); glVertex3f( 2000, 0, -2000);
glTexCoord2f(1.0f, 1.0f); glVertex3f( -2000, 0, -2000);
glTexCoord2f(0.0f, 1.0f); glVertex3f( -2000, 0, 2000);
glEnd();
glPopMatrix();
glEndList();


ObjectLists[TETRAHEDRON] = glGenLists( 1 );
glNewList( ObjectLists[TETRAHEDRON], GL_COMPILE );
glutWireTetrahedron();
glEndList();


ObjectLists[ICOSAHEDRON] = glGenLists( 1 );
glNewList( ObjectLists[ICOSAHEDRON], GL_COMPILE );
glutWireIcosahedron();
glEndList();


ObjectLists[ SHIP ] = ship();
ObjectLists[ FSAILS ] = frontSails();
ObjectLists[ BSAILS ] = backSails();


/* create the axes: */
ObjectLists[AXIS] = glGenLists( 1 );
glNewList( ObjectLists[AXIS], GL_COMPILE );
glColor3fv( AXES_COLOR );
glLineWidth( AXES_WIDTH );
Axes( 1.5 );
glLineWidth( 1. );
glEndList();
}


/**
** initialize the glui window:
**/

void
InitGlui( void )
{
GLUI_Panel *panel, *steeringPanel;
GLUI_Translation *trans, *scale;
GLUI_Rotation *rotate;
GLUI_Spinner *sailsSpinner, *rudderSpinner;

if( Debug )
fprintf( stderr, "InitGlui\n" );


/* setup the glui window: */

Glui = GLUI_Master.create_glui( (char *) GLUITITLE );


Glui->add_statictext( (char *) GLUITITLE );
Glui->add_separator();

//GLUI *subwin = GLUI_Master.create_glui_subwindow( GrWindow, GLUI_SUBWINDOW_RIGHT );

Glui->add_checkbox( "Axis", &AxesOn );

steeringPanel = Glui->add_panel( "Steering Angles" );
sailsSpinner = Glui->add_spinner_to_panel( steeringPanel, "Sail Angle", GLUI_SPINNER_FLOAT, &sailsAngle );
sailsSpinner->set_speed( 5.0 );
sailsSpinner->set_float_limits( -60.0, 60.0 );

rudderSpinner = Glui->add_spinner_to_panel( steeringPanel, "Rudder Angle", GLUI_SPINNER_FLOAT, &rudderAngle );
rudderSpinner->set_speed( 5.0 );
rudderSpinner->set_float_limits( -60.0, 60.0 );


panel = Glui->add_panel( "Object Transformation" );

rotate = Glui->add_rotation_to_panel( panel, "Rotation", (float *) RotMatrix );
rotate->set_spin( 0.995 );

Glui->add_column_to_panel( panel, false );
scale = Glui->add_translation_to_panel( panel, "Scale", GLUI_TRANSLATION_Y , &Scale2 );
scale->set_speed( 0.01 );

Glui->add_column_to_panel( panel, false );
trans = Glui->add_translation_to_panel( panel, "Trans XY", GLUI_TRANSLATION_XY, &TransXYZ[0] );
trans->set_speed( 0.1 );

Glui->add_column_to_panel( panel, false );
trans = Glui->add_translation_to_panel( panel, "Trans Z", GLUI_TRANSLATION_Z , &TransXYZ[2] );
trans->set_speed( 0.1 );

Glui->add_checkbox( "Debug", &Debug );


panel = Glui->add_panel( "", false );

Glui->add_button_to_panel( panel, "Reset", RESET, (GLUI_Update_CB) Buttons );

Glui->add_column_to_panel( panel, false );

Glui->add_button_to_panel( panel, "Quit", QUIT, (GLUI_Update_CB) Buttons );


/* tell glui what graphics window it needs to post a redisplay to: */

Glui->set_main_gfx_window( GrWindow );


/* set the graphics window's idle function: */

GLUI_Master.set_glutIdleFunc( NULL );
//GLUI_Master.set_glutIdleFunc( Animate );
}

/**
** the keyboard callback:
**/

void
Keyboard( unsigned char c, int x, int y )
{
if( Debug )
fprintf( stderr, "DoKeyboard: '%c' (0x%0x)\n", c, c );

switch( c )
{
case 'q':
case 'Q':
case ESCAPE:
Buttons( QUIT ); /* will not return here */
break; /* happy compiler */

case 'o':
case 'O':
WhichProjection = ORTHO;
break;

case 'p':
case 'P':
WhichProjection = PERSP;
break;

case 'w':
case 'W':
windSpeed++;
break;

case 'e':
case 'E':
windSpeed-=1;
break;

default:
fprintf( stderr, "Don't know what to do with keyboard hit:: '%c' (0x%0x)\n", c, c );
}


/* synchronize the GLUI display with the variables: */

Glui->sync_live();


/* force a call to Display(): */

glutSetWindow( GrWindow );
glutPostRedisplay();
}

/**
** called when the mouse button transitions down or up:
**/

void
MouseButton
(
int button, /* GLUT_*_BUTTON */
int state, /* GLUT_UP or GLUT_DOWN */
int x, int y /* where mouse was when button hit */
)
{
int b; /* LEFT, MIDDLE, or RIGHT */

if( Debug )
fprintf( stderr, "MouseButton: %d, %d, %d, %d\n", button, state, x, y );


/* get the proper button bit mask: */

switch( button )
{
case GLUT_LEFT_BUTTON:
b = LEFT; break;

case GLUT_MIDDLE_BUTTON:
b = MIDDLE; break;

case GLUT_RIGHT_BUTTON:
b = RIGHT; break;

default:
b = 0;
fprintf( stderr, "Unknown mouse button: %d\n", button );
}


/* button down sets the bit, up clears the bit: */

if( state == GLUT_DOWN )
{
Xmouse = x;
Ymouse = y;
ActiveButton |= b; /* set the proper bit */
}
else
ActiveButton &= ~b; /* clear the proper bit */
}

/**
** called when the mouse moves while a button is down:
**/

void
MouseMotion
(
int x, int y /* mouse coords */
)
{
int dx, dy; /* change in mouse coordinates */

if( Debug )
fprintf( stderr, "MouseMotion: %d, %d\n", x, y );


dx = x - Xmouse; /* change in mouse coords */
dy = y - Ymouse;

if( ActiveButton & LEFT )
{
Xrot += ( ANGFACT*dy );
Yrot += ( ANGFACT*dx );
}

if( ActiveButton & MIDDLE )
{
Scale += SCLFACT * (float) ( dx - dy );

/* keep object from turning inside-out or disappearing: */

if( Scale < MINSCALE )
Scale = MINSCALE;
}

Xmouse = x; /* new current position */
Ymouse = y;

glutSetWindow( GrWindow );
glutPostRedisplay();
}

/**
** reset the transformations and the colors:
**
** this only sets the global variables --
** the glut main loop is responsible for redrawing the scene
**/

void
Reset( void )
{
ActiveButton = 0;
AxesOn = GLUIFALSE;
Debug = GLUIFALSE;
Scale = 1.0;
Scale2 = 0.0; /* because add 1. to it in Display() */
WhichProjection = PERSP;

TransXYZ[0] = TransXYZ[1] = TransXYZ[2] = 0.;

glutSetWindow( GrWindow );
glutPostRedisplay();
}

/**
** called when user resizes the window:
**/

void
Resize( int width, int height )
{
if( Debug )
fprintf( stderr, "ReSize: %d, %d\n", width, height );

/* don't really need to do anything since window size is */
/* checked each time in Display(): */

glutSetWindow( GrWindow );
glutPostRedisplay();
}

/**
** return the time in floating point seconds
**/


/**
** handle a change to the window's visibility:
**/

void
Visibility
(
int state /* GLUT_VISIBLE or GLUT_NOT_VISIBLE */
)
{
if( Debug )
fprintf( stderr, "Visibility: %d\n", state );

if( state == GLUT_VISIBLE )
{
glutSetWindow( GrWindow );
glutPostRedisplay();
}
else
{
/* could optimize by keeping track of the fact */
/* that the window is not visible and avoid */
/* animating or redrawing it ... */
}
}

/* the stroke characters 'X' 'Y' 'Z' : */

static float xx[] = {
0., 1., 0., 1.
};

static float xy[] = {
-.5, .5, .5, -.5
};

static int xorder[] = {
1, 2, -3, 4
};


static float yx[] = {
0., 0., -.5, .5
};

static float yy[] = {
0., .6, 1., 1.
};

static int yorder[] = {
1, 2, 3, -2, 4
};


static float zx[] = {
1., 0., 1., 0., .25, .75
};

static float zy[] = {
.5, .5, -.5, -.5, 0., 0.
};

static int zorder[] = {
1, 2, 3, 4, -5, 6
};


/* fraction of the length to use as height of the characters: */

#define LENFRAC 0.10


/* fraction of length to use as start location of the characters: */

#define BASEFRAC 1.10


/**
** Draw a set of 3D axes:
** (length is the axis length in world coordinates)
**/

void
Axes( float length )
{
int i, j; /* counters */
float fact; /* character scale factor */
float base; /* character start location */


glBegin( GL_LINE_STRIP );
glVertex3f( length, 0., 0. );
glVertex3f( 0., 0., 0. );
glVertex3f( 0., length, 0. );
glEnd();
glBegin( GL_LINE_STRIP );
glVertex3f( 0., 0., 0. );
glVertex3f( 0., 0., length );
glEnd();

fact = LENFRAC * length;
base = BASEFRAC * length;

glBegin( GL_LINE_STRIP );
for( i = 0; i < 4; i++ )
{
j = xorder[i];
if( j < 0 )
{

glEnd();
glBegin( GL_LINE_STRIP );
j = -j;
}
j--;
glVertex3f( base + fact*xx[j], fact*xy[j], 0.0 );
}
glEnd();

glBegin( GL_LINE_STRIP );
for( i = 0; i < 5; i++ )
{
j = yorder[i];
if( j < 0 )
{

glEnd();
glBegin( GL_LINE_STRIP );
j = -j;
}
j--;
glVertex3f( fact*yx[j], base + fact*yy[j], 0.0 );
}
glEnd();

glBegin( GL_LINE_STRIP );
for( i = 0; i < 6; i++ )
{
j = zorder[i];
if( j < 0 )
{

glEnd();
glBegin( GL_LINE_STRIP );
j = -j;
}
j--;
glVertex3f( 0.0, fact*zy[j], base + fact*zx[j] );
}
glEnd();

}


void
GuCross( float v1[3], float v2[3], float vout[3] )
{

float tmp[3];

tmp[0] = (v1[1] * v2[2]) - (v2[1] * v1[2]);
tmp[1] = (v2[0] * v1[2]) - (v1[0] * v2[2]);
tmp[2] = (v1[0] * v2[1]) - (v2[0] * v1[1]);

vout[0] = tmp[0];
vout[1] = tmp[1];
vout[2] = tmp[2];
}

/* unitize a vector -- return the vector's original length: */

float
GuUnit( float vin[3], float vout[3] )
{

float len;

len = sqrt(pow(vin[0],2) + pow(vin[1],2) + pow(vin[2],2));

vout[0] = vin[0] / len;
vout[1] = vin[1] / len;
vout[2] = vin[2] / len;

return len;
}


That's the end of the code right there. So folks any idea why it isn't texturing my rectangle? Thanks f0lks

05-06-2002, 01:44 AM
Nice messy code, mate. http://www.opengl.org/discussion_boards/ubb/wink.gif I didn't really look at much of it, but make sure your texture has dimensions of a power of two- ie. 16 * 16, or 256 * 256, or 1024 * 256, etc etc. OpenGL won't properly load the texture if it's not in these dimensions.

31337
05-06-2002, 08:54 AM
Is that so... thanks for your help dude I'll double check that when I get H0ME

Omaha
05-06-2002, 09:20 AM
I paged through the code but ran short on time. You seem to be using display lists for everything, which is all well and good; make sure you upload your textures *before* creating and uploading the display lists so that the texture numbers are correctly stored.

Also, as said earlier, ensure your texture image has dimensions that are powers of 2.

31337
05-06-2002, 10:15 AM
I'm having a little problem understanding what you mean by upload. Remember I'm a n00b

Omaha
05-06-2002, 12:37 PM
"Uploading" textures occurs when you call glTexImage2D or an equivalent.

"Uploading" a display list entails a glNewList() and glEndList() and everything between.

31337
05-11-2002, 02:02 PM
Ok so here's the deal, I checked the image and it was NOT to the powers of 2 however I changed it, and it still doesn't work!!! I'm really confused about what you mean by upload. This is my first time texturing something so forgive me.

I also wanto play a sound, any recommendations? I have a lead now, it tells me when I compile in metroworks codewarrior:

Warning: Possible unwanted assignment line 241

WHICH IS:

if (TextureImage[0]=LoadBMP("suprise.bmp"))


I tried making it:

if (TextureImage[0]==LoadBMP("suprise.bmp"))

and the warning went away but it still wouldn't texture.

Thanks!

Miguel_dup1
05-11-2002, 04:47 PM
you want to do something like this

if ( (TextureImage[0]=LoadBMP("suprise.bmp")) == NULL)
{
//you did not load the texture
}

that is equivalen to

TextureImage[0]=LoadBMP("suprise.bmp");
if( TextureImage[0]== NULL )
{
// You did not load the image.
}

31337
05-16-2002, 11:12 AM
Aight I'll try that out dude, thanks a bunch

31337
05-17-2002, 04:24 PM
I tried that out - still no go. Does anybody know if its a possibility that my texturing functions just arn't being called? I'll check up on it right now

Penance
05-17-2002, 07:10 PM
Your texture coordinates seem to be incorrect. 0,0 is the bottom-left of your ocean square in texture space. 1,1 is the top-right.

so 2000,0,2000 would be the bottom-right of your square and the texture coordinates should be (1.0f,0.0f)...you have (0.0f, 0.0f). I won't bother checking the rest, so try fixing those up and see if it works.