#include <GL/glut.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <math.h>
#include <iostream.h>
#include "image.h"
int width=500;
int height=500;
GLfloat spin = 0.0;
int prevmx = 0;
int prevmy = 0;
GLfloat increment = 0.05f;
const float piover180 = 0.0174532925f;
float heading;
float xpos;
float zpos;
int cheat=0;
GLfloat yrot; // Y Rotation
GLfloat walkbounce = 0;
GLfloat walkbounceangle = 0;
GLfloat lookupdown = 180.0f;
int minvert = -1; //mouse inverts if you hit I
int freemouse=0;
GLuint texture[1];
#define MAZE_HEIGHT (16)
#define MAZE_WIDTH (16)
char *mazedata[MAZE_HEIGHT] = {
"****************",
"*X * *",
"* * *** * * *",
"* ** * ** * * *",
"* * * *",
"********** *** *",
"* * *",
"* ***** *** ****",
"* * * * *",
"* ******* *",
"* * * * * *",
"* ***** **** * *",
"* * * *",
"** ** **** *** *",
"* * * * *",
"************* **",
};
void LoadGLTextures();
//strings in 3d position
void draw_string_bitmap(void *font, const char* string, int x, int y)
{
glRasterPos3f(x+0.3, 0.4f, y+0.5);
while (*string)
glutBitmapCharacter(font, *string++);
}
void buildLists(){
//stored list of things to always be drawn that never change
#define MAZE_LIST 1
glNewList(MAZE_LIST, GL_COMPILE);
glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT);
glLineWidth(2.0);
for(int y=0;y<MAZE_HEIGHT;y++) {
for(int x=0;x<MAZE_WIDTH;x++) {
if(mazedata[x][y]==''*'') {
glColor3f(0.5,0.5,0.5);
glBegin(GL_QUAD_STRIP);
glVertex3f( x , 0, y);
glVertex3f( x , 1, y);
glVertex3f( x+1, 0, y);
glVertex3f( x+1, 1, y);
glVertex3f( x+1, 0, y+1);
glVertex3f( x+1, 1, y+1);
glVertex3f( x , 0, y+1);
glVertex3f( x , 1, y+1);
glVertex3f( x , 0, y);
glVertex3f( x , 1, y);
glEnd();
}
else if(mazedata[x][y]==''X''){
//cout << x << " " << y << endl;
xpos=-x-0.5;
zpos=-y-0.5;
//cout << xpos << " " << zpos << endl;
glColor3f(0.75,0.5,0.5);
glBegin(GL_QUADS);
// glTexCoord2f(0.0 , 0.0);
glVertex3f( x , 1, y);
// glTexCoord2f(0.0 , 1.0);
glVertex3f( x+1, 1, y);
// glTexCoord2f(1.0 , 1.0);
glVertex3f( x+1, 1, y+1);
// glTexCoord2f(1.0 , 0.0);
glVertex3f( x, 1, y+1);
glEnd();
}
else if(mazedata[x][y]=='' ''){
glColor3f(0.5,0.5,0.75);
// glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0 , 0.0);
glVertex3f( x , 1, y);
glTexCoord2f(0.0 , 1.0);
glVertex3f( x+1, 1, y);
glTexCoord2f(1.0 , 1.0);
glVertex3f( x+1, 1, y+1);
glTexCoord2f(1.0 , 0.0);
glVertex3f( x, 1, y+1);
glEnd();
// glDisable(GL_TEXTURE_2D);
}
else if(mazedata[x][y]==''1''){
glColor3f(1.0 , 0.0 , 0.0);
draw_string_bitmap("fixed", "Lost?", x,y);
}
}
}
glPopAttrib();
glEndList();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
GLfloat ypos = -0.65-walkbounce;
GLfloat sceneroty = 360.0f - yrot;
glRotatef(lookupdown,1.0f,0,0);
glRotatef(sceneroty,0,1.0f,0);
glTranslatef(xpos, ypos+cheat, zpos);
glCallList(MAZE_LIST);
glEnable(GL_TEXTURE_2D);
glutSwapBuffers();
}
void click_mouse(int button, int state, int x, int y)
{
//mouse buttons if state==GLUT_DOWN && button== GLUT_RIGHT_BUTTON...
}
void move_mouse(int x, int y)
{
/*annoying, but works
if(x>width-25 || x<25 || y<25 || y>height-25)
if(!freemouse)
glutWarpPointer(width/2,height/2);
if(y<prevmy){
lookupdown += 1.0f * minvert;
}
else if(y>prevmy){
lookupdown -= 1.0f * minvert;
}
if(x<prevmx){
heading -= 2.0f;
yrot = heading;
}
else if(x>prevmx){
heading += 2.0f;
yrot = heading;
}
prevmy=y;
prevmx=x;
display(); //force redraw
*/
}
void nav_kbd(unsigned char key, int x, int y)
{
if(key == ''w'')
{
xpos -= sin(heading*piover180)*0.1;
zpos -= cos(heading*piover180)*0.1;
if (walkbounceangle >= 359.0f)
walkbounceangle = 0.0f;
else
walkbounceangle += 10;
walkbounce = sin(walkbounceangle*piover180)/7.5;
}
if(key == ''s'')
{
xpos += sin(heading*piover180)*0.1f;
zpos += cos(heading*piover180)*0.1f;
if (walkbounceangle <= 1.0f)
walkbounceangle = 359.0f;
else
walkbounceangle-= 10;
walkbounce = sin(walkbounceangle*piover180)/7.5;
}
if(key == ''a'')
{
heading -= 2.5f;
yrot = heading;
}
if(key == ''d'')
{
heading += 2.5f;
yrot = heading;
}
if(key == ''i'')
{
minvert*=-1;
}
if(key == ''t'')
{
exit(0);
}
if(key == ''c'')
{
++cheat;
}
if(key == ''v'')
{
--cheat;
}
glutPostRedisplay();
}
void idle(){
glutPostRedisplay() ;
}
void reshape(int w, int h){
glViewport(0,0,(GLsizei)w,(GLsizei)h) ;
glMatrixMode(GL_PROJECTION) ;
glLoadIdentity() ;
gluPerspective(60.0,(GLfloat)w/(GLfloat)h,0.1,10.0) ;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
LoadGLTextures();
buildLists();
glutPostRedisplay();
}
void LoadGLTextures() {
Image *image1;
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
cout << "Error allocating space for image";
exit(0);
}
if (!ImageLoad("stone.bmp", image1)) {
exit(1);
}
/* Create Texture *****************************************/
glGenTextures(2, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]); /* 2d texture (x and y size)*/
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); /* scale linearly when image bigger than texture*/
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); /* scale linearly when image smalled than texture*/
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
free(image1->data);
free(image1);
};
void myinit()
{
//glShadeModel(GL_SMOOTH);
//glEnable(GL_CULL_FACE);
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
LoadGLTextures();
buildLists();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv) ;
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) ;
glutGameModeString("800x600:16@60") ;
if (glutGameModeGet(GLUT_GAME_MODE_WIDTH) != -1) // if the width is different to -1
{
myinit();
glutEnterGameMode(); // enter full screen mode
glutDisplayFunc(display) ;
glutReshapeFunc(reshape) ;
glutIdleFunc(idle) ;
glutKeyboardFunc(nav_kbd) ;
}
else // print out that this mode is not available
cout << "Chances are you don''t have glut 3.7 from xmission.com/~nate/glut.html \n";
glutMainLoop() ;
}
help w/ crash since glut 3.7
I made the jump to glut 3.7 so I could try out their new fullscreen easy switching. Problem is that now I get a crash after everytime it executes, and after that crash I have to reboot before I can try again.
This is my frist day trying opengl (yes I''ve been up for a lot of hours)
Any general advice and extra help would be greatly appreciated as well.
You should create a window with glutCreateWindow() before trying to register callbacks. That''s because callbacks are registered for a specific window (the last created or the one you last glutSetWindow()''ed ). Since you have no window yet...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement