Created
December 27, 2012 14:14
-
-
Save kopiro/4388668 to your computer and use it in GitHub Desktop.
Birilli OpenGL
This file contains hidden or 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
#include <OpenGL/gl.h> | |
#include <OpenGL/glu.h> | |
#include <GLUT/glut.h> | |
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
float base_up = 2*sqrt(3); | |
float base_down = 1; | |
float radius_sphere = 1; | |
float height = 3; | |
int R = 10; | |
int N = 8; | |
float CAMERA_X = 0; | |
float CAMERA_Y = 30; | |
float CAMERA_Z = 80; | |
float MAX_DISTANCE = 10000; | |
typedef struct point | |
{ | |
float x, y, z; | |
point(float _x, float _y, float _z) | |
{ | |
x = _x; | |
y = _y; | |
z = _z; | |
} | |
} Point; | |
void drawPoint(Point* p) | |
{ | |
glVertex3f(p->x, p->y, p->z); | |
} | |
void drawBirillo() | |
{ | |
Point* a = new Point(-base_up/2, 0, base_up/(2*sqrt(3))); | |
Point* b = new Point(+base_up/2, 0, base_up/(2*sqrt(3))); | |
Point* c = new Point(0, 0, -base_up/sqrt(3)); | |
Point* d = new Point(-base_down/2, height, +base_down/(2*sqrt(3))); | |
Point* e = new Point(+base_down/2, height, +base_down/(2*sqrt(3))); | |
Point* f = new Point(0, height, -base_down/sqrt(3)); | |
glColor3f(0,0,1); | |
glBegin(GL_TRIANGLES); | |
drawPoint(a); | |
drawPoint(b); | |
drawPoint(c); | |
drawPoint(d); | |
drawPoint(e); | |
drawPoint(f); | |
glEnd(); | |
glColor3f(1,0,0); | |
glBegin(GL_QUADS); | |
drawPoint(a); | |
drawPoint(b); | |
drawPoint(e); | |
drawPoint(d); | |
drawPoint(d); | |
drawPoint(f); | |
drawPoint(c); | |
drawPoint(a); | |
drawPoint(e); | |
drawPoint(b); | |
drawPoint(c); | |
drawPoint(f); | |
glEnd(); | |
glColor3f(0,1,0); | |
glPushMatrix(); | |
glTranslatef(0, height+radius_sphere, 0); | |
glutSolidSphere(radius_sphere, 50, 50); | |
glPopMatrix(); | |
} | |
void redraw() | |
{ | |
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); | |
glLoadIdentity(); | |
gluLookAt(CAMERA_X,CAMERA_Y,CAMERA_Z, 0,0,0, 0,1,0); | |
int i = 0; | |
for (i=0; i<N; i++) | |
{ | |
glPushMatrix(); | |
float birillo_angle = (2*M_PI*i)/N; | |
float birillo_angle_deg = 360*i/N; | |
cout<<"Birillo angle is: "<<birillo_angle_deg<<"\n"; | |
glTranslatef(R*cos(birillo_angle), 0, R*sin(birillo_angle)); | |
glRotatef(0+birillo_angle_deg, 0,1,0); | |
drawBirillo(); | |
glPopMatrix(); | |
} | |
glutSwapBuffers(); | |
} | |
void reshape(int w, int h) | |
{ | |
float ratio = w*1.0f/h; | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glViewport(5, 5, w-5*2, h-5*2); | |
gluPerspective(20.0f, ratio, 0.1f, MAX_DISTANCE); | |
glMatrixMode(GL_MODELVIEW); | |
} | |
void keysHandler(int k, int x, int y) | |
{ | |
} | |
int main(int argc, char** argv) | |
{ | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE); | |
glutInitWindowSize(1000, 600); | |
glutCreateWindow("Birilli | OpenGL"); | |
glutReshapeFunc(reshape); | |
glutDisplayFunc(redraw); | |
glutSpecialFunc(keysHandler); | |
glEnable(GL_DEPTH_TEST); | |
glEnable(GL_CULL_FACE); | |
glutMainLoop(); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment