# OpenGL Draw 3D House

house should look like from side (try to draw as best as I can)

_________

/ /

/ ____ /__

| | | |

|___ |____|____|

1. Each vertex is shared among 3+ faces. Its normal is the average of all polygon (face) normals.

2. For each polygon, you need 3 vertices to define a plane, and the normal vector to that plane.

3. The outward normal is based on the chosen vertex and two others. Any two will do as they will all be in a plane, as long as one is clockwise from the chosen one and the other is counterclockwise from it, but it's easiest to use the nearest ones in the scenefile order.

Specifics:

4. Without loss of generality, call the chosen vertex a, the closest counterclockwise one b, and the closest clockwise one c. You have 2+ edges that lie in their plane, that you can use to calculate N.

5. How to calculate the normal N to the plane containing a, b, c:

N is (b - a) x (c - a)

6. The document you found (or a vector calculus or analytic geometry reference) shows how to find the cross product and divide by its length in R^3 to get a unit normal ("orthonormal") vector.

http://jerome.jouvie.free.fr/OpenGl/Tutorials/Tutorial12.php

(second Google hit on "outward face normal OpenGL")

http://www.fullonsoftware.co.uk/snippets/content/Math_-_Calculating_Face_Normals.pdf

I found this code for polygon normal, will this work??

N = {0,0,0};

for (each vertex in the polygon Vn)
{
Nx += (Vny - V(n+1)y) * (Vnz + V(n+1)z);
Ny += (Vnz - V(n+1)z) * (Vnx> + V(n+1)x);
Nz += (Vnx - V(n+1)x) * (Vny + V(n+1)y);
}

Normalizing N after this

THIS IS THE CODE THAT I TRIED TO USE. IT DOESN'T WORK. I DON'T KNOW IF I AM USING THE CORRECT CODE.

#include // header file for the OpenGL32 library
#include // header file for the GLu32 library
#include // header file for the GLut library

#define kWindowWidth 400
#define kWindowHeight 300

GLvoid InitGL(GLvoid)
{
glClearColor(GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA);

glClearColor(0.0, 0.0, 0.0, 0.0); // Black background
}

GLvoid DrawGLScene(GLvoid)
{
// clear the screen and the depth buffer
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
// reset the view

// YOUR RENDERING CODE GOES HERE
// **************************************************
glTranslatef(-1.5f,0.0f,-6.0f); // Move Into The Screen And Left
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
glRotatef(xRot, 0.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, -25.0f, 0.0f);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glColor3f(1.0f,0.0f,0.0f); // Set The Color To RED One Time Only

//GLint nvertices[] = {5, 4, 4, 4, 4, 4, 4, 5}; // sum = 30 NumPolygnos = 7

//GLubyte vertexindex[] = { 0, 1, 2, 3, 4,
// 0, 5, 9, 1,
// 1, 9, 8, 2,
// 2, 8, 7, 3,
// 3, 7, 6, 4,
// 4, 6, 5, 0,
// 5, 6, 7, 8, 9 }; // sum = 135 30 Elements

//GLfloat vertices[] = { 2, 5, 0, 1,
// 4, 4, 0, 1,
// 4, 0, 0, 1,
// 0, 0, 0, 1,
// 0, 4, 0, 1,
// 2, 5, 4, 1,
// 0, 4, 4, 1,
// 0, 0, 4, 1,
// 4, 0, 4, 1,
// 4, 4, 4, 1 }; // sum = 75 40 Elements

GLfloat vertices[] = { -1.0f, 1.0f, 1.0f, // 0 // Front of cube
1.0f, 1.0f, 1.0f, // 1
1.0f, -1.0f, 1.0f, // 2
-1.0f, -1.0f, 1.0f, // 3
-1.0f, 1.0f, -1.0f, // 4 // Back of cube
1.0f, 1.0f, -1.0f, // 5
1.0f, -1.0f, -1.0f, // 6
-1.0f, -1.0f, -1.0f, }; // 7

GLubyte vertexindex[] = { 0, 1, 2, 3, // Front Face
4, 5, 0, 1, // Top Face
3, 2, 6, 7, // Bottom Face
5, 4, 7, 6, // Back Face
1, 5, 6, 2, // Right Face
4, 0, 3, 7 }; // Leftface

GLfloat vcolors[] = { 1.0, 0.2, 0.2,
0.2, 0.2, 1.0,
0.8, 1.0, 0.2,
0.75, 0.75, 0.75,
0.35, 0.35, 0.35,
0.5, 0.5, 0.5 };

// GLsizei kount[] = { 21, 18} ;

// GLvoid * kindices[2] = {vertices, vcolors};

// glColorPointer(3, GL_FLOAT, 0, vcolors);
glColorPointer(3, GL_C3F_V3F, 6*sizeof(GLfloat), vcolors);
glEnableClientState(GL_VERTEX_ARRAY);

glVertexPointer(3, GL_BYTE, 0, vertexindex);
glVertexPointer(3, GL_FLOAT, 0, vertices);

// glDrawElements(GL_LINE_STRIP, kount, GL_UNSIGNED_BYTE, kindices, 2);

// **************************************************

glutSwapBuffers();
glFlush();
}

GLvoid ReSizeGLScene(int Width, int Height)
{
glViewport (0, 0, (GLsizei) Width, (GLsizei) Height);
glMatrixMode(GL_PROJECTION);

gluPerspective(90.0, (GLfloat) Width / (GLfloat) Height, 0.1, 100.0);

glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (kWindowWidth, kWindowHeight);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);

InitGL();

glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);

glutMainLoop();

return 0;
}