/ Published in: C++
Expand |
Embed | Plain Text
#include <GL/glut.h> #include <GL/glu.h> #include <cmath> #include <fstream> #include <string> #include <iostream> using namespace::std; bool IsPerspective=true; struct point { GLfloat x; GLfloat y; GLfloat z; }; struct face { point vertex[3]; }; class model { public: face faces_list[200]; model() { char str[]="vert_100-faces_196.raw"; read_from_file(str); }; void draw_test(); void draw(); int read_from_file(char*); }; void model::draw() { int ii, jj; bool bb=0; glBegin(GL_TRIANGLES); for (ii=0; ii<197; ii++) { for (jj=0; jj<3; jj++) { glVertex3f ( faces_list[ii].vertex[jj].x, faces_list[ii].vertex[jj].y, faces_list[ii].vertex[jj].z ); if ( !bb ) { glColor3f(0.0f, 1.0f, 0.0f); bb=1; } else { glColor3f(1.0f, 0.0f, 0.0f); bb=0; } } } glEnd(); } int model::read_from_file(char* string_name) { int ii,jj; ifstream input_file( string_name ); if (!input_file ) {cout << "oblom/ glucky s failom...\n";} else { while (input_file ) { for (ii=0; ii<197; ii++) for (jj=0; jj<3; jj++) input_file >> faces_list[ii].vertex[jj].x >> faces_list[ii].vertex[jj].y >> faces_list[ii].vertex[jj].z; } cout<<faces_list[1].vertex[0].x<<endl; for (jj=0; jj<3; jj++) cout <<faces_list[0].vertex[jj].x <<" " <<faces_list[0].vertex[jj].y <<" " <<faces_list[0].vertex[ii].z <<" "; cout<<endl; } return 0; }; void model::draw_test() { glBegin(GL_TRIANGLES); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(50.0f, 0.0f, 0.0f); glVertex3f(0.0f, 50.0f, 0.0f); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(-50.0f, 0.0f, 0.0f); glVertex3f(0.0f, -50.0f,50.0f); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(0.0f, 50.0f, 0.0f); glVertex3f(0.0f, 50.0f, 50.0f); glVertex3f(0.0f, 0.0f,0.0f); glEnd(); }; //////////// // GLOBALS //////////// model man; // Lighting values GLfloat whiteLight[] = { 0.2f, 0.2f, 0.2f, 1.0f }; GLfloat sourceLight[] = { 0.8f, 0.8f, 0.8f, 1.0f }; GLfloat lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f }; // Rotation amounts static GLfloat xRot = 0.0f; static GLfloat yRot = 0.0f; // Flags for effects // Flags for effects int iCull = 0; int iOutline = 0; int iDepth = 0; /////////////////////////////////////////////////////////////////////////////// // Reset flags as appropriate in response to menu selections void ProcessMenu(int value) { switch(value) { case 1: iDepth = !iDepth; break; case 2: iCull = !iCull; break; case 3: iOutline = !iOutline; default: break; } glutPostRedisplay(); } // Called to draw scene void RenderScene(void) { GLfloat x,y,angle; // Storage for coordinates and angles int iPivot = 1; // Used to flag alternating colors // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Turn culling on if flag is set if(iCull) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); // Enable depth testing if flag is set if(iDepth) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); // Draw back side as a polygon only, if flag is set if(iOutline) glPolygonMode(GL_BACK,GL_LINE); else glPolygonMode(GL_BACK,GL_FILL); // Save matrix state and do the rotation glPushMatrix(); glTranslatef(0.0f, 0.0f, -150.0f); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); //////////// // start //////////// float scale=20.0f; glScalef(scale,scale,scale); glRotatef(90.0f, -1.0f, 0.0f, 0.0f); //~ man.draw_test(); man.draw(); //////////// // end //////////// // Restore transformations glPopMatrix(); glutSwapBuffers(); } // This function does any needed initialization on the rendering // context. void SetupRC() { // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); // Set drawing color to green glColor3f(0.0f, 1.0f, 0.0f); // Set color shading model to flat glShadeModel(GL_FLAT); // Clock wise wound polygons are front facing, this is reversed // because we are using triangle fans //~ glFrontFace(GL_CW); } void SpecialKeys(int key, int x, int y) { if(key == GLUT_KEY_UP) xRot-= 5.0f; if(key == GLUT_KEY_DOWN) xRot += 5.0f; if(key == GLUT_KEY_LEFT) yRot -= 5.0f; if(key == GLUT_KEY_RIGHT) yRot += 5.0f; // Refresh the Window glutPostRedisplay(); } void ChangeSize(int w, int h) { if ( !IsPerspective ) { GLfloat nRange = 100.0f; // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Reset projection matrix stack glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Establish clipping volume (left, right, bottom, top, near, far) if (w <= h) glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange); else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange); // Reset Model view matrix stack glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } else { GLfloat fAspect; // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); fAspect = (GLfloat)w/(GLfloat)h; // Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Produce the perspective projection gluPerspective(60.0f, fAspect, 1.0, 0.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("riff's programm"); glutInitWindowSize(480, 640); // Create the Menu glutCreateMenu(ProcessMenu); glutAddMenuEntry("Toggle depth test",1); glutAddMenuEntry("Toggle cull backface",2); glutAddMenuEntry("Toggle outline back",3); glutAttachMenu(GLUT_RIGHT_BUTTON); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); glutDisplayFunc(RenderScene); SetupRC(); glutMainLoop(); return 0; }
You need to login to post a comment.
