i have a triangle moving diagonally across the screen and changing color randomly. And i want the triangle to keep the same size even when the window is resized?
heres my code
#include <GL/glut.h>
#include <stdlib.h>
static void init(void){
/* select clearing color as white */
glClearColor (1.0, 1.0, 1.0, 0.0);
/* initialize viewing values */
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void triangle()
{
glBegin (GL_TRIANGLES);
glColor3ub( (char) random()%256, (char) random()%256, (char) random()%256);
glVertex3f(0.0, 0.85, 0.0);
glVertex3f(0.15, 0.95, 0.0);
glVertex3f(0.30, 0.85, 0.0);
glEnd();
}
void display(void)
{
float x = 0.0;
glClear(GL_COLOR_BUFFER_BIT);
glTranslatef(0.0025,-0.0025,0.0);
triangle();
glutPostRedisplay();
glFlush();
glutSwapBuffers();
}
/* reshape does nothing so that when the window is
- resized nothing happens to the shape or size of the
- triangle on the screen
*/
void reshape(int w, int h)
{
glViewport(0, 0, (GLint)w, (GLint)h);
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN){
init();
}
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
exit(0);
}
/* Main Loop
- Open window with initial window size, title bar,
- RGBA display mode, and handle input events.
/
int main(int argc, char* argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutCreateWindow (argv[0]);
init();
glutMouseFunc(mouse);
glutDisplayFunc (display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}