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

Nothing shows...

Started by MentalRotation May 29, 2003 at 6:47 PM 4 replies 1.1k views
Original Post
MentalRotation
MentalRotation
I have been using the NeHe tutorials along with the actual drawing commands from the red book. For some reason, it won't actually draw anything. Here is my code. If you could help out, I would greatly appreciate it! Thanks. header:
  #include <windows.h>
#include <float.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glaux.h>
#include <stdlib.h>

HGLRC hRC = NULL;         //rendering context

HDC hDC = NULL;           //device context

HWND hWnd = NULL;         //window handle

HINSTANCE hInstance;      //app instance


bool keys[256];          //keyboard array

bool active = TRUE;      //window active flag

bool fullscreen = TRUE;  //fullscreen flag


LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //window procedure declaration


void init(void)
{
   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   glShadeModel(GL_SMOOTH);
   //glEnable(GL_LIGHTING);

   glEnable(GL_DEPTH_TEST);
   glDepthFunc(GL_LEQUAL);
   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

GLvoid killwindow(void)
{
	if(fullscreen)
   {
   	ChangeDisplaySettings(NULL,0);
      ShowCursor(TRUE);
   }

   if(!wglMakeCurrent(NULL,NULL))
   {
   	MessageBox(NULL, "RC release failed!", "ERROR", MB_OK);
   }

   if(!wglDeleteContext(hRC))
   {
   	MessageBox(NULL, "RC delete failed!", "ERROR", MB_OK);
   }
   hRC = NULL;

   if(hDC && !ReleaseDC(hWnd, hDC))
   {
   	MessageBox(NULL, "DC release failed!", "ERROR", MB_OK);
      hDC = NULL;
   }

   if(hWnd && !DestroyWindow(hWnd))
   {
   	MessageBox(NULL, "Window destruction failed!", "ERROR", MB_OK);
   	hWnd = NULL;
   }

   if(!UnregisterClass("OpenGL", hInstance))
   {
   	MessageBox(NULL, "Class deregistration failed!", "ERROR", MB_OK);
      hInstance = NULL;
   }
}

void reshape(int w, int h)
{
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if(h == 0)
   {
   	h = 1;
   }
   gluPerspective(45.0, (GLfloat)w/(GLfloat)h, .1, 100.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

bool CreateGLWindow(char * title, int width, int height, int bits, bool fscreenflag)
{
	GLuint PixelFormat;

 	WNDCLASS wc;

   DWORD dwExStyle;
   DWORD dwStyle;

   RECT WindowRect;
   WindowRect.left = 0;
   WindowRect.right = width;
   WindowRect.top = 0;
   WindowRect.bottom = height;

   fullscreen = fscreenflag;

   hInstance = GetModuleHandle(NULL);
   wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
   wc.lpfnWndProc = (WNDPROC) WndProc;
   wc.cbClsExtra = 0;
   wc.cbWndExtra = 0;
   wc.hInstance = hInstance;
   wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
   wc.hbrBackground = NULL;
   wc.lpszMenuName = NULL;
   wc.lpszClassName = "OpenGL";

   if(!RegisterClass(&wc))
   {
   	MessageBox(NULL, "Class registration failed!", "ERROR", MB_OK);
      return FALSE;
   }

   if(fullscreen)
   {
   	DEVMODE dmSS;
      memset(&dmSS, 0, sizeof(dmSS));
      dmSS.dmSize = sizeof(dmSS);
      dmSS.dmPelsWidth = width;
      dmSS.dmPelsHeight = height;
      dmSS.dmBitsPerPel = bits;
      dmSS.dmFields = DM_BITSPERPEL | DM_PELSWIDTH |DM_PELSHEIGHT;

      if(ChangeDisplaySettings(&dmSS, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
      {
      	MessageBox(NULL, "Fullscreen not available.", "NOTICE", MB_OK);
         fullscreen = FALSE;
      }
   }

   if(fullscreen)
   {
   	dwExStyle = WS_EX_APPWINDOW;
      dwStyle = WS_POPUP;
      ShowCursor(FALSE);
   }
   else
   {
   	dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
      dwStyle = WS_OVERLAPPEDWINDOW;
   }

   AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);

   hWnd = CreateWindowEx(dwExStyle, "OpenGl", title, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle, 0, 0,
   	/*WindowRect.right - WindowRect.left*/width, /*WindowRect.top - WindowRect.bottom*/height, NULL, NULL,
      hInstance, NULL);

   if(hWnd == NULL)
   {
   	MessageBox(NULL, "Window creation failed!", "ERROR", MB_OK);
      killwindow();
      return FALSE;
   }

   static PIXELFORMATDESCRIPTOR pfd =
   {
   	sizeof(PIXELFORMATDESCRIPTOR),
      1,
      PFD_DRAW_TO_WINDOW |
      PFD_SUPPORT_OPENGL |
      PFD_DOUBLEBUFFER,
      PFD_TYPE_RGBA,
      bits,
      0, 0, 0, 0, 0, 0,
      0,
      0,
      0,
      0, 0, 0, 0,
      16,
      0,
      0,
      PFD_MAIN_PLANE,
      0,
      0, 0, 0
   };

   if(!(hDC = GetDC(hWnd)))
   {
   	MessageBox(NULL, "No DC!", "ERROR", MB_OK);
      killwindow();
      return FALSE;
   }

   if(!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
   {
		MessageBox(NULL, "No pixel format!", "ERROR", MB_OK);
      killwindow();
      return FALSE;
   }

   if(!SetPixelFormat(hDC, PixelFormat, &pfd))
   {
      MessageBox(NULL, "Could not set pf!", "ERROR", MB_OK);
      killwindow();
      return FALSE;
   }

   if(!(hRC = wglCreateContext(hDC)))
   {
      MessageBox(NULL, "Could not create RC!", "ERROR", MB_OK);
      killwindow();
      return FALSE;
   }

   if(!wglMakeCurrent(hDC, hRC))
   {
      MessageBox(NULL, "Could not make current!", "ERROR", MB_OK);
      killwindow();
      return FALSE;
   }

   ShowWindow(hWnd, SW_SHOW);
   SetForegroundWindow(hWnd);
   SetFocus(hWnd);
   reshape(width, height);

   init();

   return TRUE;
}
  
and here is the cpp file:
  
#include "oglh.h"

static GLfloat spin = 0.0;
static GLfloat spin2 = 0.0;
static GLfloat spin3 = 0.0;

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

	switch(msg)
   {
   	case WM_ACTIVATE:
      {
      	if(!HIWORD(wParam))
         {
         	active = TRUE;
         }
         else
         {
         	active = FALSE;
         }
      }
      case WM_SYSCOMMAND:
      {
      	switch(wParam)
         {
         	case SC_SCREENSAVE:
            case SC_MONITORPOWER:
            return 0;
         }
         break;
      }
      case WM_CLOSE:
      {
      	PostQuitMessage(0);
         return 0;
      }
      case WM_KEYDOWN:
      {
      	keys[wParam] = TRUE;
         return 0;
      }
      case WM_KEYUP:
      {
      	keys[wParam] = FALSE;
         return 0;
      }
      case WM_SIZE:
      {
      	reshape(LOWORD(lParam), HIWORD(lParam));
         return 0;
      }
   }
   return DefWindowProc(hWnd, msg, wParam, lParam);
}

void display(void)
{
   glColor3f(1.0,1.0,1.0);
   glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
   glBegin(GL_POLYGON);
   	glVertex3f(.25,.25,0);
      glVertex3f(.75,.25,0);
      glVertex3f(.75,.75,0);
      glVertex3f(.25,.75,0);
   glEnd();
   glFlush();
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG msg;
   BOOL done = FALSE;

   if(MessageBox(NULL, "Would you like to run in full screen?", "?", MB_YESNO) == IDNO)
   {
    	fullscreen = FALSE;
   }

   if(!CreateGLWindow("WOW!", 640, 480,16,fullscreen))
   {
      MessageBox(NULL, "Could not create window!", "ERROR", MB_OK);
   	return 0;
   }

   while(!done)
   {

   	if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
      {
      	if(msg.message == WM_QUIT)
         {
         	done = TRUE;
         }
         else
         {
         	TranslateMessage(&msg);
         	DispatchMessage(&msg);
         }
      }
      else
      {
      	if(active)
         {
         	if(keys[VK_ESCAPE])
            {
            	done = TRUE;
            }
            else
            {
            	display();
               SwapBuffers(hDC);
            }
         }
         //fullscreen switch

      }
   }
   killwindow();
   return(msg.wParam);



   /*_control87(MCW_EM, MCW_EM);
	glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize(500,500);
   glutInitWindowPosition(100,100);
   glutCreateWindow(argv[0]);
   init();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0;*/
}  
It is not exact with the NeHe tutorial as I have fiddled with various aspects trying to get it to work, but it is almost the same. Hope you can help. Thanks! [edited by - mentalrotation on May 29, 2003 7:50:30 PM]
mentalrotation@cox.net
lc_overlord
lc_overlord
this line is wrong
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);

it's glOrtho (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar);
this might work better
glOrtho(0.0,1.0,0.0,1.0,0.1,100.0);
try skipping this line alltogether.

second thing, set the z factor of the vertics to something other than 0, say like -1

[edited by - lc_overlord on May 29, 2003 11:15:32 PM]
MentalRotation
MentalRotation
I''m swapping buffers out anyways in the loop directly after the display func, so that should not be an issue. I changed it anyways for no effect. I took the ortho line out, but still i get a black display.
mentalrotation@cox.net
Juicy
Juicy
Don''t take glOrtho(); out. It''s required!
The reason you''re not seeing anything when you call it with znear/far valies of -1 and 1 is that the drawing is going on outside the view volume defined by these values.

Settings them as .1f and 100f will allow much more to fit into the volume and you should see your scene.
I like pie! - weebl.
Oneiric - Another graphics engine in the works.
Enigma
Enigma
The problem is that you are not in the correct matrix mode when you call glOrtho. The projection method must be set with matrix mode GL_PROJECTION. Remove the glOrtho call from the display function and put it into the reshape function in the header in place of your gluPerspective call. If you need perspective as well then don''t move the glOrtho call, but make it:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);


Enigma

Topic Locked

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

Sign in to reply to this topic.