Jump to content
  • Advertisement
Sign in to follow this  
acchuthan76

OpenGL Vertex illumination formulas

This topic is 4272 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

Hi! All, can anyone help me how i can add textures and lightings by using illumination formula for each vertex. I am new to OpenGL and need someone to help me. This is how my programe looks, i am loading the model from a .m model file. Initially i got a problem on loading the m files , later i solved it, now i am trying to add textures and lighting on the model. COuld you help me to learn. Thanks you in advance /*---------------------------------------------------------------------------- MENUS - RIGHT MOUSE BUTTON ROTATE - LEFT MOUSE BUTTON TRANSLATE - MIDDLE MOUSE BUTTON ZOOM IN/OUT - ALT + MIDDLE MOUSE BUTTON KEYBOARD INPUTS 'w' - WIREFIRM 's' - SOLID 'e' - WIREFRAME + SOLID 'f' - SHADING FLAT 'O' - SHADING SMOOTH 'l' - LIGHTS 'P' - VIEW PERSPECTIVE OR ORTHO ---------------------------------------------------------------------------*/ #include <iostream> #include <GL/glut.h> #include <cstdlib> #include <vector> #include <string> using namespace std; #define TRANSFORM_NONE 0 #define TRANSFORM_ROTATE 1 #define TRANSFORM_SCALE 2 #define TRANSFORM_TRANSLATE 3 #define OBJ_WIREFRAME 0 #define OBJ_SOLID 1 #define OBJ_EDGE 2 #define OBJ_FLAT_SHADING 3 #define OBJ_SMOOTH_SHADING 4 #define OBJ_NORM 5 #define ON 1 #define OFF 0 static int LIGHTING=OFF; static int PERSPECTIVE = ON; static int SMOOTH_SHADING = ON; static char TITLE[64] = "MANI MESH VIEWER"; static int win; static int menid; static int primitive = 0; static int press_x, press_y; static float x_angle = 0.0; static float y_angle = 0.0; static float scale_size = 1; static float tran_x = 0, tran_y = 0, tran_z = 0; static int obj_mode = 0; static int xform_mode = 0; static char type; static float x,y,z,nx,ny,nz; static int vid,fid,v1,v2,v3; int i; //const char *s; char b[12]; double max_x(-1e100), max_y(-1e100), max_z(-1e100); double min_x(1e100), min_y(1e100), min_z(1e100); struct Vertex { double x,y,z; double nx,ny,nz; }; struct Face { int v1,v2,v3; }; vector<Vertex> vertices; vector<Face> faces; void loadMFile( ) { FILE* f = fopen(b, "r"); while ((type=fgetc(f)) != EOF) { switch(type) { case 'V': //it's a vertex if (fscanf(f,"ertex %d %f %f %f {normal=(%f %f %f)}\n", &vid, &x, &y, &z, &nx, &ny, &nz) == 7 ) { Vertex vertex; vertex.x = x; vertex.y = y; vertex.z = z; vertex.nx = nx; vertex.ny = ny; vertex.nz = nz; vertices.push_back(vertex); } break; case 'F': if (fscanf(f,"ace %d %d %d %d\n", &fid, &v1, &v2, &v3) == 4 ) { Face face; face.v1 = v1-1; face.v2 = v2-1; face.v3 = v3-1; faces.push_back(face); } break; default: do { type = fgetc(f); } while (type !=EOF && type !='\n'); break; } //switch } //while for (int i=0; i<(int)vertices.size(); i++) { Vertex v = vertices; if (v.x < min_x) min_x = v.x; if (v.y < min_y) min_y = v.y; if (v.z < min_z) min_z = v.z; if (v.x > max_x) max_x = v.x; if (v.y > max_y) max_y = v.y; if (v.z > max_z) max_z = v.z; } double cent_x = (max_x + min_x) /2; double cent_y = (max_y + min_y) /2; double cent_z = (max_z + min_z) /2; double W = max_x - min_x; double H = max_y - min_y; double D = max_z - min_z; #define max(a,b) (((a)>(b))?(a):(b)) double d = max(W,max(H,D)); for ( i=0; i<(int)vertices.size(); i++) { vertices.x = (vertices.x - cent_x) / d; vertices.y = (vertices.y - cent_y) / d; vertices.z = (vertices.z - cent_z) / d; } } void dispNorm(void) { for (int i=0; i<(int)faces.size(); i++) { Face f = faces; Vertex v1 = vertices[f.v1]; Vertex v2 = vertices[f.v2]; Vertex v3 = vertices[f.v3]; glBegin(GL_LINES); glColor3f(0.0,.5,1.0); glVertex3f(v1.x, v1.y, v1.z); glVertex3f(v1.x + v1.nx,v1.y + v1.ny, v1.z+v1.nz); glVertex3f(v2.x, v2.y, v2.z); glVertex3f(v2.x + v2.nx,v2.y + v2.ny, v2.z+v2.nz); glVertex3f(v3.x, v3.y, v3.z); glVertex3f(v3.x + v3.nx,v3.y + v3.ny, v3.z+v3.nz); glEnd(); } } void drawWireframe(void) { for (int i=0; i<(int)faces.size(); i++) { Face f = faces; Vertex v1 = vertices[f.v1]; Vertex v2 = vertices[f.v2]; Vertex v3 = vertices[f.v3]; glBegin(GL_LINES); glColor3f(1,0,0); glVertex3f(v1.x,v1.y,v1.z); glVertex3f(v2.x,v2.y,v2.z); glVertex3f(v2.x,v2.y,v2.z); glVertex3f(v3.x,v3.y,v3.z); glVertex3f(v3.x,v3.y,v3.z); glVertex3f(v1.x,v1.y,v1.z); glEnd(); } } void drawSolid(void) { for (int i=0; i<(int)faces.size(); i++) { Face f = faces; Vertex v1 = vertices[f.v1]; Vertex v2 = vertices[f.v2]; Vertex v3 = vertices[f.v3]; glBegin(GL_TRIANGLES); float colour1 = (v1.z/max_z) * 10; float colour2 = (v2.z/max_z) * 10; float colour3 = (v3.z/max_z) * 10; glColor3f(1,1,colour3); glNormal3f(v1.nx, v1.ny, v1.nz); glVertex3f(v1.x, v1.y, v1.z); glNormal3f(v2.nx, v2.ny, v2.nz); glVertex3f(v2.x, v2.y, v2.z); glNormal3f(v3.nx, v3.ny, v3.nz); glVertex3f(v3.x, v3.y, v3.z); glEnd(); } } void menu(int value) { if (value == 0) { glutDestroyWindow(win); exit(0); } else { primitive=value; } // you would want to redraw now glutPostRedisplay(); } void createmenu(void) { // Create the menu, this menu becomes the current menu menid = glutCreateMenu(menu); // Create an entry glutAddMenuEntry("Clear", 1); //glutAddSubMenu("Draw", submenid1); glutAddMenuEntry("Draw", 2); //glutAddSubMenu("Shading", submenid2); // Create an entry glutAddMenuEntry("Quit", 0); // Let the menu respond on the right mouse button glutAttachMenu(GLUT_RIGHT_BUTTON); } void disp(void) { glEnable(GL_DEPTH_TEST); // Just clean the screen glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // setup the perspective projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (PERSPECTIVE) gluPerspective(60, 1, .1, 100); else glOrtho(-2.5,2.5,-2.5,2.5,-10000,10000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0,0,5,0,0,0,0,1,0); // rotate and scale the object glRotatef(x_angle, 0, 1,0); glRotatef(y_angle, 1,0,0); glScalef(scale_size, scale_size, scale_size); // translate the object glTranslatef(tran_x,tran_y,0); //lighting GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat lmodel_ambient[] = { 1, 0.1, 0.1, 1.0 }; glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light); glLightfv(GL_LIGHT0, GL_SPECULAR, white_light); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); // toggle the lighting if (LIGHTING) { glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); } else { glDisable(GL_LIGHTING); glDisable(GL_LIGHT0); } // set flat or smooth shading if (obj_mode== OBJ_SMOOTH_SHADING) glShadeModel(GL_SMOOTH); else if (obj_mode== OBJ_FLAT_SHADING) glShadeModel(GL_FLAT); // draw what the user asked if (primitive == 2) { if (obj_mode == OBJ_WIREFRAME) { drawWireframe(); } if (obj_mode == OBJ_SOLID) { drawSolid(); } else if (obj_mode == OBJ_NORM) { drawSolid(); double width; glGetDoublev(GL_LINE_WIDTH, &width); glLineWidth(2); drawWireframe(); glLineWidth(width); dispNorm(); } else { drawSolid(); //glColor3f(0.0f, 1.0f, 0.0f); double width; glGetDoublev(GL_LINE_WIDTH, &width); glLineWidth(4); drawWireframe(); glLineWidth(width); } } // swap the buffers glutSwapBuffers(); } void mymouse(int button, int state, int x, int y) { if (state == GLUT_DOWN) { press_x = x; press_y = y; if (button == GLUT_LEFT_BUTTON) xform_mode = TRANSFORM_ROTATE; else if (button == GLUT_MIDDLE_BUTTON) { int mod = glutGetModifiers(); if (mod == GLUT_ACTIVE_ALT) xform_mode = TRANSFORM_SCALE; else xform_mode = TRANSFORM_TRANSLATE; } } else if (state == GLUT_UP) { xform_mode = TRANSFORM_NONE; } } void mymotion(int x, int y) { if (xform_mode == TRANSFORM_ROTATE) { x_angle += (x - press_x)/5.0; if (x_angle > 180) x_angle -= 360; else if (x_angle <-180) x_angle += 360; press_x = x; y_angle += (y - press_y)/5.0; if (y_angle > 180) y_angle -= 360; else if (y_angle <-180) y_angle += 360; press_y = y; } else if (xform_mode == TRANSFORM_SCALE) { float old_size = scale_size; scale_size *= (1 + (y - press_y)/60.0); if (scale_size <0) scale_size = old_size; press_y = y; } else if (xform_mode == TRANSFORM_TRANSLATE) { float dist_x = (x - press_x) * .5; float dist_y = -(y - press_y) * .5; tran_x += dist_x * .1; tran_y += dist_y * .1; //tran_z += .1 * (x - press_x); press_x = x; press_y = y; } // force the redraw function glutPostRedisplay(); } void mykey(unsigned char key, int x, int y) { switch(key) { case 'w': case 'W': cout << "key 'w' is pressed! draw the object in wireframe" << endl; obj_mode = OBJ_WIREFRAME; break; case 's': case 'S': cout << "key 's' is pressed! draw the object in solid" << endl; obj_mode = OBJ_SOLID; break; case 'n': case 'N': cout<<"Normal display" <<endl; obj_mode = OBJ_NORM; break; case 'e': case 'E': cout << "key 'e' is pressed! draw the object in solid+wireframe" << endl; obj_mode = OBJ_EDGE; break; case 'f': case 'F': cout << "key 'f' is pressed! draw the object in flat shading" << endl; obj_mode = OBJ_FLAT_SHADING; break; case 'o': case 'O': cout << "key 'O' is pressed! draw the object in smooth shading" << endl; obj_mode = OBJ_SMOOTH_SHADING; break; case 'l': case 'L': cout << "key 'l' or 'L' is pressed! " << endl; if (LIGHTING) { LIGHTING = OFF; glutSetWindowTitle("Light OFF"); cout << "light off" << endl; } else { LIGHTING = ON; glutSetWindowTitle("Light ON"); cout << "light on" << endl; } break; case 'p': case 'P': cout << "key 'P' or 'p' is pressed! " << endl; if (PERSPECTIVE) { PERSPECTIVE = OFF; glutSetWindowTitle("Orthogonal Projection"); cout << "Orthogonal Projection" << endl; } else { PERSPECTIVE = ON; glutSetWindowTitle("Perspective Projection"); cout << "Perspective Projection" << endl; } break; } // force the redraw function glutPostRedisplay(); } void reshape( int w, int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); } int main(int argc, char **argv) { // const char *s ="eight.m"; //loadMFile(s); //cout<<"enter file name"; cin>>b; // normal initialisation glutInit(&argc, argv); // use double buffer to get better results on animation // use depth buffer for hidden surface removal glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(500,500); glutInitWindowPosition(100,100); win = glutCreateWindow(TITLE); // put all the menu functions in one nice procedure createmenu(); // set the clearcolor and the callback glClearColor(0.0,0.0,0.0,0.0); //glutCreateWindow("color"); // register your callback functions glutDisplayFunc(disp); glutReshapeFunc(reshape); glutMouseFunc(mymouse); glutMotionFunc(mymotion); glutKeyboardFunc(mykey); loadMFile(); // enter the main loop glutMainLoop(); return 1 ; } [Edited by - LessBread on November 7, 2006 1:25:34 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
1: Try Using Source Tags. If you don't know how to do this then see the forum FAQ.

2: Try reading Nehe. http://nehe.gamedev.net

3: Google is your friend. Use it

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!