Created
January 2, 2016 16:41
-
-
Save meshileya/121d957645580b8e46b8 to your computer and use it in GitHub Desktop.
Using opengl to create a 3D circle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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