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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| #include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159263
int angle;
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();
}
}
}
qvoid display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // 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(18,0,4);
glScalef(3,3,3);
lathe(8, jarre_2d, 360.0, 16);
glColor3f(0.6,0.2,1.0);
glTranslatef(0,5,17);
glScalef(5,5,5);
glutSolidCube(1);
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)
{
switch(k){
case 'f':
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glutPostRedisplay();
break;
case 'p':
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glutPostRedisplay();
break;
case '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
}
void setupRC(){
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glFrontFace(GL_CCW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHTING);
GLfloat diffuseLight[] = {0.7,0.7,0.7,1.0};
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
GLfloat ambientLight[] = {0.05,0.05,0.05,1.0};
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
GLfloat specularLight [] = {0.7,0.7,0.7,1.0};
GLfloat spectre [] = {1.0,1.0,1.0,1.0};
glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight);
glMaterialfv(GL_FRONT, GL_SPECULAR, spectre);
glMaterialf(GL_FRONT,GL_SHININESS,128);
glShadeModel(GL_FLAT);
GLfloat lightPosition [] = {600,10,200,1};
glLightfv(GL_LIGHT0,GL_POSITION,lightPosition);
}
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
angle = 0;
init();
setupRC();
// 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