Help with Tiling?

Started by
1 comment, last by Darkan_Fireblade 22 years ago
i need some help with getting this code to work and im haveing a load of trouble when i run the program nothing shows up? this is for tiling
  

//----The include files-------------------------------------

#include <GLMain.h>

//----The veriabols-----------------------------------------

HDC           hDC=NULL;
HGLRC         hRC=NULL;
HWND          hWnd=NULL;
HINSTANCE     hInstance;

bool          keys[256];
bool          active=TRUE;
bool          fullscreen=TRUE;
bool          light;
bool          lp;

#define       MAP_SIZEX 10
#define       MAP_SIZEY 10

GLfloat       x;
GLfloat       y;

GLuint        loop;
GLuint        texture[2];

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };

char map[10] [10] = {
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};





//----The functions-----------------------------------------

//----This Function Loads and Reads Texture Data

AUX_RGBImageRec *LoadBMP(char *Filename)				// Loads A Bitmap Image

{
	FILE *File=NULL;									// File Handle


	if (!Filename)										// Make Sure A Filename Was Given

	{
		return NULL;									// If Not Return NULL

	}

	File=fopen(Filename,"r");							// Check To See If The File Exists


	if (File)											// Does The File Exist?

	{
		fclose(File);									// Close The Handle

		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer

	}

	return NULL;										// If Load Failed Return NULL

}


//----This Function loads the bitmap into Texture Data

int LoadGLTextures()									// Load Bitmaps And Convert To Textures

{
	int Status=FALSE;									// Status Indicator


	AUX_RGBImageRec *TextureImage[2];					// Create Storage Space For The Texture


	memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL


	// Load The Bitmap, Check For Errors, If Bitmap''s Not Found Quit

	if ((TextureImage[0]=LoadBMP("Data/glass.bmp")) &&
		(TextureImage[1]=LoadBMP("Data/Land.bmp")))
	{
		Status=TRUE;									// Set The Status To TRUE


		glGenTextures(2, &texture[0]);					// Create Three Textures


		for (loop=0; loop<2; loop++)
		{
			glBindTexture(GL_TEXTURE_2D, texture[loop]);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
			gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);
		}
	}
	for (loop=0; loop<2; loop++)
	{
		if (TextureImage[loop])								// If Texture Exists

		{
			if (TextureImage[loop]->data)						// If Texture Image Exists

			{
				free(TextureImage[loop]->data);				// Free The Texture Image Memory

			}
			free(TextureImage[loop]);
		}
	}

	return Status;										// Return The Status

}

//----This function is for windowed mode mostly-------------

//----But it is called at least once in startup for fullscreen mode

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
	if (height==0)
	{
		height=1;
	}

	glViewport(0, 0, width, height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();


	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

//----This one is for OpenGL initlization--------------------

int InitGL(GLvoid)
{
	if (!LoadGLTextures())
	{
		return FALSE;
	}

	gluLookAt(10.0f, 8.0f, 20.0f, 10.0f, 8.0f, 0.0f, 0.0f, 1.0f, 0.0f);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);				// Clear The Background Color To Black

	glClearDepth(1.0);									// Enables Clearing Of The Depth Buffer

	glEnable(GL_DEPTH_TEST);							// Enable Depth Testing

	glShadeModel(GL_SMOOTH);							// Enables Smooth Color Shading

	glEnable(GL_TEXTURE_2D);							// Enable 2D Texture Mapping

	return TRUE;
}

//----And here is where we get to do all of are drawing------

int DrawGLScene(GLvoid)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();	
	//glEnable(GL_BLEND);// Enable Blending

	//glDisable(GL_DEPTH_TEST);


	/*glBlendFunc(GL_DST_COLOR,GL_ZERO);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glBegin(GL_QUADS);
	    glTexCoord2f(0.0f, 0.0f); glVertex3f(-50.0f, 50.0f, 0.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 50.0f, 50.0f, 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 50.0f,-50.0f, 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-50.0f,-50.0f, 0.0f);
	glEnd();

	glBlendFunc(GL_ONE, GL_ONE);
	glBindTexture(GL_TEXTURE_2D, texture[1]);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-50.0f, 50.0f, 0.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 50.0f, 50.0f, 0.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 50.0f,-50.0f, 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-50.0f,-50.0f, 0.0f);
	glEnd();*/
	return TRUE;
}


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

	if (hRC)
	{
		if (!wglMakeCurrent(NULL,NULL))
		{
			MessageBox(NULL,"Release of DC and RC Failed","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		}

		if (!wglDeleteContext(hRC))
		{
			MessageBox(NULL,"Could not Delete Rendering Context","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		}
		hRC=NULL;
	}

	if (hDC && !ReleaseDC(hWnd,hDC))
	{
		MessageBox(NULL,"Could not Delete Device Context","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
	}

	if (hWnd && !DestroyWindow(hWnd))
	{
		MessageBox(NULL,"Could not Delete hWnd","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		hWnd=NULL;
	}

	if (!UnregisterClass("OpenGL",hInstance))
	{
		MessageBox(NULL,"Could not Unregister Class","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		hInstance=NULL;
	}
}

BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
	GLuint       PixelFormat;
	WNDCLASS     wc;
	DWORD        dwExStyle;
	DWORD        dwStyle;
	
	RECT         WindowRect;
	WindowRect.left=(long)0;
	WindowRect.right=(long)width;
	WindowRect.top=(long)0;
	WindowRect.bottom=(long)height;

	fullscreen=fullscreenflag;

	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,"Failed to Register the Window Class","ERROR",MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

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

		if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
		{
			if (MessageBox(NULL,"The Requested Video Mode is not Supported By\nYour Video Card. Use Windowed Mode Instead?","OpenGL ERROR",MB_YESNO | MB_ICONEXCLAMATION)==IDYES)
			{
				fullscreen=FALSE;
			}
			else
			{
				MessageBox(NULL,"Program Will Now Close","ERROR",MB_OK | MB_ICONSTOP);
				return 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);

	if (!(hWnd=CreateWindowEx(      dwExStyle,
		                            "OpenGL",
									title,
									WS_CLIPSIBLINGS |
									WS_CLIPCHILDREN |
									dwStyle,
									0, 0, 
									WindowRect.right-WindowRect.left,
									WindowRect.bottom-WindowRect.top,
									NULL,
									NULL,
									hInstance,
									NULL)))
	{
		KillGLWindow();
		MessageBox(NULL,"Window Creation Error","ERROR",MB_OK | MB_ICONEXCLAMATION);
		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)))
	{
		KillGLWindow();
		MessageBox(NULL,"Can''t Create A GL Device Context","ERROR",MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))
	{
		KillGLWindow();
		MessageBox(NULL,"Can''t Find A Suitable PixelFormat.","ERROR",MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	if(!SetPixelFormat(hDC, PixelFormat, &pfd))
	{
		KillGLWindow();
		MessageBox(NULL,"Can''t Set The PixelFormat.","ERROR",MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	if (!(hRC=wglCreateContext(hDC)))
	{
		KillGLWindow();
		MessageBox(NULL,"Can''t Create A GL Rendering Context.","ERROR",MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	if (!wglMakeCurrent(hDC,hRC))
	{
		KillGLWindow();
		MessageBox(NULL,"Can''t Activate The GL Rendering Context.","ERROR",MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

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

	if(!InitGL())
	{
		KillGLWindow();
		MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	return TRUE;
}

LRESULT CALLBACK WndProc(    HWND hWnd,
						     UINT uMsg,
						     WPARAM wParam,
						     LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_ACTIVATE:
		{
			if (!HIWORD(wParam))
			{
				active=TRUE;
			}
			else
			{
				active=FALSE;
			}

			return 0;
		}

	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:
		{
			ReSizeGLScene(LOWORD(lParam), HIWORD(lParam));
			return 0;
		}
	}

	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

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

	if (MessageBox(NULL,"Would You Like to Run in Fullscreen Mode?","Fullscreen Options",MB_YESNO | MB_ICONQUESTION)==IDNO)
	{
		fullscreen=FALSE;
	}

	if (!CreateGLWindow("Raja''s OpenGL Framework",640,480,16,fullscreen))
	{
		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
				{
					DrawGLScene();
					SwapBuffers(hDC);
					
					if (keys[''L''] && !lp)
					{
						lp=TRUE;
						light=!light;
						
						if (!light)
						{
							glDisable(GL_LIGHTING);
						}
						else
						{
							glEnable(GL_LIGHTING);
						}
					}

					if (!keys[''L''])
					{
						lp=FALSE;
					}

					if (keys[VK_UP])
					{
						y-=1.0f;
					}

					if (keys[VK_DOWN])
					{
						y+=1.0f;
					}

					if (keys[VK_RIGHT])
					{
						x+=1.0f;
					}

					if (keys[VK_LEFT])
					{
						x-=1.0f;
					}
				}
			}

			if (keys[VK_F1])
			{
				keys[VK_F1]=FALSE;
				KillGLWindow();
				fullscreen=!fullscreen;
				if (!CreateGLWindow("Raja''s OpenGL Framework",640,480,16,fullscreen))
				{
					return 0;
				}
			}
		}
	}

	//Time for shutdown :()

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

[source]

Thanks  
Sir Darkan Fireblade
Sir Darkan Fireblade
Advertisement
quote:Original post by Darkan_Fireblade
i need some help with getting this code to work and im haveing a load of trouble

when i run the program nothing shows up?


It really depends on what you mean by ''nothing shows up''. Is the window drawn, but there is no OpenGL rendering? Does absolutly nothing show and you get warnings during compilation?



Regards,
Mathematix.
I got the code compiling and stuff showing on the screen. However, before I go any further...

[flippant]Of course you see nothing! You''ve commented out the code in the DrawGLScene function[/flippant]

LOL, couldn''t resist

Anyway, obvious bits out the way first. To begin with I declared the usual bumpf about window.h and so on (which I''ll assume is in your header file) and prototyped WndProc.

Then, when I first ran it, I got an exception about the textures, so I created the two into a ''data'' subdirectory named appropriately.

Those are the obvious bits out of the way, just in case you were having problems there (which I doubt).

Now, here''s a replacement for your DrawGLScene which shows something:

  int DrawGLScene(GLvoid){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		glLoadIdentity();		glEnable(GL_BLEND);// Enable Blending	glDisable(GL_DEPTH_TEST);	glBlendFunc(GL_DST_COLOR,GL_ZERO);	glBindTexture(GL_TEXTURE_2D, texture[0]);	glBegin(GL_QUADS);	    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, -4.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, -4.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,-1.0f, -4.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,-1.0f, -4.0f);	glEnd();	glBlendFunc(GL_ONE, GL_ONE);	glBindTexture(GL_TEXTURE_2D, texture[1]);	glBegin(GL_QUADS);		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, -4.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, -4.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,-1.0f, -4.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,-1.0f, -4.0f);	glEnd();	return TRUE;}  

Two points to note there: first, the main reason you weren''t seeing anything at all is the coordinates you gave. You had a *huge* quad at z=0, which doesn''t fit well with your selected viewing position and direction. I reduced the size of the shape and moved it further away from the view (the negative z value there).

Also, if you don''t disable the depth testing with glDisable(GL_DEPTH_TEST) then you won''t see anything either.

Alimonster

"If anyone can show me, and prove to me, that I am wrong in thought or deed, I will gladly change. I seek the truth, which never yet hurt anybody. It is only persistence in self-delusion and ignorance which does harm." -- Marcus Aurelius

This topic is closed to new replies.

Advertisement