disappearing floors, probably a easy fix

Hey, I have been making a system for 3d fps game but I have hit a glitch and I can’t seem to figure it out. I have tried a bunch of things to try and figure the mystery out but I haven’t had much luck yet. So far on my system I have a simple projection and a bunch of functions for drawing shapes. When I setup the shapes and run the program, depending on the cameras coordinates and other variables the floor disappears. If anyone sees any other issues PLEASE let me know. Well this is what I have (I am sorry to post my entire source code, but I can’t pinpoint the error to one exact location):


#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam);
void EnableOpenGL(HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);
void WorldOpenGL(HDC hDC);
void ProjectionOpenGL(int width, int height);
void BuildCube(float x1, float y1, float z1, float x2, float y2, float z2);
void BuildPoint(float x1, float y1, float z1, float size);
void BuildLine(float x1, float y1, float z1, float x2, float y2, float z2);
void BuildPrism(float x1, float y1, float y2, float z1);
void BuildFlat(float x1, float y1, float z1, float x2, float y2, float z2, int pos);
void BuildGrid(float width, float height, float units);

//window global varibles
long WndWidth=500;
long WndHeight=500;
//camera global varibles
float cord_x=0.0; //camera x
float cord_y=0.0; //camera y
float cord_z=0.0; //camera z

float yaw=0.0;  //direction
float pitch=0.0;  //zdirection
float roll=0.0;  //spin

int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow)
{
    WNDCLASS wc; //window API class
    HWND hWnd; //gets the handle of the message window
    HDC hDC; //Device Context
    HGLRC hRC; //Rending Context       
    MSG msg; //window message
    BOOL bQuit = FALSE; //if false program is running, if true program is quitting

    wc.style = CS_OWNDC; //defines additional elements of the window class
    wc.lpfnWndProc = WndProc; //pointer to window procedure
    wc.cbClsExtra = 0; //number of extra bits for the wndclass structor
    wc.cbWndExtra = 0; //number of extra bits for the wndinstance
    wc.hInstance = hInstance; //handle for wnd procedure
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); //handle to the class icon
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); //handle to the class cursor
    wc.hbrBackground = (HBRUSH) GetSysColorBrush (COLOR_WINDOW);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "WndConfig"; //name the current window class
    RegisterClass(&wc);

    hWnd = CreateWindow("WndConfig", "WndConfig", WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, 0, 0, WndWidth, WndHeight, NULL, NULL, hInstance, NULL);

    EnableOpenGL(hWnd, &hDC, &hRC);
    ProjectionOpenGL(WndWidth,WndHeight);

    while (!bQuit) //program loop
    {
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage(&msg); //translates vitual-key messages into chracter messages
                DispatchMessage(&msg); //dispatchs a message to a window procedure
            }
        }
        else
        {
            WorldOpenGL(hDC);
        }
    }

    DisableOpenGL(hWnd, hDC, hRC);
    DestroyWindow(hWnd); //destroys current window
    return msg.wParam;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HDC hDC;
    static HGLRC hRC;
    
    switch(message)
    {
    case WM_CREATE:
        hDC = GetDC(hWnd);
        hRC = wglCreateContext(hDC);
        wglMakeCurrent(hDC, hRC);
        return 0;
    case WM_CLOSE:
        wglMakeCurrent(hDC, hRC);
        wglDeleteContext(hRC);
        PostQuitMessage(0);
        return 0;

    case WM_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch(wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        }
        return 0;

    default:
        return DefWindowProc (hWnd, message, wParam, lParam); //default window procedure
    }
}

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int PixelFormat;

    *hDC = GetDC (hWnd);

    ZeroMemory(&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 24;
    pfd.iLayerType = PFD_MAIN_PLANE;
    PixelFormat = ChoosePixelFormat(*hDC, &pfd);
    SetPixelFormat(*hDC, PixelFormat, &pfd);

    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hRC);
    ReleaseDC(hWnd, hDC);
    
}

void WorldOpenGL(HDC hDC)
{         
     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
     glEnable(GL_DEPTH_TEST);
     glEnable(GL_POLYGON_SMOOTH);
     
     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
     
     gluLookAt(cord_x,cord_y+5.0f,cord_z,25.0f,0.0f,0.0f,0.0f,1.0f,0.0f); //y=verical height, x and z are grid points
     
     //glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); //gl_line for wireframe, gl_fill for normal

     BuildPoint(-25.0f,1.0f,-25.0f,3);
     BuildPoint(-25.0f,1.0f,25.0f,3);
     BuildPoint(25.0f,1.0f,25.0f,3);
     BuildPoint(25.0f,1.0f,-25.0f,3);
     
     BuildFlat(-25.0f,0.0f,-25.0f,25.0f,0.05f,25.0f,1); 
              
     SwapBuffers(hDC);      
}
            
void ProjectionOpenGL(int width, int height)
{
     if (height == 0)
     {
          height=1;
     }
     
     glViewport(0,0,width,height);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     gluPerspective(45.0f,width/height,1.0f,10000.0f);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
}

