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.