progs/glsl: let the mouse rotate the scene

This commit is contained in:
RALOVICH, Kristóf
2010-03-27 22:11:29 -04:00
committed by Brian Paul
parent 1cbd510688
commit 8ca937fc3b
2 changed files with 84 additions and 32 deletions

View File

@@ -27,23 +27,23 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GL/glut.h> #include <GL/glut.h>
#include "shaderutil.h" #include "shaderutil.h"
#include <math.h>
static int Win; static int Win;
static int WinWidth = 512, WinHeight = 512; static int WinWidth = 512, WinHeight = 512;
static GLfloat Xrot = 0, Yrot = 0;
static int mouseGrabbed = 0; static int mouseGrabbed = 0;
static GLuint vertShader; static GLuint vertShader;
static GLuint fragShader; static GLuint fragShader;
static GLuint program; static GLuint program;
static float rot[9] = {1,0,0, 0,1,0, 0,0,1};
static const char* vsSource = static const char* vsSource =
"varying vec2 rayDir;\n" "varying vec2 rayDir; \n"
"\n" " \n"
"void main()\n" "void main() \n"
"{\n" "{ \n"
" rayDir = gl_MultiTexCoord0.xy - vec2(0.5,0.5);\n" " rayDir = gl_MultiTexCoord0.xy - vec2(0.5,0.5); \n"
" gl_Position = gl_ProjectionMatrix * gl_Vertex;\n" " gl_Position = gl_ProjectionMatrix * gl_Vertex; \n"
"}\n"; "}\n";
static const char* fsSource = static const char* fsSource =
@@ -213,29 +213,46 @@ static const char* fsSource =
" clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n" " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
"} \n" "} \n"
" \n" " \n"
"\n" "void main() \n"
"void\n" "{ \n"
"main()\n" " const float z = -0.5; \n"
"{\n"
" const float z = -0.5;\n"
" const vec3 cameraPos = vec3(0,0,3); \n" " const vec3 cameraPos = vec3(0,0,3); \n"
" Ray r = Ray(cameraPos, normalize(vec3(rayDir, z) * rot));\n" " Ray r = Ray(cameraPos, normalize(vec3(rayDir, z) * rot)); \n"
" gl_FragColor = trace1(r);\n" " gl_FragColor = trace1(r); \n"
"}\n"; "}\n";
static inline
float
static void deg2rad(const float degree)
Idle(void)
{ {
glutPostRedisplay(); 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 static void
Draw(void) Draw(void)
{ {
float rot[9] = {1,0,0, 0,1,0, 0,0,1};
GLint location = glGetUniformLocation(program, "rot"); GLint location = glGetUniformLocation(program, "rot");
static const float m = -10.F; static const float m = -10.F;
static const float p = 10.F; static const float p = 10.F;
@@ -311,9 +328,11 @@ drag(int x, int y)
float scale = 1.5F; float scale = 1.5F;
if(mouseGrabbed) if(mouseGrabbed)
{ {
Xrot = (float)(x - WinWidth/2) / scale; static GLfloat xRot = 0, yRot = 0;
Yrot = (float)(y - WinHeight/2) / scale; xRot = (float)(x - WinWidth/2) / scale;
printf("%4.2f %4.2f\n", Xrot, Yrot); yRot = (float)(y - WinHeight/2) / scale;
identity(rot);
rotate_xy(rot, yRot, xRot);
} }
} }
@@ -380,7 +399,7 @@ main(int argc, char *argv[])
glutDisplayFunc(Draw); glutDisplayFunc(Draw);
glutMouseFunc(mouse); glutMouseFunc(mouse);
glutPassiveMotionFunc(drag); glutPassiveMotionFunc(drag);
glutIdleFunc(Idle); glutIdleFunc(Draw);
Init(); Init();
glutMainLoop(); glutMainLoop();
return 0; return 0;

View File

@@ -27,11 +27,14 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GL/glut.h> #include <GL/glut.h>
#include "shaderutil.h" #include "shaderutil.h"
#include <math.h>
static int Win; static int Win;
static int WinWidth = 256, WinHeight = 256; static int WinWidth = 256, WinHeight = 256;
static int mouseGrabbed = 0; 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 = static const char* vsSource =
"const float INF = 9999.9; \n" "const float INF = 9999.9; \n"
@@ -206,11 +209,37 @@ static const char* vsSource =
" Ray ray = Ray(cameraPos, rayDir); \n" " Ray ray = Ray(cameraPos, rayDir); \n"
" gl_Position = gl_Vertex; \n" " gl_Position = gl_Vertex; \n"
" gl_FrontColor = trace1(ray); \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 static void
Draw(void) Draw(void)
@@ -219,7 +248,6 @@ Draw(void)
const float h = 0.5F * WinHeight; const float h = 0.5F * WinHeight;
int x,y; int x,y;
float rot[9] = {1,0,0, 0,1,0, 0,0,1};
GLint location = glGetUniformLocation(program, "rot"); GLint location = glGetUniformLocation(program, "rot");
glUseProgram(program); glUseProgram(program);
@@ -287,9 +315,14 @@ static
void void
drag(int x, int y) drag(int x, int y)
{ {
float scale = 1.5F;
if(mouseGrabbed) 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); glutDisplayFunc(Draw);
glutIdleFunc(Draw); glutIdleFunc(Draw);
glutMouseFunc(mouse); glutMouseFunc(mouse);
glutMotionFunc(drag); glutPassiveMotionFunc(drag);
Init(); Init();
glutMainLoop(); glutMainLoop();
return 0; return 0;