Advertisement
Aveneid

Untitled

Jan 19th, 2021
1,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.01 KB | None | 0 0
  1.  
  2. #include <GL/glut.h>
  3. #include <stdlib.h>
  4. #include <cmath>
  5. #include <iostream>
  6. #include <windows.h>
  7. #include <string>
  8. #include <ctime>
  9.  
  10. #include "GL/freeglut.h"
  11. #define MAXFOV 120
  12. #define MINFOV 15
  13.  
  14. #define RED glColor3f(1,0,0);
  15. #define GREEN glColor3f(0,1,0);
  16. #define BLUE glColor3f(0,0,1);
  17.  
  18.  
  19.  
  20. #define STB_IMAGE_IMPLEMENTATION
  21. #include "stb/stb_image.h"
  22. #define STB_IMAGE_WRITE_IMPLEMENTATION
  23. #include "stb/stb_image_write.h"
  24.  
  25.  
  26. using namespace std;
  27. int fps = 1000/50;
  28. int width,height;
  29. char view;
  30.  
  31.  
  32. struct v3f {
  33.     float x;
  34.     float y;
  35.     float z;
  36. };
  37.  
  38. struct v2i{
  39.     int x;
  40.     int y;
  41. };
  42.  v3f camPos = {0,0,-6};
  43.  v3f camLook  = {-160,0,0};
  44.  v2i mousePos;
  45.  
  46.  float lightPos[4] = {1,-3,0, 1.0};
  47.  float lightPos2[4] = {0, 0, 0, 1.0};
  48.  
  49.  int max_carts=3;
  50. float angle = 0.0f;
  51.  
  52.  
  53. void initGL()
  54. {
  55.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  56.     glEnable(GL_COLOR_MATERIAL);
  57. }
  58.  
  59. void idle()
  60. {
  61.     glutPostRedisplay();   // Post a re-paint request to activate display()
  62. }
  63.  
  64. void Timer(int value) {
  65.    glutPostRedisplay();
  66.    glutTimerFunc(fps, Timer, 0);
  67. }
  68.  
  69. void drawRails(){
  70.     glPushMatrix();
  71.         glRotatef(90,1,0,0);
  72.         glColor3f(1,1,1);
  73.         glutSolidTorus(0.005,2,30,30);
  74.         glutSolidTorus(0.005,2.2,30,30);
  75.         glRotatef(90,1,0,0);
  76.         glPushMatrix();
  77.             glScalef(0.1,0.05,0.1);
  78.             glColor3f(0.6,0.3,0.1);
  79.             for(int i=0;i<360;i+=12){
  80.                 glPushMatrix();
  81.                     glRotatef(i,0,1,0);
  82.                     glTranslatef(21,0,0);
  83.                     glScalef(2.75,1,0.5);
  84.                     glutSolidCube(1);
  85.                 glPopMatrix();
  86.             }
  87.         glPopMatrix();
  88.  
  89.     glPopMatrix();
  90. }
  91.  
  92. void drawCarts(){
  93.     glTranslatef(0,-0.11,0);
  94.     glPushMatrix();
  95.         RED;
  96.         glTranslatef(-sin(angle)*2.1,0,-cos(angle)*2.1);
  97.         glRotatef(-angle*55+20,0,1,0);
  98.         glScalef(0.1,0.1,0.1);
  99.         glutSolidCube(2);
  100.     glPopMatrix();
  101.     glPushMatrix();
  102.         BLUE;
  103.         glTranslatef(-sin(angle+0.125)*2.1,0,-cos(angle+0.125)*2.1);
  104.         glRotatef(-angle*55+20,0,1,0);
  105.         glScalef(0.1,0.1,0.1);
  106.         glutSolidCube(2);
  107.     glPopMatrix();
  108.     glPushMatrix();
  109.         GREEN;
  110.         glTranslatef(-sin(angle+0.25)*2.1,0,-cos(angle+0.25)*2.1);
  111.         glRotatef(-angle*55+20,0,1,0);
  112.         glScalef(0.1,0.1,0.1);
  113.         glutSolidCube(2);
  114.     glPopMatrix();
  115. }
  116. GLuint tex;
  117. GLbyte* loadTexture(const char* FileName, GLint* w, GLint* h, GLint* ImComponents, GLenum* ImFormat) {
  118.     GLbyte* image;
  119.     int ch;
  120.  
  121.     *ImComponents = GL_RGB8;
  122.     *ImFormat=GL_BGR_EXT;
  123.     GLbyte* pbitsperpixel = NULL;
  124.  
  125.     image = (GLbyte*) stbi_load(FileName, w, h, &ch, 0);
  126.     if (image == NULL) {
  127.         std::cout << "error";
  128.         exit(1);
  129.     }
  130.  
  131.     switch (ch)
  132.     {
  133.         case 3:
  134.             *ImFormat = GL_BGR_EXT;
  135.             *ImComponents = GL_RGB8;
  136.             break;
  137.         case 4:
  138.             *ImFormat = GL_BGRA_EXT;
  139.             *ImComponents = GL_RGBA8;
  140.             break;
  141.         case 1:
  142.             *ImFormat = GL_LUMINANCE;
  143.             *ImComponents = GL_LUMINANCE8;
  144.             break;
  145.     };
  146.     return image;
  147. }
  148.  
  149. void InitText(const char* FileName) {
  150.     GLbyte* pBytes;
  151.     GLint ImWidth, ImHeight, ImComponents;
  152.     GLenum ImFormat;
  153.  
  154.     glEnable(GL_CULL_FACE);
  155.     glCullFace(GL_FRONT);
  156.     pBytes = loadTexture(FileName, &ImWidth, &ImHeight, &ImComponents, &ImFormat);
  157.     glTexImage2D(GL_TEXTURE_2D, 0, ImComponents, ImWidth, ImHeight, 0, ImFormat, GL_UNSIGNED_BYTE, pBytes);
  158.     free(pBytes);
  159.     glEnable(GL_TEXTURE_2D);
  160.     glBindTexture(GL_TEXTURE_2D, 0);
  161.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  162.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  163. }
  164. void light(){
  165.     glPushMatrix();
  166.         glTranslatef(lightPos[0],lightPos[1],lightPos[2]);
  167.         glLightfv(GL_LIGHT1, GL_POSITION, lightPos);
  168.         glPushMatrix();
  169.         glColor3f(1.000, 0.843, 0.000);
  170.             glScalef(0.1,0.1,0.1);
  171.             glutSolidOctahedron();
  172.         glPopMatrix();
  173.         glPushMatrix();
  174.         glRotatef(45,0,1,0);
  175.             glScalef(0.1,0.1,0.1);
  176.             glutSolidOctahedron();
  177.         glPopMatrix();
  178.  
  179.         glColor3f(1,1,1);
  180.         GLfloat ambient0[]={0.2, 0.2, 0.2, 1.0};
  181.         GLfloat diffuse0[]={0.9, 0.9, 0.9, 1.0};
  182.         GLfloat specular0[]={1.0,1.0, 1.0, 1.0};
  183.         GLfloat emission[]={1.0,1.0, 1.0, 1.0};
  184.         glLightfv(GL_LIGHT1, GL_AMBIENT, ambient0);
  185.         glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse0);
  186.         glLightfv(GL_LIGHT1, GL_SPECULAR, specular0);
  187.         glEnable(GL_LIGHTING);
  188.         glEnable(GL_LIGHT1);
  189.  
  190.     glPopMatrix();
  191. }
  192. void movelight(){
  193. lightPos[2]=(-sin(angle));
  194. lightPos[0]=(-cos(angle));
  195. }
  196. void plane(){
  197.     glPushMatrix();
  198.     glColor3f(0.502, 0.000, 0.000);
  199.         glBegin(GL_QUADS);
  200.             glVertex3f(-10,0,-10);
  201.             glVertex3f(-10,0,10);
  202.             glVertex3f(10,0,10);
  203.             glVertex3f(10,0,-10);
  204.         glEnd();
  205.     glPopMatrix();
  206. }
  207. void display()
  208. {
  209.     glClearColor(0,0,0,0);
  210.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  211.     glMatrixMode(GL_MODELVIEW);
  212.     glLoadIdentity();
  213.  
  214.  
  215.  
  216.     switch(view){
  217.     case '1':
  218.         glTranslatef(camPos.x,camPos.y,camPos.z);
  219.         glRotatef(camLook.x,1.0f,0.0f,0.0f);
  220.         glRotatef(camLook.y,0.0f,1.0f,0.0f);
  221.         glRotatef(camLook.z,0.0f,0.0f,1.0f);
  222.         break;
  223.     case '2': //top
  224.  
  225.             glTranslatef(-1,-1,-1);
  226.             glRotatef(180,1,0,0);
  227.             glRotatef(90,1,0,0);
  228.             glTranslatef(1,5,1);
  229.         break;
  230.     case '3': //left
  231.  
  232.             glTranslatef(-1,-1,-1);
  233.             glRotatef(180,1,0,0);
  234.             glRotatef(-90,0,1,0);
  235.             glTranslatef(4,0,-1);
  236.             glRotatef(90,0,1,0);
  237.         break;
  238.  
  239.     case '4': //left(45)
  240.  
  241.             glTranslatef(-1,-1,-1);
  242.             glRotatef(180,1,0,0);
  243.             glRotatef(-90,0,1,0);
  244.             glTranslatef(4,0,-1);
  245.             glRotatef(90,0,1,0);
  246.             glRotatef(45,1,0,0);
  247.             glTranslatef(0,0,1);
  248.         break;
  249.  
  250.     }
  251.     glEnable(GL_TEXTURE_GEN_S);
  252.     InitText("a.jpg");
  253.     plane();
  254.     drawRails();
  255.     drawCarts();
  256.  
  257.     light();
  258.     movelight();
  259.     angle += 0.02f;
  260.     //glutSwapBuffers();
  261.     glDisable(GL_TEXTURE_GEN_S);
  262. }
  263.  
  264. void reshape(GLsizei width, GLsizei height) {
  265.    if (height == 0) height = 1;
  266.    GLfloat aspect = (GLfloat)width / (GLfloat)height;
  267.    glViewport(0, 0, width, height);
  268.    glMatrixMode(GL_PROJECTION);
  269.    glLoadIdentity();
  270.    gluPerspective(45, aspect, 0.1f, 100.0f);
  271. }
  272. static void key(unsigned char key, int x, int y)
  273. {
  274.     switch(key){
  275.         case 'a': camPos.x += 0.5f; break;
  276.         case 'd': camPos.x -= 0.5f; break;
  277.         case 'w': camPos.z += 0.5f; break;
  278.         case 's': camPos.z -= 0.5f; break;
  279.         case 'j': camLook.y += 0.75f; break;
  280.         case 'l': camLook.y -= 0.75f; break;
  281.         case 'i': camLook.x += 0.75f; break;
  282.         case 'k': camLook.x -= 0.75f; break;
  283.         case 'Q':
  284.         case 'q':
  285.         case  27:   // ESC
  286.         exit(0); break;
  287.     }
  288.     reshape(glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
  289.     glutPostRedisplay();
  290. }
  291.  
  292.  
  293.  
  294. int main(int argc, char** argv)
  295. {
  296.     glutInit(&argc, argv);
  297.     //glEnable(GL_TEXTURE_GEN_S);
  298.  
  299.     glutInitDisplayMode(GLUT_DOUBLE);
  300.     glutInitWindowSize(800, 600);
  301.     glutInitWindowPosition(50, 50);
  302.  
  303.     width = glutGet(GLUT_WINDOW_WIDTH);
  304.     height = glutGet(GLUT_WINDOW_HEIGHT);
  305.  
  306.     cout<<"select mode: "<<endl<<"1- free cam (mouse)"<<endl<<"2- top view";
  307.     cout<<endl<<"3- from left"<<endl<<"from left (45deg)"<<endl<<"q-exit"<<endl;
  308.  
  309.     cin>>view;
  310.     if(view=='q' || view == 'Q') return 0;
  311.  
  312.  
  313.  
  314.     glutCreateWindow("GLUT - POCIAG  - FERENC BARTLOMIEJ");
  315.     cout<<"wsad - move (by 0.25)"<<endl;
  316.     cout<<"ijkl - change camera angle"<<endl;
  317.     cout<<"esc to exit"<<endl;
  318.     glEnable(GL_DEPTH_TEST);
  319.  
  320.  
  321.     glutDisplayFunc(display);
  322.     glutReshapeFunc(reshape);
  323.     glutKeyboardFunc(key);
  324.     //mouse
  325.  
  326.  
  327.     glutTimerFunc(0, Timer, 0);
  328.     initGL();
  329.     glutMainLoop();
  330.     return EXIT_SUCCESS;
  331. }
  332.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement