1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| #include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159263
GLfloat jarre_2d[]={0,0,
3,0,
4,2,
4,5,
3,7,
1,9,
1,11,
3,13};
void lathe(int nbr_vertices, GLfloat profile[], float angle, int nbr_sections) {
int s, v;
float theta1, theta2, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4;
for(s=0; s<nbr_sections; s++) {
theta1 = (s*1.0)*angle/nbr_sections*PI/180;
theta2 = (s+1.0)*angle/nbr_sections*PI/180;
for(v=0; v<nbr_vertices-1; v++) {
x1 = profile[v*2]*cos(theta1);
y1 = profile[v*2+1];
z1 = profile[v*2]*sin(theta1);
x2 = profile[v*2+2]*cos(theta1);
y2 = profile[v*2+3];
z2 = profile[v*2+2]*sin(theta1);
x3 = profile[v*2+2]*cos(theta2);
y3 = profile[v*2+3];
z3 = profile[v*2+2]*sin(theta2);
x4 = profile[v*2]*cos(theta2);
y4 = profile[v*2+1];
z4 = profile[v*2]*sin(theta2);
glBegin(GL_POLYGON);
glVertex3f(x1,y1,z1);
glVertex3f(x2,y2,z2);
glVertex3f(x3,y3,z3);
glVertex3f(x4,y4,z4);
glEnd();
}
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_DOUBLE); // Clear the colour and depth buffer
glLoadIdentity(); // Clear matrix stack
// We set the camera in position 50,50,100 and we look at origo
gluLookAt(50,50,100,0,0,0,0,1,0);
glColor3f(0.6,0.2,1.0);
// jarre
glTranslatef(-8,2,20);
glScalef(10,10,10);
lathe(8, jarre_2d, 360.0, 16);
glFlush(); // Makes sure that we output the model to the graphics card
glutSwapBuffers();
}
// Called when a key is pressed
void key(unsigned char k, int x, int y)
{
if( k == 'q' ) exit(0);
}
void reshape(int width,int height)
{
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(float)640/(float)480,0.1f,1000.0f);
// Always keeps the same aspect as a 640 wide and 480 high window
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
void init()
{
glClearColor(0,0,0,0);
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); // We want rgb display functionality
glutInitWindowSize(640,480); // Set the window dimensions
glutInitWindowPosition(0,0); // Set the window starting point
glutCreateWindow("Exercise 1"); // Set the caption and launch the window
init();
// Last things before rendering starts
glutDisplayFunc(display); // This will be called every frame
glutReshapeFunc(reshape); // Reshape the window when something changes
glutKeyboardFunc(key); // Callback for input
glutMainLoop(); // Starts the main program
// We will not reach this point unless exit(0) is called (see function keyCB)
return 0;
} |
Partager