What have you tried?
I knew somebody will ask that question .
After a few hours of thinking I finally almost completed it, there is only one issue left:
http://tinypic.com/player.php?v=6h55wl&s=6
sorry for that terrible quality(I can try to record it again if needed...), but as you can see the grid doesn't follow the screen very well.
I'll be very thankful if someone will help me fixing that problem.
I've just started learning opengl few weeks ago, so I don't know everything very well...
Here is my code(grid class, code is a probably a mess right now, I will tidy it after fixing that problem):
public class grid
{
private MainWindow mainWindow;
public float viewWidth;
public float viewHeight;
public Point cameraPos;
public Rectangle glview;
public int WidthItems { get; set; }
public int HeightItems { get; set; }
public Rectangle[] squares;
private int totalSquares;
private int squareSize = 75;
public grid(MainWindow window, int widthitems, int heightitems, int ViewWidth, int ViewHeight)
{
mainWindow = window;
cameraPos = new Point(0, 0);
viewWidth = ViewWidth;
viewHeight = ViewHeight;
WidthItems = (int)(viewHeight / squareSize);
HeightItems = (int)(viewWidth / squareSize);
totalSquares = WidthItems * HeightItems;
squares = new Rectangle[totalSquares];
}
public Point CalculateViewCenterPos()
{
return mainWindow.convertScreenToWorldCoords((0 + glview.Width) / 2, (0 + glview.Height) / 2);
}
float tempOffsetX = 0f, tempOffsetY = 0f;
float offsetX = 0f, offsetY = 0f;
public void CameraMoved(float x, float y)
{
tempOffsetX += x;
if (tempOffsetX > squareSize)
{
offsetX -= squareSize;
tempOffsetX = 0;
}
else if (tempOffsetX < -squareSize)
{
offsetX += squareSize;
tempOffsetX = 0;
}
tempOffsetY += y;
if (tempOffsetY > squareSize)
{
offsetY -= squareSize;
tempOffsetY = 0;
}
else if (tempOffsetY < -squareSize)
{
offsetY += squareSize;
tempOffsetY = 0;
}
}
public void calculateCoords()
{
Point topleft = mainWindow.convertScreenToWorldCoords(0, 0);
mainWindow.testclickX = topleft.X;
mainWindow.testclickY = topleft.Y;
int startPosX = topleft.X - squareSize * 2;
int startPosY = topleft.Y + squareSize;
int id = 0;
for (int i = 0; i < WidthItems; i++)
{
for (int j = 0; j < HeightItems; j++)
{
squares[id].X = startPosX;
squares[id].Y = startPosY;
startPosX += squareSize;
id++;
}
startPosX = topleft.X - squareSize * 2;
startPosY -= squareSize;
}
}
public void draw()
{
for (int i = 0; i < totalSquares; i++)
{
GL.Color3(Color.Green);
GL.PushMatrix();
GL.Begin(BeginMode.Lines);
GL.Vertex3(squares.X + offsetX, squares.Y + offsetY, -2);
GL.Vertex3(squares.X + offsetX, squares.Y + squareSize + offsetY, -2);
GL.Vertex3(squares.X + offsetX, squares.Y + offsetY, -2);
GL.Vertex3(squares.X + squareSize + offsetX, squares.Y + offsetY, -2);
GL.Vertex3(squares.X + squareSize + offsetX, squares.Y + squareSize + offsetY, -2);
GL.Vertex3(squares.X + offsetX, squares.Y + squareSize + offsetY, -2);
GL.Vertex3(squares.X + squareSize + offsetX, squares.Y + squareSize + offsetY, -2);
GL.Vertex3(squares.X + squareSize + offsetX, squares.Y + offsetY, -2);
GL.End();
GL.PopMatrix();
}
}
}
and MainWindow class:
// ...
grid maingrid;
// ...
maingrid.draw();
// ...
glviewRatio = (ClientRectangle.Width / zoom) / (ClientRectangle.Height / zoom) * 5;
// ...
maingrid = new grid(this, 50, 50, ClientRectangle.Width * 5 / zoom, ClientRectangle.Height * 5 / zoom);
maingrid.glview = new Rectangle(glview.Location.X, glview.Location.Y, glview.Size.Width, glview.Size.Height);
maingrid.calculateCoords();
// ...
// (every frame when mouse button is clicked)
float x = (MousePosition.X - started_mouse_pos.X) * glviewRatio / zoom;
float y = -(MousePosition.Y - started_mouse_pos.Y) * glviewRatio / zoom;
maingrid.CameraMoved(x, y);
started_mouse_pos.X = MousePosition.X;
started_mouse_pos.Y = MousePosition.Y;