banner



How To Draw A 3d House In Opengl

I assume that y'all take some knowledge of OpenGL. Otherwise, read "Introduction to OpenGL with 2d Graphics".

Example 1: 3D Shapes (OGL01Shape3D.cpp)

This example is taken from Nehe OpenGL Tutorial Lesson # 5 (@ http://nehe.gamedev.net/), which displays a 3D color-cube and a pyramid. The cube is made of of 6 quads, each having dissimilar colors. The hallow pyramid is fabricated up of four triangle, with different colors on each of the vertices.

1 2 three four v 6 7 8 9 10 xi 12 13 xiv 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 forty 41 42 43 44 45 46 47 48 49 fifty 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 seventy 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 ninety 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
                    #include <windows.h>   #include <GL/glut.h>      char title[] = "3D Shapes";    void initGL() {    glClearColor(0.0f, 0.0f, 0.0f, one.0f);     glClearDepth(1.0f);                       glEnable(GL_DEPTH_TEST);       glDepthFunc(GL_LEQUAL);        glShadeModel(GL_SMOOTH);       glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);   }    void display() {    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     glMatrixMode(GL_MODELVIEW);               glLoadIdentity();                     glTranslatef(1.5f, 0.0f, -seven.0f);        glBegin(GL_QUADS);                                     glColor3f(0.0f, i.0f, 0.0f);            glVertex3f( 1.0f, 1.0f, -one.0f);       glVertex3f(-1.0f, ane.0f, -one.0f);       glVertex3f(-1.0f, 1.0f,  1.0f);       glVertex3f( 1.0f, ane.0f,  1.0f);                glColor3f(1.0f, 0.5f, 0.0f);            glVertex3f( 1.0f, -1.0f,  1.0f);       glVertex3f(-ane.0f, -i.0f,  1.0f);       glVertex3f(-1.0f, -1.0f, -i.0f);       glVertex3f( i.0f, -1.0f, -1.0f);                glColor3f(ane.0f, 0.0f, 0.0f);            glVertex3f( 1.0f,  1.0f, 1.0f);       glVertex3f(-1.0f,  1.0f, 1.0f);       glVertex3f(-1.0f, -1.0f, 1.0f);       glVertex3f( 1.0f, -1.0f, ane.0f);                glColor3f(ane.0f, ane.0f, 0.0f);            glVertex3f( 1.0f, -ane.0f, -1.0f);       glVertex3f(-i.0f, -1.0f, -1.0f);       glVertex3f(-i.0f,  1.0f, -1.0f);       glVertex3f( one.0f,  1.0f, -1.0f);                glColor3f(0.0f, 0.0f, 1.0f);            glVertex3f(-1.0f,  1.0f,  1.0f);       glVertex3f(-one.0f,  1.0f, -1.0f);       glVertex3f(-ane.0f, -ane.0f, -one.0f);       glVertex3f(-1.0f, -1.0f,  ane.0f);                glColor3f(1.0f, 0.0f, 1.0f);            glVertex3f(1.0f,  1.0f, -1.0f);       glVertex3f(1.0f,  1.0f,  i.0f);       glVertex3f(1.0f, -ane.0f,  1.0f);       glVertex3f(1.0f, -1.0f, -1.0f);    glEnd();            glLoadIdentity();                      glTranslatef(-1.5f, 0.0f, -half dozen.0f);        glBegin(GL_TRIANGLES);                         glColor3f(1.0f, 0.0f, 0.0f);            glVertex3f( 0.0f, i.0f, 0.0f);       glColor3f(0.0f, ane.0f, 0.0f);            glVertex3f(-1.0f, -1.0f, 1.0f);       glColor3f(0.0f, 0.0f, 1.0f);            glVertex3f(1.0f, -i.0f, ane.0f);                glColor3f(1.0f, 0.0f, 0.0f);            glVertex3f(0.0f, 1.0f, 0.0f);       glColor3f(0.0f, 0.0f, i.0f);            glVertex3f(ane.0f, -one.0f, 1.0f);       glColor3f(0.0f, i.0f, 0.0f);            glVertex3f(1.0f, -i.0f, -ane.0f);                glColor3f(ane.0f, 0.0f, 0.0f);            glVertex3f(0.0f, 1.0f, 0.0f);       glColor3f(0.0f, one.0f, 0.0f);            glVertex3f(1.0f, -1.0f, -one.0f);       glColor3f(0.0f, 0.0f, 1.0f);            glVertex3f(-1.0f, -1.0f, -1.0f);                glColor3f(ane.0f,0.0f,0.0f);              glVertex3f( 0.0f, ane.0f, 0.0f);       glColor3f(0.0f,0.0f,1.0f);              glVertex3f(-1.0f,-1.0f,-1.0f);       glColor3f(0.0f,1.0f,0.0f);              glVertex3f(-one.0f,-1.0f, 1.0f);    glEnd();         glutSwapBuffers();   }    void reshape(GLsizei width, GLsizei height) {          if (height == 0) height = one;                    GLfloat aspect = (GLfloat)width / (GLfloat)meridian;          glViewport(0, 0, width, height);          glMatrixMode(GL_PROJECTION);      glLoadIdentity();                     gluPerspective(45.0f, aspect, 0.1f, 100.0f); }    int master(int argc, char** argv) {    glutInit(&argc, argv);                glutInitDisplayMode(GLUT_DOUBLE);     glutInitWindowSize(640, 480);       glutInitWindowPosition(50, 50);     glutCreateWindow(championship);              glutDisplayFunc(brandish);           glutReshapeFunc(reshape);           initGL();                           glutMainLoop();                     return 0; }

Overabundance Setup - main()

The program contains a initGL(), display() and reshape() functions.

The main() programme:

  1. glutInit(&argc, argv);
    Initializes the Glut.
  2. glutInitWindowSize(640, 480);
    glutInitWindowPosition(fifty, 50);
    glutCreateWindow(championship);

    Creates a window with a title, initial width and height positioned at initial top-left corner.
  3. glutDisplayFunc(display);
    Registers display() every bit the re-paint effect handler. That is, the graphics sub-arrangement calls back display() when the window first appears and whenever there is a re-paint request.
  4. glutReshapeFunc(reshape);
    Registers reshape() every bit the re-sized result handler. That is, the graphics sub-system calls back reshape() when the window first appears and whenever the window is re-sized.
  5. glutInitDisplayMode(GLUT_DOUBLE);
    Enables double buffering. In brandish(), nosotros use glutSwapBuffers() to signal to the GPU to swap the front-buffer and back-buffer during the next VSync (Vertical Synchronization).
  6. initGL();
    Invokes the initGL() one time to perform all erstwhile initialization tasks.
  7. glutMainLoop();
    Finally, enters the outcome-processing loop.
One-Fourth dimension Initialization Operations - initGL()

The initGL() part performs the i-time initialization tasks. Information technology is invoked from main() once (and but in one case).

glClearColor(0.0f, 0.0f, 0.0f, ane.0f);
glClearDepth(1.0f);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Set the clearing (background) colour to black (R=0, G=0, B=0) and opaque (A=1), and the clearing (background) depth to the farthest (Z=1). In brandish(), we invoke glClear() to articulate the color and depth buffer, with the clearing color and depth, before rendering the graphics. (Also the colour buffer and depth buffer, OpenGL also maintains an accumulation buffer and a stencil buffer which shall be discussed later.)

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

Nosotros need to enable depth-exam to remove the subconscious surface, and set the function used for the depth examination.

glShadeModel(GL_SMOOTH);
We enable smooth shading in color transition. The alternative is GL_FLAT. Try it out and run across the divergence.

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
In graphics rendering, there is often a trade-off between processing speed and visual quality. We tin use glHint() to decide on the trade-off. In this instance, we ask for the all-time perspective correction, which may involve more than processing. The default is GL_DONT_CARE.

Defining the Color-cube and Pyramid

