Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- #include <windows.h>
- #include <glut.h>
- #define pi (2*acos(0.0))
- int drawgrid;
- int drawaxes;
- struct point
- {
- double x,y,z;
- };
- struct point pos;
- struct point left;
- struct point right;
- struct point up;
- double limit;
- double angleX1;
- double angleY1;
- double angleX2;
- double angleY2;
- double gunlength1;
- double gunlength2;
- double gunradius1;
- double gunradius2;
- double walldistance;
- double walllength;
- int numberOfBullets;
- struct point bullets[200];
- bool isRightButtonDown;
- 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,-200,0);
- glVertex3f(0, 200,0);
- glVertex3f(0,0, 200);
- glVertex3f(0,0,-200);
- }glEnd();
- }
- }
- void drawGun1(double height, double radius, double slices, double stacks) {
- glPushMatrix();
- glRotatef(90,0,1,0);
- glTranslatef(0,0,-radius);
- struct point points[100][100];
- int i,j;
- double h,r;
- for(i=0;i<=stacks;i++)
- {
- h=radius*sin(((double)i/(double)stacks)*(pi/2));
- r=radius*cos(((double)i/(double)stacks)*(pi/2));
- for(j=0;j<=slices;j++)
- {
- points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
- points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
- points[i][j].z=h;
- }
- }
- for(i=0;i<stacks;i++) {
- for(j=0;j<slices;j++) {
- if(j%5 >= 3) glColor3f(1.0, 1.0, 1.0);
- else glColor3f(0, 0, 0);
- glBegin(GL_QUADS);
- glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
- glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
- glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
- glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
- glEnd();
- }
- }
- for(j=0;j<slices;j++) {
- if(j%5 >= 3) glColor3f(1.0, 1.0, 1.0);
- else glColor3f(0, 0, 0);
- glBegin(GL_QUADS);
- glVertex3f(points[0][j].x,points[0][j].y,points[0][j].z);
- glVertex3f(points[0][j+1].x,points[0][j+1].y,points[0][j+1].z);
- glVertex3f(points[0][j+1].x,points[0][j+1].y,points[0][j+1].z - height);
- glVertex3f(points[0][j].x,points[0][j].y,points[0][j].z - height);
- glEnd();
- }
- for(i=0;i<stacks;i++) {
- for(j=0;j<slices;j++) {
- if(j%5 >= 3) glColor3f(1.0, 1.0, 1.0);
- else glColor3f(0, 0, 0);
- glBegin(GL_QUADS);
- glVertex3f(points[i][j].x,points[i][j].y,-points[i][j].z - height);
- glVertex3f(points[i][j+1].x,points[i][j+1].y,-points[i][j+1].z - height);
- glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,-points[i+1][j+1].z - height);
- glVertex3f(points[i+1][j].x,points[i+1][j].y,-points[i+1][j].z - height);
- glEnd();
- }
- }
- glPopMatrix();
- }
- void drawGun2(double height, double radius, double slices, double stacks) {
- glPushMatrix();
- glRotatef(90,0,1,0);
- glTranslatef(0,0,-radius);
- struct point points[100][100];
- int i,j;
- double h,r;
- //generate points
- for(i=0;i<=stacks;i++)
- {
- h=radius*sin(((double)i/(double)stacks)*(pi/2));
- r=radius*cos(((double)i/(double)stacks)*(pi/2));
- for(j=0;j<=slices;j++)
- {
- points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
- points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
- points[i][j].z=h;
- }
- }
- for(i=0;i<stacks;i++) {
- for(j=0;j<slices;j++) {
- if(j%5 >= 3) glColor3f(1.0, 1.0, 1.0);
- else glColor3f(0, 0, 0);
- glBegin(GL_QUADS);
- glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
- glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
- glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
- glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
- glEnd();
- }
- }
- for(j=0;j<slices;j++) {
- if(j%5 >= 3) glColor3f(1.0, 1.0, 1.0);
- else glColor3f(0, 0, 0);
- glBegin(GL_QUADS);
- glVertex3f(points[0][j].x,points[0][j].y,points[0][j].z);
- glVertex3f(points[0][j+1].x,points[0][j+1].y,points[0][j+1].z);
- glVertex3f(points[0][j+1].x,points[0][j+1].y,points[0][j+1].z - height);
- glVertex3f(points[0][j].x,points[0][j].y,points[0][j].z - height);
- glEnd();
- }
- for(i=0;i<=stacks;i++) {
- h=radius*sin(((double)i/(double)stacks)*(pi/2));
- r=2*radius - radius*cos(((double)i/(double)stacks)*(pi/2));
- for(j=0;j<=slices;j++)
- {
- points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
- points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
- points[i][j].z=h;
- }
- }
- for(i=0;i<stacks;i++) {
- for(j=0;j<slices;j++) {
- if(j%5 >= 3) glColor3f(1.0, 1.0, 1.0);
- else glColor3f(0, 0, 0);
- glBegin(GL_QUADS);
- glVertex3f(points[i][j].x,points[i][j].y,-points[i][j].z - height);
- glVertex3f(points[i][j+1].x,points[i][j+1].y,-points[i][j+1].z - height);
- glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,-points[i+1][j+1].z - height);
- glVertex3f(points[i+1][j].x,points[i+1][j].y,-points[i+1][j].z - height);
- glEnd();
- }
- }
- glPopMatrix();
- }
- void drawWall(double distance, double length) {
- glColor3f(0.5,0.5,0.5);
- glBegin(GL_QUADS);
- glVertex3f(distance, length/2, length/2);
- glVertex3f(distance, -length/2, length/2);
- glVertex3f(distance, -length/2, -length/2);
- glVertex3f(distance, length/2, -length/2);
- glEnd();
- }
- void drawGulis() {
- int i;
- glColor3f(1.0,0,0);
- for(i=0;i<numberOfBullets;i++) {
- glBegin(GL_QUADS);
- glVertex3f(-bullets[i].x, -(bullets[i].y+3), bullets[i].z+3);
- glVertex3f(-bullets[i].x, -(bullets[i].y-3), bullets[i].z+3);
- glVertex3f(-bullets[i].x, -(bullets[i].y-3), bullets[i].z-3);
- glVertex3f(-bullets[i].x, -(bullets[i].y+3), bullets[i].z-3);
- glEnd();
- }
- }
- point calculateIntersect() {
- double a = gunlength1 + 2*gunradius1;
- double b = gunlength2 + 2*gunradius2;
- double c = walldistance - 10;
- point p;
- double t = (c - a*cos(angleY1 * pi/180)*cos(angleX1 * pi/180)) / (b*cos(angleY1 * pi/180+angleY2 * pi/180)*cos(angleX1 * pi/180));
- p.x = c;
- p.y = a*cos(angleY1 * pi/180)*sin(angleX1 * pi/180) + t * b*cos(angleY1 * pi/180+angleY2 * pi/180)*sin(angleX1 * pi/180);
- p.z = a*sin(angleY1 * pi/180) + t * b*sin(angleY1 * pi/180+angleY2 * pi/180);
- if(p.y < -walllength/2 || p.y > walllength/2 || p.z < -walllength/2 || p.z > walllength/2) p.x = 0;
- return p;
- }
- point lastpointofgun2() {
- double a = gunlength1 + 2*gunradius1;
- double b = gunlength2 + 2*gunradius2;
- point p;
- p.x = a*cos(angleY1 * pi/180)*cos(angleX1 * pi/180) + b*cos(angleY1 * pi/180+angleY2 * pi/180)*cos(angleX1 * pi/180);
- p.y = a*cos(angleY1 * pi/180)*sin(angleX1 * pi/180) + b*cos(angleY1 * pi/180+angleY2 * pi/180)*sin(angleX1 * pi/180);
- p.z = a*sin(angleY1 * pi/180) + b*sin(angleY1 * pi/180+angleY2 * pi/180);
- return p;
- }
- point getRotatedVector(point a, point b, double angle, bool pls) {
- if(!pls) angle = -angle;
- point val;
- val.x = a.x * cos(angle) + b.x * sin(angle);
- val.y = a.y * cos(angle) + b.y * sin(angle);
- val.z = a.z * cos(angle) + b.z * sin(angle);
- return val;
- }
- void rotateVector(int type, int rot) {
- double cameraAngle;
- if(rot == 1) cameraAngle = 2.0 * (pi / 180);
- else if(rot == -1) cameraAngle = -2.0 * (pi / 180);
- if(type == 1) {
- point l = getRotatedVector(left, right, cameraAngle, true);
- point r = getRotatedVector(right, left, cameraAngle, false);
- left = l;
- right = r;
- }
- else if(type == 2) {
- point l = getRotatedVector(left, up, cameraAngle, true);
- point u = getRotatedVector(up, left, cameraAngle, false);
- left = l;
- up = u;
- }
- else {
- point r = getRotatedVector(right, up, cameraAngle, true);
- point u = getRotatedVector(up, right, cameraAngle, false);
- right = r;
- up = u;
- }
- }
- void keyboardListener(unsigned char key, int x,int y){
- switch(key){
- case '1':
- rotateVector(1,-1);
- break;
- case '2':
- rotateVector(1,1);
- break;
- case '3':
- rotateVector(2,1);
- break;
- case '4':
- rotateVector(2,-1);
- break;
- case '5':
- rotateVector(3,-1);
- break;
- case '6':
- rotateVector(3,1);
- break;
- case 'q':
- if(angleX1 < limit)
- angleX1 += 2;
- break;
- case 'w':
- if(angleX1 > -limit)
- angleX1 -= 2;
- break;
- case 'e':
- if(angleY1 < limit)
- angleY1 += 2;
- break;
- case 'r':
- if(angleY1 > -limit)
- angleY1 -= 2;
- break;
- case 'a':
- if(angleY2 < limit)
- angleY2 += 2;
- break;
- case 's':
- if(angleY2 > -limit)
- angleY2 -= 2;
- break;
- case 'd':
- if(angleX2 < limit)
- angleX2 += 2;
- break;
- case 'f':
- if(angleX2 > -limit)
- angleX2 -= 2;
- break;
- default:
- break;
- }
- }
- void specialKeyListener(int key, int x,int y){
- switch(key){
- case GLUT_KEY_DOWN:
- pos.x-=(left.x*2);
- pos.y-=(left.y*2);
- pos.z-=(left.z*2);
- break;
- case GLUT_KEY_UP:
- pos.x+=(left.x*2);
- pos.y+=(left.y*2);
- pos.z+=(left.z*2);
- break;
- case GLUT_KEY_RIGHT:
- pos.x+=(right.x*2);
- pos.y+=(right.y*2);
- pos.z+=(right.z*2);
- break;
- case GLUT_KEY_LEFT:
- pos.x-=(right.x*2);
- pos.y-=(right.y*2);
- pos.z-=(right.z*2);
- break;
- case GLUT_KEY_PAGE_UP:
- pos.x+=(up.x*2);
- pos.y+=(up.y*2);
- pos.z+=(up.z*2);
- break;
- case GLUT_KEY_PAGE_DOWN:
- pos.x-=(up.x*2);
- pos.y-=(up.y*2);
- pos.z-=(up.z*2);
- break;
- case GLUT_KEY_INSERT:
- break;
- case GLUT_KEY_HOME:
- break;
- case GLUT_KEY_END:
- break;
- default:
- break;
- }
- }
- void add_bullets() {
- point p = calculateIntersect();
- if(p.x != 0) {
- bullets[numberOfBullets++] = p;
- }
- }
- 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){ // 2 times?? in ONE click? -- solution is checking DOWN or UP
- add_bullets();
- }
- break;
- case GLUT_RIGHT_BUTTON:
- if(state == GLUT_DOWN){ // 2 times?? in ONE click? -- solution is checking DOWN or UP
- isRightButtonDown = TRUE;
- }
- else {
- isRightButtonDown = FALSE;
- }
- break;
- case GLUT_MIDDLE_BUTTON:
- //........
- break;
- default:
- break;
- }
- }
- void scope() {
- point p = lastpointofgun2();
- point q = calculateIntersect();
- if(q.x != 0) {
- gluLookAt(-p.x, -p.y, p.z, -q.x, -q.y, q.z, 0,0,1);
- //printf("p : %lf , %lf, %lf\n",p.x,p.y,p.z);
- //printf("q : %lf , %lf, %lf\n",q.x,q.y,q.z);
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_LINES);
- {
- glVertex3f(-q.x,-q.y,q.z + 5);
- glVertex3f(-q.x,-q.y,q.z - 5);
- glVertex3f(-q.x,-q.y - 5,q.z);
- glVertex3f(-q.x,-q.y + 5,q.z);
- }
- glEnd();
- }
- }
- void display(){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glClearColor(0,0,0,0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- if(isRightButtonDown) {
- scope();
- }
- else {
- gluLookAt(pos.x, pos.y, pos.z, pos.x+left.x, pos.y+left.y, pos.z+left.z, up.x,up.y,up.z);
- //printf("pos : %lf , %lf, %lf\n",pos.x,pos.y,pos.z);
- //printf("pos+ left : %lf , %lf, %lf\n",pos.x+left.x, pos.y+left.y, pos.z+left.z);
- }
- glMatrixMode(GL_MODELVIEW);
- drawAxes();
- drawWall(-walldistance,walllength);
- drawGulis();
- glRotatef(angleX1,0,0,1);
- glRotatef(angleY1,0,1,0);
- drawGun1(gunlength1,gunradius1,50,50);
- glTranslatef(-(gunlength1 + 2*gunradius1),0,0);
- glRotatef(angleY2,0,1,0);
- glRotatef(angleX2,1,0,0);
- drawGun2(gunlength2,gunradius2,50,50);
- glutSwapBuffers();
- }
- void animate(){
- glutPostRedisplay();
- }
- void init(){
- drawgrid=0;
- drawaxes=1;
- pos.x = 100;
- pos.y = 100;
- pos.z = 20;
- left.x = -1/sqrt(2);
- left.y = -1/sqrt(2);
- left.z = 0;
- right.x = -1/sqrt(2);
- right.y = 1/sqrt(2);
- right.z = 0;
- up.x = 0;
- up.y = 0;
- up.z = 1;
- limit = 45;
- angleX1 = 0;
- angleY1 = 0;
- angleX2 = 0;
- angleY2 = 0;
- gunlength1 = 20;
- gunlength2 = 40;
- gunradius1 = 10;
- gunradius2 = 5;
- walldistance = 200;
- walllength = 150;
- numberOfBullets = 0;
- isRightButtonDown = FALSE;
- glClearColor(0,0,0,0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(80, 1, 1, 1000.0);
- }
- int main(int argc, char **argv){
- glutInit(&argc,argv);
- glutInitWindowSize(500, 500);
- glutInitWindowPosition(0, 0);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
- glutCreateWindow("My OpenGL First Offline");
- init();
- glEnable(GL_DEPTH_TEST);
- glutDisplayFunc(display);
- glutIdleFunc(animate);
- glutKeyboardFunc(keyboardListener);
- glutSpecialFunc(specialKeyListener);
- glutMouseFunc(mouseListener);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement