glOrtho has odd offset

I'm making a program to simulate a group of particles moving with a variant of the Lennard-Jones potential function to test out using such a function for the swarm AI in a game I'm working on. The simulation works pretty well, except that there is an offset, as you can see in the picture. The white lines are there because I wanted to draw a definite structure at a known point to see if there really was an offset, which there is. After the picture is my code. Does anyone know what's causing this offset?

image showing what my program renders: http://www.majhost.com/gallery/DagonEcelstraun/Others/HelpNeeded/offset.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")

#include "stdafx.h"

#include "Particle.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);
void paint( HDC );
void init();
void update();

bool createdDialog = false, canDraw = false;
float A, B, m, n, U, d;
float g_rotation;
int mouseXPos = 0, mouseYPos = 0, centerX = 0, centerY = 0, wheelDelta = 0;
HWND g_hDialog = NULL, sr_hDialog = NULL;
RECT rcClient;
ofstream debug( "debug.txt" );

Particle *particles[50];
Vector2 dist, u;

void init()
{
	glMatrixMode( GL_PROJECTION );
	glViewport( 100, 100, 500, 500 );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
	glOrtho( -250, 250, 250, -250, 0.01, 3 );
	glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
	glPointSize( 5.0f );

	srand( time(NULL) );
	for( int i = 0; i < 50; i++ )
		particles[i] = new Particle( (rand()%500)-250, (rand()%500)-250, 5 );
}

void update()
{
	for( int i = 0; i < 50; i++ )
	{
		particles[i]->setVel( Vector2( 0, 0 ) );
		for( int j = 0; j < 50; j++ )
			if( i != j )
			{
				dist = particles[j]->getPos()-particles[i]->getPos(); //find displacement from particle i to j
				if( dist.magnitude() < 50 )
				{
					u = dist;
					u.normalize();	//normalize the distance, for later use
					n = 2.25;
					m = 3;
					d = dist.magnitude()/10;	//distance in terms of the diameter of a particle
					U = -4000/pow( d, n ) + 2000/pow( d, m );
					U /= 1000;
					u  = u * U;
					particles[i]->setVel( particles[i]->getVel()+u );
				}
			}
		dist = Vector2( mouseXPos, mouseYPos ) - particles[i]->getPos();
		dist.normalize();
		dist = dist * 2;
		particles[i]->setVel( particles[i]->getVel()+dist );
		particles[i]->update();
	}
}

void paint( HDC hDC )
{
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glMatrixMode( GL_PROJECTION );
	glPushMatrix();
	glLoadIdentity();
	glOrtho( -250, 250, 250, -250, -0.01, -3 );
	glMatrixMode( GL_MODELVIEW );
	glPushMatrix();
	glLoadIdentity();

	glColor3f( 1.0f, 1.0f, 1.0f );
	glBegin( GL_LINES );
	for( int i = -250; i < 250; i += 10 )
	{
		glVertex3i( i, 250, 1 );
		glVertex3i( 1, -250, 1 );
	}
	glEnd();

	glBegin( GL_POINTS );
		for( int i = 0; i < 50; i++ )
			particles[i]->draw();
	glEnd();

	glPopMatrix();
	glPopMatrix();
	glFinish();
    SwapBuffers (hDC);
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;
    MSG msg;
    BOOL bQuit = FALSE;
    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 = "FBX Renderer";
    RegisterClass (&wc);
    hWnd = CreateWindow ( "FBX Renderer", "FBX Renderer",
      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      100, 100, 500, 500,
      NULL, NULL, hInstance, NULL);
    EnableOpenGL (hWnd, &hDC, &hRC);
	init();
    while (!bQuit)
    {
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                bQuit = TRUE;
            else
				if(!IsDialogMessage(g_hDialog, &msg))
				{
					TranslateMessage (&msg);
					DispatchMessage (&msg);
				}
        }
        else
        {
			update();
            paint( 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_MOUSEMOVE:
			{
				GetClientRect(hWnd, &rcClient);
				centerX = rcClient.left + (rcClient.right-rcClient.left)/2;
				centerY = rcClient.top + (rcClient.bottom-rcClient.top)/2;				
				mouseXPos = GET_X_LPARAM( lParam )+rcClient.left;
				mouseYPos = GET_Y_LPARAM( lParam )+rcClient.top;
			}
			return 0;
		case WM_MOUSEWHEEL:
			wheelDelta += GET_WHEEL_DELTA_WPARAM(wParam)/120;
			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);
}
The topic should belong to the category Windows Programming. More people will ready to help you and you'll get more helpful opinions, comments and suggestions.
Does that help?
Last edited on
Without testing, I'm reasonably certain it's the glViewPort definition. The window you create is 500x500 pixels. The glViewPort is also 500x500, but it's offset by 100 pixels from the x/y origin (so, the rendering coordinates begin 100 pixels from the bottom and 100 pixels from the left of the window.)
Topic archived. No new replies allowed.