Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Template1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <math.h>
- #include <iostream>
- #include <Windows.h>
- #include "stb_image.h"
- #include "include\GL\GL.H"
- #include "include\GL\GLU.H"
- #include "include\GL\glut.h"
- //#include "Template1.h"
- #define PI 3.14
- using namespace std;
- double rotate_z = 0;
- double rotate_x = -260;
- double zoom = -32;
- double horizontal = 0;
- double vertical = 10;
- double stretch = 1;
- double flag = 0.1;
- double swing = 0;
- int sign = 1;
- void drawSpringPoints(void) {
- double x, y, z;
- double t, u;
- glBegin(GL_POINTS);
- for (t = 0; t < 8 * PI; t += 0.1) {
- for (u = 0; u < 2 * PI; u += 0.1) {
- x = cos(t) * (3 + cos(u));
- y = sin(t) * (3 + cos(u));
- z = 0.6 * t + sin(u);
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3d(x, y, z);
- }
- }
- glEnd();
- }
- void setColor(char index) {
- switch (index) {
- case 'r':
- glColor3f(1.0f, 0.0f, 0.0f);
- break;
- case 'g':
- glColor3f(0.0f, 1.0f, 0.0f);
- break;
- case 'b':
- glColor3f(0.0f, 0.0f, 1.0f);
- break;
- case 'w':
- glColor3f(1.0f, 1.0f, 0.0f);
- break;
- case 'y':
- glColor3f(1.0f, 1.0f, 0.0f);
- break;
- case 'm':
- glColor3f(1.0f, 0.0f, 1.0f);
- break;
- case 'c':
- glColor3f(0.0f, 1.0f, 1.0f);
- break;
- default:
- glColor3f(1.0f, 0.0f, 1.0f);
- }
- }
- void drawSpring(void) {
- double x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4;
- double t, u;
- int color = 0;
- double max_y = 0;
- double max_x = 0;
- double min_y = 0;
- double min_x = 0;
- setColor('b');
- //glTranslated(0.0, 0.0, -4.0);
- for (t = 0.1; t < 8 * PI; t += 0.1) {
- for (u = 0; u < 2 * PI; u += 0.5) {
- x1 = cos(t) * (3 + cos(u));
- y1 = sin(t) * (3 + cos(u));
- z1 = 0.6 * t + sin(u);
- x2 = cos(t + 0.1) * (3 + cos(u));
- y2 = sin(t + 0.1) * (3 + cos(u));
- z2 = 0.6 * (t + 0.1) + sin(u);
- x3 = cos(t) * (3 + cos(u + 0.5));
- y3 = sin(t) * (3 + cos(u + 0.5));
- z3 = 0.6 * t + sin(u + 0.5);
- x4 = cos(t + 0.1) * (3 + cos(u + 0.5));
- y4 = sin(t + 0.1) * (3 + cos(u + 0.5));
- z4 = 0.6 * (t + 0.1) + sin(u + 0.5);
- //setColor(color % 4 + 1);
- glBegin(GL_QUADS);
- glVertex3d(x1, y1, stretch * z1);
- glVertex3d(x3, y3, stretch * z3);
- glVertex3d(x4, y4, stretch * z4);
- glVertex3d(x2, y2, stretch * z2);
- glEnd();
- color += 1;
- if (x1 > max_x) {
- max_x = x1;
- }
- if (y1 > max_y) {
- max_y = y1;
- }
- if (x1 < min_x) {
- min_x = x1;
- }
- if (y1 < min_y) {
- min_y = y1;
- }
- //cout << "x: " << x1 << " y: " << y1 << " z: " << z1 << endl;
- }
- }
- //cout << "stretch " << stretch << endl;
- //cout << "max x: " << max_x << " max y: " << max_y << endl;
- //cout << "min x: " << min_x << " min y: " << min_y << endl;
- /*
- glColor3f(1.0f, 0.0f, 1.0f);
- glBegin(GL_QUADS);
- glVertex3d(x1, y1, z1);
- glVertex3d(x3, y3, z3);
- glVertex3d(x4, y4, z4);
- glVertex3d(x2, y2, z2);
- glEnd();
- */
- }
- void DrawReferenceLines(void){
- //x is dark green
- glColor3f(0.137255, 0.556863, 0.137255);
- glBegin(GL_LINES);
- glVertex3f(-250.0, 0.0, 0.0);
- glVertex3f(250.0, 0.0, 0.0);
- glEnd();
- //y red
- glColor3f(1.0, 0.0, 0.0);
- glBegin(GL_LINES);
- glVertex3f(0.0, -250.0, 0.0);
- glVertex3f(0.0, 250.0, 0.0);
- glEnd();
- //z orange
- glColor3f(1.0, 0.5, 0.0);
- glBegin(GL_LINES);
- glVertex3f(0.0, 0.0, -250.0);
- glVertex3f(0.0, 0.0, 250.0);
- glEnd();
- }
- void drawCylinder(void) {
- setColor('b');
- GLUquadric* gluQuadratic = NULL;
- gluQuadratic = gluNewQuadric();
- glPushMatrix();
- glTranslated(0.0, 0.0, -4.0);
- gluCylinder(gluQuadratic, 1.0, 1.0, 4.0, 20, 20);
- glPopMatrix();
- glPushMatrix(); //up end modeling
- gluSphere(gluQuadratic, 1.0, 20, 20);
- glRotated(90, 0.0, 1.0, 0.0);
- gluCylinder(gluQuadratic, 1.0, 1.0, 3.0, 20, 20);
- glTranslated(0.0, 0.0, 3.0);
- gluSphere(gluQuadratic, 1.0, 20, 20);
- glPopMatrix();
- // glPushMatrix();
- //how the ball should move
- glPushMatrix(); //the entire bottom part, must work with the animation
- //glTranslated(0.0, 0.0, (abs(stretch - 1) * 1.6));
- glTranslated(0.0, 0.0, 16.0 + swing); //up and down movement
- glPushMatrix();
- // setColor('w');
- glTranslated(0.0, 0.0, -0.5);
- gluSphere(gluQuadratic, 1.0, 20, 20);
- //glTranslated(0.0, 0.0, -0.5);
- glPushMatrix();
- glRotated(100, 0.0, 1.0, 0.0);
- gluCylinder(gluQuadratic, 1.0, 1.0, 3.0, 20, 20);
- glPopMatrix();
- glRotated(90, 0.0, 1.0, 0.0);
- //setColor('g');
- glTranslated(0.5, 0.0, 3.0);
- gluSphere(gluQuadratic, 1.0, 20, 20);
- glPopMatrix();
- //ready part
- setColor('b');
- gluCylinder(gluQuadratic, 1.0, 1.0, 4.0, 20, 20); //end of the spring
- setColor('r');
- glTranslated(0.0, 0.0, 6.0);
- gluSphere(gluQuadratic, 4.0, 20, 20); //ball at the end of the spring
- glPopMatrix();
- //glPopMatrix();
- }
- void drawBoard(void) {
- setColor('r');
- //bottom
- glBegin(GL_POLYGON);
- //glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3d(-4, -4, -4);
- glVertex3d(-4, 4, -4);
- glVertex3d(4, 4, -4);
- glVertex3d(4, -4, -4);
- glEnd();
- //top
- glBegin(GL_POLYGON);
- //glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3d(-4, -4, -5);
- glVertex3d(-4, 4, -5);
- glVertex3d(4, 4, -5);
- glVertex3d(4, -4, -5);
- glEnd();
- //right side
- glBegin(GL_POLYGON);
- //glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3d(4, -4, -4);
- glTexCoord2f(0.0, 0.0);
- glVertex3d(4, -4, -5);
- glTexCoord2f(0.0, 1.0);
- glVertex3d(4, 4, -5);
- glTexCoord2f(1.0, 1.0);
- glVertex3d(4, 4, -4);
- glTexCoord2f(1.0, 0.0);
- glEnd();
- //left side
- glBegin(GL_POLYGON);
- //glColor3f(1.0f, 1.0f, 0.0f);
- glVertex3d(-4, -4, -4);
- glVertex3d(-4, -4, -5);
- glVertex3d(-4, 4, -5);
- glVertex3d(-4, 4, -4);
- glEnd();
- //top
- glBegin(GL_POLYGON);
- //glColor3f(1.0f, 0.0f, 1.0f);
- glVertex3d(-4, -4, -4);
- glVertex3d(-4, -4, -5);
- glVertex3d(4, -4, -5);
- glVertex3d(4, -4, -4);
- glEnd();
- //bottom
- glBegin(GL_POLYGON);
- //glColor3f(0.0f, 1.0f, 1.0f);
- glVertex3d(-4, 4, -4);
- glVertex3d(-4, 4, -5);
- glVertex3d(4, 4, -5);
- glVertex3d(4, 4, -4);
- glEnd();
- }
- void timer(int) {
- if (stretch >= 1.5) {
- flag = -0.1;
- sign = -1;
- }
- if (stretch <= 0.8) {
- flag = 0.1;
- sign = 1;
- }
- swing += sign * 1.6;
- cout << "flag is " << flag << endl;
- stretch += flag;
- cout << "stretch is " << stretch << endl;
- glutPostRedisplay();
- }
- void textureInit(void) {
- unsigned int texture;
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- // set the texture wrapping/filtering options (on the currently bound texture object)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- // load and generate the texture
- int width, height, nrChannels;
- unsigned char* data = stbi_load("drewno.jpg", &width, &height, &nrChannels, 0);
- if (data)
- {
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
- //glGenerateMipmap(GL_TEXTURE_2D);
- gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
- }
- else
- {
- std::cout << "Failed to load texture" << std::endl;
- }
- stbi_image_free(data);
- }
- void MyDisplay(void) {
- glLoadIdentity();
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- textureInit();
- glPushMatrix();
- glTranslated(horizontal, vertical, zoom);
- glRotatef(rotate_x, 1.0, 0.0, 0.0);
- glRotatef(rotate_z, 0.0, 0.0, 1.0);
- DrawReferenceLines();
- drawSpring();
- drawCylinder();
- drawBoard();
- glPopMatrix();
- glFlush();
- // glutTimerFunc(100, timer, 0);
- //necessary for GLU_DOUBLE displayMode
- //glutSwapBuffers();
- }
- void keyboard(int key, int x, int y) {
- if (key == GLUT_KEY_RIGHT) {
- rotate_z += 5;
- cout << "rotate_z is: " << rotate_z << endl;
- }
- else if (key == GLUT_KEY_LEFT) {
- rotate_z -= 5;
- cout << "rotate_z is: " << rotate_z << endl;
- }
- else if (key == GLUT_KEY_UP) {
- rotate_x -= 5;
- cout << "rotate_x is: " << rotate_x << endl;
- }
- else if (key == GLUT_KEY_DOWN) {
- rotate_x += 5;
- cout << "rotate_x is: " << rotate_x << endl;
- }
- // Request display update
- glutPostRedisplay();
- }
- void MyInit(void) {
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glViewport(0, 500, 500, 500);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60, 1, 1, 40.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glDrawBuffer(GL_BACK);
- glEnable(GL_DEPTH_TEST);
- }
- void NormalKeyHandler(unsigned char key, int x, int y) {
- if (key == 'z') {
- zoom -= 0.5;
- cout << "zoom is: " << zoom << endl;
- }
- else if (key == 'x') {
- zoom += 0.5;
- cout << "zoom is: " << zoom << endl;
- }
- else if (key == 'a') {
- horizontal -= 1;
- cout << "horizontal is: " << horizontal << endl;
- }
- else if (key == 'd') {
- horizontal += 1;
- cout << "horizontal is: " << horizontal << endl;
- }
- else if (key == 's') {
- vertical -= 1;
- cout << "vertical is: " << vertical << endl;
- }
- else if (key == 'w') {
- vertical += 1;
- cout << "vertical is: " << vertical << endl;
- }
- glutPostRedisplay();
- }
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);//single buffer and RGBA
- glutInitWindowSize(500, 500);//initial window size
- glutInitWindowPosition(100, 100);
- glutCreateWindow("Cube");//create widnow, hello title bar
- MyInit();
- glutDisplayFunc(MyDisplay);
- glutSpecialFunc(keyboard);
- glutKeyboardFunc(NormalKeyHandler);
- glutMainLoop();//enter main loop and process events
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement