gay quien lo lea
authorroot <root@liso-desktop.(none)>
Sat, 12 Sep 2009 11:00:54 +0000 (13:00 +0200)
committerroot <root@liso-desktop.(none)>
Sat, 12 Sep 2009 11:00:54 +0000 (13:00 +0200)
Makefile
dl_init.c
dl_init.h
main.c

index 13e9174a3ea0bf61c17da6f829003dc2a0bdaac0..a57799261f9494382c4ae5a6e0af088594879bdb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@
 CFLAGS=-m32 -Os -std=c99 -fomit-frame-pointer -ffast-math -Wall -DX86_ASM  \
        $(shell sdl-config --cflags) -fno-strict-aliasing #-DDEBUG
 
-LDFLAGS=-m32 -nostdlib -nostartfiles -ldl -lpthread  -lm
+LDFLAGS=-m32 -nostdlib -nostartfiles -ldl -lpthread  -lm -lGL -lGLU
 
 all: main
        ./pack.sh main
index d48cbd5762dce6ed167936bf42c0637627d28db5..22e88ab541d289db700dc9ad91bc3126f0dcde8c 100644 (file)
--- a/dl_init.c
+++ b/dl_init.c
@@ -1,6 +1,7 @@
 #include "dl_init.h"
 #include <dlfcn.h>
 
+
 FN_STRUCT fn;
 
 char *names="libSDL-1.2.so.0\0SDL_Init\0SDL_Quit\0SDL_OpenAudio\0"
@@ -15,7 +16,10 @@ char *names="libSDL-1.2.so.0\0SDL_Init\0SDL_Quit\0SDL_OpenAudio\0"
            "libGL.so\0glRotatef\0"
            "glBindTexture\0glCopyTexSubImage2D\0glInterleavedArrays\0"
            "glEnable\0glDisable\0glMatrixMode\0glLoadIdentity\0glClear\0"
-           "glRecti\0glClearColor\0"
+           "glRecti\0glClearColor\0glGetError\0"
+           "glCreateShader\0glCreateProgram\0"
+           "glShaderSource\0glCompileShader\0" 
+           "glAttachShader\0glLinkProgram\0glUseProgram\0"
 
            "libGLU.so\0gluPerspective\0gluLookAt\0"
            "\0";
index 1db01d37106772c1bd11762ebd7f604161aa1b5f..d21895a14deb8d47a16b00e409dfa0e02794bb20 100644 (file)
--- a/dl_init.h
+++ b/dl_init.h
@@ -52,18 +52,30 @@ typedef struct
        //void  (*glRasterPos2f)(GLfloat, GLfloat);
        //void  (*glCallLists)(GLsizei, GLenum, const GLvoid *);
 
+
+       GLenum (*glGetError)(void);
+       //Shaders
+       GLuint(*glCreateShader)(GLint );
+       GLuint(*glCreateProgram)(void); 
+       void (*glShaderSource)(GLuint,GLsizei,const GLchar **, const GLint *);
+       void (*glCompileShader)(GLuint);
+       void (*glAttachShader)(GLuint , GLuint );
+       void (*glLinkProgram)(GLuint);
+       void (*glUseProgram)(GLuint);
+       
        // libGLU
        void    (*gluPerspective)(GLdouble, GLdouble, GLdouble, GLdouble);
        void    (*gluLookAt)(GLdouble, GLdouble, GLdouble,
                        GLdouble, GLdouble, GLdouble,
                        GLdouble, GLdouble, GLdouble);
+       
        //GLint (*gluBuild2DMipmaps)(GLenum, GLint, GLsizei, GLsizei,
        //                      GLenum, GLenum, const void *);
 
        // libX11.so
        //Display* (*XOpenDisplay)(int);
        //XFontStruct* (*XLoadQueryFont)(Display*, char *);
-
+       
 } FN_STRUCT;
 
 extern void dl_init(void);
diff --git a/main.c b/main.c
index 0f25cad338488e39d691f6c08a27160bcab5ec22..20877c6e9ab89182850b2cc7f1c547a832482143 100644 (file)
--- a/main.c
+++ b/main.c
@@ -3,25 +3,44 @@
 #include <stdlib.h>
 
 #include "dl_init.h"
+//#include "cube.h"
+
+#define sin(x) __builtin_sinf(x)
+#define cos(x) __builtin_cosf(x)
+#define powf(p,n) __builtin_powf(p,n)
+
+#define absf(x) (x>0.0)?x:-x
+#define sign(x) (x > 0)? 1:-1
 
-//#define sin(x) __builtin_sinf(x)
-//#define cos(x) __builtin_cosf(x)
 //#define sqrt(x) __builtin_sqrtf(x)
 //#define expf(x) __builtin_expf(x)
 
+#define DTOR 0.01745329252
+
+
 #define SCREEN_WIDTH 1280
-#define SCREEN_HEIGHT 720 
+#define SCREEN_HEIGHT 720
 
 float current_t;
 
 #define renderTexture 1
 
-typedef struct 
+typedef struct{
+       float x;
+       float y;
+       float z;
+} Vec3;
+
+#define VERTEX_SIZE 5000
+Vec3 vertices[VERTEX_SIZE];
+Vec3 normales[VERTEX_SIZE];
+GLuint p ;
+typedef struct
 {
        float s,t,aux1,aux2; // tex coords
        float c1,c2,c3,c4; // color
        float n1,n2,n3; // normal
-       float v1,v2,v3,aux3; // vertice
+       int v1,v2,v3,aux3; // vertice
 } VertexInfo;
 
 #define MODEL_SIZE 1000000
@@ -29,46 +48,274 @@ typedef struct
 VertexInfo gurru1[MODEL_SIZE];
 VertexInfo gurru2[MODEL_SIZE];
 
-void initTextures()
+
+
+float power(float f,float p)
 {
-       fn.glEnable(GL_TEXTURE_2D);
+   int sign;
+   double absf;
+
+   sign = (f <0 ? -1 : 1);
+   absf = (f < 0 ? -f : f);
+
+   if (absf < 0.00001)
+      return(0.0);
+   else
+      return(sign * powf(absf,p));
 }
 
 void initModels()
 {
        // init gurrus
-       for(float u=0.0;u<10.0;u++) 
+       float e=.3;
+       float n=.3;
+       int i=0;
+       int j = 0;
+       for(int iu=0;iu<50;iu++)
        {
-               for(float v=0.0;v<10.0;v++)
+               for(int iv= 0; iv < 100; iv++)//-180;v<180;v+=360/100.0)
                {
+                       float u = -90 + 180 * (float)iu / 49;
+                       float v = -180 + 360 * (float)iv / 99;
+                       float l;
                        // calc superquadric for gurru1
                        // calc superquadric for gurru2
+                       /*
+                       vertices[i].x =(powf(cos(u),n))*(powf(cos(v),e));
+                       vertices[i].y = (powf(cos(u),n))*(powf(sin(v),e));
+                       vertices[i].z = powf( sin(u) ,n);*/
+                       /*
+                       vertices[i].x =(sign(cos(u*DTOR))*powf(absf(cos(u*DTOR)),n))*(sign(cos(v*DTOR))*powf(absf(cos(v*DTOR)),e));
+                       vertices[i].y =(sign(cos(u*DTOR))*powf(absf(cos(u*DTOR)),n))*(sign(sin(v*DTOR))*powf(absf(sin(v*DTOR)),e));
+                       vertices[i].z = sign(sin(u*DTOR))*powf( absf(sin(u*DTOR)) ,e);
+                       */
+                       vertices[i].x =(power(cos(u*DTOR),n))*(power(cos(v*DTOR),e));
+                       vertices[i].y =  (power(cos(u*DTOR),n))*(power(sin(v*DTOR),e));
+                       vertices[i].z = power( sin(u*DTOR) ,n);
+                       
+                       normales[i].x=power(cos(u*DTOR),2-n)*power(cos(v*DTOR),2-e);
+                       normales[i].y=power(cos(u*DTOR),2-n)*power(sin(v*DTOR),2-e);
+                       normales[i].z=power(sin(u*DTOR),2-n);
+                       
+                       l = sqrtf(normales[i].x * normales[i].x + normales[i].y * normales[i].y + normales[i].z * normales[i].z);                       
+                       normales[i].x/= l;
+                       normales[i].y/= l;
+                       normales[i].z/= l;
+                       //vertices[i].x = cos(DTOR*u)* cos(DTOR*v);
+                       //vertices[i].y = cos(DTOR*u) * sin(DTOR*v) ;
+                       //vertices[i].z = sin(DTOR*u) ;
+                       
+                       //printf("%f %f %f %f %f\n",vertices[i].x,vertices[i].y,vertices[i].z,u,v);
+                       i++;
+
                }
        }
-}
+       
+       
+       for(j = 0; j < 99 * 99; j++)
+       {
+               int u = j % 100;
+               int v = j / 100;
+       
+               gurru1[2*j].v1 = u + v * 100;
+               gurru1[2*j].v2 = u + 1 + v * 100;//(i + 1) % 100;//(i+100)%100+i;
+               gurru1[2*j].v3 = u + 1 + (v + 1) * 100;//(i + 1) % 100 + 100;//(i+101)%100+i;
 
-void endRenderTexture() 
-{
-       fn.glBindTexture(GL_TEXTURE_2D, renderTexture);
-       fn.glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
+               gurru1[2*j+1].v1 = u + v * 100;//i;//(i+100)%100+i;
+               gurru1[2*j+1].v2 = u + 1 + (v + 1) * 100;//(i + 1) % 100 + 100;//(i+1)%100+i;
+               gurru1[2*j+1].v3 = u + (v + 1) * 100;//(i % 100) + 100;//(i+101)%100+i;
+       }
+       
 }
 
-void drawScene() 
+void drawScene()
 {
-       if(current_t<10.0) 
+       /*if(current_t<10.0)
        {
                fn.glRotatef(current_t,1.0,1.0,1.0);
                fn.glInterleavedArrays(GL_T4F_C4F_N3F_V4F,0,gurru1);
        }
-       else if(current_t>=10.0 && current_t<20.0) 
-       {       
+       else if(current_t>=10.0 && current_t<20.0)
+       {
                fn.glInterleavedArrays(GL_T4F_C4F_N3F_V4F,0,gurru2);
-       }       
+       }*/
+        // animate
+       const float ftime = 0.1f*(float)current_t;
+    float pos[3] = { 3.0f*cosf( ftime*1.0f ), 
+                     3.0f*cosf( ftime*0.6f ), 
+                     3.0f*sinf( ftime*1.0f ) };
+    float tar[3] = { 0.0f, 0.0f, 0.0f };
+
+    // render
+   glEnable(GL_DEPTH_TEST);
+    glEnable( GL_LIGHTING );
+    glEnable( GL_LIGHT0 );
+    
+   // glEnable( GL_NORMALIZE );
+
+       
+    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
+       glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
+
+    glMatrixMode( GL_PROJECTION );
+       glLoadIdentity();
+    glOrtho(-5, 5, -5*10/16, 5*10/16, -5, 50);
+      
+    glMatrixMode( GL_MODELVIEW );
+    glLoadIdentity();
+   gluLookAt( pos[0], pos[1], pos[2], tar[0], tar[1], tar[2], 0.0f, 1.0f, 0.0f );
+       glShadeModel(GL_SMOOTH);
+       glBegin(GL_TRIANGLES);
+       for(int i=0;i<99*99/*VERTEX_SIZE*/;i++)
+           {
+       //glVertex3f(vertices[i].x,vertices[i].y,vertices[i].z);
+               //if(i%2) glColor3f(1, 1, 1);
+               //else glColor3f(1, 0, 0);
+                    glNormal3fv(&normales[gurru1[i].v1].x);
+                       glVertex3fv(&vertices[gurru1[i].v1].x);
+                  glNormal3fv(&normales[gurru1[i].v2].x);      
+                       glVertex3fv(&vertices[gurru1[i].v2].x);
+           glNormal3fv(&normales[gurru1[i].v3].x);             
+                   glVertex3fv(&vertices[gurru1[i].v3].x);
+
+                   }
+       glEnd();
 }
 
