Drawing shape using opengl - opengl

how can i draw shape like (sin) if i describe the shape like half circle closed and the other half circle connected with the first one. using Cartesian method this is my trying:
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include<math.h>
#include<cstdlib>
static void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glColor3f(1.0,0.0,0.0);
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
double xc=200, yc=200,r=100;
double x,y;
for (x = xc - r; x<= xc + r;x++)
{
y = sqrt((r*r)-((xc - x)*(xc - x)));
glVertex2d(x, yc + y);
}
for (x = xc +r ; x<= xc - r ; x++)
{
y = sqrt((r*r)-((xc - x)*(xc - x)));
glVertex2d(x , yc - y);
}
glEnd();
glFlush();
}
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0); // set white background color
glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color
glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
glutInitWindowSize(640,480); // set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("Line Scan Conversion"); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
myInit();
glutMainLoop(); // go into a perpetual loop
}

Just sample the function (sin()) directly:
#include <GL/glut.h>
#include <cmath>
static void myDisplay()
{
glClearColor( 0, 0, 0, 1 ); // set white background color
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( -0.5, 7, -1.2, 1.2, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// axes
glColor3ub( 255, 255, 255 );
glBegin( GL_LINES );
glVertex2i( 0, 0 );
glVertex2i( 7, 0 );
glVertex2i( 0, -1 );
glVertex2i( 0, 1 );
glEnd();
// sin() function plot
glColor3ub( 255, 0, 0 );
glBegin(GL_LINE_STRIP);
const unsigned int samples = 100;
for( unsigned int i = 0; i <= samples; ++i )
{
const float pct = ( (float)i / samples );
const float x = 2 * 3.14159 * pct;
const float y = sin( x );
glVertex2f( x, y );
}
glEnd();
glutSwapBuffers();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // set display mode
glutInitWindowSize(600, 600); // set window size
glutCreateWindow("Line Scan Conversion"); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
glutMainLoop(); // go into a perpetual loop
}

There is an issue in your second for loop:
for( x = xc + r; x <= xc - r; x++ )
this is being executed as x = 400; x <= 0; x++ which clearly will never run. Your issue (other than the obvious pointed out above) is that you are trying to account for the x offset in the loop iterations. I suggest changing to the following:
for( x = 0; x < 2r; x++ )
and then instead of
glVertex2d(x, yc + y);
glVertex2d(x, yc - y);
do
glVertex2d((xc - r) + x, yc + y);
glVertex2d((xc + r) + x, yc - y);
or something similar (not in a position to actual test this, but you should get the idea).

Related

adding mouse event to change the color of simple circle [closed]

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.

Trouble drawing Sierpinski with OpenGL

I am trying to generate the Sierpinski Gasket using a function that draws dot patterns and will generate the gasket.
But when I compile and run the program it displays nothing but black screen. What's causing the problem?
Here is my code:
#include <Windows.h>
#include <gl/GL.h>
#include <glut.h>
void myInit(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
class GLintPoint {
public:
GLint x, y;
};
int random(int m) {
return rand() % m;
}
void drawDot(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void Sierpinski(void) {
GLintPoint T[3] = {{ 10, 10 }, {300, 30}, {200, 300}};
int index = random(3);
GLintPoint point = T[index];
for (int i = 0; i < 1000; i++)
{
index = random(3);
point.x = (point.x + T[index].x) / 2;
point.y = (point.y + T[index].y) / 2;
drawDot(point.x, point.y);
}
glFlush();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Sierpinski Gasket");
glutDisplayFunc(drawDot);
myInit();
glutMainLoop();
}
If you want to draw black dots on a white background, then you have to clear glClear the background:
glClear( GL_COLOR_BUFFER_BIT );
Note, glClearColor sets the color which is used to clear the view port, but doesn't clear anything itself.
Your code should look somehow like this:
void drawDot(GLint x, GLint y)
{
glVertex2i(x, y);
}
void Sierpinski(void) {
GLintPoint T[3] = {{ 10, 10 }, {300, 30}, {200, 300}};
int index = random(3);
GLintPoint point = T[index];
glClearColor(1.0, 1.0, 1.0, 1.0); // set up white clear color
glClear( GL_COLOR_BUFFER_BIT ); // clear the back ground (white)
glMatrixMode( GL_MODELVIEW );
glPushMatrix(); // setup model matrix
glScalef( 1.5f, 1.5f, 1.0f ); // scale the point distribution
glColor3f( 0.0f, 0.0f, 0.0f ); // set black draw color
glPointSize( 5.0f ); // set the size of the points
glBegin(GL_POINTS);
for (int i = 0; i < 1000; i++)
{
index = random(3);
point.x = (point.x + T[index].x) / 2;
point.y = (point.y + T[index].y) / 2;
drawDot(point.x, point.y);
}
glEnd();
glPopMatrix(); // reset model matrix
glFlush();
}

Rendering the mandelbrot set in OpenGl

I have to render the mandelbrot set and I was wondering if someone could point out some flaws with my code - at the moment, the output windows just shows a black screen. I think that my mandelbrot mathematics are correct, because I have used the same code to output a .tga of the mandelbrot - is it something to do with the OpenGl method I am using to output the pixels?
Full code:
#include <Windows.h>
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <complex>
#include <fstream>
#include <thread>
#include <mutex>
#include <vector>
#include <Windows.h>
// Import things we need from the standard library
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::complex;
using std::cout;
using std::endl;
using std::ofstream;
// ...other useful includes
using std::cout;
using std::endl;
using std::thread;
using std::mutex;
using std::lock;
using std::unique_lock;
using std::vector;
const int width = 600, height = 600; // window size
int windowID;
// The number of times to iterate before we assume that a point isn't in the
// Mandelbrot set.
// (You may need to turn this up if you zoom further into the set.)
const int MAX_ITERATIONS = 500;
bool fullScreen = false;
bool need_to_draw = true;
//****************************************
// Render the Mandelbrot set into the image array.
// The parameters specify the region on the complex plane to plot.
void compute_mandelbrot(double left, double right, double top, double bottom)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the screen buffer
glBegin(GL_POINTS); // start drawing in single pixel mode
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
// Work out the point in the complex plane that
// corresponds to this pixel in the output image.
complex<double> c(left + (x * (right - left) / width),
top + (y * (bottom - top) / height));
// Start off z at (0, 0).
complex<double> z(0.0, 0.0);
// Iterate z = z^2 + c until z moves more than 2 units
// away from (0, 0), or we've iterated too many times.
int iterations = 0;
while (abs(z) < 2.0 && iterations < MAX_ITERATIONS)
{
z = (z * z) + c;
++iterations;
}
if (iterations == MAX_ITERATIONS)
{
glColor3f(1.0, 0.0, 0.0); // Set color to draw mandelbrot
// z didn't escape from the circle.
// This point is in the Mandelbrot set.
glVertex2i(x, y);
}
else
{
glColor3f(0.0, 0.0, 0.0); //Set pixel to black
// z escaped within less than MAX_ITERATIONS
// iterations. This point isn't in the set.
glVertex2i(x, y);
}
}
}
glEnd();
glutSwapBuffers();
need_to_draw = false;
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GLsizei windowX = (glutGet(GLUT_SCREEN_WIDTH) - width) / 2;
GLsizei windowY = (glutGet(GLUT_SCREEN_HEIGHT) - height) / 2;
glutInitWindowPosition(windowX, windowY);
glutInitWindowSize(width, height);
windowID = glutCreateWindow("Mandelbrot");
if (need_to_draw)
{
compute_mandelbrot(-2.0, 1.0, 1.125, -1.125);
}
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutMainLoop();
return 0;
}
The identity GL_PROJECTION matrix doesn't give you a 1-to-1 unit-to-pixel mapping like you're assuming, it's a +/-(1,1,1) cube.
Use glOrtho() to get the matrix you want:
glOrtho( 0, width, 0, height, -1, 1 );
All together:
#include <GL/glut.h>
#include <complex>
using std::complex;
// Render the Mandelbrot set into the image array.
// The parameters specify the region on the complex plane to plot.
void compute_mandelbrot( double left, double right, double top, double bottom )
{
// The number of times to iterate before we assume that a point isn't in the
// Mandelbrot set.
// (You may need to turn this up if you zoom further into the set.)
const int MAX_ITERATIONS = 500;
const int width = glutGet( GLUT_WINDOW_WIDTH );
const int height = glutGet( GLUT_WINDOW_HEIGHT );
glBegin( GL_POINTS ); // start drawing in single pixel mode
for( int y = 0; y < height; ++y )
{
for( int x = 0; x < width; ++x )
{
// Work out the point in the complex plane that
// corresponds to this pixel in the output image.
complex<double> c( left + ( x * ( right - left ) / width ),
top + ( y * ( bottom - top ) / height ) );
// Start off z at (0, 0).
complex<double> z( 0.0, 0.0 );
// Iterate z = z^2 + c until z moves more than 2 units
// away from (0, 0), or we've iterated too many times.
int iterations = 0;
while( abs( z ) < 2.0 && iterations < MAX_ITERATIONS )
{
z = ( z * z ) + c;
++iterations;
}
if( iterations == MAX_ITERATIONS )
{
glColor3f( 1.0, 0.0, 0.0 ); // Set color to draw mandelbrot
// z didn't escape from the circle.
// This point is in the Mandelbrot set.
glVertex2i( x, y );
}
else
{
glColor3f( 0.0, 0.0, 0.0 ); //Set pixel to black
// z escaped within less than MAX_ITERATIONS
// iterations. This point isn't in the set.
glVertex2i( x, y );
}
}
}
glEnd();
}
void display()
{
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
const int width = glutGet( GLUT_WINDOW_WIDTH );
const int height = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( 0, width, 0, height, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
compute_mandelbrot( -2.0, 1.0, 1.125, -1.125 );
glutSwapBuffers();
}
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize( 300, 300 );
glutCreateWindow( "Mandelbrot" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}

opengl rubberbanded box not showing

I'm trying to draw a selection box over an image drawn using glDrawPixels, but I cannot get it to show. In order to represent the coordinates of the box, I have 4 global integers that I update on mouse actions (the first 2 on click, the others when the mouse is dragged), which I then use in the drawing function. I'm sure that the drawing function is called when the mouse gets dragged, and that the four values at least get updated, since I tried printing them every time the drawing function gets called.
The OpenGL calls I have in the main are:
glutInit(&argc, argv);
glutInitWindowSize(WINDOW_DIM, WINDOW_DIM);
glutInitWindowPosition(0, 0);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutCreateWindow("Window");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glClearColor(0.0, 0.0, 0.0, 1.0);
glDisable(GL_DEPTH_TEST);
glutMainLoop();
and my display function:
void display()
{
printf("calling display\n");
for(unsigned int i=0; i<WINDOW_DIM*WINDOW_DIM; ++i)
{
pixels[i]=colors[img[i]];
}
glClear(GL_COLOR_BUFFER_BIT);
glDrawPixels(WINDOW_DIM, WINDOW_DIM, GL_RGB, GL_FLOAT, (float*)pixels);
glutSwapBuffers();
if(upd_xmin!=0 || upd_ymin!=0 || upd_xmax!=0 || upd_ymax!=0)
{
glDrawBuffer(GL_FRONT);
glLogicOp(GL_XOR);
glEnable(GL_COLOR_LOGIC_OP);
printf("drawing selection\n");
printf("coords %d %d %d %d\n", upd_xmin, upd_ymin, upd_xmax, upd_ymax);
glColor3f(1.0, 1.0, 1.0);
glLineWidth(3.0);
glBegin(GL_LINE_LOOP);
glVertex2i(upd_xmin, upd_ymin);
glVertex2i(upd_xmin, upd_ymax);
glVertex2i(upd_xmax, upd_ymax);
glVertex2i(upd_xmax, upd_ymin);
glEnd();
glDisable(GL_COLOR_LOGIC_OP);
glDrawBuffer(GL_BACK);
}
}
As I said, I don't think the problem is with the mouse and motion function, seeing how the coordinates of the box (the upd_x and upd_y variables in the code above) get updated when there's a mouse event, but if needed I can post those as well.
Don't swap the buffers in the middle. Just draw the selection box on the back-buffer like everything else:
#include <GL/glut.h>
int StartX = -1;
int StartY = -1;
int EndX = -1;
int EndY = -1;
void mouse( int button, int state, int x, int y )
{
if( button == GLUT_LEFT && state == GLUT_DOWN )
{
StartX = x;
StartY = y;
}
if( button == GLUT_LEFT && state == GLUT_UP )
{
StartX = -1;
StartY = -1;
EndX = -1;
EndY = -1;
}
}
void motion( int x, int y )
{
EndX = x;
EndY = y;
glutPostRedisplay();
}
void display()
{
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double ar = w / h;
glOrtho( -2 * ar, 2 * ar, -2, 2, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glBegin( GL_TRIANGLES );
glColor3ub( 255, 0, 0 );
glVertex2i( -1, -1 );
glColor3ub( 0, 255, 0 );
glVertex2i( 1, -1 );
glColor3ub( 0, 0, 255 );
glVertex2i( 0, 1 );
glEnd();
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, w, h, 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
if( StartX > 0 && StartY > 0 && EndX > 0 && EndY > 0 )
{
glLogicOp(GL_XOR);
glEnable(GL_COLOR_LOGIC_OP);
glColor3f(1.0, 1.0, 1.0);
glLineWidth(3.0);
glBegin(GL_LINE_LOOP);
glVertex2i(StartX, StartY);
glVertex2i(EndX, StartY);
glVertex2i(EndX, EndY);
glVertex2i(StartX, EndY);
glEnd();
glDisable(GL_COLOR_LOGIC_OP);
}
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMouseFunc( mouse );
glutMotionFunc( motion );
glutMainLoop();
return 0;
}
Since you weren't specifying them I added projection and modelview matrices.
I believe the problem lies here:
glClear(GL_COLOR_BUFFER_BIT);
glDrawPixels(WINDOW_DIM, WINDOW_DIM, GL_RGB, GL_FLOAT, (float*)pixels);
glutSwapBuffers();
rest of drawing
Your render call should look like:
clear
render
swap buffers
Where render is all of your draw calls (including glDrawPixels).
Also, glDrawPixels was removed in OpenGL 3.2. Unless you absolutely need to use that particular method, why not use an orthographic projection to draw GUI elements (which the selection box can be though of as)?
Edit: Also, be aware that if you make draw calls after glDrawPixels, those may overwrite what you drew with glDrawPixels.

how to rotate this openGl code

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