Advertisement

I Hope Your Ready (Reading From a File Into Display List)

Started by November 04, 2004 08:48 PM
4 comments, last by Hola 20 years ago
Ok, so I tried to mix lesson 10 and 12, reading from a file, into a display list. Im not getting any errors (which took a whiel to get to), and I was sure it would work, but when I run the program it says its encountered an error and wants me to send a report to microsoft. I know I'm beyond my abilities... but that was the whole point of this project :D Heres the important stuff:

#include <windows.h> // Header File For Windows
#include <math.h>
#include <stdio.h>
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library

HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application

bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default

const float piover180 = 0.0174532925f;
float heading;
float xpos;
float zpos;

GLfloat yrot; // Y Rotation

GLfloat lookupdown = 0.0f;
GLfloat z=0.0f; 

GLuint texture[2]; // My Pretty Pictures
GLuint astr; // Display List
GLuint sun; // Display List

typedef struct tagVERTEX // Where The Vertex Point Go
{
float x, y, z;
float u, v;
} VERTEX;

typedef struct tagQUAD // Collects Vertex Points
{
VERTEX vertex[4];
} QUAD;

typedef struct tagSECTOR // Loads Sector
{
int numquads;
QUAD* quad;
} SECTOR;

SECTOR sector1; // Creats An Instance Of SECTOR


GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f }; // Position Of Light Source
GLfloat LightPosition[]={ 0.0f, 0.0f, -50.0f, 1.0f }; // Coords Of Light Source

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc



//------------------------------------
void readstr(FILE *f,char *string) // Reads From A File
{
do
{
fgets(string, 255, f);
} while ((string[0] == '/') || (string[0] == '\n'));// So We Dont Get The Spaces Or Comments
return;
}



//------------------------------------
void SetupWorld() // Read From Data File
{
float x, y, z, u, v; // Coord Vars
int numquads; // Number Of Quads In Total
FILE *filein;
char oneline[255];
filein = fopen("data/world.txt", "rt"); // Makes A Handle

readstr(filein, oneline);
sscanf(oneline, "NUMPOLLIES %d/n", &numquads); // Looks For How Many Quads In File

sector1.quad = new QUAD[numquads]; // New QUAD Object
sector1.numquads = numquads; 
for (int loop = 0; loop< numquads; loop++) // Start Of Loop, Collects Coords
{
for (int vert = 0; vert < 4; vert++)
{
readstr(filein,oneline);
sscanf(oneline, "%f %f %f %f %f", &x, &y, &z, &u, &v);
sector1.quad[loop].vertex[vert].x = x;
sector1.quad[loop].vertex[vert].y = y;
sector1.quad[loop].vertex[vert].z = z;
sector1.quad[loop].vertex[vert].u = u;
sector1.quad[loop].vertex[vert].v = v;
}
}
fclose(filein);
return;
}