OpenGL'due south object is made upwardly of primitives (such equally triangle, quad, polygon, point and line). A archaic is defined via i or more vertices. The color-cube is fabricated up of 6 quads. Each quad is made up of 4 vertices, defined in counter-clockwise (CCW) order, such equally the normal vector is pointing out, indicating the front face. All the iv vertices have the same color. The color-cube is defined in its local space (called model space) with origin at the middle of the cube with sides of 2 units.

Similarly, the pyramid is made upward of four triangles (without the base). Each triangle is made up of 3 vertices, defined in CCW order. The 5 vertices of the pyramid are assigned different colors. The color of the triangles are interpolated (and alloy smoothly) from its iii vertices. Once more, the pyramid is defined in its local space with origin at the heart of the pyramid.

Model Transform

The objects are defined in their local spaces (model spaces). Nosotros need to transform them to the common earth space, known equally model transform.

To perform model transform, we need to operate on the then-called model-view matrix (OpenGL has a few transformation matrices), by setting the electric current matrix mode to model-view matrix:

glMatrixMode(GL_MODELVIEW);

Nosotros perform translations on cube and pyramid, respectively, to position them on the world space:


glLoadIdentity();
glTranslatef(ane.5f, 0.0f, -seven.0f);

glLoadIdentity();
glTranslatef(-one.5f, 0.0f, -6.0f);

View Transform

The default camera position is:

gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0)

That is, EYE=(0,0,0) at the origin, AT=(0,0,-100) pointing at negative-z axis (into the screen), and UP=(0,ane,0) corresponds to y-centrality.

OpenGL graphics rendering pipeline performs so-chosen view transform to bring the earth space to camera's view space. In the case of the default camera position, no transform is needed.

Viewport Transform

void reshape(GLsizei width, GLsizei height) {
glViewport(0, 0, width, tiptop);

The graphics sub-system calls back reshape() when the window first appears and whenever the window is resized, given the new window'due south width and height, in pixels. Nosotros set our application viewport to embrace the unabridged window, elevation-left corner at (0, 0) of width and top, with default minZ of 0 and maxZ of 1. We also use the aforementioned attribute ratio of the viewport for the projection view frustum to prevent baloney. In the viewport, a pixel has (x, y) value equally well as z-value for depth processing.

Projection Transform

GLfloat aspect = (GLfloat)width / (GLfloat)top;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, attribute, 0.1f, 100.0f);
A photographic camera has limited field of view. The projection models the view captured by the camera. There are two types of projection: perspective project and orthographic project. In perspective projection, object further to the photographic camera appears smaller compared with object of the same size nearer to the photographic camera. In orthographic projection, the objects appear the same regardless of the z-value. Orthographic projection is a special case of perspective project where the camera is placed very far abroad. We shall discuss the orthographic projection in the afterwards example.

To set up the project, we need to operate on the projection matrix. (Retrieve that we operated on the model-view matrix in model transform.)

We set the matrix way to projection matrix and reset the matrix. We utilize the gluPerspective() to enable perspective projection, and set the fovy (view bending from the bottom-plane to the meridian-airplane), aspect ratio (width/summit), zNear and zFar of the View Frustum (truncated pyramid). In this example, we set the fovy to 45°. We use the same aspect ratio as the viewport to avert distortion. We set the zNear to 0.1 and zFar to 100 (z=-100). Take that note the color-cube (1.five, 0, -7) and the pyramid (-ane.5, 0, -half-dozen) are contained within the View Frustum.

The projection transform transforms the view frustum to a 2x2x1 cuboid clipping-volume centered on the almost plane (z=0). The subsequent viewport transform transforms the clipping-volume to the viewport in screen space. The viewport is ready before via the glViewport() function.

Example ii: 3D Shape with Animation (OGL02Animation.cpp)

Allow's modify the previous case to comport out animation (rotating the cube and pyramid).

