Skip to content

Instantly share code, notes, and snippets.

@meshileya
Created January 2, 2016 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meshileya/121d957645580b8e46b8 to your computer and use it in GitHub Desktop.
Save meshileya/121d957645580b8e46b8 to your computer and use it in GitHub Desktop.
Using opengl to create a 3D circle
// circle_assignment.cpp : Defines the entry point for the console application.
//to draw a circle using 3 dimentional graph style
#include "stdafx.h"
#include "glut.h"
GLfloat xRotated, yRotated, zRotated;
GLdouble radius=1;//to set the radius of the circle
void display()
{
glMatrixMode(GL_MODELVIEW);//clears the identity matrix
glClear(GL_COLOR_BUFFER_BIT);//clears the buffer drawing
glLoadIdentity();
// traslate the draw by z = -4.0
// Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
glTranslatef(0.0,0.0,-5.0);
glColor3f(0.9, 0.0, 0.9); // Red color used to draw.
// changing in transformation matrix.
// rotation about X axis
glRotatef(xRotated,1.0,0.0,0.0);
glRotatef(yRotated,0.0,1.0,0.0);// rotation about Y axis
glRotatef(zRotated,0.0,0.0,1.0);// rotation about Z axis
glScalef(1.0,1.0,1.0);// scaling transfomation
// NOTE: built-in (glut library) function , draw you a sphere.
glutSolidSphere(radius,20,20);
glFlush(); // This makes the drawing happen as soon as possible
}
void init(int x, int y)
{
if (y == 0 || x == 0) return;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(39.0,(GLdouble)x/(GLdouble)y,0.6,21.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y); //Use the whole window for rendering
}
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(500,500);
glutCreateWindow("Our purple Sphere");
xRotated = yRotated = zRotated = 30.0;
xRotated=43;
yRotated=50;
glutDisplayFunc(display);
glutReshapeFunc(init);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment