Need help starting an fps

Started by
22 comments, last by pheonix2468 19 years, 2 months ago
Thanks. It compile and runs but i cant see anything never have been able to maybe its just me heres the code laugh at it all you like:



#include <windows.h>
#include <cmath>
#include <gl/glaux.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);

int keys[256];
int camx;
int camy;
int camz;
float camang;


int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;


wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);


hWnd = CreateWindow (
"GLSample", "Coridoor",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 1024, 768,
NULL, NULL, hInstance, NULL);


EnableOpenGL (hWnd, &hDC, &hRC);


while (!bQuit)
{

if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{

if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{

glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glMatrixMode (GL_MODELVIEW);
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glRotatef (-camang, 0, 1, 0);
glTranslatef (-camx, -camy, -camz);
glPushMatrix ();

// left wall
glBegin (GL_QUADS);
glColor3f(0, 0, 1); glVertex3f( -5, 0, 0);
glColor3f(0, 0, 1); glVertex3f( -5, 10, 0);
glColor3f(0, 0, 1); glVertex3f( -5, 10,-50);
glColor3f(0, 0, 1); glVertex3f( -5, 0,-50);
glEnd ();
//Top wall
glBegin (GL_QUADS);
glColor3f ( 1, 0, 0); glVertex3f ( 5, 0, 0);
glColor3f ( 1, 0, 0); glVertex3f ( 5, 10, 0);
glColor3f ( 1, 0, 0); glVertex3f ( 5, 10,-50);
glColor3f ( 1, 0, 0); glVertex3f ( 5, 0,-50);
glEnd ();
// Floor
glBegin (GL_QUADS);
glColor3f ( 0, 1, 0); glVertex3f ( 5, 0, 0);
glColor3f ( 0, 1, 0); glVertex3f ( -5, 0, 0);
glColor3f ( 0, 1, 0); glVertex3f ( -5, 0,-50);
glColor3f ( 0, 1, 0); glVertex3f ( 5, 0,-50);
glEnd ();
// ceiling
glBegin (GL_QUADS);
glColor3f ( 1, 0, 0); glVertex3f ( 5, 10, 0);
glColor3f ( 0, 1, 0); glVertex3f ( -5, 10, 0);
glColor3f ( 0, 0, 1); glVertex3f ( -5, 10,-50);
glColor3f ( 0, 1, 0); glVertex3f ( 5, 10,-50);
glEnd ();
glPopMatrix ();

// Move camera

if (keys['A']);
{
camx = camx + 1;
}
if (keys['D']);
{
camx = camx - 1;
}
if (keys['W']);
{
camx = camx - sin (camang) * 0.2;
camz = camz - cos (camang) * 0.2;

}
if (keys['S']);
{
camx = camx + sin (camang) * .2;
camz = camz + cos (camang) * .2;
}
camx = 0;
camy = 2;
camz = 0;
SwapBuffers (hDC);
Sleep (1);
}
}


DisableOpenGL (hWnd, hDC, hRC);

DestroyWindow (hWnd);

return msg.wParam;
}




LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{

switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
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);
}
}

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


*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 = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);


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

}




void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}
Advertisement
The first thing you need to do is add [_source_] [/_source_] tags (without the underscores) to that code, people dont like code the way youve pasted it, plus it preserves tabs and is syntax highlighted, once you add the source tags people will be more likely to take a look at it

