Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.75 KB | None | 0 0
  1. #include <SDL/SDL.h>
  2. #include <GL/glew.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. #define WIDTH 1024
  7. #define HEIGHT 1024
  8.  
  9. #define OSCN 3
  10. #define FN 2
  11. #define SAMPLERATE 48000
  12.  
  13. GLUquadricObj *q;
  14.  
  15. GLuint myp;
  16.  
  17. GLint ut, ut2, vb, tim, ttim, i;
  18.  
  19. float o_p[OSCN]={0}, o_f[OSCN]={0}, lp[FN][4] = {0}, f_f[2]={0}, bassd = 0;
  20. long mlc = 0, ml = -1;
  21.  
  22. void sa(void *u, Uint8 *s, int l) {
  23.     float *ass;
  24.     long i,k;
  25.     Sint16 *p = (Sint16*)s;
  26.     for(; l > 0; l -= 2) {
  27.         f_f[0] = 0.00004 + .999 * f_f[0];
  28.         f_f[1] *= .9996;
  29.         if((mlc -= (int)(0x1000000 * 9.6667 / SAMPLERATE)) < 0) {
  30.             float kgg;
  31.             ml++;
  32.             kgg = pow(1.059, "<<HH<<H<<<HHHHFH"[15&ml]-'A') * 55.0f / SAMPLERATE;
  33.             o_f[0] = kgg;
  34.             o_f[1] = 0.5*kgg * 1.012;
  35.             o_f[2] = kgg * 0.993;
  36.             if("..##.###.###.###"[15&ml] == '#') f_f[0] = .3; else o_f[0]=0;
  37.             if("#...#...#...#..."[15&ml] == '#') bassd = 0;
  38.             f_f[1] = ("CCbCICbCCCbCICbC"[15&ml] - 'A') / 40.0;
  39.             mlc += 0x1000000;
  40.         }
  41.         for(i = 0; i < OSCN; i++) o_p[i] = fmod(o_p[i] + o_f[i] + .5, 1) - .5;
  42.         lp[0][0] = (mlc > 0x300000 ? o_p[0] + o_p[1]/2 + o_p[2]/9 : 0) - lp[0][3];
  43.         lp[1][0] = (0x3FFF & rand()) - 0x2000 - 5 * lp[1][3];
  44.         for(k = FN; k--;) {
  45.             for(i=1;i<4;i++) lp[k][i] = fmax((1-f_f[k]) * lp[k][i] + f_f[k] * lp[k][i-1], -18000);
  46.         }
  47.         *p++ = (9999 * lp[0][3] + 500 * tanh(.003 * lp[1][2]) + 3000 * tanh(5*sin(bassd / (23 + 0.01*bassd++))));
  48.     }
  49. }
  50.  
  51. void tr(int n, float lgth)
  52. {
  53.    
  54.     glUniform1iARB(ut,n);
  55.     glUniform1iARB(ut2,tim);
  56.     gluCylinder(q,lgth/10.,lgth/10.,lgth,4,2);
  57.     glPushMatrix();
  58.     if (n == 1)
  59.         glScalef(2*(tim/6000.),2*(tim/6000.),0.1);
  60.     if (n--)
  61.     {
  62.    
  63.         glTranslatef(0,0, lgth);
  64.         glRotatef(60, 0, 0.4, 1);
  65.         tr(n, lgth*0.8);
  66.         glRotatef(-80, 0, 1, 1);
  67.         tr(n, lgth*0.8);
  68.     }
  69.     glPopMatrix();
  70.  
  71.  
  72. }
  73.  
  74. /* Some texture shit. */
  75.  
  76. GLuint world_tex;
  77. int tex_size_x = WIDTH;
  78. int tex_size_y = HEIGHT;
  79.  
  80. int lol = 0;
  81.  
  82. int sc1()
  83. {
  84.  
  85.  
  86. GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
  87. glLightfv(GL_LIGHT0, GL_AMBIENT, specular);
  88.  
  89.  
  90. glPushMatrix();
  91.  
  92. /* Here be trees */
  93.   glTranslatef(0,0,0);
  94.   glRotatef(270, 1, 0, 0);
  95.  
  96.   glDisable(GL_TEXTURE_2D);
  97.  
  98.   glColor4f(0.5, 0.5, 1.5, 0.5);
  99.   for (i = -1; i < 12; i++)
  100.   {
  101.       glRotatef(40, 0, 0, 1);
  102.       glPushMatrix();
  103.     glTranslatef((sin(i/4.5)*5.), cos(i/1.5)*4.5, 0);
  104.    
  105.     tr(7, 1.0);
  106.      glPopMatrix();
  107.   }
  108.   glPopMatrix();
  109.  
  110.   /* Here be water */
  111.  
  112.   glUniform1iARB(ut,12);
  113.   glTranslatef(0, 0, -10);
  114.   glRotatef(90, 1, 0, 0);
  115.   gluDisk (q, 0.0, 100, 4, 1);
  116.   glRotatef(-90, 1, 0, 0);
  117.  
  118.    /* Here be the ground. */
  119. glUniform1iARB(ut,10);
  120. GLfloat specular2[] = {0.0, 1.0, 1.0, 1.0};
  121. glLightfv(GL_LIGHT0, GL_AMBIENT, specular2);
  122.  
  123.  
  124.   //glEnable(GL_TEXTURE_2D);
  125.     glBindTexture(GL_TEXTURE_2D, world_tex);
  126.     //glDisable(GL_LIGHTING);
  127.     //glRotatef(90, 0, 1, 0);
  128.     glRotatef(90, 1, 0, 0);
  129.     glTranslatef(-10, -10, 0);
  130.  
  131.  
  132.  
  133.     for (int i = 0; i < 15; i++) {
  134.         for (int j = 0; j < 15; j++) {
  135.             glTranslatef(2, 0, 0);
  136.             gluDisk (q, 0.0, 1, 4, 1);
  137.         }
  138.         glTranslatef(-30, 2, 0);
  139.     }
  140.    
  141.     glTranslatef(1, -31, 0);
  142.    
  143.     for (int i = 0; i < 15; i++) {
  144.         for (int j = 0; j < 15; j++) {
  145.             glTranslatef(2, 0, 0);
  146.             gluDisk (q, 0.0, 1, 4, 1);
  147.         }
  148.         glTranslatef(-30, 2, 0);
  149.     }
  150. /*
  151.     for (int i = 0; i < 20; i++) {
  152.         glRotatef(tim/100.+50, 0, 0, 1);
  153.         gluDisk (q, 0.0, 1.4, 4, 1);
  154.     }
  155. */
  156.  
  157.     lol++;
  158.    
  159.     //glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, WIDTH, HEIGHT, 0);
  160.    
  161.       glEnable(GL_LIGHTING);
  162. }
  163.  
  164.  
  165. int sc2()
  166. {
  167. }
  168.  
  169.  
  170.  
  171. void printLog(GLuint obj)
  172. {
  173.     int infologLength = 0;
  174.     char infoLog[1024];
  175.  
  176.     if (glIsShader(obj))
  177.         glGetShaderInfoLog(obj, 1024, &infologLength, infoLog);
  178.     else
  179.         glGetProgramInfoLog(obj, 1024, &infologLength, infoLog);
  180.  
  181.     if (infologLength > 0)
  182.         printf("%s\n", infoLog);
  183. }
  184.  
  185.  
  186.  
  187.  
  188. int main()
  189. {  
  190.     q=gluNewQuadric();
  191.     gluQuadricTexture(q, GL_TRUE);
  192.     gluQuadricNormals(q, GL_AUTO_NORMAL);
  193.  
  194.     SDL_Event e;
  195.     SDL_SetVideoMode(WIDTH, HEIGHT,32, SDL_OPENGL);
  196.     glMatrixMode(GL_PROJECTION);
  197.     gluPerspective(45.0f,800/640,0.1f,90.0f);
  198.     glMatrixMode(GL_MODELVIEW);
  199.    
  200.     SDL_AudioSpec r={SAMPLERATE,AUDIO_S16,1,0,0x200,0,0,sa,NULL};
  201.     SDL_OpenAudio(&r,NULL);
  202. //  SDL_Delay(1000);
  203. //  SDL_PauseAudio(0);
  204.  
  205.     // some texture shit
  206.    
  207. printf( "VENDOR = %s\n", glGetString( GL_VENDOR ) ) ;
  208.    
  209. printf( "RENDERER = %s\n", glGetString( GL_RENDERER ) ) ;
  210. printf( "VERSION = %s\n", glGetString( GL_VERSION ) ) ;
  211.  
  212.    
  213.    
  214.     unsigned int* data;
  215.         // Create Storage Space For Texture Data (128x128x4)
  216.     data = (unsigned int*)new GLuint[((tex_size_x * tex_size_y)* 4 * sizeof(unsigned int))];
  217.    
  218.     data = (unsigned int*)memset(data, 0, ((tex_size_x * tex_size_y)* 4 * sizeof(unsigned int)));
  219.  
  220.     glEnable(GL_TEXTURE_2D);
  221.  
  222.     glGenTextures(1, &world_tex);                               // Create 1 Texture
  223.     glBindTexture(GL_TEXTURE_2D, world_tex);                    // Bind The Texture
  224.    
  225.         glTexImage2D(GL_TEXTURE_2D, 0, 4, tex_size_x, tex_size_y, 0,
  226.         GL_RGBA, GL_UNSIGNED_BYTE, data);                       // Build Texture Using Information In data
  227.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  228.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  229.  
  230.     glEnable(GL_DEPTH_TEST);
  231.    
  232.     delete [] data;
  233.     glBlendFunc(GL_SRC_ALPHA,GL_ONE);
  234.     glEnable(GL_BLEND);
  235.  
  236.    
  237.  
  238.  
  239. glewInit();
  240. printf("TESTI\n");
  241.  
  242.     char ves[] =    "uniform int time; uniform int no; varying vec4 diffuse,ambient; varying vec3 normal,lightDir,halfVector;\
  243.     void main() { \
  244.     normal = normalize(gl_NormalMatrix * gl_Normal); \
  245.     lightDir = normalize(vec3(gl_LightSource[0].position)); \
  246.     halfVector = normalize(gl_LightSource[0].halfVector.xyz);\
  247.     diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;\
  248.     ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;\
  249.     ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;\
  250.     gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\
  251.         gl_Position.y += sin(gl_Position.z*0.5)*2.5;\
  252.         if (no == 10) { \
  253.         gl_Position.y += cos(gl_Position.x*3.0);\
  254.         normal.x += sin(gl_Vertex.x);\
  255.     }\n\
  256.     }";
  257.     char frs[] = "uniform int time; uniform int no; varying vec4 diffuse,ambient; varying vec3 normal,lightDir,halfVector; \n\
  258.     void main() { \n\
  259.     vec3 n,halfV;\n\
  260.     float NdotL,NdotHV;\n\
  261.     vec4 color = ambient\n;\
  262.     n = normalize(normal);\n\
  263.     NdotL = max(dot(n,lightDir),0.0);\n\
  264.     if (NdotL > 0.0) { \n\
  265.     color += diffuse * NdotL; \n\
  266.     halfV = normalize(halfVector); \n\
  267.     NdotHV = max(dot(n,halfV),0.0); \n\
  268.     color += gl_FrontMaterial.specular * \n\
  269.         gl_LightSource[0].specular * \n\
  270.     pow(NdotHV, gl_FrontMaterial.shininess);} \n\
  271.     gl_FragColor = color;\n\
  272.     gl_FragColor[0] *= 0.39;\n\
  273.     if (no == 10) \n\
  274.     gl_FragColor[0] *= 0.39 * sin(n.x);\n\
  275.     if (no == 12) \n\
  276.     gl_FragColor[2] *= 3.0;\n\
  277.     gl_FragColor[1] *= 0.26;\n\
  278.     gl_FragColor[2] *= 0.13;\n\
  279.     if (no == 0) { \
  280.         gl_FragColor[1] *= 1.5; \
  281.     } \
  282. }";
  283.    
  284.  
  285.    
  286.     const GLcharARB * myfs;
  287.     const GLcharARB * myvs;
  288.  
  289.     myfs = &frs[0];
  290.     myvs = &ves[0];
  291.    
  292.     GLenum myv;
  293.     GLenum myf;
  294.    
  295.         printf("TESTI\n");
  296.    
  297.     /* SEURAAVAAN KAATUU ********************/
  298.     myp = glCreateProgram();
  299.     printf("TESTI\n");
  300.     myv = glCreateShader(GL_VERTEX_SHADER_ARB);
  301.     myf = glCreateShader(GL_FRAGMENT_SHADER_ARB);
  302.  
  303.     glShaderSource(myv, 1, &myvs, NULL);
  304.     glShaderSource(myf, 1, &myfs, NULL);
  305.    
  306.     glCompileShader(myv);
  307.     glCompileShader(myf);
  308.  
  309.     glAttachShader(myp, myv);
  310.     glAttachShader(myp, myf);
  311.    
  312.     printLog(myv);
  313.     printLog(myf);
  314.    
  315.     glLinkProgram(myp);
  316.  
  317.     glUseProgram(myp);
  318.    
  319.     GLint errors;
  320.    
  321.     glGetShaderiv(myp,GL_COMPILE_STATUS, &errors);
  322.  
  323.     ttim = SDL_GetTicks();
  324.    
  325.     /* C shit
  326.     const static void (*sc[])() = { sc1, sc2};
  327.     const unsigned int scs[] ={30, 90};
  328.     */
  329.    
  330.     GLfloat LP[]= { 0.0f,4.0f, 2.0f, 1.0f };
  331.  
  332.     glLightfv(GL_LIGHT0, GL_POSITION,LP);
  333.    
  334.     glEnable(GL_LIGHT0);
  335.     glEnable(GL_LIGHTING);
  336.    
  337.     ut = glGetUniformLocationARB(myp,"no");
  338.     ut2 = glGetUniformLocationARB(myp,"time");
  339.    
  340.     int FPS = 0;
  341.     int FPS2 = 0;
  342.    
  343.     float rotatex, rotatey = 0;
  344.     float movex, movez = 0;
  345.    
  346.     while (1)
  347.     {
  348.        
  349.         /* MOUSE + keyboard HANDLING FOR TESTING ONLY! */
  350.     while ( SDL_PollEvent(&e) ) {
  351.         switch (e.type) {
  352.             case SDL_MOUSEMOTION:
  353.                 rotatex += e.motion.xrel;
  354.                 rotatey += e.motion.yrel;
  355.                 break;
  356.             case SDL_MOUSEBUTTONDOWN:
  357.                 printf("Mouse button %d pressed at (%d,%d)\n",
  358.                        e.button.button, e.button.x, e.button.y);
  359.                 break;
  360.             case SDL_QUIT:
  361.                 exit(0);
  362.         }
  363.   }
  364.        
  365.        
  366.         /* THE 4K key handling.
  367.         SDL_PollEvent(&e);
  368.         if (e.type==SDL_KEYDOWN)
  369.             break;
  370.              */
  371.              
  372.         tim=SDL_GetTicks() - ttim;
  373.         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  374.         glLoadIdentity();
  375.        
  376.         GLfloat LP[]= { 0,cos(tim/500.)*20, sin(tim/500.)*20, 1.0f };
  377.  
  378.         glLightfv(GL_LIGHT0, GL_POSITION,LP);
  379.        
  380.         glTranslatef(0,-4,-15);
  381.         glRotatef(rotatey, 1, 0, 0);
  382.         glRotatef(rotatex, 0, 1, 0);
  383.     //glRotatef(5 + (fabs(sin(tim/2000.))*30), 1, 0, 0);
  384.         int sk = -1;
  385.        
  386.         sc1();
  387.        
  388.        
  389.         FPS++;
  390.        
  391.         if (tim - FPS2 > 2000)
  392.         {
  393.           printf("%i\n", FPS/2);
  394.           FPS2 = tim;
  395.           FPS = 0;
  396.         }
  397.        
  398.        
  399.         SDL_GL_SwapBuffers();
  400.     }
  401.     SDL_Quit();
  402.     return 0;
  403. }
  404. // Directional Lighting PER VERTEX
  405. /*
  406.     char ves[] =    "uniform int time; void main() {\
  407.         vec3 normal, lightDir; \
  408.         vec4 diffuse; \
  409.         float NdotL; \
  410.         normal = normalize(gl_NormalMatrix * gl_Normal); \
  411.         lightDir = normalize(vec3(gl_LightSource[0].position)); \
  412.         NdotL = max(dot(normal, lightDir), 0.0); \
  413.         diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse; \
  414.         gl_FrontColor =  NdotL * diffuse; \
  415.         gl_Position = ftransform(); \
  416.     }";
  417.     char frs[] = "uniform int time; void main() {gl_FragColor = gl_Color;\
  418.     if (time > 1){\
  419.     gl_FragColor[0] *= 0.39;\
  420.     gl_FragColor[1] *= 0.26;\
  421.     gl_FragColor[2] *= 0.13;}\
  422.     else{\
  423.     gl_FragColor[0] *= 0.29;\
  424.     gl_FragColor[1] *= 0.46;\
  425.     gl_FragColor[2] *= 0.03;}\
  426. }";
  427. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement