140 likes | 199 Views
Lecture 3:. 3D Rendering Surface of Revolution. float r[12] = {0.0,0.3,0.3,0.25,0.25,0.35,0.35,0.3,0.15,0.15,0.05,0.0}; float y[12] = {0.0,0.0,0.05,0.1,0.3,0.35,0.45,0.5,0.5,0.4,0.3,0.3};.
E N D
Lecture 3: 3D Rendering Surface of Revolution
float r[12] = {0.0,0.3,0.3,0.25,0.25,0.35,0.35,0.3,0.15,0.15,0.05,0.0}; float y[12] = {0.0,0.0,0.05,0.1,0.3,0.35,0.45,0.5,0.5,0.4,0.3,0.3};
void normalize(float v[3]) { float d = sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]); if (d != 0.0) { v[0]/=d; v[1]/=d; v[2]/=d; } } void normCrossProd(float v1[3], float v2[3], float out[3]) { out[0] = v1[1]*v2[2] - v1[2]*v2[1]; out[1] = v1[2]*v2[0] - v1[0]*v2[2]; out[2] = v1[0]*v2[1] - v1[1]*v2[0]; normalize(out); }
vang p2 ang p1 p0 glBegin(GL_LINES); for(vang=0;vang<=180;vang+=delang) { y0=r0*cos((double)(vang)*2.0*pi/360.0); y1=r0*cos((double)(vang+delang)*2.0*pi/360.0); x0=r0*sin((double)vang*2.0*pi/360.0); z0=0.0; for (ang=0;ang<=360;ang+=delang) { x1=r0*cos((double)ang*2.0*pi/360.0)*sin((double)vang*2.0*pi/360.0); x2=r0*cos((double)ang*2.0*pi/360.0)*sin((double)(vang+delang)*2.0*pi/360.0); z1=r0*sin((double)ang*2.0*pi/360.0)*sin((double)vang*2.0*pi/360.0); z2=r0*sin((double)ang*2.0*pi/360.0)*sin((double)(vang+delang)*2.0*pi/360.0); glColor3f((r0-x0)/r0,(r0-y0)/r0,(r0-z0)/r0); glVertex3f(x0,y0,z0); glVertex3f(x1,y0,z1); glVertex3f(x1,y0,z1); glVertex3f(x2,y1,z2); x0=x1; z0=z1; } } glEnd();
#include "stdafx.h" #include <GL/glut.h> #include <math.h> GLfloat yang = 0.0; GLfloat xang = 0.0; double const pi = 3.1415926; void display() { int vang,ang; int delang = 10; float r0 = 0.4; float x0,y0,z0,x1,y1,z1,x2,z2; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef(yang,1.0,1.0,0.0); glTranslatef(-0.4,0.25,0.0); glRotatef(xang,0.0,0.0,1.0);
glBegin(GL_LINES); for(vang=0;vang<=180;vang+=delang) { y0=r0*cos((double)(vang)*2.0*pi/360.0); y1=r0*cos((double)(vang+delang)*2.0*pi/360.0); x0=r0*sin((double)vang*2.0*pi/360.0); z0=0.0; for (ang=0;ang<=360;ang+=delang) { x1=r0*cos((double)ang*2.0*pi/360.0)*sin((double)vang*2.0*pi/360.0); x2=r0*cos((double)ang*2.0*pi/360.0)*sin((double)(vang+delang)*2.0*pi/360.0); z1=r0*sin((double)ang*2.0*pi/360.0)*sin((double)vang*2.0*pi/360.0); z2=r0*sin((double)ang*2.0*pi/360.0)*sin((double)(vang+delang)*2.0*pi/360.0); glVertex3f(x0,y0,z0); glVertex3f(x1,y0,z1); glVertex3f(x1,y0,z1); glVertex3f(x2,y1,z2); x0=x1; z0=z1; } } glEnd(); yang = yang + 0.05; if (yang>360.0) yang = 0.0; xang = xang - 0.015; if (xang<0.0) xang = 360.0; glutSwapBuffers(); glFlush();
void init() { glClearColor(0.2,0.2,0.5,1.0); glColor3f(0.8,0.8,0.0); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); } void main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(700,700); glutInitWindowPosition(0,0); glutCreateWindow("3D Rotation"); glutDisplayFunc(display); glutIdleFunc(display); init(); glutMainLoop(); }