Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "vector.h"
- #include <cstdlib>
- #include "GL/glut.h"
- #include <cmath>
- #include <cstdio>
- #include <vector>
- using namespace std;
- const double PI = acos(-1.0);
- // baru 03-09-2014
- double a = 10.0, b = 10.0, c = 10.0;
- double speed = 0.1;
- vector4d oby[210][105];
- int point;
- vector<vector4d> points;
- bool mouseState = false;
- void ellipsoid() {
- int i = 0;
- for (double u = -PI; i < 20; u += PI / 10, i++) {
- int j = 0;
- for (double v = -PI / 2; j < 10; v += PI / 10, j++) {
- double x = a * cos(v)*cos(u); oby[i][j].setX(x);
- double y = b * cos(v)*sin(u); oby[i][j].setY(y);
- double z = c * sin(v); oby[i][j].setZ(z);
- }
- }
- }
- void keyboard(unsigned char key, int x, int y) {
- if (key == ' ' && !points.empty())
- points.pop_back();
- if (key == 'a')
- speed -= 0.05;
- if (key == 'd')
- speed += 0.05;
- if (key == 'j')
- a -= 5;
- if (key == 'u')
- a += 5;
- if (key == 'k')
- b -= 5;
- if (key == 'i')
- b += 5;
- if (key == 'l')
- c -= 5;
- if (key == 'o')
- c += 5;
- ellipsoid();
- glutPostRedisplay();
- }
- void mouse(int button, int state, int x, int y) {
- float fx = x/600.f, fy = y/600.0f;
- if (button == 0 && state == 0) {
- for (int i = 0; i < points.size(); i++) {
- if (hypot(fx-points[i].getX(), fy-points[i].getY()) > 0.03) //out circle
- continue;
- point = i;
- points[point].setX(points[i].getX());
- points[point].setY(points[i].getY());
- mouseState = true;
- }
- if (!mouseState)
- points.push_back(vector4d(fx, fy));
- }
- if (button == 0 && state == 1) {
- mouseState = false;
- }
- glutPostRedisplay();
- }
- void motion(int x, int y) {
- if (mouseState) {
- points[point].setX(x/600.0f);
- points[point].setY(y/600.0f);
- }
- glutPostRedisplay();
- }
- void display() {
- glClear(GL_COLOR_BUFFER_BIT);
- // baru 03-09-2014
- glColor3f(0.5, 0.8, 0.1);
- for (int i = 0; i < 20; i++)
- for (int j = 0; j < 9; j++) {
- glBegin(GL_LINES);
- glVertex3f(oby[i][j].getX(), oby[i][j].getY(), oby[i][j].getZ());
- glVertex3f(oby[i][(j+1)%10].getX(), oby[i][(j+1)%10].getY(), oby[i][(j+1)%10].getZ());
- glEnd();
- }
- glColor3f(1.0, 1.0, 0.0);
- for (int j = 0; j < 10; j++)
- for (int i = 0; i < 20; i++) {
- glBegin(GL_LINES);
- glVertex3f(oby[i][j].getX(), oby[i][j].getY(), oby[i][j].getZ());
- glVertex3f(oby[(i+1)%20][j].getX(), oby[(i+1)%20][j].getY(), oby[(i+1)%20][j].getZ());
- glEnd();
- }
- glutSwapBuffers();
- }
- void idle() {
- //speed += 0.5;
- glRotatef(speed, 0, 1, 1);
- glutPostRedisplay();
- }
- void myInit() {
- glMatrixMode (GL_PROJECTION);
- //gluOrtho2D (-1.0, 1.0, 1.0, -1.0);
- glOrtho(-100.0, 100.0, 100.0, -100.0, -1000.0, 1000.0);
- glClearColor(0.0, 0.0, 0.0, 0.1);
- // baru 03-09-2014
- a = 80.0, b = 50.0, c = 50.0;
- ellipsoid();
- }
- int main(int argc, char**argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowSize(600,600);
- glutInitWindowPosition(100,100);
- glutCreateWindow("GeLUT");
- glutDisplayFunc(display);
- glutKeyboardFunc(keyboard);
- glutMouseFunc(mouse);
- glutMotionFunc(motion);
- glutIdleFunc(idle);
- myInit();
- glutMainLoop();
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement