hello everyone
here is a program including drawing a NURBS object using the function and a routine to handle mouse. the problem is that in the reshape function, to draw a NURBS object perspective projection is required followed by translation. whereas for mouse handling routine, in order to draw small polygon where mouse is clicked, an ortho projection is required without translation. now, both r to put in reshape function, but only one of the 2 works at a time. so, i’ve commented the other one.
can anyone please help me in combining the 2 modules so that both work together.
here is the code:
#include<GL/glut.h>
#include<stdlib.h>
#include<stdio.h>
#ifndef CALLBACK
#define CALLBACK
#endif
GLsizei wh=300,ww=300;
GLfloat size=1.0;
GLfloat ctlpoints[4][4][3];
int showPoints=0;
GLUnurbsObj *theNurb;
/*
draw a polygon where mouse is clicked
*/
void drawSquare(int btn,int state,int x,int y)
{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
y=wh-y;
glColor3f(0.5,0.0,0.6);
glBegin(GL_POLYGON);
glVertex2f(x+size,y+size);
glVertex2f(x-size,y+size);
glVertex2f(x-size,y-size);
glVertex2f(x+size,y-size);
glEnd();
glFlush();
}
if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
exit(0);
}
/*
defining control points for a regular NURBS object
draws a semi sphere
/
void init_surface(void)
{
int u,v;
for (u = 0; u < 4; u++)
for (v = 0; v < 4; v++)
{ ctlpoints[u][v][0]=2.0((GLfloat)u-1.5);
ctlpoints[u][v][1]=2.0*((GLfloat)v-1.5);
if((u==1 | | u==2) && (v==1 | | v==2))
ctlpoints[u][v][2]=3.0;
else
ctlpoints[u][v][2]=-3.0;
}
}
void CALLBACK nurbsError(GLenum errorCode)
{
const GLubyte *estring;
estring=gluErrorString(errorCode);
fprintf(stderr,"Nurbs Error: %s
",estring);
exit(0);
}
void reshape(int w, int h)
{
/*
to draw NURBS object
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75.0,(GLdouble)w/(GLdouble)h,3.0,8.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,(GLsizei) w,(GLsizei) h);
glTranslatef(0.0,0.0,-5.0);
glViewport(0,0,w,h);
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
/*
draw a small polygon at mouse click event
which i've commented out coz both do not work together
*/
/*
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,(GLdouble)w,0.0,(GLdouble)h,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,w,h);
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
*/
ww=w;
wh=h;
}
void init(void)
{
GLfloat mat_diffuse[]={0.7,0.7,0.7,1.0};
GLfloat mat_specular[]={0.4,0.0,0.6,1.0};
GLfloat mat_shininess[]={100.0};
glClearColor(0.0,0.0,0.0,0.0);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
init_surface();
theNurb=gluNewNurbsRenderer();
gluNurbsProperty(theNurb,GLU_SAMPLING_TOLERANCE,25.0);
gluNurbsProperty(theNurb,GLU_DISPLAY_MODE,GLU_OUTLINE_POLYGON);
gluNurbsCallback(theNurb,GLU_ERROR,nurbsError);
}
/*
event to display the control points
*/
void keyboard(unsigned char key,int x ,int y)
{ switch(key)
{ case ‘c’:
case ‘C’:
showPoints=!showPoints;
glutPostRedisplay();
break;
case ‘27’:
exit(0);
break;
default:
break;
}
}
void display(void)
{
GLfloat knots[8]={0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0};
int i,j;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0,2.0,0.0);
glRotatef(270.0,1.0,0.0,0.0);
glScalef(0.5,0.5,0.5);
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb,8,knots,8,knots,4*3,3,&ctlpoints[0][0][0],4,4,GL_MAP2_VERTEX_3);
gluEndSurface(theNurb);
/*
display the control points
*/
if(showPoints)
{
glPointSize(5.0);
glDisable(GL_LIGHTING);
glColor3f(0.0,1.0,1.0);
glBegin(GL_POINTS);
for (i = 0; i < 4; i++){
for (j = 0; j < 4; j++){
glVertex3f(ctlpoints[i][j][0],ctlpoints[i][j][1],ctlpoints[i][j][2]);
}
}
glEnd();
glEnable(GL_LIGHTING);
}
glPopMatrix();
glFlush();
}
int main(int argc,char **argv)
{ glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(ww,wh);
glutInitWindowPosition(100,100);
glutCreateWindow(“NURBS”);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(drawSquare);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
thanks a lot!
anxious