OpenGL i Dev-C++ HJÆLP
Jeg har fået fat i noget source-code til et program, hvor man skal bruge SDL. Jeg har installeret SDL helt korrekt (har fuldt punkt og prikke i manualen til hvordan man installerer SDL).Men jeg får hele tiden fejlen:
Undefined reference to 'GluPerspective@32'
Koden ser således ud:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <windows.h>
#include <gl/gl.h> // Header File For The OpenGL32 Library
#include <gl/glu.h> // Header File For The GLu32 Library
#define SCREEN_WIDTH 800 // We want our screen width 800 pixels
#define SCREEN_HEIGHT 600 // We want our screen height 600 pixels
#define SCREEN_DEPTH 16 // We want 16 bits per pixel
#include "SDL.h" // The SDL Header File
int VideoFlags = 0; // Video Flags for the Create Window function
SDL_Surface * MainWindow = NULL; // drawing surface on the SDL window
// This is our main rendering function prototype. It's up here for now so MainLoop() can call it.
void RenderScene();
// This Quits SDL
void Quit(int);
/////////////////////////////////// TOGGLE FULL SCREEN \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
///////
/////// This function TOGGLES between FULLSCREEN and WINDOWED mode
///////
/////////////////////////////////// TOGGLE FULL SCREEN \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void ToggleFullScreen(void)
{
if(SDL_WM_ToggleFullScreen(MainWindow) == 0) // try to toggle fullscreen mode for window 'MainWindow'
{
cerr << "Failed to Toggle Fullscreen mode : " << SDL_GetError() << endl; // report error in case toggle fails
Quit(0);
}
}
/////////////////////////////// CREATE MY WINDOW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
////////
//////// This function CREATES our WINDOW for drawing the GL stuff
////////
/////////////////////////////// CREATE MY WINDOW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void CreateMyWindow(const char * strWindowName, int width, int height, int VideoFlags)
{
MainWindow = SDL_SetVideoMode(width, height, SCREEN_DEPTH, VideoFlags); // SCREEN_DEPTH is macro for bits per pixel
if( MainWindow == NULL ) // if window creation failed
{
cerr << "Failed to Create Window : " << SDL_GetError() << endl; // report error
Quit(0);
}
SDL_WM_SetCaption(strWindowName, strWindowName); // set the window caption (first argument) and icon caption (2nd arg)
}
///////////////////////////// SETUP PIXEL FORMAT \\\\\\\\\\\\\\\\\\\\\\\\\\\\*
///////
/////// Sets the pixel format for openGL and video flags for SDL
///////
///////////////////////////// SETUP PIXEL FORMAT \\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void SetupPixelFormat(void)
{
//////// SURFACE IS THE DRAWABLE PORTION OF AN SDL WINDOW \\\\\\\\*
///////////// we set the common flags here
VideoFlags = SDL_OPENGL; // it's an openGL window
VideoFlags |= SDL_HWPALETTE; // exclusive access to hardware colour palette
VideoFlags |= SDL_RESIZABLE; // the window must be resizeable
const SDL_VideoInfo * VideoInfo = SDL_GetVideoInfo(); // query SDL for information about our video hardware
if(VideoInfo == NULL) // if this query fails
{
cerr << "Failed getting Video Info : " << SDL_GetError() << endl; // report error
Quit(0);
}
///////////// we set the system dependant flags here
if(VideoInfo -> hw_available) // is it a hardware surface
VideoFlags |= SDL_HWSURFACE;
else
VideoFlags |= SDL_SWSURFACE;
// Blitting is fast copying / moving /swapping of contiguous sections of memory
// for more about blitting check out : http://www.csc.liv.ac.uk/~fish/HTML/blitzman/bm_blitter.html
if(VideoInfo -> blit_hw) // is hardware blitting available
VideoFlags |= SDL_HWACCEL;
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // tell SDL that the GL drawing is going to be double buffered
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, SCREEN_DEPTH); // size of depth buffer
SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0); // we aren't going to use the stencil buffer
SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0); // this and the next three lines set the bits allocated per pixel -
SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0); // - for the accumulation buffer to 0
SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0);
SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0);
}
//////////////////////////// RESIZE OPENGL SCREEN \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This function resizes the viewport for OpenGL.
/////
//////////////////////////// RESIZE OPENGL SCREEN \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void SizeOpenGLScreen(int width, int height) // Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero error
{
height=1; // Make the Height Equal One
}
glViewport(0,0,width,height); // Make our viewport the whole window
// We could make the view smaller inside
// Our window if we wanted too.
// The glViewport takes (x, y, width, height)
// This basically means, what our drawing boundries
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
// The parameters are:
// (view angle, aspect ration of the width to the height,
// The closest distance to the camera before it clips,
// FOV // Ratio // The farthest distance before it stops drawing)
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, 1 ,150.0f);
// * Note * - The farthest distance should be at least 1 if you don't want some
// funny artifacts when dealing with lighting and distance polygons. This is a special
// thing that not many people know about. If it's less than 1 it creates little flashes
// on far away polygons when lighting is enabled.
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
//////////////////////////////// INITIALIZE GL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This function handles all the initialization for openGL
/////
//////////////////////////////// INITIALIZE GL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void InitializeOpenGL(int width, int height)
{
SizeOpenGLScreen(width, height); // resize the OpenGL Viewport to the given height and width
}
///////////////////////////////// INIT GAME WINDOW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This function initializes the game window.
/////
///////////////////////////////// INIT GAME WINDOW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void Init()
{
InitializeOpenGL(SCREEN_WIDTH,SCREEN_HEIGHT); // Initialize openGL
// *Hint* We will put all our game init stuff here
// Some things include loading models, textures and network initialization
}
////////////////////////////////// HANDLE KEY PRESS EVENT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
//////
////// This function handles the keypress events generated when the user presses a key
//////
////////////////////////////////// HANDLE KEY PRESS EVENT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void HandleKeyPressEvent(SDL_keysym * keysym)
{
switch(keysym -> sym) // which key have we got
{
case SDLK_F1 : // if it is F1
ToggleFullScreen(); // toggle between fullscreen and windowed mode
break;
case SDLK_ESCAPE: // if it is ESCAPE
Quit(0); // quit after cleaning up
default: // any other key
break; // nothing to do
}
}
////////////////////////////// MAIN GAME LOOP \\\\\\\\\\\\\\\\\\\\\\\\\\\\*
//////
////// This function handles the main game loop
//////
////////////////////////////// MAIN GAME LOOP \\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void MainLoop(void)
{
bool done = false; // is our job done ? not yet !
SDL_Event event;
while(! done) // as long as our job's not done
{
while( SDL_PollEvent(& event) ) // look for events (like keystrokes, resizing etc.)
{
switch ( event.type ) // what kind of event have we got ?
{
case SDL_QUIT : // if user wishes to quit
done = true; // this implies our job is done
break;
case SDL_KEYDOWN : // if the user has pressed a key
HandleKeyPressEvent( & event. key.keysym ); // callback for handling keystrokes, arg is key pressed
break;
case SDL_VIDEORESIZE : // if there is a resize event
// request SDL to resize the window to the size and depth etc. that we specify
MainWindow = SDL_SetVideoMode( event.resize.w, event.resize.h, SCREEN_DEPTH, VideoFlags );
SizeOpenGLScreen(event.resize.w, event.resize.h); // now resize the OpenGL viewport
if(MainWindow == NULL) // if window resize has failed
{
cerr << " Failed resizing SDL window : " << SDL_GetError() << endl; // report error
Quit(0);
}
break;
default: // any other event
break; // nothing to do
} // switch
} // while( SDL_ ...
RenderScene(); // draw our OpenGL scene
} // while( ! done)
}
////////////////////////////// MAIN \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
//////
////// create the window and calling the initialization functions
//////
////////////////////////////// MAIN \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
int main(void)
{
// print user instructions
cout << " Hit the F1 key to Toggle between Fullscreen and windowed mode" << endl;
cout << " Hit ESC to quit" << endl;
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) // try to initialize SDL video module
{
cerr << "Failed initializing SDL Video : " << SDL_GetError() << endl; // report error if it fails
return 1; // we use this instead of Quit because SDL isn't yet initialized
}
// Set up the format for the pixels of the OpenGL drawing surface
SetupPixelFormat();
// Create our window, we pass caption for the window, the width, height and video flags required
CreateMyWindow("www.GameTutorials.com - First OpenGL Program", SCREEN_WIDTH, SCREEN_HEIGHT, VideoFlags);
// Initializes our OpenGL drawing surface
Init();
// Run our message loop
MainLoop();
// quit main returning success
return 0;
}
///////////////////////////////// RENDER SCENE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This function renders the entire scene.
/////
///////////////////////////////// RENDER SCENE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void RenderScene()
{
int i=0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glColor3f ( 1.0, 1.0, 1.0 );
SDL_GL_SwapBuffers(); // Swap the backbuffers to the foreground
}
////////////////////////////// QUIT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
//////
////// This will shutdown SDL and quit the program
//////
////////////////////////////// QUIT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void Quit(int ret_val)
{
SDL_Quit(); // shuts down SDL stuff
exit(ret_val); // quit the program
}