Sign in to follow this  

OpenGL anyone using SGI's trackball code?

This topic is 4549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been using SGI's trackball code to rotate the view around objects with the mouse but I haven't been able to get it to work around an arbitrary center of rotation. Did they hard-code their trackball stuff so it only works around the origin? (if you don't know what I'm talking about the source is below) Can anyone point me to a good resource on how to achieve this effect? I don't care if it means moving away from SGI's code as long its easy (1-2 hours max) to implement. header
/*

 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.

 * ALL RIGHTS RESERVED

 * Permission to use, copy, modify, and distribute this software for

 * any purpose and without fee is hereby granted, provided that the above

 * copyright notice appear in all copies and that both the copyright notice

 * and this permission notice appear in supporting documentation, and that

 * the name of Silicon Graphics, Inc. not be used in advertising

 * or publicity pertaining to distribution of the software without specific,

 * written prior permission.

 *

 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"

 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,

 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR

 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON

 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,

 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY

 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,

 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF

 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN

 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON

 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE

 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.

 *

 * US Government Users Restricted Rights

 * Use, duplication, or disclosure by the Government is subject to

 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph

 * (c)(1)(ii) of the Rights in Technical Data and Computer Software

 * clause at DFARS 252.227-7013 and/or in similar or successor

 * clauses in the FAR or the DOD or NASA FAR Supplement.

 * Unpublished-- rights reserved under the copyright laws of the

 * United States.  Contractor/manufacturer is Silicon Graphics,

 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.

 *

 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.

 */

/*

 * trackball.h

 * A virtual trackball implementation

 * Written by Gavin Bell for Silicon Graphics, November 1988.

 */



/*

 * Pass the x and y coordinates of the last and current positions of

 * the mouse, scaled so they are from (-1.0 ... 1.0).

 *

 * The resulting rotation is returned as a quaternion rotation in the

 * first paramater.

 */

void

trackball(float q[4], float p1x, float p1y, float p2x, float p2y);



/*

 * Given two quaternions, add them together to get a third quaternion.

 * Adding quaternions to get a compound rotation is analagous to adding

 * translations to get a compound translation.  When incrementally

 * adding rotations, the first argument here should be the new

 * rotation, the second and third the total rotation (which will be

 * over-written with the resulting new total rotation).

 */

void

add_quats(float *q1, float *q2, float *dest);



/*

 * A useful function, builds a rotation matrix in Matrix based on

 * given quaternion.

 */

void

build_rotmatrix(float m[4][4], float q[4]);



/*

 * This function computes a quaternion based on an axis (defined by

 * the given vector) and an angle about which to rotate.  The angle is

 * expressed in radians.  The result is put into the third argument.

 */

void

axis_to_quat(float a[3], float phi, float q[4]);

source
/*

 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.

 * ALL RIGHTS RESERVED

 * Permission to use, copy, modify, and distribute this software for

 * any purpose and without fee is hereby granted, provided that the above

 * copyright notice appear in all copies and that both the copyright notice

 * and this permission notice appear in supporting documentation, and that

 * the name of Silicon Graphics, Inc. not be used in advertising

 * or publicity pertaining to distribution of the software without specific,

 * written prior permission.

 *

 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"

 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,

 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR

 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON

 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,

 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY

 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,

 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF

 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN

 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON

 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE

 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.

 *

 * US Government Users Restricted Rights

 * Use, duplication, or disclosure by the Government is subject to

 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph

 * (c)(1)(ii) of the Rights in Technical Data and Computer Software

 * clause at DFARS 252.227-7013 and/or in similar or successor

 * clauses in the FAR or the DOD or NASA FAR Supplement.

 * Unpublished-- rights reserved under the copyright laws of the

 * United States.  Contractor/manufacturer is Silicon Graphics,

 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.

 *

 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.

 */

/*

 * Trackball code:

 *

 * Implementation of a virtual trackball.

 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and

 *   the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.

 *

 * Vector manip code:

 *

 * Original code from:

 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli

 *

 * Much mucking with by:

 * Gavin Bell

 */

#include <math.h>

#include "trackball.h"



/*

 * This size should really be based on the distance from the center of

 * rotation to the point on the object underneath the mouse.  That

 * point would then track the mouse as closely as possible.  This is a

 * simple example, though, so that is left as an Exercise for the

 * Programmer.

 */

#define TRACKBALLSIZE  (0.8)



/*

 * Local function prototypes (not defined in trackball.h)

 */

static float tb_project_to_sphere(float, float, float);

static void normalize_quat(float [4]);



void

vzero(float *v)

{

    v[0] = 0.0;

    v[1] = 0.0;

    v[2] = 0.0;

}



void

vset(float *v, float x, float y, float z)

{

    v[0] = x;

    v[1] = y;

    v[2] = z;

}



void

vsub(const float *src1, const float *src2, float *dst)

{

    dst[0] = src1[0] - src2[0];

    dst[1] = src1[1] - src2[1];

    dst[2] = src1[2] - src2[2];

}



void

vcopy(const float *v1, float *v2)

{

    register int i;

    for (i = 0 ; i < 3 ; i++)

        v2[i] = v1[i];

}



void

vcross(const float *v1, const float *v2, float *cross)

{

    float temp[3];



    temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);

    temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);

    temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);

    vcopy(temp, cross);

}



float

vlength(const float *v)

{

    return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);

}



void

vscale(float *v, float div)

{

    v[0] *= div;

    v[1] *= div;

    v[2] *= div;

}



void

vnormal(float *v)

{

    vscale(v,1.0/vlength(v));

}



float

vdot(const float *v1, const float *v2)

{

    return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];

}



void

vadd(const float *src1, const float *src2, float *dst)

{

    dst[0] = src1[0] + src2[0];

    dst[1] = src1[1] + src2[1];

    dst[2] = src1[2] + src2[2];

}



/*

 * Ok, simulate a track-ball.  Project the points onto the virtual

 * trackball, then figure out the axis of rotation, which is the cross

 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)

 * Note:  This is a deformed trackball-- is a trackball in the center,

 * but is deformed into a hyperbolic sheet of rotation away from the

 * center.  This particular function was chosen after trying out

 * several variations.

 *

 * It is assumed that the arguments to this routine are in the range

 * (-1.0 ... 1.0)

 */

void

trackball(float q[4], float p1x, float p1y, float p2x, float p2y)

{

    float a[3]; /* Axis of rotation */

    float phi;  /* how much to rotate about axis */

    float p1[3], p2[3], d[3];

    float t;



    if (p1x == p2x && p1y == p2y) {

        /* Zero rotation */

        vzero(q);

        q[3] = 1.0;

        return;

    }



    /*

     * First, figure out z-coordinates for projection of P1 and P2 to

     * deformed sphere

     */

    vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));

    vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));



    /*

     *  Now, we want the cross product of P1 and P2

     */

    vcross(p2,p1,a);



    /*

     *  Figure out how much to rotate around that axis.

     */

    vsub(p1,p2,d);

    t = vlength(d) / (2.0*TRACKBALLSIZE);



    /*

     * Avoid problems with out-of-control values...

     */

    if (t > 1.0) t = 1.0;

    if (t < -1.0) t = -1.0;

    phi = 2.0 * asin(t);



    axis_to_quat(a,phi,q);

}



/*

 *  Given an axis and angle, compute quaternion.

 */

void

axis_to_quat(float a[3], float phi, float q[4])

{

    vnormal(a);

    vcopy(a,q);

    vscale(q,sin(phi/2.0));

    q[3] = cos(phi/2.0);

}



/*

 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet

 * if we are away from the center of the sphere.

 */

static float

tb_project_to_sphere(float r, float x, float y)

{

    float d, t, z;



    d = sqrt(x*x + y*y);

    if (d < r * 0.70710678118654752440) {    /* Inside sphere */

        z = sqrt(r*r - d*d);

    } else {           /* On hyperbola */

        t = r / 1.41421356237309504880;

        z = t*t / d;

    }

    return z;

}



