I'm attempting to create a simple screen saver of sorts using Tao framework and C#. I've managed to create the square, have it rotate on its origin point as well as translate it. The issue I'm having is that I'm not able to make it detect the window's boundaries. The square eventually floats past the edge of the screen and disappears.
Line 27: holds the variables that indicate the max value for both x and y.
I tried placing the boundary check over in line 49, however the square continued to drift outside the window.
Lines 60-63 show what a friend suggested. However this has also proved unsuccessful.
I would like to keep the square bouncing inside the window, while I understand in theory how it should work its translating it to the code that's giving me issues.
public partial class Form1 : Form
int width = 800;
int height = 600;
double xrot, yrot, zrot; //rotate;
float xtrans, ytrans;
int xmax = 786, ymax = 562;
Gl.glClearColor(0, 0, 0, 0);
Gl.glViewport(0, 0, width, height);
Glu.gluPerspective(45.0f, (double)width / (double)height, 0.01f, 5000.0f);
private void timer1_Tick(object sender, EventArgs e)
xtrans += 0.5f;
ytrans += 0.5f;
private void simpleOpenGlControl1_Paint(object sender, PaintEventArgs e)
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
if (xtrans >= xmax - 2 || ytrans >= ymax - 2)
Gl.glTranslatef(0, 0, -10);
Gl.glTranslatef(xtrans, ytrans, -10);
Gl.glRotated(xrot += 0.5, 1, 0, 0);
Gl.glRotated(yrot += 0.3, 0, 1, 1);
Gl.glRotated(zrot += 0.2, 0, 0, 1);
Gl.glColor3d(1, 0, 0); //red
Gl.glVertex3d(-1, 1, 0);
Gl.glColor3d(0, 0.8, 0); // green
Gl.glVertex3d(-1, -1, 0);
Gl.glColor3d(0, 0, 0.5); // blue
Gl.glVertex3d(1, -1, 0);
Gl.glColor3d(1, 1, 1); // white
Gl.glVertex3d(1, 1, 0);