3D Primitives with OpenGL
OpenGL is mainly considered an API (an Application
Programming Interface) that provides us with a large set of functions
that we can use to manipulate graphics and images. However, OpenGL by itself is
not an API, but merely a specification, developed and maintained by the Khronos
Group.
The OpenGL specification specifies exactly
what the result/output of each function should be and how it should perform. It
is then up to the developers implementing this
specification to come up with a solution of how this function should operate.
Since the OpenGL specification does not give us implementation details, the
actual developed versions of OpenGL are allowed to have different
implementations, as long as their results comply with the specification (and
are thus the same to the user).
Output Screenshots:
Code:
#include <GL\glut.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
using namespace std;
GLfloat xAngle, yAngle, zAngle;
GLdouble radius = 1, side = 1, b_radius = 1, height = 3, inner_radius = 1, outer_radius = 2;
int option;
void display(void);
void reshape(int x, int y);
void get_key(unsigned char key, int x, int y) {
option = int(key) - int('0');
switch (option) {
case 1:
cout << "You have chosen Sphere\nEnter radius:";
cin >> radius;
cout << "\n";
break;
case 2:
cout << "YOu have chosen Cube\nEnter side length: ";
cin >> side;
cout << "\n";
break;
case 3:
cout << "You have chose Cone\nEnter base radius: ";
cin >> b_radius;
cout << "Enter height: ";
cin >> height;
cout << "\n";
break;
case 4:
cout << "You have chosen Torus\nEnter inner radius: ";
cin >> inner_radius;
cout << "Enter outer radius: ";
cin >> outer_radius;
cout << "\n";
break;
}
}
void idle(void)
{
xAngle += 0.01;
yAngle += 0.01;
zAngle += 0.01;
display();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowPosition(350, 350);
glutInitWindowPosition(500, 500);
glutCreateWindow("3D Primitives in OpenGl");
xAngle = yAngle = zAngle = 0;
cout << "Enter option\n1.Sphere\n2.Cube\n3.Cone\n4.Torus\n5.Dodecahedron\n6.Octahedron\n7.Tetrahedron";
cin >> option;
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(get_key);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}
void display(void)
{
//initalize
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
//object depth
glTranslatef(0.0, 0.0, -3.0);
//changing transformation matrix
//rotation about X aixs
glRotatef(xAngle, 1.0, 0.0, 0.0);
//Y axis
glRotatef(yAngle, 0.0, 1.0, 0.0);
//Z axis
glRotatef(zAngle, 0.0, 0.0, 1.0);
//scaling
glScalef(1.0, 1.0, 1.0);
switch (option) {
case 1:
glutWireSphere(radius, 10, 10);
break;
case 2:
glutWireCube(side);
break;
case 3:
glutWireCone(b_radius, height, 10, 10);
break;
case 4:
glutWireTorus(inner_radius, outer_radius, 10, 10);
break;
case 5:
glutWireDodecahedron();
break;
case 6:
glutWireOctahedron();
break;
case 7:
glutWireTetrahedron();
break;
case 0:
cout << "Exit\n";
return;
}
glFlush();
}
void reshape(int x, int y)
{
if (y == 0 || x == 0) return;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(120, (GLdouble)x / (GLdouble)y, 0.6, 21.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, x, y);
}
#include <stdlib.h>
#include <math.h>
#include <iostream>
using namespace std;
GLfloat xAngle, yAngle, zAngle;
GLdouble radius = 1, side = 1, b_radius = 1, height = 3, inner_radius = 1, outer_radius = 2;
int option;
void display(void);
void reshape(int x, int y);
void get_key(unsigned char key, int x, int y) {
option = int(key) - int('0');
switch (option) {
case 1:
cout << "You have chosen Sphere\nEnter radius:";
cin >> radius;
cout << "\n";
break;
case 2:
cout << "YOu have chosen Cube\nEnter side length: ";
cin >> side;
cout << "\n";
break;
case 3:
cout << "You have chose Cone\nEnter base radius: ";
cin >> b_radius;
cout << "Enter height: ";
cin >> height;
cout << "\n";
break;
case 4:
cout << "You have chosen Torus\nEnter inner radius: ";
cin >> inner_radius;
cout << "Enter outer radius: ";
cin >> outer_radius;
cout << "\n";
break;
}
}
void idle(void)
{
xAngle += 0.01;
yAngle += 0.01;
zAngle += 0.01;
display();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowPosition(350, 350);
glutInitWindowPosition(500, 500);
glutCreateWindow("3D Primitives in OpenGl");
xAngle = yAngle = zAngle = 0;
cout << "Enter option\n1.Sphere\n2.Cube\n3.Cone\n4.Torus\n5.Dodecahedron\n6.Octahedron\n7.Tetrahedron";
cin >> option;
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(get_key);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}
void display(void)
{
//initalize
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
//object depth
glTranslatef(0.0, 0.0, -3.0);
//changing transformation matrix
//rotation about X aixs
glRotatef(xAngle, 1.0, 0.0, 0.0);
//Y axis
glRotatef(yAngle, 0.0, 1.0, 0.0);
//Z axis
glRotatef(zAngle, 0.0, 0.0, 1.0);
//scaling
glScalef(1.0, 1.0, 1.0);
switch (option) {
case 1:
glutWireSphere(radius, 10, 10);
break;
case 2:
glutWireCube(side);
break;
case 3:
glutWireCone(b_radius, height, 10, 10);
break;
case 4:
glutWireTorus(inner_radius, outer_radius, 10, 10);
break;
case 5:
glutWireDodecahedron();
break;
case 6:
glutWireOctahedron();
break;
case 7:
glutWireTetrahedron();
break;
case 0:
cout << "Exit\n";
return;
}
glFlush();
}
void reshape(int x, int y)
{
if (y == 0 || x == 0) return;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(120, (GLdouble)x / (GLdouble)y, 0.6, 21.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, x, y);
}
No comments:
Post a Comment