/*

 * Given two rotations, e1 and e2, expressed as quaternion rotations,

 * figure out the equivalent single rotation and stuff it into dest.

 *

 * This routine also normalizes the result every RENORMCOUNT times it is

 * called, to keep error from creeping in.

 *

 * NOTE: This routine is written so that q1 or q2 may be the same

 * as dest (or each other).

 */



#define RENORMCOUNT 97



void

add_quats(float q1[4], float q2[4], float dest[4])

{

    static int count=0;

    float t1[4], t2[4], t3[4];

    float tf[4];



    vcopy(q1,t1);

    vscale(t1,q2[3]);



    vcopy(q2,t2);

    vscale(t2,q1[3]);



    vcross(q2,q1,t3);

    vadd(t1,t2,tf);

    vadd(t3,tf,tf);

    tf[3] = q1[3] * q2[3] - vdot(q1,q2);



    dest[0] = tf[0];

    dest[1] = tf[1];

    dest[2] = tf[2];

    dest[3] = tf[3];



    if (++count > RENORMCOUNT) {

        count = 0;

        normalize_quat(dest);

    }

}



/*

 * Quaternions always obey:  a^2 + b^2 + c^2 + d^2 = 1.0

 * If they don't add up to 1.0, dividing by their magnitued will

 * renormalize them.

 *

 * Note: See the following for more information on quaternions:

 *

 * - Shoemake, K., Animating rotation with quaternion curves, Computer

 *   Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.

 * - Pletinckx, D., Quaternion calculus as a basic tool in computer

 *   graphics, The Visual Computer 5, 2-13, 1989.

 */

static void

normalize_quat(float q[4])

{

    int i;

    float mag;



    mag = (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);

    for (i = 0; i < 4; i++) q[i] /= mag;

}



/*

 * Build a rotation matrix, given a quaternion rotation.

 *

 */

void

build_rotmatrix(float m[4][4], float q[4])

{

    m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);

    m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);

    m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);

    m[0][3] = 0.0;



    m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);

    m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);

    m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);

    m[1][3] = 0.0;



    m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);

    m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);

    m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);

    m[2][3] = 0.0;



    m[3][0] = 0.0;

    m[3][1] = 0.0;

    m[3][2] = 0.0;

    m[3][3] = 1.0;

}





Share this post


Link to post
Share on other sites

This topic is 4549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By xhcao
      Does sync be needed to read texture content after access texture image in compute shader?
      My simple code is as below,
      glUseProgram(program.get());
      glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
      glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
      glDispatchCompute(1, 1, 1);
      // Does sync be needed here?
      glUseProgram(0);
      glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
      glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                     GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
      glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
       
      Compute shader is very simple, imageLoad content from texture[0], and imageStore content to texture[1]. Does need to sync after dispatchCompute?
    • By Jonathan2006
      My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below:
      // This works quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); quat quaternion2 = GEMultiplyQuaternions(quaternion1, GEQuaternionFromAngleRadians(angleRadiansVector2)); quat quaternion3 = GEMultiplyQuaternions(quaternion2, GEQuaternionFromAngleRadians(angleRadiansVector3)); glMultMatrixf(GEMat4FromQuaternion(quaternion3).array); // The first two work fine but not the third. Why? quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); vec3 vector1 = GETransformQuaternionAndVector(quaternion1, angularVelocity1); quat quaternion2 = GEQuaternionFromAngleRadians(angleRadiansVector2); vec3 vector2 = GETransformQuaternionAndVector(quaternion2, angularVelocity2); // This doesn't work //quat quaternion3 = GEQuaternionFromAngleRadians(angleRadiansVector3); //vec3 vector3 = GETransformQuaternionAndVector(quaternion3, angularVelocity3); vec3 angleVelocity = GEAddVectors(vector1, vector2); // Does not work: vec3 angleVelocity = GEAddVectors(vector1, GEAddVectors(vector2, vector3)); static vec3 angleRadiansVector; vec3 angularAcceleration = GESetVector(0.0, 0.0, 0.0); // Sending it through one angular velocity later in my motion engine angleVelocity = GEAddVectors(angleVelocity, GEMultiplyVectorAndScalar(angularAcceleration, timeStep)); angleRadiansVector = GEAddVectors(angleRadiansVector, GEMultiplyVectorAndScalar(angleVelocity, timeStep)); glMultMatrixf(GEMat4FromEulerAngle(angleRadiansVector).array); Also how do I combine multiple angularAcceleration variables? Is there an easier way to transform the angular values?
    • By dpadam450
      I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.
       
      #define NUM_LIGHTS 2
      struct Light
      {
          vec3 position;
          vec3 diffuse;
          float attenuation;
      };
      uniform Light Lights[NUM_LIGHTS];
       
       
    • By pr033r
      Hello,
      I have a Bachelor project on topic "Implenet 3D Boid's algorithm in OpenGL". All OpenGL issues works fine for me, all rendering etc. But when I started implement the boid's algorithm it was getting worse and worse. I read article (http://natureofcode.com/book/chapter-6-autonomous-agents/) inspirate from another code (here: https://github.com/jyanar/Boids/tree/master/src) but it still doesn't work like in tutorials and videos. For example the main problem: when I apply Cohesion (one of three main laws of boids) it makes some "cycling knot". Second, when some flock touch to another it scary change the coordination or respawn in origin (x: 0, y:0. z:0). Just some streng things. 
      I followed many tutorials, change a try everything but it isn't so smooth, without lags like in another videos. I really need your help. 
      My code (optimalizing branch): https://github.com/pr033r/BachelorProject/tree/Optimalizing
      Exe file (if you want to look) and models folder (for those who will download the sources):
      http://leteckaposta.cz/367190436
      Thanks for any help...

    • By Andrija
      I am currently trying to implement shadow mapping into my project , but although i can render my depth map to the screen and it looks okay , when i sample it with shadowCoords there is no shadow.
      Here is my light space matrix calculation
      mat4x4 lightViewMatrix; vec3 sun_pos = {SUN_OFFSET * the_sun->direction[0], SUN_OFFSET * the_sun->direction[1], SUN_OFFSET * the_sun->direction[2]}; mat4x4_look_at(lightViewMatrix,sun_pos,player->pos,up); mat4x4_mul(lightSpaceMatrix,lightProjMatrix,lightViewMatrix); I will tweak the values for the size and frustum of the shadow map, but for now i just want to draw shadows around the player position
      the_sun->direction is a normalized vector so i multiply it by a constant to get the position.
      player->pos is the camera position in world space
      the light projection matrix is calculated like this:
      mat4x4_ortho(lightProjMatrix,-SHADOW_FAR,SHADOW_FAR,-SHADOW_FAR,SHADOW_FAR,NEAR,SHADOW_FAR); Shadow vertex shader:
      uniform mat4 light_space_matrix; void main() { gl_Position = light_space_matrix * transfMatrix * vec4(position, 1.0f); } Shadow fragment shader:
      out float fragDepth; void main() { fragDepth = gl_FragCoord.z; } I am using deferred rendering so i have all my world positions in the g_positions buffer
      My shadow calculation in the deferred fragment shader:
      float get_shadow_fac(vec4 light_space_pos) { vec3 shadow_coords = light_space_pos.xyz / light_space_pos.w; shadow_coords = shadow_coords * 0.5 + 0.5; float closest_depth = texture(shadow_map, shadow_coords.xy).r; float current_depth = shadow_coords.z; float shadow_fac = 1.0; if(closest_depth < current_depth) shadow_fac = 0.5; return shadow_fac; } I call the function like this:
      get_shadow_fac(light_space_matrix * vec4(position,1.0)); Where position is the value i got from sampling the g_position buffer
      Here is my depth texture (i know it will produce low quality shadows but i just want to get it working for now):
      sorry because of the compression , the black smudges are trees ... https://i.stack.imgur.com/T43aK.jpg
      EDIT: Depth texture attachment:
      glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT24,fbo->width,fbo->height,0,GL_DEPTH_COMPONENT,GL_FLOAT,NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, fbo->depthTexture, 0);
  • Popular Now