Skip to main content
GameDev.net gamedev.net
🔒 Locked

source code problem, please help

Started by neonoblivion May 30, 2002 at 2:30 PM 4 replies 150+ views
Original Post
neonoblivion
neonoblivion
I am trying to get opengl up and running. so I went through the nehe website copyed bits of code here and there (hopeing to learn more by studying rather than just copying) , but it won't work. #include <windows.h> #include <GL\gl.h> #include <GL\glu.h> #include <GL\glaux.h> #include <stdio.h> #define width 800 #define height 600 #define bpp 16 #define WINDOW_CLASS_NAME "WINXCLASS" #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) HGLRC hrc=NULL; HDC hdc=NULL; HWND hwnd=NULL; HINSTANCE hin; bool keys[256]; bool active=TRUE; bool fullscreen=TRUE; GLfloat xrot; GLfloat yrot; GLfloat zrot; GLuint texture[1]; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int init(void); int gmain(void); void shutdown(void); AUX_RGBImageRec *LoadBMP(char *filename); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ MSG msg; BOOL done=FALSE; GLuint PixelFormat; WNDCLASS winclass; RECT WindowRect; WindowRect.left=(long)0; WindowRect.right=(long)width; WindowRect.top=(long)0; WindowRect.bottom=(long)height; hin = GetModuleHandle(NULL); winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; winclass.lpfnWndProc = WndProc; winclass.cbClsExtra = 0; winclass.cbWndExtra = 0; winclass.hInstance = hin; winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = NULL; winclass.lpszMenuName = NULL; winclass.lpszClassName = WINDOW_CLASS_NAME; RegisterClass(&winclass); if (!(hwnd = CreateWindowEx(WS_EX_APPWINDOW, WINDOW_CLASS_NAME, "ha ha ha", WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0,0, width,height, NULL, NULL, hin, NULL))){ return(0); } DEVMODE dms; memset(&dms,0,sizeof(dms)); dms.dmSize=sizeof(dms); dms.dmPelsWidth = width; dms.dmPelsHeight = height; dms.dmBitsPerPel = bpp; dms.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; ChangeDisplaySettings(&dms,CDS_FULLSCREEN); ShowCursor(FALSE); static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format bpp, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 16, // 16Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 // Layer Masks Ignored }; hdc=GetDC(hwnd); PixelFormat=ChoosePixelFormat(hdc,&pfd); SetPixelFormat(hdc,PixelFormat,&pfd); hrc=wglCreateContext(hdc); wglMakeCurrent(hdc,hrc); ShowWindow(hwnd,SW_SHOW); SetForegroundWindow(hwnd); SetFocus(hwnd); init(); ///load texture /*AUX_RGBImageRec *tex[1]; memset(tex,0,sizeof(void *)*1); tex[0]=LoadBMP("c:\\happy.bmp"); glGenTextures(1, &texture[0]); glBindTexture(GL_TEXTURE_2D, texture[0]); glTexImage2D(GL_TEXTURE_2D, 0, 3, tex[0]->sizeX, tex[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, tex[0]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); if(tex[0]){ if(tex[0]->data){ free(tex[0]->data); } free(tex[0]); } */ ///end load texture while(1){ if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)){ if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } if(gmain()==0) break; SwapBuffers(hdc); } shutdown(); return (msg.wParam); } LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){ PAINTSTRUCT ps; HDC hdc; switch(msg){ case WM_CREATE:{ return(0); } break; case WM_PAINT:{ hdc = BeginPaint(hwnd,&ps); EndPaint(hwnd,&ps); return(0); } break; case WM_DESTROY:{ PostQuitMessage(0); return(0); } break; default:break; } return (DefWindowProc(hwnd, msg, wparam, lparam)); } int init(void){ glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); return(0); } void shutdown(void){ ChangeDisplaySettings(NULL,0); ShowCursor(TRUE); wglMakeCurrent(NULL,NULL); if(hrc){ wglDeleteContext(hrc); hrc=NULL; } if(hdc){ ReleaseDC(hwnd,hdc); hdc=NULL; } if(hwnd){ DestroyWindow(hwnd); hwnd=NULL; } UnregisterClass("WINDOWS_CLASS_NAME",hin); hin=NULL; } int gmain(void){ if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE)){ PostMessage(hwnd, WM_DESTROY,0,0); return(0); } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(-1.5f,0.0f,-6.0f); //glBindTexture(GL_TEXTURE_2D, texture[0]); glColor3f(1.0f,0.0f,0.0f); glBegin(GL_TRIANGLES); glVertex3f( 0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glEnd(); //glTranslatef(3.0f,0.0f,0.0f); return(1); } AUX_RGBImageRec *LoadBMP(char *filename){ FILE *file; if(!(file=fopen("filename","r"))) return(0); fclose(file); return auxDIBImageLoad(filename); } I don't know enough yet to debug so could sombody please help me? thanks, Klaus ps. how do you do those scroll boxes around code? [edited by - neonoblivion on May 30, 2002 3:32:07 PM]
Anthracks
Anthracks
You use [ source ] and [ /source ] without the extra spaces to get the boxes around your code.

What about your program doesn''t work? Are you getting a compile error, or an error when you run it, or it just doesn''t do what you expected when you run it or what? You have to say what the problem is, not just throw 3 pages of code at people heh.

Anthracks
Jallen
Jallen
doesn''t look like you are setting the viewport or projection matrix.

add a function like this one:


  
void ResizeWnd(int Width, int Height)
{
GLfloat fAspect;

if (!Height)
Height = 1;

glViewport(0, 0, Width, Height);

fAspect = (float)Width/(float)Height;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0, fAspect, 1.0, 400.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


then call this function whenever the window size changes (WM_SIZE message).
---I write code.DelphiGL (http://delphigl.cfxweb.net)
Jallen
Jallen
Also, a good idea when having problems like this is to set your clear color (glClearColor) to light grey so you can see if any geometry is actually being drawn.
---I write code.DelphiGL (http://delphigl.cfxweb.net)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.