I have been trying to animate a simple box that moves from left to right in the viewport while changing its color at random. It is meant to restart animating on a left mouse click, and close on a right mouse click. I have been debugging my code below, and though I think my methodology is reasonable, whenever I run the code, color change does not happen, and the animation hangs immediately. Principally I am wondering if this has to do with my use of Sleep() (which documentation leads me to believe should accept milliseconds as arguments), or if my loop structure in general is the problem. When I debug, though, it seems to exit the loop without throwing an exception.
Code below - any help in resolving the hanging issue?
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>
#include <GL/gl.h>
#include <unistd.h>
//#include <assert.h>
float boxX = 0;
float boxY = 100;
float boxWidth = 75;
float boxHeight = 75;
void display(void)
{
int const windowWidth = glutGet(GLUT_WINDOW_WIDTH);
int const windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
float const aspectRatio = (float)windowWidth / (float)windowHeight;
glClearColor (0.0, 0.0, 0.0, 0.0); /* Set background to black */
glClear (GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(aspectRatio, aspectRatio, aspectRatio, aspectRatio, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
while (boxX != windowWidth)
{
glClear (GL_COLOR_BUFFER_BIT);
float leftCorner = (float)boxX / windowWidth;
float rightCorner = leftCorner + (float)boxWidth / windowWidth;
float bottomCorner = (float)boxY / windowHeight;
float topCorner = bottomCorner + (float)boxY / windowHeight;
glBegin(GL_POLYGON);
glColor3f (rand(), rand(), rand()); //Randomize the box's colour as it moves
glVertex2d(leftCorner, topCorner);
glVertex2d(rightCorner, topCorner);
glVertex2d(rightCorner, bottomCorner);
glVertex2d(leftCorner, bottomCorner);
glEnd();
boxX += 0.1; //Move the box
glutSwapBuffers();
Sleep(200000);
glutPostRedisplay();
}
}
void mouseBehav( int button, int state, int x, int y )
{
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
int windowID = glutCreateWindow ("Rainbow Box Animation");
glutDestroyWindow (windowID);
exit (0);
glutPostRedisplay();
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
boxX = 0;
glutPostRedisplay();
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition ((glutGet(GLUT_SCREEN_WIDTH)-250)/2, (glutGet(GLUT_SCREEN_HEIGHT)-250)/2);
glutCreateWindow ("Rainbow Box Animation");
glutMouseFunc(mouseBehav);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
using sleep() in a glutDisplayFunc() callback
No. Just...no.
Set up a timer callback for ~16-33ms and update boxX in that:
#include <GL/glut.h>
float boxX = 0;
float boxY = 0;
float boxWidth = 75;
float boxHeight = 75;
bool animate = false;
void timer( int value )
{
const int w = glutGet(GLUT_WINDOW_WIDTH);
if( boxX + boxWidth >= w / 2 )
{
animate = false;
}
if( animate )
{
boxX += 1; //Move the box
}
glutTimerFunc( 16, timer, 0 );
glutPostRedisplay();
}
void display()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
const int w = glutGet(GLUT_WINDOW_WIDTH);
const int h = glutGet(GLUT_WINDOW_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( -w/2, w/2, -h/2, h/2, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
const float leftCorner = boxX;
const float rightCorner = leftCorner + boxWidth;
const float bottomCorner = boxY;
const float topCorner = bottomCorner + boxHeight;
glBegin(GL_POLYGON);
glColor3ub(rand()%255, rand()%255, rand()%255); //Randomize the box's colour as it moves
glVertex2d(leftCorner, topCorner);
glVertex2d(rightCorner, topCorner);
glVertex2d(rightCorner, bottomCorner);
glVertex2d(leftCorner, bottomCorner);
glEnd();
glutSwapBuffers();
}
void mouse( int button, int state, int x, int y )
{
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
exit(0);
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
boxX = 0;
animate = true;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition( (glutGet(GLUT_SCREEN_WIDTH)-250)/2, (glutGet(GLUT_SCREEN_HEIGHT)-250)/ 2);
glutCreateWindow ("Rainbow Box Animation");
glutMouseFunc(mouse);
glutDisplayFunc(display);
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 0;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I draw a circle and trying to add mouse event, that change the color of the circle i cannot find good sources
Here is the whole code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define window_width 1080
#define window_height 720
void drawFilledSun() {
//static float angle;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -10);
int i, x, y;
double radius = 0.30;
//glColor3ub(253, 184, 19);
glColor3ub(255, 0, 0);
double twicePi = 2.0 * 3.142;
x = 0, y = 0;
glBegin(GL_TRIANGLE_FAN); //BEGIN CIRCLE
glVertex2f(x, y); // center of circle
for (i = 0; i <= 20; i++) {
glVertex2f(
(x + (radius * cos(i * twicePi / 20))), (y + (radius * sin(i * twicePi / 20)))
);
}
glEnd(); //END
}
void main_loop_function() {
int c;
drawFilledSun();
glutSwapBuffers();
c = getchar();
}
void GL_Setup(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
gluPerspective(45, (float)width / height, .1, 100);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("GLUT Example!!!");
glutIdleFunc(main_loop_function);
GL_Setup(window_width, window_height);
glutMainLoop();
}
Add an array for the red green and blue color components of the sun and a color index:
int color_i = 0;
GLubyte color[][3] = { {255, 0, 0 }, {253, 184, 19} };
Use the color arrays to set the color attribute
void drawFilledSun() {
// [...]
glColor3ubv(color[color_i]);
Get rid of the getchar in main_loop_function, it prevents the window from responding. But call glutPostRedisplay for continuously updating the window:
void main_loop_function() {
drawFilledSun();
glutSwapBuffers();
glutPostRedisplay();
}
Use glutDisplayFunc rather than glutIdleFunc and add a glutMouseFunc callback:
int main(int argc, char** argv) {
// [...]
glutDisplayFunc(main_loop_function);
glutMouseFunc( mouseFunc );
Change the color index in the mouse callback:
void mouseFunc(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
color_i = (color_i == 0) ? 1 : 0;
}
}
See the example:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define window_width 1080
#define window_height 720
int color_i = 0;
GLubyte color[][3] = { {255, 0, 0 }, {253, 184, 19} };
void drawFilledSun() {
//static float angle;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -10);
int i, x, y;
double radius = 0.30;
glColor3ubv(color[color_i]);
double twicePi = 2.0 * 3.142;
x = 0, y = 0;
glBegin(GL_TRIANGLE_FAN); //BEGIN CIRCLE
glVertex2f(x, y); // center of circle
for (i = 0; i <= 20; i++) {
glVertex2f(
(x + (radius * cos(i * twicePi / 20))), (y + (radius * sin(i * twicePi / 20)))
);
}
glEnd(); //END
}
void main_loop_function() {
drawFilledSun();
glutSwapBuffers();
glutPostRedisplay();
}
void GL_Setup(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
gluPerspective(45, (float)width / height, .1, 100);
glMatrixMode(GL_MODELVIEW);
}
void mouseFunc(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
color_i = (color_i == 0) ? 1 : 0;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("GLUT Example!!!");
glutDisplayFunc(main_loop_function);
glutMouseFunc( mouseFunc );
GL_Setup(window_width, window_height);
glutMainLoop();
}
GLFW provided some callback functions which you need such as glfwSetCursorPosCallback.
In this case you can simply check it by a if branch while drawing, main_loop_function in your code. The rest work is about C/C++, and there are many paths to reach it such as kbhit.
By the way, it's not elegant to ues several naming styles.
I've checked every other related question on this site but none of the solutions have worked for me. I'm simply trying to follow my rectangle, which moves left and right with key presses in OpenGL. Here's my very simple program:
/*Begin useful backend functions/vars*/
/*************************************/
//Window size and refresh rate (60 fps)
int width = 500;
int height = 500;
int interval = 1000 / 60;
//Used to draw rectangles
void drawRect(float x, float y, float width, float height) {
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x + width, y);
glVertex2f(x + width, y + height);
glVertex2f(x, y + height);
glEnd();
}
/***********************************/
/*End useful backend functions/vars*/
/*Game vars*/
/***********/
//keycodes
#define keyA 0x41
#define keyD 0x44
//player
int playerWidth = 30;
int playerHeight = 50;
int playerSpeed = 3;
//player starting position
float playerX = width / 2;
float playerY = 25.0f;
/***************/
/*End game vars*/
/*Game specific functions*/
/*************************/
void keyboard() {
//Move player (and camera) on key presses
if (GetAsyncKeyState(keyA)) {
playerX -= playerSpeed;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-playerSpeed,0,0);
}
if (GetAsyncKeyState(keyD)) {
playerX += playerSpeed;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(playerSpeed, 0, 0);
}
}
/********************/
/*End game functions*/
/*Draw and update for window*/
/****************************/
void draw() {
//Initialliy clear
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Draw player
drawRect(playerX, playerY, playerWidth, playerHeight);
//Swap buffers to end
glutSwapBuffers();
}
void update(int value) {
// input handling
keyboard();
// Call update() again in 'interval' milliseconds
glutTimerFunc(interval, update, 0);
// Redisplay frame
glutPostRedisplay();
}
/*****************/
/*End draw/update*/
/*Main function*/
/***************/
int main(int argc, char** argv) {
// initialize opengl (via glut)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutCreateWindow("My Game");
// Register callback functions
glutDisplayFunc(draw);
glutTimerFunc(interval, update, 0);
// setup scene to be 2d
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//set draw color to white
glColor3f(1.0f, 1.0f, 1.0f);
//start the whole thing
glutMainLoop();
return 0;
}
/*************/
/*End of main*/
The keyboard movement works perfectly, however:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-playerSpeed,0,0);
inside my keyboard() function, does nothing. And if I try it with GL_PROJECTION, it turns my screen black.
First of all note, that drawing by glBegin/glEnd sequences and the fixed function matrix stack is deprecated since decades. See Fixed Function Pipeline and Legacy OpenGL.
Simplify things.
Add a the keyboard events for key up and down (glutKeyboardFunc / glutKeyboardUpFunc). This functions only modifies the speed of the player. The speed is set when a button is pressed and is set 0, when a button is release:
int playerSpeed = 0;
void keyboardDown(unsigned char key, int x, int y)
{
if (key == 'a')
playerSpeed -= 3;
else if (key == 'd')
playerSpeed = 3;
}
void keyboardUp( unsigned char key, int x, int y )
{
playerSpeed = 0;
}
The timer event just modifies the position of the player:
void update(int value)
{
playerX += playerSpeed;
glutTimerFunc(interval, update, 0);
glutPostRedisplay();
}
In draw the model matrix is set and the rectangle is drawn:
void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(playerX, playerY, 0);
//Draw player
drawRect(0, 0, playerWidth, playerHeight);
//Swap buffers to end
glutSwapBuffers();
}
Set the callback functions in main:
int main(int argc, char** argv)
{
// ...
glutDisplayFunc(draw);
glutTimerFunc(interval, update, 0);
glutKeyboardFunc( keyboardDown );
glutKeyboardUpFunc( keyboardUp );
// ...
}
#include <stdio.h> // this library is for standard input and output
#include "glut.h"// this library is for glut the OpenGL Utility Toolkit
#include <math.h>
float squareX = 0.0f;
float squareY = -0.3f;
float squareZ = 0.0f;
static int flag = 1;
void drawShape(void) {
glTranslatef(squareX, squareY, squareZ);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(162, 50);
glVertex2f(162, 10);
glVertex2f(220, 10);
glVertex2f(220, 50);
glVertex2f(162, 50);
glEnd();
}
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
// called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, (float)w, 0.0f, (float)h, -1.0f, 1.0f);
}
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawShape();
glutSwapBuffers();
}
// make the square go up
void update(int value) {
if (flag) {
squareY += 1.0f;
if (squareY > 400.0) {
flag = 0;
}
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
// make the square go right
/* void update(int value) {
if (flag) {
squareX += 1.0f;
if (squareX > 400.0) {
flag = 0;
}
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
} */
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("Moving Square");
initRendering();
glutDisplayFunc(drawScene);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);
glutMainLoop();
return(0);
}
I have uploaded this code before but this time I made the square go all the way up. The code just moves the square up, but I don't know how to position it on the left once it reaches the top, so then I can make it move to the right. I have uploaded a demonstration on how I want it to look below.
Preview:
What I want it to do next:
I recommend to initialize the variables squareX, squareY and squareZ with the start position of the rectangle:
float squareX = 162.0f;
float squareY = 0.0f;
float squareZ = 0.0f;
Do not draw a rectangle specific position, but draw a rectangle on the position (0,0) with a length (width, height). Let the model matrix (set by glTranslatef), do the job of the positioning:
void drawShape(void)
{
float width = 58.0f;
float height = 40.0f;
glTranslatef(squareX, squareY, squareZ);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0, 0);
glVertex2f(width, 0);
glVertex2f(width, height);
glVertex2f(0, height);
glVertex2f(0, 0);
glEnd();
}
Use a variable state, which has stated the direction of the current movement:
int state = 1; // 0: stop; 1: move up; 2: move right
If the rectangle a certain position has reached, then the state has to be changed and a the new start position can be set. At the final position, the rectangle can stop or the process can even be restarted:
void update(int value)
{
if (state == 1) // 1 : move up
{
squareY += 1.0f;
if (squareY > 400.0)
{
state = 2;
squareX = 0.0f;
squareY = 180.0f;
}
}
else if (state == 2) // 2 : move right
{
squareX += 1.0f;
if (squareX > 400.0)
{
state = 0;
// restart
//state = 1;
//squareX = 162.0f;
//squareY = 0.0f;
}
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
I am trying to write a simple program that moves a Sphere around a Ellipse in OpenGL. I thought that if I set translate coordinates to the same as the Ellipse coordinates it would simulate motion.
This is the code I already have:
#include "stdafx.h"
#include <windows.h>
#include <GL/Gl.h>
#include <GL/GLU.h>
#include <GL/glut.h>
#include <math.h>
const float eRad = 6.5;
void drawSphere(void)
{
float x = 0.5;
float y = 0.4;
float z = 0.0;
glMatrixMode(GL_PROJECTION | GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
// Draw the Ellipse
glColor3d(1.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP);
for (float i = 0; i <= eRad; i += 0.1)
{
glVertex2f(x*cos(i), y*sin(i));
}
glEnd();
//Draw the Sphere
glColor3d(1.0, 1.0, 0.0);
glPushMatrix();
glTranslatef(0.5, 0, 0);
glutWireSphere(0.15, 30, 30);
glPopMatrix();
glFlush();
}
void animation(void)
{
float x = 0.2;
float y = 0.1;
float z = 0.0;
for (float i = 0; i <= eRad; i += 0.1)
{
glTranslated(x*cos(i), y*sin(i), z);
}
drawSphere();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("Moving Sphere Test");
glutDisplayFunc(drawSphere);
glutIdleFunc(animation);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glViewport(0, 0, 600, 600);
glutMainLoop();
}
The issue I am having is that the Ellipse is drawn in and so is the sphere, but its just staying at one point on the ellipse. So what am I doing wrong?
Yes, you need to use the functions correctly. The animation happens in glutDisplayFunc, the state setting can happen in glutIdleFunc(It does not have to). The gultTimerFunc() callback is what you want to be called every frame. I made a few modifications to your code. But you might want to use the GLUT correctly.
const float eRad = 6.5;
static float iter = 0.0;
void drawSphere(void)
{
float x = 0.5;
float y = 0.4;
float z = 0.0;
glMatrixMode(GL_PROJECTION | GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
// Draw the Ellipse
glColor3d(1.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP);
for (float i = 0; i <= eRad; i += 0.1)
{
glVertex2f(x*cos(i), y*sin(i));
}
glEnd();
//Draw the Sphere
glColor3d(1.0, 1.0, 0.0);
glPushMatrix();
glTranslatef(x*cos(iter), y*sin(iter), 0);
glutWireSphere(0.15, 30, 30);
glPopMatrix();
glFlush();
}
void animation(void)
{
iter = (iter < 6.5) ? iter+0.0001 : 0.0;
}
void Timer(int value) {
glutTimerFunc(33, Timer, 0);
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("Moving Sphere Test");
glutDisplayFunc(drawSphere);
glutIdleFunc(animation);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glViewport(0, 0, 600, 600);
Timer(0);
glutMainLoop();
}
in this code i'm try to draw simple olympic ring and rotate it... the below work fine but i can't rotate the rings.. help me to solve this problme...
void myReshape (int width, int height)
{
glViewport (0, 0, width, height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (-5, 105, -5, 105);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.375, 0.375, 0.0);
}
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(110*PIXEL_SIZE, 110*PIXEL_SIZE);
glutCreateWindow ("Olymipc Rings || rotation ");
glClearColor(1.0, 1.0, 1.0, 0.0);
glPointSize(PIXEL_SIZE);
glShadeModel (GL_FLAT);
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutMainLoop();
return 0;
}
Use glRotatef(axis_x,axis_y,axis,z, angle) function before you draw the rings .
If you want to keep rotating the ring always use glutIdle(myidle) in your main() function and increment the value of angle there and also use glutPostRedisplay().
Use glPushMatrix() before and glPopMatrix() and after your ring drawings if you do not want the rotation to effect other drawings.
for example if you want to rotate your rings about x axis your code will look like
float angle=0;
void display (void) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glVertex2i(-1,-1);
glVertex2i(100,-1);
glVertex2i(100,100);
glVertex2i(-1,100);
glEnd();
glPushMatrix(); //enters temporarily in a stack
for(int i = 0 ; i <5; i++)
{
glRotatef(1,0,0, angle)
glColor3f(color[i][0],color[i][1],color[i][2]);
draw_circle(center[i][0],center[i][1],ring_radius);
}
glPopMatrix(); // comes out of the stack
glScalef(0.001, 0.001, 0.001);
drawText(MESSAGE);
glFlush();
}
void myidle()
{
angle++; //angle value keeps on increasing
glutPostRedisplay(); // draws your drawing with updated value of angle to the screen
}
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(110*PIXEL_SIZE, 110*PIXEL_SIZE);
glutCreateWindow ("Olymipc Rings || rotation ");
glClearColor(1.0, 1.0, 1.0, 0.0);
glPointSize(PIXEL_SIZE);
glShadeModel (GL_FLAT);
glutDisplayFunc(display);
glutIdleFunc(myidle); //just like DisplayFunc keeps on getting calls
glutReshapeFunc(myReshape);
glutMainLoop();
return 0;
Read about glPopMatrix(), glPushMatrix() and call back functions like glutIdleFunc().
I hope this will help!!
Try this:
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <math.h>
#define PIXEL_SIZE 3
#define MESSAGE "hello world !"
void draw_circle(int x, int y, int r);
int ring_radius = 19;
int color[5][3]={{0,0,1}, {0,0,0},{1,0,0}, {1,1,0},{0,1,0}};
int center[5][2]={{15,60},{50,60},{85,60},{33,45},{68,45}};
//=========================================================
void drawText(const char * message)
{
glRasterPos2f((GLfloat)0, (GLfloat)-400);
while (*message)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *message);
glutStrokeCharacter(GLUT_STROKE_ROMAN,*message++);
}
}
void display (void)
{
int ms = glutGet(GLUT_ELAPSED_TIME);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (-5, 105, -5, 105);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.375, 0.375, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glVertex2i(-1,-1);
glVertex2i(100,-1);
glVertex2i(100,100);
glVertex2i(-1,100);
glEnd();
const float deg_per_sec = 60.0f;
float angle = deg_per_sec * ( (float)ms / 1000.0f );
for(int i = 0 ; i <5; i++)
{
glColor3f(color[i][0],color[i][1],color[i][2]);
glPushMatrix();
glTranslatef( center[i][0], center[i][1], 0 );
glRotatef(angle, 0, 0, 1);
glTranslatef( -center[i][0], -center[i][1], 0 );
draw_circle(center[i][0],center[i][1],ring_radius);
glPopMatrix();
}
glScalef(0.001, 0.001, 0.001);
drawText(MESSAGE);
glFlush();
glutSwapBuffers();
}
void draw_circle(int center_x, int center_y , int radius)
{
int r = radius;
int h = 1 - r ; /*initialization */
int x = 0;
int y = r;
int dU=3;
int dD = 5 - 2*r;
int i = center_x;
int j = center_y;
glPointSize(6);
glBegin(GL_POINTS);
while( y > x )
{
if (h<0)
{
dU= dU + 2;
h = h + dU;
x = x + 1;
dD = dD + 2;
}
else
{
dD = 2*(x-y) + 5;
h = h + dD;
x = x + 1;
y = y - 1;
dU = dU + 2;
dD = dD + 4;
}
glVertex2i(x+i, y+j);
glVertex2i(-x+i, y+j);
glVertex2i(x+i, -y+j);
glVertex2i(-x+i,-y+j);
glVertex2i(y+i, x+j);
glVertex2i(y+i, -x+j);
glVertex2i(-y+i, x+j);
glVertex2i(-y+i, -x+j);
}
glEnd();
}
void myReshape (int width, int height)
{
glViewport (0, 0, width, height);
}
void idle()
{
glutPostRedisplay();
}
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(110*PIXEL_SIZE, 110*PIXEL_SIZE);
glutCreateWindow ("Olymipc Rings || rotation ");
glClearColor(1.0, 1.0, 1.0, 0.0);
glPointSize(PIXEL_SIZE);
glShadeModel (GL_FLAT);
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}
glRotatef