Hi, here is the code:-
#include <stdio.h>
#include <iostream.h>
#include <ipm\CsaVector3D.h>
#include “GL/glut.h”
typedef struct
{
CsaVector3D vertex[3];
} triangle;
triangle *tri;
int nTriangles;
CsaVector3D min, max;
enum {FILL, WIRE};
int rendermode = WIRE;
static float transx = 0.0, transy = 0.0 , rotx = 0, roty = 0;
static int ox = -1, oy = -1;
static int mot = 0;
#define PAN 1
#define ROT 2
void menu(int selection)
{
rendermode = selection;
glutPostRedisplay();
}
void initDisplay()
{
GLfloat lightpos[] = {50.f, 50.f, 20.f, 1.f};
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_ambient[] = {0.2f, 0.2f, 0.2f, 1.0f};
GLfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
// PROJECTION
glMatrixMode(GL_PROJECTION);
CsaVector3D dis = max - min;
CsaVector3D cen = min + dis/2.0;
GLdouble left = cen[0] - dis[0];
GLdouble right = cen[0] + dis[0];
GLdouble bottom = cen[1] - dis[1];
GLdouble top = cen[1] + dis[1];
GLdouble zNear = dis[2];
GLdouble zFar = zNear + 3*dis[2];
glLoadIdentity();
glOrtho(left, right, bottom, top, zNear, zFar);
//glFrustum(-200., 200., -200., 200., 320., 640.);
// MODEL VIEW
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* turn on features */
glEnable(GL_DEPTH_TEST);
/* turn on spot light */
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
/* place light 0 in the right place */
//glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
glClearColor(.0f, .0f, .0f, .0f);
}
void drawTriangles()
{
glBegin(GL_TRIANGLES);
for(int i=0;i<nTriangles;i++)
{
CsaVector3D vec = tri[i].vertex[0];
glVertex3d(vec[0], vec[1], vec[2]);
vec = tri[i].vertex[1];
glVertex3d(vec[0], vec[1], vec[2]);
vec = tri[i].vertex[2];
glVertex3d(vec[0], vec[1], vec[2]);
}
glEnd();
}
void drawscene()
{
glPushMatrix();
CsaVector3D dis = max - min;
CsaVector3D cen = min + dis/2.0;
glTranslated(0, 0, transx * transy - cen[2] - 2*dis[2]);
cout << "trans = " << transx * transy - cen[2] << endl;
//gluLookAt(cos(roty),sin(rotx),0, 0,0,-1, 0,1,0);
glRotatef(roty, 0.0, 1.0, 0.0);
glRotatef(rotx, 1.0, 0.0, 0.0);
glColor3f(1.0f, 0.0f, 0.f);
drawTriangles();
glPopMatrix();
}
/* Called when window needs to be redrawn */
void redraw()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
switch(rendermode) {
case FILL:
drawscene();
break;
case WIRE: /* basic wireframe mode /
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
drawscene();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
break;
}
if(glGetError()) / to catch programming errors; should never happen */
printf("Oops! I screwed up my OpenGL calls somewhere
");
glutSwapBuffers();
}
void key(unsigned char key, int x, int y)
{
if(key == ‘\033’)
exit(0);
}
void pan(const int x, const int y)
{
transx += (x-ox)/10.;
transy -= (y-oy)/10.;
ox = x; oy = y;
glutPostRedisplay();
}
void rotate(const int x, const int y)
{
rotx += (y-oy) / 60.0;
if (rotx > 360.) rotx -= 360.;
else if (rotx < -360.) rotx += 360.;
roty += (x-ox) / 60.0;
if (roty > 360.) roty -= 360.;
else if (roty < -360.) roty += 360.;
ox = x; oy = y;
glutPostRedisplay();
}
void motion(int x, int y)
{
if (mot == PAN) pan(x, y);
else if (mot == ROT) rotate(x,y);
}
void mouse(int button, int state, int x, int y)
{
/* hack for 2 button mouse */
//if (button == GLUT_LEFT_BUTTON && glutGetModifiers() & GLUT_ACTIVE_SHIFT)
//button = GLUT_MIDDLE_BUTTON;
if(state == GLUT_DOWN) {
switch(button) {
case GLUT_LEFT_BUTTON:
mot = ROT;
motion(ox = x, oy = y);
break;
case GLUT_MIDDLE_BUTTON:
mot = ROT;
motion(ox = x, oy = y);
break;
case GLUT_RIGHT_BUTTON:
mot = PAN;
motion(ox = x, oy = y);
break;
}
} else if (state == GLUT_UP) {
mot = 0;
}
}
void getBounds(CsaVector3D &min, CsaVector3D &max)
{
CsaVector3D vec = tri[0].vertex[0];
min = max = vec;
cout << "init=" << min << "," << max << endl;
for(int i=0; i < nTriangles; i++)
{
for(int k=0;k<3;k++)
{
vec = tri[i].vertex[k];
for(int j=0; j<3 ; j++)
{
if(min[j] > vec[j]) min[j] = vec[j];
if(max[j] < vec[j]) max[j] = vec[j];
}
}
}
cout << "final=" << min << "," << max << endl;
}
/* Parse arguments, and set up interface between OpenGL and window system */
void glDisplay(triangle *triangles, int nTri)
{
if(triangles == NULL) return;
tri = triangles;
nTriangles = nTri;
getBounds(min, max);
//glutInit(&argc, argv);
glutInitWindowSize(512, 512);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("triangle display");
glutDisplayFunc(redraw);
glutKeyboardFunc(key);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutCreateMenu(menu);
glutAddMenuEntry("Solid Fill", FILL);
glutAddMenuEntry("Wireframe", WIRE);
//glutAttachMenu(GLUT_RIGHT_BUTTON);
initDisplay();
glutMainLoop();
}
void main(int argc, char *argv[])
{
CsaVector3D offsetVec = CsaVector3D(100,200,300);
triangle *t = (triangle *)malloc(sizeof(triangle) * 4);
t[0].vertex[0] = CsaVector3D(0,0,0) - offsetVec;
t[0].vertex[1] = CsaVector3D(100,0,0) - offsetVec;
t[0].vertex[2] = CsaVector3D(50,50,50) - offsetVec;
t[1].vertex[0] = CsaVector3D(50,50,50) - offsetVec;
t[1].vertex[1] = CsaVector3D(0,0,100) - offsetVec;
t[1].vertex[2] = CsaVector3D(0,0,0) - offsetVec;
t[2].vertex[0] = CsaVector3D(50,50,50) - offsetVec;
t[2].vertex[1] = CsaVector3D(0,0,100) - offsetVec;
t[2].vertex[2] = CsaVector3D(100,0,100) - offsetVec;
t[3].vertex[0] = CsaVector3D(50,50,50) - offsetVec;
t[3].vertex[1] = CsaVector3D(100,0,100) - offsetVec;
t[3].vertex[2] = CsaVector3D(100,0,0) - offsetVec;
glDisplay(t, 4);
}