void BuildCube(float x1, float y1, float z1, float x2, float y2, float z2)
{
     glColor3f(1.0f, 1.0f, 1.0f);
     //bottom
     glBegin(GL_POLYGON);
     glVertex3f(x1, y1, z1); glVertex3f(x1, y1, z2); glVertex3f(x2, y1, z2); glVertex3f(x2, y1, z1);    
     glEnd();
     //top
     glBegin(GL_POLYGON);
     glVertex3f(x1, y2, z1); glVertex3f(x1, y2, z2); glVertex3f(x2, y2, z2); glVertex3f(x2, y2, z1);
     glEnd();  
     //sides
     glBegin(GL_POLYGON);
     glVertex3f(x1, y1, z1); glVertex3f(x2, y1, z1); glVertex3f(x2, y2, z1); glVertex3f(x1, y2, z1);
     glEnd(); 
     glBegin(GL_POLYGON); 
     glVertex3f(x1, y1, z2); glVertex3f(x2, y1, z2); glVertex3f(x2, y2, z2); glVertex3f(x1, y2, z2);
     glEnd(); 
     glBegin(GL_POLYGON); 
     glVertex3f(x1, y1, z1); glVertex3f(x1, y1, z2); glVertex3f(x1, y2, z2); glVertex3f(x1, y2, z1);
     glEnd();  
     glBegin(GL_POLYGON);
     glVertex3f(x2, y1, z1); glVertex3f(x2, y1, z2); glVertex3f(x2, y2, z2); glVertex3f(x2, y2, z1);
     glEnd(); 
}

void BuildPoint(float x1, float y1, float z1, float size)
{
     glColor3f(1.0f, 1.0f, 1.0f);
     glPointSize(size);
     glBegin(GL_POINTS);
     glVertex3f(x1, y1, z1);
     glEnd();
}

void BuildLine(float x1, float y1, float z1, float x2, float y2, float z2)
{
     glColor3f(1.0f, 1.0f, 1.0f);
     glBegin(GL_LINES);
     glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2);
     glEnd();
} 

void BuildPrism(float x1, float y1, float y2, float z1)
{
     float w = (y1 -= y2);
    
     glColor3f(1.0f, 1.0f, 1.0f);
     glBegin(GL_TRIANGLES);
     //side1
     glVertex3f(x1-w, y1, z1-w); glVertex3f(x1, y2, z1); glVertex3f(x1+w, y1, z1-w);
     glEnd();
     //side2
     glBegin(GL_TRIANGLES);
     glVertex3f(x1-w, y1, z1+w); glVertex3f(x1, y2, z1); glVertex3f(x1+w, y1, z1+w);                  
     glEnd();
     //side3
     glBegin(GL_TRIANGLES);
     glVertex3f(x1-w, y1, z1-w); glVertex3f(x1, y2, z1); glVertex3f(x1-w, y1, z1+w);
     glEnd();  
     //side2
     glBegin(GL_TRIANGLES);
     glVertex3f(x1+w, y1, z1-w); glVertex3f(x1, y2, z1); glVertex3f(x1+w, y1, z1-w);                  
     glEnd();  
     //bottom              
     glBegin(GL_QUADS);
     glVertex3f(x1-w, y1, z1-w); glVertex3f(x1+w, y1, z1-w); glVertex3f(x1+w, y1, z1+w); glVertex3f(x1-w, y1, z1+w);
     glEnd(); 
}

void BuildFlat(float x1, float y1, float z1, float x2, float y2, float z2, int pos)
{
     glColor3f(0.5f, 0.5f, 0.5f);
     glBegin(GL_POLYGON);
     if (pos==1)
     {
     glVertex3f(x1, y1, z1); glVertex3f(x1, y1, z2); glVertex3f(x2, y2, z2); glVertex3f(x2 ,y2 ,z1);
     }
     else if (pos==2)
     {
     glVertex3f(x1, y1, z1); glVertex3f(x2, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x1, y2, z2);
     }
     glEnd();
}

void BuildGrid(float width, float height, float units)
{ 
}

I am 90% sure that the error has nothing to do with the drawing functions, I believe it has to do with gluLookAt or some other function like that.

What do you by “the floor disappears”? Does it disappear completely instantaneously or does it flicker? When you go back with camera does the plane reappear?

Some hints:
you can draw more than one polygon, points, quad,… between glBegin and glEnd calls, this way your code is more readable and you will gain performance a little.
It is the same with the glEnable calls, until you call glDisable with the same parameter the state won’t change, so you can move some of these calls to your initialization function.

One last thing, your zfar value that you set in parameter to the glPerspective function is quite huge. You should try a smaller value if you can.

Well see I don’t know if my window is rendering in real time correctly. I mean it should be if I did everything correctly, but see I am new to the whole window setup thing so I could easily have made a mistake (maybe a major one). This is what I notice about the floor, I know that the coordinates are correct and everything, but depending on where the cameras x,y,z is it can disappear from site. I am pretty sure gluLookAt is messed up, but I don’t think that is the main issue with this right now.

What I know: I have the program setup to draw a simple 2d floor and have a simple point at every corner. Well I was trying to test the camera so I would move the camera around (stopping the program, change a variable then start again. Whenever I changed something like the cameras location I had to stop and start the program) and I would notice that the sometimes the floor would be completely gone when it should be right in front of me. I think the same problem applied to the points (I don’t know about this but it may be true).

What I need to know: Does this issue have anything to do with my window rendering system or is it something else like a wrong function. Is everything else about my program ok (any issues or fatal errors I should know about)?

Edit: ok I was just messing around with my program and it seems that gluLookAt is defiantly wrong (but I am sure that it isn’t the cause of the bigger issues). The whole coordinate system is messed up, I tryed putting some random numbers down for cord_z of the camera and instead of moving the camera back or forward it twisted the camera. I have no idea y this is happening but something in this code is messing up a bunch of things.

Your problem is very confusing… maybe you should restart with a minimal code and then add “functionaliities” progressively and see what does not work.

I don’t understand what you mean by “having to stop/start the program” whenever something change…
Iff you have problems with setting up the GL rendering context with windows, I can’t help you.
When I had to do some gl applications on windows I have used the NeHe’s code in this tutorial:

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=01

Well after further research and testing I found what the main issue was. gluLookAt was set to look right onto of itself (as in its coordinates were on 0,0 and it was told to look at 0,0). But I think I got that much fixed now. Think that was the only issue I was having, unless someone sees a problem with what I have done so far.