Hi , i have just started OpenGL. So i am sorry if my question is stupid, but i was trying to move a circle inside a square and was unable to do it . .can you please help me out please ??
#include <GL/glut.h>
#include <math.h>
bool* keyStates = new bool[256];
double angle =17.0;
double yPos=0,xPos=0;
int WIDTH,HEIGHT;
float rot =0.0;
bool movRight = false;
void keyOperations (void) {
}
void display (void) {
keyOperations();
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity(); // Load the Identity Matrix to reset drawing locations
glPushMatrix();
glTranslatef(0, 0,-5);
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2f(-1.0,-1.0);
glVertex2f(-1.0,1.0);
glVertex2f(-1.0,1.0);
glVertex2f(1.0,1.0);
glVertex2f(1.0,1.0);
glVertex2f(1.0,-1.0);
glVertex2f(1.0,-1.0);
glVertex2f(-1.0,-1.0);
glEnd();
glPopMatrix();
glColor3f(1.0,1.0,1.0);
glTranslatef(xPos,0.0,-5.0);
//glRotatef(rot,0,0,1);
glBegin(GL_LINE_LOOP);
for(int i=0;i<=360;i++)
glVertex2f(0.2*cos(i*(3.14/180)),0.2*sin(i*(3.14/180)));
glEnd();
/*if(xPos <= 1.0 && yPos <= 1.0 && xPos >= -1.0 && yPos >= -1.0)
{
xPos = 2.0*cos(angle)*0.01;
yPos = 2.0*sin(angle)*0.01;
}
else
{
angle = 90-angle;
xPos = 2.0*cos(angle)*0.01;
yPos = 2.0*sin(angle)*0.01;
}*/
glFlush();
if(movRight)
xPos-=.005;
else
xPos+=0.005;
if(xPos < -1.0)
movRight= false;
if(xPos > 1.0)
movRight = true;
glPopMatrix();
}
void reshape (int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void keyPressed (unsigned char key, int x, int y) {
keyStates[key] = true;
}
void keyUp (unsigned char key, int x, int y) {
keyStates[key] = false;
}
int main (int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("You’re first OpenGL Window");
WIDTH = 500;
HEIGHT = 500;
glutDisplayFunc(display);
glutReshapeFunc(reshape);
gluOrtho2D(0,WIDTH,0,HEIGHT);
glutKeyboardFunc(keyPressed);
glutKeyboardUpFunc(keyUp);
glutMainLoop();
}