ane 2 iii 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 lx 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 lxxx 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
                    #include <windows.h>   #include <GL/glut.h>      char title[] = "3D Shapes with animation";                  GLfloat anglePyramid = 0.0f;   GLfloat angleCube = 0.0f;      int refreshMills = 15;                                     void initGL() {    glClearColor(0.0f, 0.0f, 0.0f, one.0f);     glClearDepth(1.0f);                       glEnable(GL_DEPTH_TEST);       glDepthFunc(GL_LEQUAL);        glShadeModel(GL_SMOOTH);       glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);   }    void display() {    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     glMatrixMode(GL_MODELVIEW);               glLoadIdentity();                     glTranslatef(one.5f, 0.0f, -vii.0f);                    glRotatef(angleCube, 1.0f, 1.0f, i.0f);                                    glBegin(GL_QUADS);                                     glColor3f(0.0f, 1.0f, 0.0f);            glVertex3f( ane.0f, 1.0f, -1.0f);       glVertex3f(-1.0f, 1.0f, -1.0f);       glVertex3f(-1.0f, one.0f,  i.0f);       glVertex3f( one.0f, 1.0f,  ane.0f);                glColor3f(1.0f, 0.5f, 0.0f);            glVertex3f( one.0f, -1.0f,  one.0f);       glVertex3f(-1.0f, -1.0f,  1.0f);       glVertex3f(-1.0f, -1.0f, -1.0f);       glVertex3f( 1.0f, -ane.0f, -ane.0f);                glColor3f(1.0f, 0.0f, 0.0f);            glVertex3f( 1.0f,  1.0f, 1.0f);       glVertex3f(-1.0f,  1.0f, ane.0f);       glVertex3f(-ane.0f, -1.0f, 1.0f);       glVertex3f( 1.0f, -one.0f, one.0f);                glColor3f(1.0f, 1.0f, 0.0f);            glVertex3f( 1.0f, -1.0f, -ane.0f);       glVertex3f(-one.0f, -1.0f, -ane.0f);       glVertex3f(-1.0f,  1.0f, -1.0f);       glVertex3f( 1.0f,  1.0f, -1.0f);                glColor3f(0.0f, 0.0f, 1.0f);            glVertex3f(-i.0f,  1.0f,  one.0f);       glVertex3f(-1.0f,  1.0f, -1.0f);       glVertex3f(-one.0f, -1.0f, -1.0f);       glVertex3f(-1.0f, -1.0f,  ane.0f);                glColor3f(1.0f, 0.0f, 1.0f);            glVertex3f(1.0f,  ane.0f, -1.0f);       glVertex3f(1.0f,  1.0f,  i.0f);       glVertex3f(1.0f, -ane.0f,  i.0f);       glVertex3f(1.0f, -i.0f, -1.0f);    glEnd();            glLoadIdentity();                      glTranslatef(-1.5f, 0.0f, -6.0f);                    glRotatef(anglePyramid, i.0f, 1.0f, 0.0f);                                    glBegin(GL_TRIANGLES);                         glColor3f(ane.0f, 0.0f, 0.0f);            glVertex3f( 0.0f, 1.0f, 0.0f);       glColor3f(0.0f, 1.0f, 0.0f);            glVertex3f(-1.0f, -ane.0f, 1.0f);       glColor3f(0.0f, 0.0f, 1.0f);            glVertex3f(1.0f, -1.0f, 1.0f);                glColor3f(ane.0f, 0.0f, 0.0f);            glVertex3f(0.0f, 1.0f, 0.0f);       glColor3f(0.0f, 0.0f, 1.0f);            glVertex3f(1.0f, -one.0f, 1.0f);       glColor3f(0.0f, ane.0f, 0.0f);            glVertex3f(1.0f, -1.0f, -1.0f);                glColor3f(1.0f, 0.0f, 0.0f);            glVertex3f(0.0f, one.0f, 0.0f);       glColor3f(0.0f, 1.0f, 0.0f);            glVertex3f(one.0f, -ane.0f, -1.0f);       glColor3f(0.0f, 0.0f, ane.0f);            glVertex3f(-1.0f, -1.0f, -i.0f);                glColor3f(1.0f,0.0f,0.0f);              glVertex3f( 0.0f, 1.0f, 0.0f);       glColor3f(0.0f,0.0f,1.0f);              glVertex3f(-ane.0f,-1.0f,-1.0f);       glColor3f(0.0f,1.0f,0.0f);              glVertex3f(-one.0f,-1.0f, 1.0f);    glEnd();         glutSwapBuffers();                                        anglePyramid += 0.2f;    angleCube -= 0.15f;                  }                                      void timer(int value) {    glutPostRedisplay();          glutTimerFunc(refreshMills, timer, 0);  }                   void reshape(GLsizei width, GLsizei superlative) {          if (tiptop == 0) peak = ane;                    GLfloat aspect = (GLfloat)width / (GLfloat)height;          glViewport(0, 0, width, superlative);          glMatrixMode(GL_PROJECTION);      glLoadIdentity();                     gluPerspective(45.0f, aspect, 0.1f, 100.0f); }    int main(int argc, char** argv) {    glutInit(&argc, argv);                glutInitDisplayMode(GLUT_DOUBLE);     glutInitWindowSize(640, 480);       glutInitWindowPosition(l, 50);     glutCreateWindow(title);              glutDisplayFunc(display);           glutReshapeFunc(reshape);           initGL();                                         glutTimerFunc(0, timer, 0);                                    glutMainLoop();                     return 0; }

