Created
February 1, 2019 07:09
-
-
Save ajoydas/66c6a32470846b946947ea633146497c to your computer and use it in GitHub Desktop.
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
//#include <windows.h> | |
#include <GL/glut.h> | |
#include <bits/stdc++.h> | |
#include "bitmap_image.hpp" | |
#define pi (2*acos(0.0)) | |
using namespace std; | |
int drawaxes; | |
struct point{ | |
double x; | |
double y; | |
double z; | |
point() {} | |
point(double x1,double y1,double z1) {x=x1; y=y1; z=z1;} | |
point operator +(point p) {return point(x+p.x,y+p.y, z+p.z);} | |
point operator -(point p) {return point(x-p.x,y-p.y, z-p.z);} | |
point operator *(double d) {return point(x*d,y*d,z*d);} | |
point operator /(double d) {return point(x/d,y/d,z/d);} | |
}; | |
typedef point Vector; | |
double vectorDot(Vector a, Vector b) {return a.x*b.x+a.y*b.y+a.z*b.z;} | |
Vector vectorCross(Vector a, Vector b) {return point(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);} | |
point pos(105,120,80); | |
Vector u(0, 0, 1); | |
Vector r(-1/sqrt(2), 1/sqrt(2), 0); | |
Vector l(-1/sqrt(2), -1/sqrt(2), 0); | |
double rotation_rate = pi/90; | |
double translation_rate = 5; | |
void drawAxes() | |
{ | |
if(drawaxes==1) | |
{ | |
glColor3f(1.0, 1.0, 1.0); | |
glBegin(GL_LINES);{ | |
glVertex3f( 200,0,0); | |
glVertex3f(-200,0,0); | |
glVertex3f(0,-1050,0); | |
glVertex3f(0, 1050,0); | |
glVertex3f(0,0, 200); | |
glVertex3f(0,0,-200); | |
}glEnd(); | |
} | |
} | |
void keyboardListener(unsigned char key, int x,int y){ | |
switch(key){ | |
case '1': | |
l = l * cos(-rotation_rate) + r * sin(-rotation_rate); | |
r = vectorCross(l, u); | |
break; | |
case '2': | |
l = l * cos(rotation_rate) + r * sin(rotation_rate); | |
r = vectorCross(l, u); | |
break; | |
case '3': | |
u = u * cos(-rotation_rate) + l * sin(-rotation_rate); | |
l = vectorCross(u, r); | |
break; | |
case '4': | |
u = u * cos(rotation_rate) + l * sin(rotation_rate); | |
l = vectorCross(u, r); | |
break; | |
case '5': | |
r = r * cos(-rotation_rate) + u * sin(-rotation_rate); | |
u = vectorCross(r, l); | |
break; | |
case '6': | |
r = r * cos(rotation_rate) + u * sin(rotation_rate); | |
u = vectorCross(r, l); | |
break; | |
default: | |
break; | |
} | |
} | |
void specialKeyListener(int key, int x,int y){ | |
switch(key){ | |
case GLUT_KEY_DOWN: //down arrow key | |
pos = pos - l*translation_rate; | |
break; | |
case GLUT_KEY_UP: // up arrow key | |
pos = pos + l*translation_rate; | |
break; | |
case GLUT_KEY_RIGHT: | |
pos = pos + r*translation_rate; | |
break; | |
case GLUT_KEY_LEFT: | |
pos = pos - r*translation_rate; | |
break; | |
case GLUT_KEY_PAGE_UP: | |
pos = pos + u*translation_rate; | |
break; | |
case GLUT_KEY_PAGE_DOWN: | |
pos = pos - u*translation_rate; | |
break; | |
case GLUT_KEY_INSERT: | |
break; | |
case GLUT_KEY_HOME: | |
break; | |
case GLUT_KEY_END: | |
break; | |
default: | |
break; | |
} | |
// cout<<pos.x<<" "<<pos.y<<" "<<pos.z<<endl; | |
} | |
void mouseListener(int button, int state, int x, int y){ //x, y is the x-y of the screen (2D) | |
switch(button){ | |
case GLUT_LEFT_BUTTON: | |
if(state == GLUT_DOWN){ | |
} | |
break; | |
case GLUT_RIGHT_BUTTON: | |
//........ | |
break; | |
case GLUT_MIDDLE_BUTTON: | |
//........ | |
break; | |
default: | |
break; | |
} | |
} | |
void display(){ | |
//clear the display | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glClearColor(0,0,0,0); //color black | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
gluLookAt(pos.x, pos.y, pos.z, pos.x + l.x, pos.y + l.y, pos.z + l.z, | |
u.x, u.y, u.z); | |
//again select MODEL-VIEW | |
glMatrixMode(GL_MODELVIEW); | |
/**************************** | |
/ Add your objects from here | |
****************************/ | |
//add objects | |
drawAxes(); | |
// drawSphere(radius1,segment,stacks, 0); | |
//ADD this line in the end --- if you use double buffer (i.e. GL_DOUBLE) | |
glutSwapBuffers(); | |
} | |
void animate(){ | |
//codes for any changes in Models, Camera | |
glutPostRedisplay(); | |
} | |
void init(){ | |
//codes for initialization | |
drawaxes=1; | |
//clear the screen | |
glClearColor(0,0,0,0); | |
/************************ | |
/ set-up projection here | |
************************/ | |
//load the PROJECTION matrix | |
glMatrixMode(GL_PROJECTION); | |
//initialize the matrix | |
glLoadIdentity(); | |
//give PERSPECTIVE parameters | |
gluPerspective(80, 1, 1, 1500.0); | |
//field of view in the Y (vertically) | |
//aspect ratio that determines the field of view in the X direction (horizontally) | |
//near distance | |
//far distance | |
} | |
int main(int argc, char **argv){ | |
glutInit(&argc,argv); | |
glutInitWindowSize(500, 500); | |
glutInitWindowPosition(0, 0); | |
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB); //Depth, Double buffer, RGB color | |
glutCreateWindow("My OpenGL Program"); | |
init(); | |
glEnable(GL_DEPTH_TEST); //enable Depth Testing | |
glutDisplayFunc(display); //display callback function | |
glutIdleFunc(animate); //what you want to do in the idle time (when no drawing is occuring) | |
glutKeyboardFunc(keyboardListener); | |
glutSpecialFunc(specialKeyListener); | |
glutMouseFunc(mouseListener); | |
glutMainLoop(); //The main loop of OpenGL | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment