Project Help MoveingTriangles

Started by
1 comment, last by Mat1515 20 years, 5 months ago
Hey This is a project ive been working on for the past few days. I am a begginer to OpenGL. This has been boggling my mind. When i run my program the triangle that moves with the arrrow keys move nice and smooth and quickly, but the triangle that moves with the NUM pad is chopped up and slow? Why is this. Please help! Thanks Here is the WHOLE code

#define WINDOWS_LEAN_AND_MEAN	
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>


HWND		hwnd = NULL;//global handle to our window	

HINSTANCE	hInstance;//instance of application

HGLRC		hRC = NULL;//Rendering Context

HDC			hDC	= NULL;//Device Context

bool		fullscreen;
bool		keys[256];
bool		color = false;
bool		antialias = false;

bool		FOne = false;

float		translateXTriOne=0.0f;
float		translateYTriOne=0.0f;
float		translateYTriTwo=0.0f;
float		translateXTriTwo=0.0f;
float		scaleX=1.0f;
float		scaleY=1.0f;

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
void SetupPixelFormat(HDC hDC);
void KillGLWindow();
void RenderScene();
void KeyCheck();
void SetUpGL();

void SetupPixelFormat(HDC hDC)
{
	int nPixelFormat;				

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

	nPixelFormat = ChoosePixelFormat(hDC, &pfd);

	SetPixelFormat(hDC, nPixelFormat, &pfd);	
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int height, width;

	switch(message)
	{
	case WM_CREATE:
		hDC = GetDC(hwnd);
		SetupPixelFormat(hDC);

		hRC = wglCreateContext(hDC);
		wglMakeCurrent(hDC, hRC);
		return 0;
		break;
	case WM_KEYDOWN:
		keys[wParam] = true;
		return 0;
		break;
	case WM_KEYUP:
		keys[wParam] = false;

		if(!keys[VK_F1])
			FOne = false;
		return 0;
		break;
	case WM_CLOSE:
		KillGLWindow();
		return 0;
		break;
	case WM_SIZE:
		height = HIWORD(lParam);
		width = LOWORD(lParam);
		
		if (height == 0)
		{
			height = 1;
		}

		glViewport(0,0,width,height);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();

		gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
		
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		return 0;
		break;

	default:
		break;
	}
	return (DefWindowProc(hwnd,message,wParam,lParam));

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	WNDCLASSEX	windowClass;
	MSG			msg;
	bool		done;
	DWORD		dwExStyle;
	DWORD		dwStyle;
	RECT		windowRect;

	int width = 800;
	int height = 600; 
	int bits = 32;

	if(MessageBox(NULL,"FullScreen?","FullScreen?",MB_YESNO)==IDYES)
	{
		fullscreen = true;
	}


	windowRect.top = (long)0;
	windowRect.left = (long)0;
	windowRect.bottom = (long)height;
	windowRect.right = (long)width;


	windowClass.cbClsExtra = 0;
	windowClass.cbSize = sizeof(WNDCLASSEX);
	windowClass.cbWndExtra = 0;
	windowClass.hbrBackground = NULL;
	windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	windowClass.hIcon = NULL;
	windowClass.hIconSm =NULL;
	windowClass.hInstance = hInstance;
	windowClass.lpfnWndProc = WndProc;
	windowClass.lpszClassName = "MyClass";
	windowClass.lpszMenuName = NULL;
	windowClass.style = CS_HREDRAW|CS_VREDRAW;

	if (!RegisterClassEx(&windowClass))
		return 0;

	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)
		{
		
			MessageBox(NULL, "Display mode failed", NULL, 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(NULL,
		"MyClass",
		"Matt Janas",
		dwStyle | WS_CLIPCHILDREN |
		WS_CLIPSIBLINGS|WS_MAXIMIZE,
		 0, 0,							
		windowRect.right - windowRect.left,
		windowRect.bottom - windowRect.top,
		NULL,
		NULL,
		hInstance,
		NULL);

	if(!hwnd)
		return 0;

	ShowWindow(hwnd,SW_SHOW);
	UpdateWindow(hwnd);

	done = false;

	while(!done)
	{
		PeekMessage(&msg,hwnd,NULL,NULL, PM_REMOVE);

		if (msg.message == WM_QUIT)
		{
			done = true;
		}
		else
		{	
			TranslateMessage(&msg);	
			DispatchMessage(&msg);
		}
		KeyCheck();
		RenderScene();
		
		SwapBuffers(hDC);
	}

	if (fullscreen)
	{
		ChangeDisplaySettings(NULL,0);
		ShowCursor(true);
	}

	return msg.wParam;
}

void KillGLWindow()
{
	wglMakeCurrent(hDC,NULL);
	wglDeleteContext(hRC);

	PostQuitMessage(0);
}

void RenderScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	

	glPushMatrix();
		glTranslatef(translateXTriTwo,translateYTriTwo,-10.0f);

		glBegin(GL_TRIANGLES);
			glVertex3f(0.0f,2.0f,0.0f);
			glVertex3f(2.0f,-2.0,0.0f);
			glVertex3f(-2.0f,-2.0f,0.0f);
		glEnd();
	glPopMatrix();

	glPushMatrix();
		glTranslatef(translateXTriOne,translateYTriOne,-10.0f);
		glScalef(scaleX,scaleY,1.0f);

		glBegin(GL_TRIANGLES);
			glVertex3f(0.0f,2.0f,0.0f);
			glVertex3f(2.0f,-2.0f,0.0f);
			glVertex3f(-2.0f,-2.0f,0.0f);
		glEnd();
	glPopMatrix();
}

void KeyCheck()
{
	if(keys[VK_ESCAPE])
	{
		keys[VK_ESCAPE] = false;
		PostQuitMessage(0);
	}

	if(keys[VK_F1] && !FOne)
	{
		if(!color)
		{
		FOne = true;
		glColor3f(0.0f,1.0f,0.0f);
		color = !color;
		}
		else
		{
		FOne = true;
		glColor3f(1.0,1.0,1.0);
		color = !color;
		}
	}

	if(keys[VK_NUMPAD8])
	{
		keys[VK_BACK] = false;
		if(!(translateYTriTwo == 3.0f))
		{
			translateYTriTwo += 0.5f;
		}
	}

	if(keys[VK_NUMPAD2])
	{
		keys[VK_NUMPAD2] = false;
		if(!(translateYTriTwo == -3.0f))
		{
			translateYTriTwo -= 0.5f;
		}
	}

	if(keys[VK_NUMPAD4])
	{
		keys[VK_NUMPAD4] = false;
		if(!(translateXTriTwo == -4.0f))
		{
			translateXTriTwo -= 0.5f;
		}
	}

	if(keys[VK_NUMPAD6])
	{
		keys[VK_NUMPAD6] = false;
		if(!(translateXTriTwo == 4.0f))
		{
			translateXTriTwo += 0.5f;
		}
	}

	if(keys[VK_ADD])
	{
		keys[VK_ADD] = false;
		scaleX += 0.05f;
		scaleY += 0.05f;
	}
	
	if(keys[VK_SUBTRACT])
	{
		keys[VK_SUBTRACT] = false;
		scaleX -= 0.05f;
		scaleY -= 0.05f;
	}
	
	if(keys[VK_UP])
	{
		keys[VK_UP] = false;
		if(!(translateYTriOne == 3.0f))
		{
		translateYTriOne += 0.5f;
		}
	}
	if(keys[VK_DOWN])
	{
		keys[VK_DOWN] = false;
		if(!(translateYTriOne == -3.0f))
		{
		translateYTriOne -= 0.5f;
		}
	}
	if(keys[VK_LEFT])
	{
		keys[VK_LEFT] = false;
		if(!(translateXTriOne == -4.0f))
		{
		translateXTriOne -= 0.5f;
		}
	}
	if(keys[VK_RIGHT])
	{
		keys[VK_RIGHT] = false;
		if(!(translateXTriOne == 4.0f))
		{
		translateXTriOne += 0.5f;
		}
	}


}

void SetUpGL()
{
	

}
Ut o not me again:) lol
Advertisement
Does the problem only occur if you are pressing both the cursor keys and numpad keys at the same time, or even if you only press the numpad keys?

One comment about your code that won''t help with the problem but may help you avoid future problems:
if(!(translateYTriTwo == 3.0f)) 

Never never never test two floating point values for equality (well, you can sometimes but only if you can guarentee the values). Floating point numbers are, by their very nature, inaccurate. Instead either do:
if (std::fabs(translateYTriTwo - 3.0f) < FLT_EPSILON) 

or instead of checking for equality just test a bound on the value:
if (translateYTriTwo < 3.0f) 


Enigma
Thanks i figured out my problem I just got rid of the set

keys[blablabla] = false;

in the keyCheckfunction
Now why did that fix it I dont know lol
Ut o not me again:) lol

This topic is closed to new replies.

Advertisement