hope that helps
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
ok
#include <windows.h>#include <cmath>#include <gl/glaux.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);int keys[256];int camx;int camy;int camz;float camang;int WINAPI WinMain (HINSTANCE hInstance,                    HINSTANCE hPrevInstance,                    LPSTR lpCmdLine,                    int iCmdShow){    WNDCLASS wc;    HWND hWnd;    HDC hDC;    HGLRC hRC;            MSG msg;    BOOL bQuit = FALSE;    float theta = 0.0f;    wc.style = CS_OWNDC;    wc.lpfnWndProc = WndProc;    wc.cbClsExtra = 0;    wc.cbWndExtra = 0;    wc.hInstance = hInstance;    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);    wc.hCursor = LoadCursor (NULL, IDC_ARROW);    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);    wc.lpszMenuName = NULL;    wc.lpszClassName = "GLSample";    RegisterClass (&wc);        hWnd = CreateWindow (      "GLSample", "Coridoor",       WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,      0, 0, 1024, 768,      NULL, NULL, hInstance, NULL);        EnableOpenGL (hWnd, &hDC, &hRC);        while (!bQuit)    {        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))        {                        if (msg.message == WM_QUIT)            {                bQuit = TRUE;            }            else            {                TranslateMessage (&msg);                DispatchMessage (&msg);            }        }        else        {                        glMatrixMode (GL_PROJECTION);            glLoadIdentity();            glMatrixMode (GL_MODELVIEW);            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);            glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);            glLoadIdentity ();            glRotatef (-camang, 0, 1, 0);            glTranslatef (-camx, -camy, -camz);            glPushMatrix ();                           // left wall                glBegin (GL_QUADS);                glColor3f(0, 0, 1); glVertex3f( -5,  0,  0);                glColor3f(0, 0, 1); glVertex3f( -5, 10,  0);                glColor3f(0, 0, 1); glVertex3f( -5, 10,-50);                glColor3f(0, 0, 1); glVertex3f( -5,  0,-50);                glEnd ();                //Top wall                glBegin (GL_QUADS);                glColor3f ( 1, 0, 0); glVertex3f (  5,  0,  0);                glColor3f ( 1, 0, 0); glVertex3f (  5, 10,  0);                glColor3f ( 1, 0, 0); glVertex3f (  5, 10,-50);                glColor3f ( 1, 0, 0); glVertex3f (  5,  0,-50);                glEnd ();                // Floor                glBegin (GL_QUADS);                glColor3f ( 0, 1, 0); glVertex3f (  5,  0,  0);                glColor3f ( 0, 1, 0); glVertex3f ( -5,  0,  0);                glColor3f ( 0, 1, 0); glVertex3f ( -5,  0,-50);                glColor3f ( 0, 1, 0); glVertex3f (  5,  0,-50);                glEnd ();                // ceiling                glBegin (GL_QUADS);                glColor3f ( 1, 0, 0); glVertex3f (  5, 10,  0);                glColor3f ( 0, 1, 0); glVertex3f ( -5, 10,  0);                glColor3f ( 0, 0, 1); glVertex3f ( -5, 10,-50);                glColor3f ( 0, 1, 0); glVertex3f (  5, 10,-50);                glEnd ();               glPopMatrix ();                 // Move camera        if (keys['A']);        {                camx = camx + 1;        }            if (keys['D']);        {                camx = camx - 1;        }              if (keys['W']);        {                camx = camx - sin (camang) * 0.2;                camz = camz - cos (camang) * 0.2;             }            if (keys['S']);        {            camx = camx + sin (camang) * .2;            camz = camz + cos (camang) * .2;        }                camx = 0;            camy = 2;            camz = 0;            SwapBuffers (hDC);            Sleep (1);        }    }        DisableOpenGL (hWnd, hDC, hRC);    DestroyWindow (hWnd);    return msg.wParam;}LRESULT CALLBACK WndProc (HWND hWnd, UINT message,                          WPARAM wParam, LPARAM lParam){    switch (message)    {    case WM_CREATE:        return 0;    case WM_CLOSE:        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);    }}void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC){    PIXELFORMATDESCRIPTOR pfd;    int iFormat;        *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 = 24;    pfd.cDepthBits = 16;    pfd.iLayerType = PFD_MAIN_PLANE;    iFormat = ChoosePixelFormat (*hDC, &pfd);    SetPixelFormat (*hDC, iFormat, &pfd);        *hRC = wglCreateContext( *hDC );    wglMakeCurrent( *hDC, *hRC );}void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC){    wglMakeCurrent (NULL, NULL);    wglDeleteContext (hRC);    ReleaseDC (hWnd, hDC);}[\source]
Well, first of all, I may have missed it, but where do you set your viewport? or your projection matrix? the identity matrix isnt going to cut it. First you need to call glViewport() somewhere it should look like glViewport(0, 0, Width, Height); replace Width and Height with whatever the window width and height are for your program, then you need to make sure you have actually set the projection matrix to something useful, the identity matrix isnt all you need, call gluPerspective() should look something like this: gluPerspective(45, Width / Height, 1.0f, 1000.0f) the last two parameters are the near and far plane distances, and the first parameter is half of the fov or field of view, the second parameter is the aspect ratio, simply the width divided by the height

(for the record this is a great reference http://rush3d.com/reference/opengl-bluebook-1.0/

but in all seriousness, it doesnt sount like your very familiar with openGL, get familiar with it before you try and do something like this, even if you had done a simple triangle program you wouldnt have this problem

hope that helps
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
Thanks i'll get on it. I may have overlooked glveiwport and gluperspective as this is the first program i have made from without copying and pasteing someone else's code and the last language i used set up the window and everything for you.Ill keep trying and maybe eventually ill get it. You lot have been great though.
see ya when i get it together.
-Pheonix2468-
Would it be ok if i used nehe's lesson 10 as a base and use different textures and change the map??
Would it be ok if i used nehe's lesson 10 as a base and use different textures and change the map??
if (keys['A']);
{
camx = camx + 1;
}

this will not work as expected. throw away semicolons from every of your if (keys.. lines
Quote:Original post by pheonix2468
Would it be ok if i used nehe's lesson 10 as a base and use different textures and change the map??


As a basic start, yes! I even recomend that you do so. That's the advantage of having the map on a text file and loading it, instead of hardcoding everything in code! The format nehe uses is very simple, being good for experiment.

The main question that will pop into your mind after a while is how do you change the map without using notepad, i.e having some sort of level editor for it. That's because, for a while, it will be *mighty cool* to have a map loaded from file that you can just change over in notepad and see the changes in your graphic engine. But you'll get bored after a while and want to make more complex examples for your maps and using notepad for it is just out of the question. But i think i'm talking too much... when (and if) that times comes, you'll deal with it. Until you get there, go ahead.
Thanks alot for the encouragment i guess yesterday i wasn't really thinking straight for some reason.

This topic is closed to new replies.

Advertisement