//------------------------------------
AUX_RGBImageRec *LoadBMP(char *Filename) // Checks Textures
{
FILE *File=NULL;

if (!Filename)
{
return NULL;
}

File=fopen(Filename,"r");

if (File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}



//------------------------------------
int LoadGLTextures() // Gets Textures
{
int Status=FALSE;

AUX_RGBImageRec *TextureImage[2];

memset(TextureImage,0,sizeof(void *)*1);

if (TextureImage[0]=LoadBMP("Data/Astr.bmp")) // Texture One
{
Status=TRUE;

glGenTextures(2, &texture[0]);

glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

gluBuild2DMipmaps(GL_TEXTURE_2D, 2, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);



}

if (TextureImage[0]=LoadBMP("Data/Sun.bmp")) // Texture Two
{
Status=TRUE;

glGenTextures(2, &texture[1]);

glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

gluBuild2DMipmaps(GL_TEXTURE_2D, 2, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);


}

if (TextureImage[0]) // Free The Memory
{
if (TextureImage[0]->data)
{
free(TextureImage[0]->data);
}

free(TextureImage[0]);
}

return Status;
}



//------------------------------------
GLvoid BuildLists() // Make A BuildList
{

astr=glGenLists(2);
glNewList(astr,GL_COMPILE);
GLfloat x_m, y_m, z_m, u_m, v_m;
int numquads;
numquads = sector1.numquads;
for (int loop_m = 0; loop_m < numquads; loop_m++) // Read BuildList From File
{
glBegin(GL_QUADS);
glNormal3f ( 0.0f, 0.0f, 1.0f);
x_m = sector1.quad[loop_m].vertex[0].x;
y_m = sector1.quad[loop_m].vertex[0].y;
z_m = sector1.quad[loop_m].vertex[0].z;
u_m = sector1.quad[loop_m].vertex[0].u;
v_m = sector1.quad[loop_m].vertex[0].v;
glTexCoord2f(u_m, v_m);glVertex3f(x_m, y_m, z_m);

x_m = sector1.quad[loop_m].vertex[1].x;
y_m = sector1.quad[loop_m].vertex[1].y;
z_m = sector1.quad[loop_m].vertex[1].z;
u_m = sector1.quad[loop_m].vertex[1].u;
v_m = sector1.quad[loop_m].vertex[1].v;
glTexCoord2f(u_m, v_m);glVertex3f(x_m, y_m, z_m);

x_m = sector1.quad[loop_m].vertex[2].x;
y_m = sector1.quad[loop_m].vertex[2].y;
z_m = sector1.quad[loop_m].vertex[2].z;
u_m = sector1.quad[loop_m].vertex[2].u;
v_m = sector1.quad[loop_m].vertex[2].v;
glTexCoord2f(u_m, v_m);glVertex3f(x_m, y_m, z_m);

x_m = sector1.quad[loop_m].vertex[3].x;
y_m = sector1.quad[loop_m].vertex[3].y;
z_m = sector1.quad[loop_m].vertex[3].z;
u_m = sector1.quad[loop_m].vertex[3].u;
v_m = sector1.quad[loop_m].vertex[3].v;
glTexCoord2f(u_m, v_m);glVertex3f(x_m, y_m, z_m);

glEnd();
glEndList();
}
}




//------------------------------------
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

glViewport(0,0,width,height); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}



//------------------------------------
int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
if (!LoadGLTextures()) // Jump To Texture Loading Routine
{
return FALSE; // If Texture Didn't Load Return FALSE
}
BuildLists(); 
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
glEnable(GL_LIGHT1);
return TRUE; // Initialization Went OK
}



//------------------------------------
int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f,0.0f,-10.0f);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glCallList(astr);


return TRUE; // Keep Going
}


This is whats in the world.txt file:
NUMPOLLIES 1

// 1Sun! 
-3.0 0.0 0.0 0.0 0.0
-3.0 3.0 0.0 0.0 0.0
3.0 3.0 0.0 0.0 0.0
3.0 0.0 0.0 0.0 0.0

[Edit: Use the
 and  tags for formatting. - Oluseyi]

Any help would be great, sorry I'm such a bad coder in advance.
-Matt
[Edited by - Hola on November 5, 2004 6:39:36 PM]
The Best Is Yet To Come
You should run it in a debugger and figure out where it is crashing. It's a little unreasonable to ask us to read through your entire program without any thing other than that it crashes. It would also be nice if you put your source code in a [ source ]
[ /source ] tag (just remove the spaces).
Advertisement
Moved to NeHe.
Usually this tutorial and variations on it crash because the world.txt file is not in the correct place and the program can't find it. It runs without crashing for me, which doesn't mean there isn't a code error, just that if there is one then it's more insidious.

Enigma
Well then, I wont be posting in multiple places again, makes things veyr confusing and pisses people off. Even though I cant delete this post, I would be happy if a moderator deleted all its copies (the one moved to NeHe from begginers, and the one going in OpenGl. Wont do it again.
That was me..I'm having a bad day.

So this program really works for you, I don't understand, 3 computers I've tried, none work. The debugger tells me the problem starts right when I start reading in the coords.

[Edited by - Hola on November 5, 2004 3:37:55 PM]
The Best Is Yet To Come

This topic is closed to new replies.

Advertisement