From 8ca937fc3b1bf15c820a1c7d3bf0283a91bec72c Mon Sep 17 00:00:00 2001 From: "RALOVICH, Kristóf" Date: Sat, 27 Mar 2010 22:11:29 -0400 Subject: progs/glsl: let the mouse rotate the scene --- progs/glsl/vsraytrace.c | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) (limited to 'progs/glsl/vsraytrace.c') diff --git a/progs/glsl/vsraytrace.c b/progs/glsl/vsraytrace.c index 962b1bdb4c..327d48b897 100644 --- a/progs/glsl/vsraytrace.c +++ b/progs/glsl/vsraytrace.c @@ -27,11 +27,14 @@ #include #include #include "shaderutil.h" - +#include static int Win; static int WinWidth = 256, WinHeight = 256; static int mouseGrabbed = 0; +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" @@ -206,11 +209,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 inline +float +deg2rad(const float degree) +{ + return( degree * 0.017453292519943295769236907684886F); +} + +static inline 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 inline 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 +248,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 +315,14 @@ 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); } } @@ -355,7 +388,7 @@ main(int argc, char *argv[]) glutDisplayFunc(Draw); glutIdleFunc(Draw); glutMouseFunc(mouse); - glutMotionFunc(drag); + glutPassiveMotionFunc(drag); Init(); glutMainLoop(); return 0; -- cgit v1.2.3 From 909f0b356eab04f03389ea36ebf0a83dbb930f5d Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Mon, 29 Mar 2010 11:44:39 -0600 Subject: progs/glsl: improve the mouse drag/rotate code a little --- progs/glsl/fsraytrace.c | 8 +++----- progs/glsl/vsraytrace.c | 10 ++++------ 2 files changed, 7 insertions(+), 11 deletions(-) (limited to 'progs/glsl/vsraytrace.c') diff --git a/progs/glsl/fsraytrace.c b/progs/glsl/fsraytrace.c index 357da67b9c..392f01b200 100644 --- a/progs/glsl/fsraytrace.c +++ b/progs/glsl/fsraytrace.c @@ -333,6 +333,7 @@ drag(int x, int y) yRot = (float)(y - WinHeight/2) / scale; identity(rot); rotate_xy(rot, yRot, xRot); + glutPostRedisplay(); } } @@ -341,10 +342,7 @@ static void mouse(int button, int state, int x, int y) { - if(state == GLUT_DOWN) - { - mouseGrabbed = !mouseGrabbed; - } + mouseGrabbed = (state == GLUT_DOWN); } @@ -398,7 +396,7 @@ main(int argc, char *argv[]) glutKeyboardFunc(Key); glutDisplayFunc(Draw); glutMouseFunc(mouse); - glutPassiveMotionFunc(drag); + glutMotionFunc(drag); glutIdleFunc(Draw); Init(); glutMainLoop(); diff --git a/progs/glsl/vsraytrace.c b/progs/glsl/vsraytrace.c index 327d48b897..ee4fe47761 100644 --- a/progs/glsl/vsraytrace.c +++ b/progs/glsl/vsraytrace.c @@ -31,7 +31,7 @@ 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}; @@ -323,6 +323,7 @@ drag(int x, int y) yRot = (float)(y - WinHeight/2) / scale; identity(rot); rotate_xy(rot, yRot, xRot); + glutPostRedisplay(); } } @@ -331,10 +332,7 @@ static void mouse(int button, int state, int x, int y) { - if(state == GLUT_DOWN) - { - mouseGrabbed = !mouseGrabbed; - } + mouseGrabbed = (state == GLUT_DOWN); } @@ -388,7 +386,7 @@ main(int argc, char *argv[]) glutDisplayFunc(Draw); glutIdleFunc(Draw); glutMouseFunc(mouse); - glutPassiveMotionFunc(drag); + glutMotionFunc(drag); Init(); glutMainLoop(); return 0; -- cgit v1.2.3 From 52f728921563192ccbabf5ffe98234d8d330b933 Mon Sep 17 00:00:00 2001 From: Vinson Lee Date: Mon, 29 Mar 2010 13:58:39 -0700 Subject: progs/glsl: Remove inline keyword. Fixes MSVC build. --- progs/glsl/fsraytrace.c | 6 +++--- progs/glsl/vsraytrace.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'progs/glsl/vsraytrace.c') diff --git a/progs/glsl/fsraytrace.c b/progs/glsl/fsraytrace.c index 392f01b200..dcfc194987 100644 --- a/progs/glsl/fsraytrace.c +++ b/progs/glsl/fsraytrace.c @@ -221,14 +221,14 @@ static const char* fsSource = " gl_FragColor = trace1(r); \n" "}\n"; -static inline +static float deg2rad(const float degree) { return( degree * 0.017453292519943295769236907684886F); } -static inline void +static void rotate_xy(float* mat3, const float degreesAroundX, const float degreesAroundY) { const float rad1 = deg2rad(degreesAroundX); @@ -242,7 +242,7 @@ rotate_xy(float* mat3, const float degreesAroundX, const float degreesAroundY) mat3[2] = -c1*s2;mat3[5] = s1; mat3[8] = c1*c2; } -static inline void +static void identity(float* mat3) { mat3[0] = 1.0F; mat3[3] = 0.0F; mat3[6] = 0.0F; diff --git a/progs/glsl/vsraytrace.c b/progs/glsl/vsraytrace.c index ee4fe47761..67832b8a46 100644 --- a/progs/glsl/vsraytrace.c +++ b/progs/glsl/vsraytrace.c @@ -212,14 +212,14 @@ static const char* vsSource = "}\n"; -static inline +static float deg2rad(const float degree) { return( degree * 0.017453292519943295769236907684886F); } -static inline void +static void rotate_xy(float* mat3, const float degreesAroundX, const float degreesAroundY) { const float rad1 = deg2rad(degreesAroundX); @@ -233,7 +233,7 @@ rotate_xy(float* mat3, const float degreesAroundX, const float degreesAroundY) mat3[2] = -c1*s2;mat3[5] = s1; mat3[8] = c1*c2; } -static inline void +static void identity(float* mat3) { mat3[0] = 1.0F; mat3[3] = 0.0F; mat3[6] = 0.0F; -- cgit v1.2.3 From 90075f34e24a6d6c16198c5b99fb25d713910e8a Mon Sep 17 00:00:00 2001 From: Vinson Lee Date: Mon, 29 Mar 2010 14:56:12 -0700 Subject: progs/glsl: Add workarounds for Apple GLSL compiler crash. fsraytrace and vsraytrace were crashing on Mac OS X 10.6.3 in the Apple GLSL compiler function TPPStreamCompiler::assignOperands. Removing some const qualifers made the crashes go away. --- progs/glsl/fsraytrace.c | 7 +++++++ progs/glsl/vsraytrace.c | 7 +++++++ 2 files changed, 14 insertions(+) (limited to 'progs/glsl/vsraytrace.c') diff --git a/progs/glsl/fsraytrace.c b/progs/glsl/fsraytrace.c index dcfc194987..af72a99099 100644 --- a/progs/glsl/fsraytrace.c +++ b/progs/glsl/fsraytrace.c @@ -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" diff --git a/progs/glsl/vsraytrace.c b/progs/glsl/vsraytrace.c index 67832b8a46..64d928883e 100644 --- a/progs/glsl/vsraytrace.c +++ b/progs/glsl/vsraytrace.c @@ -64,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" -- cgit v1.2.3