PDA

View Full Version : How do i put lighting on my 3d building?



ajinamoto21
04-27-2016, 10:02 PM
hello ,
I'm trying to build a building by using opengl but i have no idea on how to put the lighting on my building. I hope somebody here can help me .



#include <windows.h> // for MS Windows
#include <glut.h> // GLUT, include glu.h and gl.h
#include <gl\GL.h>
#include <gl\GLU.h>
#include <iostream>
/*Global variable*/
char title[]="2nd assignment";

void initGL()
{
glClearColor(0.0f, 0.0f ,0.0f , 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
glDepthFunc(GL_LEQUAL); // Set the type of depth-test
glShadeModel(GL_SMOOTH); // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice perspective corrections


glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -20.0, 10.0, -17.0, 10.0);



}
/* Handler for window-repaint event. Called back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix

// Render a color-cube consisting of 6 quads with different colors
glLoadIdentity(); // Reset the model-view matrix
gluLookAt(-20.0, 15.0, 30.0,
-5.0, 3.0, 2.0,
0.0, 1.0, 0.0);
glTranslatef(1.5f, 0.0f, -7.0f); // Move right and into the screen



glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
// Top face (y = 1.0f)
// Define vertices in counter-clockwise (CCW) order with normal pointing out
glColor3f(1.0f, 0.5f, 0.0f); // Green
glVertex3f( 5.0f, 4.0f, -1.0f);
glVertex3f(-5.0f, 4.0f, -1.0f);
glVertex3f(-5.0f, 4.0f, 1.0f);
glVertex3f( 5.0f, 4.0f, 1.0f);

glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f( 5.0f, 4.5f, -1.0f);
glVertex3f(-5.0f, 4.5f, -1.0f);
glVertex3f(-5.0f, 4.5f, 1.0f);
glVertex3f( 5.0f, 4.5f, 1.0f);


// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f( 9.0f, 0.0f, 2.0f);
glVertex3f(-9.0f, 0.0f, 2.0f);
glVertex3f(-9.0f, 0.0f, -1.0f);
glVertex3f( 9.0f, 0.0f, -1.0f);


glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f( 5.0f, 4.0f, -1.5f);
glVertex3f(-5.0f, 4.0f, -1.5f);
glVertex3f(-5.0f, 4.0f, -1.0f);
glVertex3f( 5.0f, 4.0f, -1.0f);


// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( -0.5f, 4.0f, 1.0f);
glVertex3f(-5.0f, 4.0f, 1.0f);
glVertex3f(-5.0f, 0.0f, 1.0f);
glVertex3f( -0.5f, 0.0f, 1.0f);

glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 0.5f, 4.0f, 1.0f);
glVertex3f(-0.5f, 4.0f, 1.0f);
glVertex3f(-0.5f, 2.5f, 1.0f);
glVertex3f( 0.5f, 2.5f, 1.0f);



glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 5.0f, 4.0f, 1.0f);
glVertex3f(0.5f, 4.0f, 1.0f);
glVertex3f(0.5f, 0.0f, 1.0f);
glVertex3f( 5.0f, 0.0f, 1.0f);


// Back face (z = -1.0f)

glColor3f(1.0f, 1.0f, 0.0f); // Red
glVertex3f( -0.5f, 4.0f, -1.0f);
glVertex3f(-5.0f, 4.0f, -1.0f);
glVertex3f(-5.0f, 0.0f, -1.0f);
glVertex3f( -0.5f, 0.0f, -1.0f);

glColor3f(1.0f, 1.0f, 0.0f); // Red
glVertex3f( 0.5f, 4.0f, -1.0f);
glVertex3f(-0.5f, 4.0f, -1.0f);
glVertex3f(-0.5f, 2.5f, -1.0f);
glVertex3f( 0.5f, 2.5f, -1.0f);



glColor3f(1.0f, 1.0f, 0.0f); // Red
glVertex3f( 5.0f, 4.0f, -1.0f);
glVertex3f(0.5f, 4.0f, -1.0f);
glVertex3f(0.5f, 0.0f, -1.0f);
glVertex3f( 5.0f, 0.0f, -1.0f);


glColor3f(1.0f, 0.0f, 1.0f); // Red
glVertex3f( 5.0f, 4.5f, -1.0f);
glVertex3f(-5.0f, 4.5f, -1.0f);
glVertex3f(-5.0f, 4.0f, -1.5f);
glVertex3f( 5.0f, 4.0f, -1.5f);


// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-5.0f, 4.0f, 1.0f);
glVertex3f(-5.0f, 4.0f, -1.0f);
glVertex3f(-5.0f, 0.0f, -1.0f);
glVertex3f(-5.0f, 0.0f, 1.0f);




// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(5.0f, 4.0f, -1.0f);
glVertex3f(5.0f, 4.0f, 1.0f);
glVertex3f(5.0f, 0.0f, 1.0f);
glVertex3f(5.0f, 0.0f, -1.0f);



//part dalam
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(-0.5f, 3.0f, 0.5f);
glVertex3f(-0.5f, 3.0f, 1.0f);
glVertex3f(-0.5f, 0.0f, 1.0f);
glVertex3f(-0.5f, 0.0f, 0.5f);

glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(0.5f, 3.0f, 0.5f);
glVertex3f(0.5f, 3.0f, 1.0f);
glVertex3f(0.5f, 0.0f, 1.0f);
glVertex3f(0.5f, 0.0f, 0.5f);

glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f( -0.25f, 3.0f, 0.5f);
glVertex3f(-0.5f, 3.0f, 0.5f);
glVertex3f(-0.5f, 0.0f, 0.5f);
glVertex3f( -0.25f, 0.0f, 0.5f);

glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f( 0.5f, 3.0f, 0.5f);
glVertex3f(-0.25f, 3.0f, 0.5f);
glVertex3f(-0.25f, 1.5f, 0.5f);
glVertex3f( 0.5f, 1.5f, 0.5f);

glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f( 0.5f, 3.0f, 0.5f);
glVertex3f(0.25f, 3.0f, 0.5f);
glVertex3f(0.25f, 0.0f, 0.5f);
glVertex3f( 0.5f, 0.0f, 0.5f);





//bumbung atas

glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 5.0f, 5.0f, 1.0f);
glVertex3f(-5.0f, 5.0f, 1.0f);
glVertex3f(-5.0f, 4.5f, 1.0f);
glVertex3f( 5.0f, 4.5f, 1.0f);

glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 5.0f, 5.0f, -1.0f);
glVertex3f(-5.0f, 5.0f, -1.0f);
glVertex3f(-5.0f, 4.5f, -1.0f);
glVertex3f( 5.0f, 4.5f, -1.0f);

// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-5.0f, 5.0f, 1.0f);
glVertex3f(-5.0f, 5.0f, -1.0f);
glVertex3f(-5.0f, 4.5f, -1.0f);
glVertex3f(-5.0f, 4.5f, 1.0f);




// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(5.0f, 5.0f, -1.0f);
glVertex3f(5.0f, 5.0f, 1.0f);
glVertex3f(5.0f, 4.5f, 1.0f);
glVertex3f(5.0f, 4.5f, -1.0f);


glColor3f( 0.196078f, 0.6f, 0.8f); // sky Blue
glVertex3f( 6.5f, 4.5f, 1.0f);
glVertex3f(-10.5f, 4.5f, 1.0f);
glVertex3f(-10.5f, 4.0f, 2.5f);
glVertex3f( 6.5f, 4.0f, 2.5f);

glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f( 5.0f, 4.0f, 2.5f);
glVertex3f(-7.0f, 4.0f, 2.5f);
glVertex3f(-7.0f, 4.0f, 1.0f);
glVertex3f( 5.0f, 4.0f, 1.0f);

glColor3f( 0.196078f, 0.6f, 0.8f); // sky Blue
glVertex3f( 5.0f, 6.0f, 0.0f);
glVertex3f(-5.0f, 6.0f, 0.0f);
glVertex3f(-5.0f, 5.0f, 1.5f);
glVertex3f( 5.0f, 5.0f, 1.5f);

glColor3f( 0.196078f, 0.6f, 0.8f); //sky Blue
glVertex3f( 5.0f, 6.0f, 0.0f);
glVertex3f(-5.0f, 6.0f, 0.0f);
glVertex3f(-5.0f, 5.0f, -1.5f);
glVertex3f( 5.0f, 5.0f, -1.5f);





//2nd building


//front
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( -6.0f, 4.0f, 1.0f);
glVertex3f(-9.0f, 4.0f, 1.0f);
glVertex3f(-9.0f, 0.0f, 1.0f);
glVertex3f( -6.0f, 0.0f, 1.0f);

glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( -6.0f, 4.0f, 1.0f);
glVertex3f(-5.0f, 4.0f, 1.0f);
glVertex3f(-5.0f, 1.5f, 1.0f);
glVertex3f( -6.0f, 1.5f, 1.0f);

glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( -5.0f, 4.0f, 1.0f);
glVertex3f(-5.5, 4.0f, 1.0f);
glVertex3f(-5.5f, 0.0f, 1.0f);
glVertex3f( -5.0f, 0.0f, 1.0f);

// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(-7.0f, 4.5f, -12.0f);
glVertex3f(-7.0f, 4.5f, 1.0f);
glVertex3f(-7.0f, 0.0f, 1.0f);
glVertex3f(-7.0f, 0.0f, -12.0f);




//left
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-9.0f, 4.5f, 1.0f);
glVertex3f(-9.0f, 4.5f, -15.0f);
glVertex3f(-9.0f, 0.0f, -15.0f);
glVertex3f(-9.0f, 0.0f, 1.0f);

//back
glColor3f(0.137255f, 0.556863f, 0.419608f); // sea green
glVertex3f( -7.0f, 4.0f, -12.0f);
glVertex3f(-9.0f, 4.0f, -12.0f);
glVertex3f(-9.0f, 0.0f, -12.0f);
glVertex3f( -7.0f, 0.0f, -12.0f);

//top
glColor3f( 0.196078f, 0.6f, 0.8f);
glVertex3f( -7.0f, 4.5f, -12.0f);
glVertex3f(-9.0f, 4.5f, -12.0f);
glVertex3f(-9.0f, 4.5f, 1.0f);
glVertex3f( -7.0f, 4.5f, 1.0f);

glColor3f( 0.196078f, 0.6f, 0.8f); //sky Blue
glVertex3f( -9.0f, 4.5f, 1.0f);
glVertex3f(-9.0f, 4.5f, -12.0f);
glVertex3f(-10.5f, 4.0f, -12.0f);
glVertex3f( -10.5f, 4.0f, -1.0f);

glColor3f( 0.196078f, 0.6f, 0.8f); //sky Blue
glVertex3f( -7.0f, 4.5f, -12.0f);
glVertex3f(-7.0f, 4.5f, 2.5f);
glVertex3f(-6.5f, 4.0f, 2.5f);
glVertex3f( -6.5f, 4.0f, -12.0f);




//3rd building

glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 5.0f, 4.5f, 1.0f);
glVertex3f(6.0f, 4.5f, 1.0f);
glVertex3f(6.0f, 0.0f, 1.0f);
glVertex3f( 5.0f, 0.0f, 1.0f);

glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 6.0f, 4.5f, 1.0f);
glVertex3f( 6.5f, 4.5f, 1.0f);
glVertex3f( 6.5f, 1.5f, 1.0f);
glVertex3f( 6.0f, 1.5f, 1.0f);

glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 9.0f, 4.5f, 2.0f);
glVertex3f( 6.5, 4.5f, 2.0f);
glVertex3f(6.5f, 0.0f, 2.0f);
glVertex3f( 9.0f, 0.0f, 2.0f);


// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(9.0f, 4.5f, -15.0f);
glVertex3f(9.0f, 4.5f, 2.0f);
glVertex3f(9.0f, 0.0f, 2.0f);
glVertex3f(9.0f, 0.0f, -15.0f);


// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(6.50f, 4.5f, 2.0f);
glVertex3f(6.50f, 4.5f, 1.0f);
glVertex3f(6.50f, 0.0f, 1.0f);
glVertex3f(6.50f, 0.0f, 2.0f);


glColor3f(0.74902f, 0.847059f, 0.847059f); //sky blue
glVertex3f(7.0f, 4.5f, 2.0f);
glVertex3f(7.0f, 4.5f, -12.0f);
glVertex3f(7.0f, 0.0f, -12.0f);
glVertex3f(7.0f, 0.0f, 2.0f);




//back
glColor3f(0.137255f, 0.556863f, 0.419608f); // sea green
glVertex3f( 9.0f, 4.5f, -12.0f);
glVertex3f(7.0f, 4.5f, -12.0f);
glVertex3f(7.0f, 0.0f, -12.0f);
glVertex3f( 9.0f, 0.0f, -12.0f);

//top
glColor3f( 0.196078f, 0.6f, 0.8f);
glVertex3f( 6.0f, 4.5f, -15.0f);
glVertex3f(9.0f, 4.5f, -15.0f);
glVertex3f(9.0f, 4.5f, 2.0f);
glVertex3f( 6.0f, 4.5f, 2.0f);

glColor3f( 0.196078f, 0.6f, 0.8f); //sky Blue
glVertex3f( 9.0f, 4.5f, 2.5f);
glVertex3f(9.0f, 4.5f, -15.0f);
glVertex3f(9.5f, 4.0f, -15.0f);
glVertex3f( 9.5f, 4.0f, 2.5f);

glColor3f( 0.196078f, 0.6f, 0.8f); //sky Blue
glVertex3f( 9.0f, 4.5f, 2.0f);
glVertex3f(6.0f, 4.5f, 2.0f);
glVertex3f(6.0f, 4.0f, 2.5f);
glVertex3f( 9.5f, 4.0f, 2.5f);



//4th building

glColor3f(1.0f, 1.0f, 0.0f); // yellow
glVertex3f( 9.0f, 4.5f, -15.0f);
glVertex3f(-9.0f, 4.5f, -15.0f);
glVertex3f(-9.0f, 0.0f, -15.0f);
glVertex3f( 9.0f, 0.0f, -15.0f);




// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-7.0f, 4.5f, -13.0f);
glVertex3f(-7.0f, 4.5f, -15.0f);
glVertex3f(-7.0f, 0.0f, -15.0f);
glVertex3f(-7.0f, 0.0f, -13.0f);




// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(7.0f, 4.5f, -15.0f);
glVertex3f(7.0f, 4.5f, -13.0f);
glVertex3f(7.0f, 0.0f, -13.0f);
glVertex3f(7.0f, 0.0f, -15.0f);


// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 7.0f, 4.5f, -13.0f);
glVertex3f(-7.0f, 4.5f, -13.0f);
glVertex3f(-7.0f, 0.0f, -13.0f);
glVertex3f( 7.0f, 0.0f, -13.0f);

//top

glColor3f( 0.196078f, 0.6f, 0.8f); // sky Blue
glVertex3f( -9.0f, 4.5f, -15.0f);
glVertex3f(7.0f, 4.5f, -15.0f);
glVertex3f(7.0f, 4.5f, -13.0f);
glVertex3f( -9.0f, 4.5f, -13.0f);

glColor3f( 0.196078f, 0.6f, 0.8f); // sky Blue
glVertex3f( -7.0f, 4.5f, -13.0f);
glVertex3f(7.0f, 4.5f, -13.0f);
glVertex3f(7.0f, 4.0f, -12.5f);
glVertex3f( -7.0f, 4.0f, -12.5f);








glEnd(); // End of drawing color-cube


glutSwapBuffers(); // Swap the front and back frame buffers (double buffering)
}

/* Handler for window re-size event. Called back when the window first appears and
whenever the window is re-sized with its new width and height */
void reshape(GLsizei width, GLsizei height) { // GLsizei for non-negative integer
// Compute aspect ratio of the new window
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height;

// Set the viewport to cover the new window
glViewport(0, 0, width, height);

// Set the aspect ratio of the clipping volume to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset
// Enable perspective projection with fovy, aspect, zNear and zFar
gluPerspective(35.0f, 1.0f, 1.0f, 70.0f);



}

/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
glutInitWindowSize(640, 480); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutCreateWindow(title); // Create window with the given title
glutPostRedisplay();
glutDisplayFunc(display); // Register callback handler for window re-paint event
glutReshapeFunc(reshape); // Register callback handler for window re-size event
initGL(); // Our own OpenGL initialization
glutMainLoop(); // Enter the infinite event-processing loop
return 0;
}

gwartney
04-28-2016, 01:26 PM
While i am not sure my self on how to create lighting as iam still learning how to use open gl. Check out this resource its what i have been using has a a lot of information on using lighting in opengl. http://learnopengl.com/#!Lighting/Colors

BBeck1
05-03-2016, 07:17 PM
So, there are different types of lighting in 3D graphics. I'm just starting out in OGL and I'm skipping straight to modern OGL (mostly 4.2). (I've done DX11 and some other things before this.) But I believe older OGL had some lighting routines you could use and I'm not sure how that worked.

But for modern OGL you should be using shaders. And it gets deep into vector and matrix algebra pretty quick. At the risk of self promotion, I recommend my YouTube channel "VirtuallyProgramming.com". I have tutorials on vectors (https://www.youtube.com/watch?v=56v9BgwSzsg), matrices, and HLSL. This is all stuff you need to know if you are going to do 3D graphics in modern OGL. HLSL is basically the same thing as GL Shader Language (GLSL). The more GLSL I learn, the more I see the similarities. Most of the answer is math and math is not particularly specific to any computer language.

The main think, besides shaders, that you are missing is normals. Each triangle should, at minimum, have one normal which describes what direction it faces. You can imagine an arrow sticking out of each triangle face.

One of the easiest methods of illumination is Gouraud shading. You have one single light that basically acts kind of like sunlight in that it comes from a general direction like "the entire eastern sky" rather than a specific point. And so, the light is represented by an arrow also. When the triangle face's normal/arrow points directly at the light's arrow (they are 180 degrees different) the triangle gets 100% of the light's color. If the triangle is facing more than 90 degrees away from the light it gets none of the light's color. Between 0 degrees (pointing straight at the light which is directly opposite of the light's direction) and 90 degrees you can imagine a percentage between the two and at that angle the triangle gets that percentage of the light's color.

So, you have two lights in Gouraud shading. You have ambient light which is just light that is applied to everything and can be though of the light in the shadows. Then you have the direct light I just described.

Anyway, my videos cover this in great depth.

Lighting only gets far more complicated from there and more math intensive. But this type of shading can be all you need to make quite a few games and for a lot of non-game projects.