The new codes are:

GLfloat anglePyramid = 0.0f;
GLfloat angleCube = 0.0f;
int refreshMills = 15;
Nosotros define 2 global variables to keep track of the current rotational angles of the cube and pyramid. We also ascertain the refresh period as 15 msec (66 frames per second).

void timer(int value) {
glutPostRedisplay();
glutTimerFunc(refreshMills, timer, 0); //
}

To perform blitheness, we ascertain a office called timer(), which posts a re-pigment request to activate display() when the timer expired, and then run the timer over again. In primary(), nosotros perform the first timer() call via glutTimerFunc(0, timer, 0).

glRotatef(angleCube, 1.0f, 1.0f, ane.0f);
......
glRotatef(anglePyramid, i.0f, i.0f, 0.0f);
......
anglePyramid += 0.2f;
angleCube -= 0.15f;
In display(), we rotate the cube and pyramid based on their rotational angles, and update the angles after each refresh.

Example iii: Orthographic Project (OGL03Orthographic.cpp)

As mentioned, OpenGL support two blazon of projections: perspective and orthographic. In orthographic projection, an object appears to be the same size regardless of the depth. Orthographic is a special instance of perspective project, where the camera is placed very far away.

To utilize orthographic projection, modify the reshape() part to invoke glOrtho().

void reshape(GLsizei width, GLsizei superlative) {          if (top == 0) height = ane;                    GLfloat aspect = (GLfloat)width / (GLfloat)height;          glViewport(0, 0, width, height);          glMatrixMode(GL_PROJECTION);      glLoadIdentity();                                   if (width >= height) {             glOrtho(-3.0 * attribute, 3.0 * aspect, -3.0, iii.0, 0.1, 100);    } else {             glOrtho(-three.0, 3.0, -3.0 / aspect, iii.0 / aspect, 0.one, 100);    }          }

In this example, we set the cross-section of view-volume according to the aspect ratio of the viewport, and depth from 0.one to 100, corresponding to z=-0.1 to z=-100. Take annotation that the cube and pyramid are contained within the view-volume.

Example 4: Vertex Array

In the before example, drawing a cube requires at least 24 glVertex functions and a pair of glBegin and glEnd. Function calls may involve high overhead and hinder the performance. Furthermore, each vertex is specified and candy three times.

Link to OpenGL/Estimator Graphics References and Resources

Source: https://www3.ntu.edu.sg/home/ehchua/programming/opengl/CG_Examples.html

Posted by: mcculloughglelavold.blogspot.com

0 Response to "How To Draw A 3d House In Opengl"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel