Pages

Thursday, September 24, 2009

References to do report

Graph Editor - a brief discussion about node, edge, selection and move tools.
Tutorials - Raw J3D - discusses on the principal 3D graphics in Java

Interaction with Mouse in OpenGL (Part 2)

In fact, transformations are relative to the object. You need to keep track of your transforms and move things as appropriate for the current state of the model view matrix. Everything is relative, so it doesn't really make sense to talk about absolute position in this way. You can try glPushMatrix(), glLoadIdentity(), glTranslatef(0.1f0, 0.2f0, 0.0f0), draw-gl-scene, and glPopMatrix(), which may or may not be what you actually want to do.

Moving selected objects in OpenGL
What you need to do is check whenever the mouse is moved AND if the player is holding the button you use to move objects. You also need to check if the user has already clicked on an object to move and is then holding the mouse to move it.

Here's the steps:
1. User mouse clicks down on object
2. Call function with mouse down
3. User keeps mouse held down and moves mouse
4. Call function with mouse motion
5. User stops holding mouse down
6. Call function with mouse up

void reshape(int w, int h) {
glViewport(0, 0, (GLint) w, (GLint) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if ( h==0 )
gluPerspective(45, (GLdouble)w, 1.0, 2000.0);
else
gluPerspective(45, (GLdouble)w/ (GLdouble)h, 1.0, 2000.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void mouse ( int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
moving = 1;
startx = x;
starty = y;
}
if ( button == GLUT_LEFT_BUTTON && state == GLUT_UP )
moving = 0;

if ( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN )
{
moving = 2;
starty = y;
}
if ( button == GLUT_RIGHT_BUTTON && state == GLUT_UP )
moving = 0;
}

void motion ( int x, int y )
{
if ( moving == 1 )
{
angle = angle + ( x - startx );
angle2 = angle2 + ( y - starty );
startx = x;
starty = y;
glutPostRedisplay ( );
}
if ( moving == 2 )
{
zoom = zoom + ( y - starty );
starty = y;
glutPostRedisplay ( );
}
}

And make sure you call
glutMouseFunc ( mouse );
glutMotionFunc ( motion );

after init() in your main loop

And needed global variables

float angle, angle2, cloud_rot;
float zoom = -25.50;

int moving, startx, starty;

See more here

Drag Mouse to move Objects
According to the docs, the mouse state has to be checkd like this:
Uint8 ms;
ms = SDL_GetMouseState(&x, &y);
if(ms & SDL_BUTTON(SDL_BUTTON_LEFT))
{
MouseButtonUp(x,y,LEFT);
}
else if (ms & SDL_BUTTON(SDL_BUTTON_RIGHT))
{
MouseButtonUp(x,y,RIGHT);
}
else if (ms & SDL_BUTTON(SDL_BUTTON_MIDDLE))
{
MouseButtonUp(x,y,MIDDLE);
}

// my mouse event code cut from my game engine.
// All the game events go thru here
while ( SDL_PollEvent(&event) )
{
// Send Keyboard events to the keyboard handler
done = HandleKeys(event);

// Mouse events
int x,y;
switch (event.type)
{
// A button on the mouse was pressed
case SDL_MOUSEBUTTONDOWN:
switch(SDL_GetMouseState(&x,&y))
{
case SDL_BUTTON(SDL_BUTTON_LEFT):
MouseButtonDown(x, y, LEFT);
break;
case SDL_BUTTON(SDL_BUTTON_RIGHT):
MouseButtonDown(x, y, RIGHT);
break;
case SDL_BUTTON(SDL_BUTTON_MIDDLE):
MouseButtonDown(x, y, MIDDLE);
break;
default:
break;
}
break;

// It was realeased
case SDL_MOUSEBUTTONUP:
switch(SDL_GetMouseState(&x, &y))
{
case SDL_BUTTON(SDL_BUTTON_LEFT):
MouseButtonUp(x, y, LEFT);
break;
case SDL_BUTTON(SDL_BUTTON_RIGHT):
MouseButtonUp(x, y, RIGHT);
break;
case SDL_BUTTON(SDL_BUTTON_MIDDLE):
MouseButtonUp(x, y, MIDDLE);
break;
default:
break;
}
break;

// Mouse is on the move.
case SDL_MOUSEMOTION:
SDL_GetMouseState(&x, &y);
MouseMove(x, y);
break;

}

http://www.gamedev.net/community/forums/topic.asp?topic_id=400975

Getting mouse button and/or keyboard events
Hi all,
I have a Windows program I am using for an experiment - I am recording people's reaction times in various situations, and to control the duration of the displays people see, I increase the priority of the current process and the current thread using the Windows functions SetPriorityClass(hProcess,
HIGH_PRIORITY_CLASS) and SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST),
respectively. Raising the priority of the current thread locks out other
Windows processes (so they don't affect the timing of my displays), but it also
appears to lock out the keyboard and mouse, so I can't use SDL_PollEvent() to
get mouse button presses or key presses. I'm wondering if there is another
function that will allow me to directly poll the mouse or check for specific key
presses that would not be affected by changing the priority of the thread. I
realize that this might be beyond the scope of the forum, but any information
would be helpful and appreciated. (I also realize that recording reaction times
with a mouse or keyboard event is likely to have some built-in variability; I'm
just trying to minimize that variability as much as possible). Thank you in
advance.

Chris Dickinson
http://lists.libsdl.org/pipermail/sdl-libsdl.org/2008-October/066952.html
Maybe:
Uint8* keystates = SDL_GetKeyState(0);
...
if(keystates[SDLK_SPACE])
{
// do stuff
}

and... (from docs)
SDL_PumpEvents();
if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1))
printf("Mouse Button 1(left) is pressed.\n");


Jonny D


basically, if you're just wanting a ray in the direction of the mouse click, then its pretty simple using gluUnProject.
below is a simple usage to get the ray

GLdouble ray_x, ray_y, ray_z;
GLint viewport[4];
GLdouble proj[16];
GLdouble modelview[16];

// we query OpenGL for the necessary matrices etc.
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);

// assuming you have mouse coordinates as mouseX and mouseY
// gluUnproject assumes coordinates are measured from bottom of screen
// so we invert the mouseY you got from glut or SDL or watever
GLdouble _mouseY = viewport[3] - mouseY;

// using 1.0 for winZ gives u a ray
gluUnProject(mouseX, _mouseY, 1.0f, modelview, proj, viewport, &ray_x, &ray_y, &ray_z);


and there you have the ray direction coords... it isn't normalized tho fyi.

i think you can actually get the z coordinate of the object you clicked.. but i haven't needed that yet... then you'd pass in something else as winZ, getting it from the Z-buffer... but not too sure.

hope that helped.
----------------
If you just want the 3d coordinates of the point in space the mouse is over, the following code will give you just that:

GLdouble model_view[16];
glGetDoublev(GL_MODELVIEW_MATRIX, model_view);
GLdouble projection[16];
glGetDoublev(GL_PROJECTION_MATRIX, projection);
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
double dx; double dy; double dz;
GLfloat depth[2];
glReadPixels (x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
gluUnProject(x, y, depth[0], model_view, projection, viewport, &dx, &dy, &dz);
x3d = float(dx);
y3d = float(dy);
z3d = float(dz);

Zoom in and out a large surface
There are 2 common ways to zoom in 3D:
  • by scaling
  • by manipulating the projection matrix
Scaling just involves calling glScalef with the correct scaling factor before rendering the zoomed object. Unfortunately, you will still run into problems with the clipping planes.

The correct way to zoom is to modify the projection matrix. If you are using an orthographic projection, just scale the width and height arguments to glOrtho. If you are using a perspective projection, try modifying the field of view (narrow field of view is more zoomed).

glTranslate is not a very good way to zoom. You are basically moving the object closer of further away from the camera, which will sort of work in perspective projection, but eventually you will translate past the a clipping plane.

I was recently doing this by changing glViewPort function and increasing the number (from both sides)
See more here

Friday, September 11, 2009

Some notes working with Common Lisp

1. When using multiple-value-bind, there are some values that we don't need. We can ignore the warning "The variable x, y,... is never used...". Below is an example:
(defun my-round (x)
(round (+ x 0.5)))

(defun next-power-of-two (x)
(let ((log-base2 (/ (log x) (log 2))))
(multiple-value-bind (y z)
(my-round (expt 2 (ceiling log-base2)))
(declare (ignorable z))
y)))
Because the my-round functions returns two values, but I only use the first one. When I apply this function inside the next-power-of-two function, I also need the first.

2. In order to use the hexadecimal values in Common Lisp, we can use "#x" or "x16r" to represent them.

Saturday, September 5, 2009

Interaction with Mouse in OpenGL (Part 1)

Cytoscape
Selection Objects (www.3dkingdoms.com)
BioLayout Express 3D, click here to get more details of BioLayoutJava
Interactive Graph Drawing is an open source for drawing interactive graph drawing on the WWW, inspired by Arthur van Hoff's early GraphLayout Java demonstration applet. He implemented how to select and move nodes on the graph.
VisAnt is an Integrative Visual Analysis Tool for Biological Networks and Pathways. Currently, there is not available source code.

/* Function prototypes */
/*
* Retrieve the current state of the mouse.
* The current button state is returned as a button bitmask, which can
* be tested using the SDL_BUTTON(X) macros, and x and y are set to the
* current mouse cursor position.  You can pass NULL for either x or y.
*/
extern DECLSPEC Uint8 SDLCALL SDL_GetMouseState(int *x, int *y);

With:
/* Used as a mask when testing buttons in buttonstate
Button 1: Left mouse button
Button 2: Middle mouse button
Button 3: Right mouse button
Button 4: Mouse wheel up  (may also be a real button)
Button 5: Mouse wheel down (may also be a real button)
*/
#define SDL_BUTTON(X)  (1 << ((X)-1))
#define SDL_BUTTON_LEFT  1
#define SDL_BUTTON_MIDDLE 2
#define SDL_BUTTON_RIGHT 3
#define SDL_BUTTON_WHEELUP 4
#define SDL_BUTTON_WHEELDOWN 5
#define SDL_BUTTON_LMASK SDL_BUTTON(SDL_BUTTON_LEFT)
#define SDL_BUTTON_MMASK SDL_BUTTON(SDL_BUTTON_MIDDLE)
#define SDL_BUTTON_RMASK SDL_BUTTON(SDL_BUTTON_RIGHT)


Thursday, September 3, 2009

Technology terms in Graphics Processing

Rasterization or Rasterisation is the task of taking an image described in a vector graphics format (shapes) and converting it into a raster image (pixels or dots) for output on a video display or printer, or for storage in a bitmap file format.

Wednesday, September 2, 2009

NewLisp

newLISP Fan Club: http://www.alh.net/newlisp/phpbb/index.php?sid=86dab258338cbad37c81adeee447cb6d

OpenGL Programming with GLFW

OpenGL: Overview from Answers.com: http://www.answers.com/topic/opengl
GLFW: An OpenGL Library: http://glfw.sourceforge.net/index.html
Introduction to OpenGL: http://opengl.joudoki.com/
Get GLFW at SourceForge.net: http://sourceforge.net/projects/glfw/
Forums for GLFW: http://sourceforge.net/forum/?group_id=72569