0

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(); 
}
3
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by diamondfist
0

What do you mean move circle inside a square? You mean move the circle when a key is pressed? If so then in your display function check if certain key is pressed, if it is, increase xpos

1

i was trying to move a circle inside a square and was unable to do it

Don't despair; you're almost there. (Hey, that rhymes!)

What you're missing is the fact that glutMainLoop does not call your display function repeatedly. You need a way to tell the GLUT code that it's time to draw the scene again. Try clicking in your window, or dragging another window over it, and you should see the circle move--that's because the OS is telling your program that it's time to redraw everything, and your animation code is in the display function.

glutMainLoop never returns, though, so you have to do this in a callback. It can be educational to experiment with putting the call in various callbacks, but for a smooth, framerate-independent animation, it's best to use a timer callback, which you register with glutTimerFunc.

When you have that working, you'll notice that if you click or drag other windows around, it will sometimes throw in an extra redraw, so your animation isn't as smooth any more. That's because your animation logic (the lines starting with if(movRight)) is still in the display function--you only really want that happening in your timer function, so move that code there.

Edited by Dani: Formatting fixed

0

i have a query .. When the window is created where is the center of my cordinate system ? is it at the top left corner or at the center of the window screen? also how will i know how much i have to translate to move like 1cm on my screen towards right?

1

When the window is created where is the center of my cordinate system ? is it at the top left corner or at the center of the window screen?

That's up to your code to decide. Look at the call to gluOrtho2D; in this case, you're putting (0, 0) at the bottom left of the window.

how will i know how much i have to translate to move like 1cm on my screen towards right?

You should be able to use glutGet to find out the screen size. For example, between GLUT_SCREEN_WIDTH, GLUT_SCREEN_WIDTH_MM, you should be able to figure how many pixels are in a centimeter.

Don't forget that your coordinate system may not directly correspond to pixels; you'll need to get GLUT_WINDOW_WIDTH to compare with your orthographic matrix.

Edited by Reverend Jim: Fixed formatting

0

ok .i figured out how to get textures .. but the image is coming out to be kind of crooked ... as in colorless for some images and kind of dull .. any idea as to why it is happening ??

0

ok .i figured out how to get textures .. but the image is coming out to be kind of crooked ... as in colorless for some images and kind of dull .. any idea as to why it is happening ??

Might be a problem loading the images. "Before" and "after" pictures would help. Also, this should probably be a new post, since the question has changed.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.