PDA

View Full Version : problem with translation using glFrustrum



mccarun
04-07-2009, 05:36 AM
I'm facing problem with the translation in the below. Zooming and rotation works perfectly but not the translation. The code basically reads input form the file and draws it and performs translation,rotation and zooming ob mousemove. Can you guys check the else if (mouse_mode == MOUSE_TRANSLATE) under the myMouseMouse() function. That alone is causing problem. I dont know how to fix the parameters for the translate function. i want to translate the zoomed and rotated object.

/*Program to visualize Circle_surf_fields_1a..*/
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<string.h>
/*File name...*/
#define FILE_NAME "circle_surf_field_1a.zfem"
/*The three different operations the mouse can control for the canvas */
#define MOUSE_ROTATE_YX 0
#define MOUSE_TRANSLATE 1
#define MOUSE_ZOOM 2
/* The current mode the mouse is in */
int mouse_mode;
/* The last position of the mouse since the last callback */
int m_last_x, m_last_y;
/* Function declarations */
void myMouseButton(int button, int state, int x, int y);
void myMouseMotion(int x, int y);
/*Structure Declaration*/
struct points
{
double x;
double y;
double z;
}pts[2080];

struct shape
{
int x;
int y;
int z;
}shps[4160];

struct fields
{
float x;
int color;
float cx;
float cy;
float cz;
}flds[2080];

/* Function Declarations */
void myInit (int argc, char **argv);
void myDisplay (void);
void myReshape (int, int);
void myKeyHandler (unsigned char, int, int);
void resetCamera(void);
void drawSphere(void);
void drawtriangle(void);
void color_map(void);

#define FALSE 0
#define TRUE 1

/* The canvas's width and height, in pixels */
int win_width = 500;
int win_height = 500;

int nt;
int n=0,pindx = 0,tindx = 0,num_points;

/* The dimensions of the viewing frustum */
GLfloat fleft = -1.0;
GLfloat fright = 1.0;
GLfloat fbottom = -1.0;
GLfloat ftop = 1.0;
GLfloat zNear = -1.0;
GLfloat zFar = -3.0;

/* Global zoom factor. Modified by mouse movement Initially 1.0 */
GLfloat zoomFactor = 1.0;
/* The current mode the mouse is in, based on button pressed */
int mouse_mode;
/* The last position of the mouse since the last callback */
int m_last_x, m_last_y;

/* Constants for specifying the 3 coordinate axes */
#define X_AXIS 0
#define Y_AXIS 1
#define Z_AXIS 2

/* Begin function definitions */
void myInit (int argc, char **argv) {
/*Reading the file and Storing it in Structure*/
int t[20000],ti=0,c=0,flag=0,j=0;
char str[15] ;
FILE *f;
int i=0;
float values[100000];
f = fopen(FILE_NAME,"r");
while(!feof(f))
{
fscanf(f,"%s",str);
if(!strcmp(str,"END"))
break;
values[n++] = atof(str);
}
fclose(f);
num_points = values[9];
for(i=10,pindx=0; pindx<num_points;pindx++)
{
pts[pindx].x = values[i++];
pts[pindx].y = values[i++];
pts[pindx].z = values[i++];
}
i = i+2;
for(j=0;j<num_points;j++)
{
flds[j].x = values[i++];
}
f = fopen(FILE_NAME,"r");
while(!feof(f))
{
fscanf(f,"%s",str);
if(!strcmp(str,"mesh"))
{
c = 1;
continue;
}
if(c==1)
{
flag = 1;
nt = atoi(str);
c = 0;
continue;
}
if(flag==1)
{
t[ti] = atoi(str);
ti++;
}
}
for(tindx=0,i=0; tindx<nt;tindx++)
{
shps[tindx].x = t[i++];
shps[tindx].y = t[i++];
shps[tindx].z = t[i++];
}

color_map();
/* Set up a black background */
glClearColor(0.0, 0.0, 0.0, 0.0);
resetCamera();
}

/*sets the color for each triangle using the field values..sorted the field values..
*and a simple interpolation between the color and field values*/

void color_map(void)
{
int i=0,j=0,n;
double c1,c2,c3;
double t_array[num_points];
for(i=0;i<num_points;i++)
{
t_array[i] = flds[i].x;
}
c1 = t_array[1*(num_points/3)];
c2 = t_array[2*(num_points/3)];
c3 = t_array[3*(num_points/3)];
for(i=0;i<num_points;i++)
{
if(flds[i].x < c1)
{
flds[i].color = 1;
}
else if((flds[i].x > c1) && (flds[i].x < c2))
{
flds[i].color = 2;
}
else if((flds[i].x > c2) && (flds[i].x < c3))
{
flds[i].color = 3;
}
}

for(i=0;i<num_points;i++)
{
if((flds[i].color)==1)
{
flds[i].cx = 1.0f;
flds[i].cy = 0.0f;
flds[i].cz = 0.0f;
}
else if((flds[i].color)==2)
{
flds[i].cx = 0.0f;
flds[i].cy = 1.0f;
flds[i].cz = 0.0f;
}
else if((flds[i].color)==3)
{
flds[i].cx = 0.0f;
flds[i].cy = 0.0f;
flds[i].cz = 1.0f;
}
}
return;
}

/*
* The main drawing routine. Based on the current display mode, other
* helper functions may be called.
*/
void myDisplay (void) {
glEnable(GL_DEPTH_TEST); /* Use the Z - buffer for visibility */
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawSphere();
drawtriangle();
glFlush();
glutSwapBuffers();
}

void drawSphere(void)
{

int i=0;
glPushMatrix();
glColor3f(0.1,0.7,0.5); /* sets the color */
glBegin(GL_POINTS);
for(i=0;i<num_points;i++)
{
glVertex3f(pts[i].x,pts[i].y,pts[i].z);
}
glEnd();
glFlush();
return;
}

/*Draws the triangle..sets the colr*/
void drawtriangle(void)
{
int i=0,v1,v2,v3;
glShadeModel(GL_SMOOTH);
glBegin(GL_TRIANGLES);
for(i=0;i<nt;i++)
{
/*Getting the vetrices for traingle*/
v1 = shps[i].x;
v2 = shps[i].y;
v3 = shps[i].z;
/*using color values from field structure*/
glColor3f(flds[v1].cx,flds[v1].cy,flds[v1].cz);
/*using x,y,z co-ordinates from the points structure*/
glVertex3f(pts[v1-1].x,pts[v1-1].y,pts[v1-1].z);

glColor3f(flds[v2].cx,flds[v2].cy,flds[v2].cz);
glVertex3f(pts[v2-1].x,pts[v2-1].y,pts[v2-1].z);

glColor3f(flds[v3].cx,flds[v3].cy,flds[v3].cz);
glVertex3f(pts[v3-1].x,pts[v3-1].y,pts[v3-1].z);
}
glEnd();
}

void myResize (int x, int y) {
glViewport(0,0,x,y);
glutReshapeWindow(x, y);
}


/* Stretch the image to fit the reshaped window */
void myReshape (int x, int y) {
glViewport(0,0,x,y);
}

/*
* The rotation is specified in degrees about a certain axis of
* the original model.AXIS should be either X_AXIS, Y_AXIS, or Z_AXIS.
*/
void rotateCamera(double deg, int axis) {
double x, y, z;

x = 0;
y = 0;
z = 0;

if (axis == X_AXIS) {
x = 1.0f;
} else if (axis == Y_AXIS) {
y = 1.0f;
} else if (axis == Z_AXIS) {
z = 1.0f;
}

glRotatef(deg, x, y, z);
}


/*
* Changes the level of zooming by adjusting the dimenstions of the viewing
* frustum.
*/

void zoomCamera(double delta) {
zoomFactor += delta;

if (zoomFactor <= 0.0) {
/* The zoom factor should be positive */
zoomFactor = 0.001;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/*
* use of glFrustum finction
*/
glFrustum(fleft*zoomFactor, fright*zoomFactor,
fbottom*zoomFactor, ftop*zoomFactor,
-zNear, -zFar);

}

/*
* Resets the viewing frustum and moves the drawing point to the center of
* the frustum.
*/
void resetCamera() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glFrustum(fleft, fright, fbottom, ftop, -zNear, -zFar);

/* Set the drawing point at the center of the frustum*/
glMatrixMode(GL_MODELVIEW );
glLoadIdentity( );
glTranslatef(
(fleft + fright) / 2,
(fbottom + ftop) / 2,
(zNear + zFar) / 2);

zoomFactor = 1.0;
}

/*function that govern mouse movement and draws the object accordingly*/
void myMouseButton(int button, int state, int x, int y) {
if (state == GLUT_DOWN) {
m_last_x = x;
m_last_y = y;

if (button == GLUT_LEFT_BUTTON) {
mouse_mode = MOUSE_ROTATE_YX;
}
else if (button == GLUT_MIDDLE_BUTTON) {
/*resetCamera();*/
mouse_mode = MOUSE_ZOOM;
}
else if (button == GLUT_RIGHT_BUTTON) {
mouse_mode = MOUSE_TRANSLATE;
}
}
}


void myMouseMotion(int x, int y) {
double d_x, d_y; /* The change in x and y since the last callback */

printf("\n%d\t%d",x,y);
d_x = x - m_last_x;
d_y = y - m_last_y;

m_last_x = x;
m_last_y = y;

if (mouse_mode == MOUSE_ROTATE_YX) {
/* scaling factors */
d_x /= 2.0;
d_y /= 2.0;

glRotatef(d_x, 0.0, 1.0, 0.0); /* y-axis rotation */
glRotatef(-d_y, 1.0, 0.0, 0.0); /* x-axis rotation */
}
else if (mouse_mode == MOUSE_ZOOM) {
d_y /= 100.0;
zoomFactor += d_y;
if (zoomFactor <= 0.0) {
/* The zoom factor should be positive */
zoomFactor = 0.001;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(fleft*zoomFactor, fright*zoomFactor,
fbottom*zoomFactor, ftop*zoomFactor,
-zNear, -zFar);
}else if (mouse_mode == MOUSE_TRANSLATE) {

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glFrustum(fleft*zoomFactor, fright*zoomFactor, fbottom*zoomFactor, ftop*zoomFactor, -zNear, -zFar);

/* Set the drawing point at the center of the frustum*/
glMatrixMode(GL_MODELVIEW );
glLoadIdentity( );
d_x = d_x/100.0;
d_y = d_y/100.0;

glTranslatef(d_x,d_y,-2);

zoomFactor = 1.0;
}
/* Redraw the screen */
glutPostRedisplay();
}


int main(int argc, char **argv)
{
glutInit(&argc, argv);
/* Set initial window size and screen offset */
glutInitWindowSize(win_width, win_height);
glutInitWindowPosition(50, 50);
/* Using: RGB, double buffering, z-buffer */
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Circle");
/* Set the function callbacks */
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
glutMouseFunc(myMouseButton);
glutMotionFunc(myMouseMotion);
/* User specific initialization */
myInit(argc, argv);
glutMainLoop();
return 0;
}

I need help with the translation alone. What parametrs should i use in the translate function in order to dynamically control the movement usign mouse.
Its kind lengthy code. Sorry about that.
Thanks is advance