Advertisement
Monika__

spring adding textures

Dec 8th, 2020
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.60 KB | None | 0 0
  1. // Template1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <math.h>
  6. #include <iostream>
  7. #include <Windows.h>
  8. #include "stb_image.h"
  9. #include "include\GL\GL.H"
  10. #include "include\GL\GLU.H"
  11. #include "include\GL\glut.h"
  12. //#include "Template1.h"
  13. #define PI 3.14
  14.  
  15. using namespace std;
  16.  
  17. double rotate_z = 0;
  18. double rotate_x = -260;
  19. double zoom = -32;
  20. double horizontal = 0;
  21. double vertical = 10;
  22. double stretch = 1;
  23. double flag = 0.1;
  24. double swing = 0;
  25. int sign = 1;
  26.  
  27.  
  28. void drawSpringPoints(void) {
  29.     double x, y, z;
  30.     double t, u;
  31.     glBegin(GL_POINTS);
  32.     for (t = 0; t < 8 * PI; t += 0.1) {
  33.         for (u = 0; u < 2 * PI; u += 0.1) {
  34.             x = cos(t) * (3 + cos(u));
  35.             y = sin(t) * (3 + cos(u));
  36.             z = 0.6 * t + sin(u);
  37.             glColor3f(1.0f, 0.0f, 0.0f);
  38.  
  39.             glVertex3d(x, y, z);
  40.  
  41.         }
  42.     }
  43.     glEnd();
  44.  
  45.  
  46. }
  47.  
  48. void setColor(char index) {
  49.     switch (index) {
  50.     case 'r':
  51.         glColor3f(1.0f, 0.0f, 0.0f);
  52.         break;
  53.     case 'g':
  54.         glColor3f(0.0f, 1.0f, 0.0f);
  55.         break;
  56.     case 'b':
  57.         glColor3f(0.0f, 0.0f, 1.0f);
  58.         break;
  59.     case 'w':
  60.         glColor3f(1.0f, 1.0f, 0.0f);
  61.         break;
  62.     case 'y':
  63.         glColor3f(1.0f, 1.0f, 0.0f);
  64.         break;
  65.     case 'm':
  66.         glColor3f(1.0f, 0.0f, 1.0f);
  67.         break;
  68.     case 'c':
  69.         glColor3f(0.0f, 1.0f, 1.0f);
  70.         break;
  71.     default:
  72.         glColor3f(1.0f, 0.0f, 1.0f);
  73.  
  74.     }
  75. }
  76.  
  77. void drawSpring(void) {
  78.     double x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4;
  79.     double t, u;
  80.     int color = 0;
  81.     double max_y = 0;
  82.     double max_x = 0;
  83.     double min_y = 0;
  84.     double min_x = 0;
  85.     setColor('b');
  86.     //glTranslated(0.0, 0.0, -4.0);
  87.     for (t = 0.1; t < 8 * PI; t += 0.1) {
  88.         for (u = 0; u < 2 * PI; u += 0.5) {
  89.            
  90.             x1 = cos(t) * (3 + cos(u));
  91.             y1 = sin(t) * (3 + cos(u));
  92.             z1 = 0.6 * t + sin(u);
  93.  
  94.             x2 = cos(t + 0.1) * (3 + cos(u));
  95.             y2 = sin(t + 0.1) * (3 + cos(u));
  96.             z2 = 0.6 * (t + 0.1) + sin(u);
  97.  
  98.             x3 = cos(t) * (3 + cos(u + 0.5));
  99.             y3 = sin(t) * (3 + cos(u + 0.5));
  100.             z3 = 0.6 * t + sin(u + 0.5);
  101.  
  102.             x4 = cos(t + 0.1) * (3 + cos(u + 0.5));
  103.             y4 = sin(t + 0.1) * (3 + cos(u + 0.5));
  104.             z4 = 0.6 * (t + 0.1) + sin(u + 0.5);
  105.  
  106.             //setColor(color % 4 + 1);
  107.  
  108.  
  109.             glBegin(GL_QUADS);
  110.  
  111.             glVertex3d(x1, y1,  stretch * z1);
  112.             glVertex3d(x3, y3, stretch * z3);
  113.             glVertex3d(x4, y4, stretch * z4);
  114.             glVertex3d(x2, y2, stretch * z2);
  115.  
  116.  
  117.             glEnd();
  118.             color += 1;
  119.  
  120.             if (x1 > max_x) {
  121.                 max_x = x1;
  122.             }
  123.             if (y1 > max_y) {
  124.                 max_y = y1;
  125.             }
  126.             if (x1 < min_x) {
  127.                 min_x = x1;
  128.             }
  129.             if (y1 < min_y) {
  130.                 min_y = y1;
  131.             }
  132.             //cout << "x: " << x1 << " y: " << y1 << " z: " << z1 << endl;
  133.         }
  134.     }
  135.     //cout << "stretch " << stretch << endl;
  136.  
  137.     //cout << "max x: " << max_x << " max y: " << max_y << endl;
  138.     //cout << "min x: " << min_x << " min y: " << min_y << endl;
  139.     /*
  140.     glColor3f(1.0f, 0.0f, 1.0f);
  141.     glBegin(GL_QUADS);
  142.  
  143.     glVertex3d(x1, y1, z1);
  144.     glVertex3d(x3, y3, z3);
  145.     glVertex3d(x4, y4, z4);
  146.     glVertex3d(x2, y2, z2);
  147.  
  148.  
  149.     glEnd();
  150.     */
  151.  
  152. }
  153.  
  154. void DrawReferenceLines(void){
  155.  
  156.     //x is dark green
  157.     glColor3f(0.137255, 0.556863, 0.137255);
  158.     glBegin(GL_LINES);
  159.     glVertex3f(-250.0, 0.0, 0.0);
  160.     glVertex3f(250.0, 0.0, 0.0);
  161.     glEnd();
  162.  
  163.     //y  red
  164.     glColor3f(1.0, 0.0, 0.0);
  165.     glBegin(GL_LINES);
  166.     glVertex3f(0.0, -250.0, 0.0);
  167.     glVertex3f(0.0, 250.0, 0.0);
  168.     glEnd();
  169.  
  170.     //z orange
  171.     glColor3f(1.0, 0.5, 0.0);
  172.     glBegin(GL_LINES);
  173.     glVertex3f(0.0, 0.0, -250.0);
  174.     glVertex3f(0.0, 0.0, 250.0);
  175.     glEnd();
  176.  
  177. }
  178.  
  179. void drawCylinder(void) {
  180.  
  181.     setColor('b');
  182.     GLUquadric* gluQuadratic = NULL;
  183.     gluQuadratic = gluNewQuadric();
  184.  
  185.     glPushMatrix();
  186.     glTranslated(0.0, 0.0, -4.0);
  187.     gluCylinder(gluQuadratic, 1.0, 1.0, 4.0, 20, 20);
  188.     glPopMatrix();
  189.  
  190.     glPushMatrix(); //up end modeling
  191.    
  192.     gluSphere(gluQuadratic, 1.0, 20, 20);
  193.    
  194.     glRotated(90, 0.0, 1.0, 0.0);
  195.     gluCylinder(gluQuadratic, 1.0, 1.0, 3.0, 20, 20);
  196.    
  197.     glTranslated(0.0, 0.0, 3.0);
  198.    
  199.     gluSphere(gluQuadratic, 1.0, 20, 20);
  200.     glPopMatrix();
  201.  
  202. //  glPushMatrix();
  203.  //how the ball should move
  204.  
  205.     glPushMatrix(); //the entire bottom part, must work with the animation
  206.     //glTranslated(0.0, 0.0, (abs(stretch - 1) * 1.6));
  207.     glTranslated(0.0, 0.0, 16.0 + swing); //up and down movement
  208.  
  209.     glPushMatrix();
  210. //  setColor('w');
  211.     glTranslated(0.0, 0.0, -0.5);
  212.     gluSphere(gluQuadratic, 1.0, 20, 20);
  213.     //glTranslated(0.0, 0.0, -0.5);
  214.  
  215.     glPushMatrix();
  216.     glRotated(100, 0.0, 1.0, 0.0);
  217.     gluCylinder(gluQuadratic, 1.0, 1.0, 3.0, 20, 20);
  218.     glPopMatrix();
  219.     glRotated(90, 0.0, 1.0, 0.0);
  220.  
  221.     //setColor('g');
  222.     glTranslated(0.5, 0.0, 3.0);
  223.     gluSphere(gluQuadratic, 1.0, 20, 20);
  224.     glPopMatrix();
  225.    
  226.     //ready part
  227.     setColor('b');
  228.     gluCylinder(gluQuadratic, 1.0, 1.0, 4.0, 20, 20); //end of the spring
  229.     setColor('r');
  230.     glTranslated(0.0, 0.0, 6.0);
  231.     gluSphere(gluQuadratic, 4.0, 20, 20); //ball at the end of the spring
  232.  
  233.     glPopMatrix();
  234.  
  235.     //glPopMatrix();
  236. }
  237.  
  238. void drawBoard(void) {
  239.  
  240.     setColor('r');
  241.     //bottom
  242.     glBegin(GL_POLYGON);
  243.     //glColor3f(1.0f, 0.0f, 0.0f);
  244.     glVertex3d(-4, -4, -4);
  245.     glVertex3d(-4, 4, -4);
  246.     glVertex3d(4, 4, -4);
  247.     glVertex3d(4, -4, -4);
  248.     glEnd();
  249.  
  250.     //top
  251.     glBegin(GL_POLYGON);
  252.     //glColor3f(0.0f, 1.0f, 0.0f);
  253.     glVertex3d(-4, -4, -5);
  254.     glVertex3d(-4, 4, -5);
  255.     glVertex3d(4, 4, -5);
  256.     glVertex3d(4, -4, -5);
  257.     glEnd();
  258.  
  259.     //right side
  260.     glBegin(GL_POLYGON);
  261.     //glColor3f(0.0f, 0.0f, 1.0f);
  262.     glVertex3d(4, -4, -4);
  263.     glTexCoord2f(0.0, 0.0);
  264.     glVertex3d(4, -4, -5);
  265.     glTexCoord2f(0.0, 1.0);
  266.     glVertex3d(4, 4, -5);
  267.     glTexCoord2f(1.0, 1.0);
  268.     glVertex3d(4, 4, -4);
  269.     glTexCoord2f(1.0, 0.0);
  270.     glEnd();
  271.  
  272.     //left side
  273.     glBegin(GL_POLYGON);
  274.     //glColor3f(1.0f, 1.0f, 0.0f);
  275.     glVertex3d(-4, -4, -4);
  276.     glVertex3d(-4, -4, -5);
  277.     glVertex3d(-4, 4, -5);
  278.     glVertex3d(-4, 4, -4);
  279.     glEnd();
  280.  
  281.     //top
  282.     glBegin(GL_POLYGON);
  283.     //glColor3f(1.0f, 0.0f, 1.0f);
  284.     glVertex3d(-4, -4, -4);
  285.     glVertex3d(-4, -4, -5);
  286.     glVertex3d(4, -4, -5);
  287.     glVertex3d(4, -4, -4);
  288.     glEnd();
  289.  
  290.     //bottom
  291.     glBegin(GL_POLYGON);
  292.     //glColor3f(0.0f, 1.0f, 1.0f);
  293.     glVertex3d(-4, 4, -4);
  294.     glVertex3d(-4, 4, -5);
  295.     glVertex3d(4, 4, -5);
  296.     glVertex3d(4, 4, -4);
  297.     glEnd();
  298.  
  299. }
  300.  
  301. void timer(int) {
  302.  
  303.     if (stretch >= 1.5) {
  304.         flag = -0.1;
  305.         sign = -1;
  306.        
  307.     }
  308.     if (stretch <= 0.8) {
  309.         flag = 0.1;
  310.         sign = 1;
  311.     }
  312.     swing += sign * 1.6;
  313.     cout << "flag is " << flag << endl;
  314.     stretch += flag;
  315.     cout << "stretch is " << stretch << endl;
  316.     glutPostRedisplay();
  317.  
  318. }
  319.  
  320. void textureInit(void) {
  321.  
  322.     unsigned int texture;
  323.     glGenTextures(1, &texture);
  324.     glBindTexture(GL_TEXTURE_2D, texture);
  325.     // set the texture wrapping/filtering options (on the currently bound texture object)
  326.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  327.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  328.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  329.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  330.     // load and generate the texture
  331.     int width, height, nrChannels;
  332.     unsigned char* data = stbi_load("drewno.jpg", &width, &height, &nrChannels, 0);
  333.     if (data)
  334.     {
  335.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  336.         //glGenerateMipmap(GL_TEXTURE_2D);
  337.         gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
  338.     }
  339.     else
  340.     {
  341.         std::cout << "Failed to load texture" << std::endl;
  342.     }
  343.     stbi_image_free(data);
  344. }
  345.  
  346. void MyDisplay(void) {
  347.     glLoadIdentity();
  348.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  349.  
  350.     textureInit();
  351.     glPushMatrix();
  352.     glTranslated(horizontal, vertical, zoom);
  353.     glRotatef(rotate_x, 1.0, 0.0, 0.0);
  354.     glRotatef(rotate_z, 0.0, 0.0, 1.0);
  355.  
  356.     DrawReferenceLines();
  357.  
  358.     drawSpring();
  359.  
  360.     drawCylinder();
  361.  
  362.     drawBoard();
  363.  
  364.     glPopMatrix();
  365.  
  366.  
  367.     glFlush();
  368. //  glutTimerFunc(100, timer, 0);
  369.    
  370.    
  371.     //necessary for GLU_DOUBLE displayMode
  372.     //glutSwapBuffers();
  373.  
  374. }
  375.  
  376.  
  377.  
  378. void keyboard(int key, int x, int y) {
  379.  
  380.     if (key == GLUT_KEY_RIGHT) {
  381.         rotate_z += 5;
  382.         cout << "rotate_z is: " << rotate_z << endl;
  383.     }
  384.  
  385.     else if (key == GLUT_KEY_LEFT) {
  386.         rotate_z -= 5;
  387.         cout << "rotate_z is: " << rotate_z << endl;
  388.     }
  389.  
  390.     else if (key == GLUT_KEY_UP) {
  391.         rotate_x -= 5;
  392.         cout << "rotate_x is: " << rotate_x << endl;
  393.     }
  394.  
  395.     else if (key == GLUT_KEY_DOWN) {
  396.         rotate_x += 5;
  397.         cout << "rotate_x is: " << rotate_x << endl;
  398.  
  399.     }
  400.  
  401.     // Request display update
  402.     glutPostRedisplay();
  403.  
  404. }
  405.  
  406.  
  407. void MyInit(void) {
  408.     glClearColor(0.0, 0.0, 0.0, 0.0);
  409.     glViewport(0, 500, 500, 500);
  410.     glMatrixMode(GL_PROJECTION);
  411.     glLoadIdentity();
  412.     gluPerspective(60, 1, 1, 40.0);
  413.     glMatrixMode(GL_MODELVIEW);
  414.     glLoadIdentity();
  415.     glDrawBuffer(GL_BACK);
  416.     glEnable(GL_DEPTH_TEST);
  417. }
  418.  
  419. void NormalKeyHandler(unsigned char key, int x, int y) {
  420.     if (key == 'z') {
  421.         zoom -= 0.5;
  422.         cout << "zoom is: " << zoom << endl;
  423.     }
  424.  
  425.     else if (key == 'x') {
  426.         zoom += 0.5;
  427.         cout << "zoom is: " << zoom << endl;
  428.     }
  429.  
  430.     else if (key == 'a') {
  431.         horizontal -= 1;
  432.         cout << "horizontal is: " << horizontal << endl;
  433.     }
  434.  
  435.  
  436.     else if (key == 'd') {
  437.         horizontal += 1;
  438.         cout << "horizontal is: " << horizontal << endl;
  439.     }
  440.  
  441.     else if (key == 's') {
  442.         vertical -= 1;
  443.         cout << "vertical is: " << vertical << endl;
  444.     }
  445.  
  446.     else if (key == 'w') {
  447.         vertical += 1;
  448.         cout << "vertical is: " << vertical << endl;
  449.     }
  450.  
  451.     glutPostRedisplay();
  452. }
  453.  
  454.  
  455. int main(int argc, char** argv) {
  456.     glutInit(&argc, argv);
  457.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);//single buffer and RGBA
  458.     glutInitWindowSize(500, 500);//initial window size
  459.     glutInitWindowPosition(100, 100);
  460.     glutCreateWindow("Cube");//create widnow, hello title bar
  461.     MyInit();
  462.     glutDisplayFunc(MyDisplay);
  463.     glutSpecialFunc(keyboard);
  464.     glutKeyboardFunc(NormalKeyHandler);
  465.     glutMainLoop();//enter main loop and process events
  466.     return 0;
  467. }
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement