diff options
Diffstat (limited to 'progs')
-rw-r--r-- | progs/gallium/python/samples/tri.py | 20 | ||||
-rw-r--r-- | progs/glsl/fsraytrace.c | 80 | ||||
-rw-r--r-- | progs/glsl/vsraytrace.c | 60 |
3 files changed, 111 insertions, 49 deletions
diff --git a/progs/gallium/python/samples/tri.py b/progs/gallium/python/samples/tri.py index d7fbdb10ac..9f6d787dcb 100644 --- a/progs/gallium/python/samples/tri.py +++ b/progs/gallium/python/samples/tri.py @@ -30,19 +30,19 @@ from gallium import * -def make_image(surface): - data = surface.get_tile_rgba8(0, 0, surface.width, surface.height) +def make_image(ctx, surface): + data = ctx.surface_read_rgba8(surface, 0, 0, surface.width, surface.height) import Image outimage = Image.fromstring('RGBA', (surface.width, surface.height), data, "raw", 'RGBA', 0, 1) return outimage -def save_image(filename, surface): - outimage = make_image(surface) +def save_image(ctx, surface, filename): + outimage = make_image(ctx, surface) outimage.save(filename, "PNG") -def show_image(surface): - outimage = make_image(surface) +def show_image(ctx, surface): + outimage = make_image(ctx, surface) import Tkinter as tk from PIL import Image, ImageTk @@ -216,10 +216,10 @@ def test(dev): ctx.flush() - show_image(cbuf) - #show_image(zbuf) - #save_image('cbuf.png', cbuf) - #save_image('zbuf.png', zbuf) + show_image(ctx, cbuf) + show_image(ctx, zbuf) + save_image(ctx, cbuf, 'cbuf.png') + save_image(ctx, zbuf, 'zbuf.png') diff --git a/progs/glsl/fsraytrace.c b/progs/glsl/fsraytrace.c index 8d54757e59..af72a99099 100644 --- a/progs/glsl/fsraytrace.c +++ b/progs/glsl/fsraytrace.c @@ -27,23 +27,23 @@ #include <GL/glew.h> #include <GL/glut.h> #include "shaderutil.h" - +#include <math.h> static int Win; static int WinWidth = 512, WinHeight = 512; -static GLfloat Xrot = 0, Yrot = 0; static int mouseGrabbed = 0; static GLuint vertShader; static GLuint fragShader; static GLuint program; +static float rot[9] = {1,0,0, 0,1,0, 0,0,1}; static const char* vsSource = - "varying vec2 rayDir;\n" - "\n" - "void main()\n" - "{\n" - " rayDir = gl_MultiTexCoord0.xy - vec2(0.5,0.5);\n" - " gl_Position = gl_ProjectionMatrix * gl_Vertex;\n" + "varying vec2 rayDir; \n" + " \n" + "void main() \n" + "{ \n" + " rayDir = gl_MultiTexCoord0.xy - vec2(0.5,0.5); \n" + " gl_Position = gl_ProjectionMatrix * gl_Vertex; \n" "}\n"; static const char* fsSource = @@ -76,10 +76,17 @@ static const char* fsSource = " vec3 n; \n" "}; \n" " \n" +#ifdef __APPLE__ + "Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n" + "Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n" + "Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n" + "Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n" +#else "const Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n" "const Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n" "const Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n" "const Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n" +#endif " \n" "// Mesa intel gen4 generates \"unsupported IR in fragment shader 13\" for\n" "// sqrt, let's work around. \n" @@ -213,29 +220,46 @@ static const char* fsSource = " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n" "} \n" " \n" - "\n" - "void\n" - "main()\n" - "{\n" - " const float z = -0.5;\n" + "void main() \n" + "{ \n" + " const float z = -0.5; \n" " const vec3 cameraPos = vec3(0,0,3); \n" - " Ray r = Ray(cameraPos, normalize(vec3(rayDir, z) * rot));\n" - " gl_FragColor = trace1(r);\n" + " Ray r = Ray(cameraPos, normalize(vec3(rayDir, z) * rot)); \n" + " gl_FragColor = trace1(r); \n" "}\n"; - +static +float +deg2rad(const float degree) +{ + return( degree * 0.017453292519943295769236907684886F); +} static void -Idle(void) +rotate_xy(float* mat3, const float degreesAroundX, const float degreesAroundY) { - glutPostRedisplay(); + const float rad1 = deg2rad(degreesAroundX); + const float c1 = cosf(rad1); + const float s1 = sinf(rad1); + const float rad2 = deg2rad(degreesAroundY); + const float c2 = cosf(rad2); + const float s2 = sinf(rad2); + mat3[0] = c2; mat3[3] = 0.0F; mat3[6] = s2; + mat3[1] = s1*s2; mat3[4] = c1; mat3[7] = -s1*c2; + mat3[2] = -c1*s2;mat3[5] = s1; mat3[8] = c1*c2; } +static void +identity(float* mat3) +{ + mat3[0] = 1.0F; mat3[3] = 0.0F; mat3[6] = 0.0F; + mat3[1] = 0.0F; mat3[4] = 1.0F; mat3[7] = 0.0F; + mat3[2] = 0.0F; mat3[5] = 0.0F; mat3[8] = 1.0F; +} static void Draw(void) { - float rot[9] = {1,0,0, 0,1,0, 0,0,1}; GLint location = glGetUniformLocation(program, "rot"); static const float m = -10.F; static const float p = 10.F; @@ -311,9 +335,12 @@ drag(int x, int y) float scale = 1.5F; if(mouseGrabbed) { - Xrot = (float)(x - WinWidth/2) / scale; - Yrot = (float)(y - WinHeight/2) / scale; - printf("%4.2f %4.2f\n", Xrot, Yrot); + static GLfloat xRot = 0, yRot = 0; + xRot = (float)(x - WinWidth/2) / scale; + yRot = (float)(y - WinHeight/2) / scale; + identity(rot); + rotate_xy(rot, yRot, xRot); + glutPostRedisplay(); } } @@ -322,10 +349,7 @@ static void mouse(int button, int state, int x, int y) { - if(state == GLUT_DOWN) - { - mouseGrabbed = !mouseGrabbed; - } + mouseGrabbed = (state == GLUT_DOWN); } @@ -379,8 +403,8 @@ main(int argc, char *argv[]) glutKeyboardFunc(Key); glutDisplayFunc(Draw); glutMouseFunc(mouse); - glutPassiveMotionFunc(drag); - glutIdleFunc(Idle); + glutMotionFunc(drag); + glutIdleFunc(Draw); Init(); glutMainLoop(); return 0; diff --git a/progs/glsl/vsraytrace.c b/progs/glsl/vsraytrace.c index 962b1bdb4c..64d928883e 100644 --- a/progs/glsl/vsraytrace.c +++ b/progs/glsl/vsraytrace.c @@ -27,11 +27,14 @@ #include <GL/glew.h> #include <GL/glut.h> #include "shaderutil.h" - +#include <math.h> static int Win; static int WinWidth = 256, WinHeight = 256; -static int mouseGrabbed = 0; +static GLboolean mouseGrabbed = GL_FALSE; +static GLuint vertShader; +static GLuint program; +float rot[9] = {1,0,0, 0,1,0, 0,0,1}; static const char* vsSource = "const float INF = 9999.9; \n" @@ -61,10 +64,17 @@ static const char* vsSource = " vec3 n; \n" "}; \n" " \n" +#ifdef __APPLE__ + "Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n" + "Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n" + "Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n" + "Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n" +#else "const Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n" "const Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n" "const Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n" "const Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n" +#endif " \n" "// Mesa intel gen4 generates \"unsupported IR in fragment shader 13\" for\n" "// sqrt, let's work around. \n" @@ -206,11 +216,37 @@ static const char* vsSource = " Ray ray = Ray(cameraPos, rayDir); \n" " gl_Position = gl_Vertex; \n" " gl_FrontColor = trace1(ray); \n" - "} \n"; + "}\n"; -static GLuint vertShader; -static GLuint program; +static +float +deg2rad(const float degree) +{ + return( degree * 0.017453292519943295769236907684886F); +} + +static void +rotate_xy(float* mat3, const float degreesAroundX, const float degreesAroundY) +{ + const float rad1 = deg2rad(degreesAroundX); + const float c1 = cosf(rad1); + const float s1 = sinf(rad1); + const float rad2 = deg2rad(degreesAroundY); + const float c2 = cosf(rad2); + const float s2 = sinf(rad2); + mat3[0] = c2; mat3[3] = 0.0F; mat3[6] = s2; + mat3[1] = s1*s2; mat3[4] = c1; mat3[7] = -s1*c2; + mat3[2] = -c1*s2;mat3[5] = s1; mat3[8] = c1*c2; +} + +static void +identity(float* mat3) +{ + mat3[0] = 1.0F; mat3[3] = 0.0F; mat3[6] = 0.0F; + mat3[1] = 0.0F; mat3[4] = 1.0F; mat3[7] = 0.0F; + mat3[2] = 0.0F; mat3[5] = 0.0F; mat3[8] = 1.0F; +} static void Draw(void) @@ -219,7 +255,6 @@ Draw(void) const float h = 0.5F * WinHeight; int x,y; - float rot[9] = {1,0,0, 0,1,0, 0,0,1}; GLint location = glGetUniformLocation(program, "rot"); glUseProgram(program); @@ -287,9 +322,15 @@ static void drag(int x, int y) { + float scale = 1.5F; if(mouseGrabbed) { - printf("%4d %4d\n", x, y); + static GLfloat xRot = 0, yRot = 0; + xRot = (float)(x - WinWidth/2) / scale; + yRot = (float)(y - WinHeight/2) / scale; + identity(rot); + rotate_xy(rot, yRot, xRot); + glutPostRedisplay(); } } @@ -298,10 +339,7 @@ static void mouse(int button, int state, int x, int y) { - if(state == GLUT_DOWN) - { - mouseGrabbed = !mouseGrabbed; - } + mouseGrabbed = (state == GLUT_DOWN); } |