-void drawPostProcess()
+
+
+// glow shader ;)
+const GLchar *vsh="varying vec2  uv;\
+void main(void){\
+   gl_Position = vec4( gl_Vertex.xy, 0.0, 1.0 );\
+   gl_Position = sign( gl_Position );\
+   uv = (vec2( gl_Position.x, - gl_Position.y ) + vec2( 1.0 ) ) / vec2( 2.0 );}";
+
+
+const GLchar *fsh="uniform sampler2D tex;\
+varying vec2 uv;\
+const float sampleDist = 1.0;\
+const float sampleStrength = 2.2;\
+void main(void){\
+       float samples[10] = float[](-0.08,-0.05,-0.03,-0.02,-0.01,0.01,0.02,0.03,0.05,0.08);\
+vec2 dir = 0.5 - uv;\
+float dist = sqrt(dir.x*dir.x + dir.y*dir.y);\
+    dir = dir/dist;\
+    vec4 color = texture2D(tex,uv);\
+    vec4 sum = color;\
+    for (int i = 0;\ i < 5;\ i++){\
+      sum += texture2D( tex, uv + dir * samples[i] * sampleDist );\
+    }\
+    sum *= 1.0/11.0;\
+    float t = dist * sampleStrength;\
+    t = clamp( t ,0.0,1.0);\
+    gl_FragColor = mix( color, sum, t );}";
+
+
+
+
+
+
+void intro_init( void )
 {
-} 
+       unsigned int* data= (GLuint *)calloc( 1, ((512 * 512)* 4 * sizeof(GLuint)) );
+
+       glBindTexture(GL_TEXTURE_2D, renderTexture);                                    // Bind The Texture
+       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0,
+               GL_RGBA, GL_UNSIGNED_BYTE, data);       
+glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
+       glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
+
+
+       free( data );                                                                                           // Release data
+
+       
+}
+
+
+
+
+void initShaders()
+{
+       
+       printf("%x", fn.glCreateShader);
+       GLint e= fn.glGetError();
+       printf("\n%i\n",e);
+       GLuint v =fn.glCreateShader(GL_VERTEX_SHADER);
+        e= fn.glGetError();
+       printf("\n%i\n",e);
+       GLuint f =fn.glCreateShader(GL_FRAGMENT_SHADER);        
+       
+       
+        e= fn.glGetError();
+       printf("\n%i\n",e);
+       p=fn.glCreateProgram();
+                e= fn.glGetError();
+       printf("\n%i\n",e);
+       fn.glShaderSource(v, 1, &vsh, NULL);
+       fn.glCompileShader(v);
+       fn.glShaderSource(f, 1, &fsh, NULL);
+       fn.glCompileShader(f);
+       fn.glAttachShader(p,v);
+       fn.glAttachShader(p,f);
+       fn.glLinkProgram(p);
+       
+       
+}
+
+
+void endRenderTexture()
+{
+       
+       glBindTexture(GL_TEXTURE_2D, renderTexture);
+       glCopyTexSubImage2D(GL_TEXTURE_2D,0, 0, 0, 350, 100, 512, 512);
+       glClearColor(0.0f, 0.0f, 0.0f, 0.0f);                           // Set The Clear Color To Medium Blue
+       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);             // Clear The Screen And Depth Buffer
+
+
+}
+
+void drawR2T()
+{
+       
+       
+       glMatrixMode( GL_PROJECTION );
+       glLoadIdentity();
+//   glOrtho(-5, 5, -5*10/16, 5*10/16, -5, 50);
+      
+    glMatrixMode( GL_MODELVIEW );
+    glLoadIdentity();
+   //gluLookAt( 0, 0, 10, 0,0,0, 0.0f, 1.0f, 0.0f );
+
+       glDisable(GL_LIGHTING);
+       //glDisable(GL_BLEND);
+       glEnable(GL_TEXTURE_2D);                                        // Enable 2D Texture Mapping
+       glBindTexture(GL_TEXTURE_2D,renderTexture);                     // Bind To The Blur Texture
+                               // Switch To An Ortho View
+
+       glBegin(GL_TRIANGLES);
+               glTexCoord2f(0,0);
+               glVertex2f(-1,-1);
+               
+               glTexCoord2f(1,0);
+               glVertex2f(1,-1);
+               
+               glTexCoord2f(1,1);
+               glVertex2f(1,1);
+               
+               glTexCoord2f(0,0);
+               glVertex2f(-1,-1);
+               glTexCoord2f(1,1);
+               glVertex2f(1,1);
+               glTexCoord2f(0,1);
+               glVertex2f(-1,1);
+       glEnd();
+
+
+fn.glUseProgram(0);
+       glDisable(GL_TEXTURE_2D);                                       // Enable 2D Texture Mapping
+       
+}
 
 #ifdef DEBUG
 int main()
@@ -76,18 +323,41 @@ int main()
 void __attribute__((externally_visible)) _start(void)
 #endif
 {
+        initModels();
        SDL_Event event;
        dl_init();
-       
+
        fn.SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
        fn.SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
        fn.SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL,1);
        fn.SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,32,SDL_OPENGL);
        fn.SDL_ShowCursor(0);
 
+       intro_init();
+       //initShaders();
+       
+       //for(int i=0;i<VERTEX_SIZE;i++)
+       //      printf("%f %f %f\n",vertices[i].x,vertices[i].y,vertices[i].z);
+       initShaders();
        do
        {
+               //fn.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+               // fn.glRotatef(0.3f,1,1,1);
+               
+        // draw a single flat rectangle on screen...
+       //      fn.glRecti(-1,-1,1,1);
+
+               
+
                current_t = fn.SDL_GetTicks()/1000.0f;
+               drawScene();
+               endRenderTexture();
+               fn.glUseProgram(p);
+               drawR2T();
+               fn.glUseProgram(0);
+               
+               //intro_do(fn.SDL_GetTicks());
+               //intro_do(current_t);
                fn.SDL_GL_SwapBuffers();
                fn.SDL_PollEvent(&event);
        } while(event.type != SDL_KEYDOWN);