Lecture Six. Three Dimensional Programs Using OpenGL
|
|
|
- Conrad Ellis
- 2 years ago
- Views:
Transcription
1
2 References Lab Lectures, Computer Graphics, Taif University, Faculty Of Computers And Information Technology, TA. Maha Thafar &TA. Haifa Alshehri, TA.Sohair Soliman & L.Shakila Bano. OpenGL Programming Guide: The Official Guide to Learning OpenGL, Versions 4.3, 8th edition, Dave Shreiner, Graham Sellers, John Kessenich, Bill Licea-Kane & The Khronos OpenGL ARB Working Group, Addison-Wesley.
3 Lecture Six Three Dimensional Programs Using OpenGL
4 Content 1. The Three Dimension 2. OpenGL 3D Objects 3. Some Built-In Functions 4. Transformations in Open GL 5. Writing your First 3D Program 6. Another 3D Program 7. Projection Transformation Program
5 The Three Dimensions In OpenGL: 2-dimensional applications are a special case of 3 dimensional graphics. (glvertex3f(x,y,z) (in 2D Z = zero) ). In 3D: Not much changes. For 3D, we use a vector graphics representation. When we make 3D objects we will make them out of triangles (or other polygons). By placing the triangles side by side, we can create complex shapes. 3D graphics take lots of processing power to do things like vector calculations, shading, lighting and texture mapping and we can use them to make objects look more real.
6 The Three Dimensions There are many ways to draw 3D shapes: We can simply connect some 2D faces together. Example: a cube (3D) is made of 6 square (2D) faces. 1.Use glvertex3*(x,y,z). 2.Use built in functions that draw 3D opengl objects.
7 OpenGL 3D Objects GLUT provides the follow objects: (we can draw it by built in functions) Cube. Sphere. Cylinder. Cone. Tea pot. CD shaped disc. Torus. icosahedrons, Ocrtahedron Tetrahedron.etc.
8 OpenGL 3D Objects Icosahedrons Ocrtahedron Tetrahedron
9 OpenGL 3D Objects Both wireframe and solid : glutsolidsphere(1.0, 24, 24) or: glutwirecube(1.0) Note: Most of 3D objects looks like 2D object until we apply lighting on it.
10 Some Built In Functions There are several built in functions in opengl to draw 3D objects as the following:
11 Transformations in OpenGL 1) Projection Transformation Refer to the transformation from scene to image. Adjust the lens of camera. 2) Viewing Transformation Refer to the transformation on the camera coordinates. 3) Modeling Transformation Refer to the transformation of models (objects). 4) Viewport Transformations Enlarge or reduce the physical photograph (screen coordinates). Viewport aspect ratio should be same as projection transformation.
12 Projections Transformation Specifying the projection transformation is like choosing a lens for a camera. You can think of this transformation as determining the field of view and which objects are inside it and, how they should look. There are 2 basic types of projections provided for you by OpenGL: 1) Orthographic Projection: glortho(left, right, bottom, top, near, far) -> to 3D graphics. gluortho2d( left, right, bottom, top) -> to 2D graphics. 2) Perspective Projection: gluperspective (double angle, double aspect, double near, double far) glfrustum( xleft, xright, ybottom, ytop, znear, zfar )
13 Projections Transformation
14 Projections Transformation
15 Projections Transformation
16 Viewing Transformation
17 Setting up the Camera
18 #include<windows.h> #include<gl/glut.h> void Display() { glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glcolor3f(1.0, 0.2, 0.2); gllinewidth(6); //glulookat(eyex, eyey, eyez, cx, cy, cz, upx, upy, upz) glulookat(0, 2, 5, 0, 1, 1, 0, 1, 2); glutwirecube(2); //Size = 2 glflush(); } void init() { glclearcolor(1.0, 0.8, 0.8, 0.0); glshademodel(gl_flat); glmatrixmode(gl_projection); glloadidentity(); //Clear the Matrix //gluperspective(angle, aspect(width/hight), near, far) gluperspective(60, 1, 1, 20); } int main() { glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(300, 300); glutinitwindowposition(100, 100); glutcreatewindow("the Cube"); init(); glutdisplayfunc(display); glutmainloop(); return 0; } Writing your First 3D Program
19 Writing your First 3D Program
20 Writing your First 3D Program
21 Another 3D Program #include<windows.h> #include<gl/glut.h> void Display() { glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glcolor3f(1.0, 0.2, 0.2); gllinewidth(6); //glulookat(eyex, eyey, eyez, cx, cy, cz, upx, upy, upz) glulookat(0, 2, 5, 0, 1, 1, 0, 1, 2); // A Cone with (Bottom Radius, Height, Slice, Stack) glutwirecone(1.8, 3.5, 35, 35); glflush(); } void init() { glclearcolor(1.0, 0.8, 0.8, 0.0); glshademodel(gl_flat); //or GL_SMOOTH glmatrixmode(gl_projection); //Load the Matrix glloadidentity(); //Clear the Matrix //glfrustum(left, right, bottom, top, near, far) glfrustum(-2, 2, -2, 2, 2, 20); } int main() { glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(300, 300); glutinitwindowposition(0, 0); glutcreatewindow("draw a Cone"); init(); glutdisplayfunc(display); glutmainloop(); return 0; }
22 Another 3D Program
23 Another 3D Program
24 #include<windows.h> #include<gl/glut.h> #include<math.h> static int W=0, H=0; static int Window1, Window2, Window3, Window4; void Draw() { glcolor3f(0, 0, 0); gltranslatef(0.0, 0.0, ); glscalef(20, 20, 20); glutwireicosahedron(); } النافذة األولى// { render_1() void glutsetwindow(window1); glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glmatrixmode(gl_projection); glloadidentity(); gluperspective(45, 1.0, 10.0, 200.0); Draw(); glutswapbuffers(); } النافذة الثانية// { render_2() void glutsetwindow(window2); glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glmatrixmode(gl_projection); glloadidentity(); gluperspective(30, 3.0, 10.0, 200.0); Draw(); glutswapbuffers(); } النافذة الثالثة// { render_3() void glutsetwindow(window3); glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glmatrixmode(gl_projection); glloadidentity(); gluperspective(20, 1.0, 10.0, 200.0); Draw(); glutswapbuffers(); } النافذة الرابعة// { render_4() void glutsetwindow(window4); glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glmatrixmode(gl_projection); glloadidentity(); gluperspective(75, 1.0, 10.0, 200.0); Draw(); glutswapbuffers(); } Projection Transformation Program
25 int main() { glutinitdisplaymode (GLUT_DEPTH GLUT_DOUBLE); النافذة األولى// ;(40 glutinitwindowposition(230, glutinitwindowsize(180, 180); Window1 = glutcreatewindow("perspective 1"); glutsetwindow(window1); glclearcolor(1.0, 0.5, 0.5, 0); glutdisplayfunc(render_1); النافذة الثانية// ;(40 glutinitwindowposition(40, glutinitwindowsize(180, 180); Window2 = glutcreatewindow("perspective 2"); glutsetwindow(window2); glclearcolor(1.0, 0.5, 0.5, 0); glutdisplayfunc(render_2); النافذة الثالثة// ;(260 glutinitwindowposition(230, glutinitwindowsize(180, 180); Window3 = glutcreatewindow("perspective 3"); glutsetwindow(window3); glclearcolor(1.0, 0.5, 0.5, 0); glutdisplayfunc(render_3); النافذة الرابعة// ;(260 glutinitwindowposition(40, glutinitwindowsize(180, 180); Window4 = glutcreatewindow("perspective 4"); glutsetwindow(window4); glclearcolor(1.0, 0.5, 0.5, 0); glutdisplayfunc(render_4); glutmainloop(); return 0;} Projection Transformation Program
26 Projection Transformation Program
27 Projection Transformation Program
28 The End Summary of Lecture Six T.Mariah Khayat Adham University College
