PROGRAM 5: LIGHT
·
Header
File
·
Render
func
·
Handle
resize
·
Display
·
Timer
·
Main
func
1.
Header
File.(gl/glut.h , math.h, windows.h) // REFER 2d
link: http://simplifiedopengl.blogspot.in/2016/08/program-3-write-program-to-implement-2d.html
link:
2.
Initialise
rendering function for the 2d object// REFER 2d and include the below mentioned
effects for 3d
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_NORMALIZE); //Automatically normalize normals
3.
Create
Handle Resize // REFER 2d
4.
Display Function // REFER 2d
void
drawScene()
{
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);//dark
blue
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(_angle,0.0f,0.0f,1.0f); //rotate
object by 30 degree with respect to y-axis
glTranslatef(0.0f, 0.0f, -10.0f);
GLfloat
ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
GLfloat light_ambient[] = {1.0,
1.0,1.0,1.0};//producing the color
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,
ambientColor);
GLfloat lightPos0[] = {0.0f, 0.0f, 1.0f,
0.0f}; //Positioned at (4, 0, 8)
glLightfv(GL_LIGHT0, GL_DIFFUSE,
light_ambient);
glLightfv(GL_LIGHT0, GL_POSITION,
lightPos0);
//************************
//OBJ
//Right
face (x = 1.0f), Left face (x = -1.0f), Back face (z = -1.0f), Front face (z = 1.0f), Bottom face (y = -1.0f), top
face(z=1.0f)
glutSwapBuffers();
}
5. Timer
6. Main Function
SAMPLE PROGRAM
#include
<iostream>
#include
<stdlib.h>
#include
<GL/glut.h>
using
namespace std;
//Initializes
3D rendering
void
initRendering() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable
lighting
glEnable(GL_LIGHT0); //Enable light
#0
glEnable(GL_NORMALIZE);
//Automatically normalize normals
}
void
handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w /
(double)h, 1.0, 200.0);
}
float
_angle = 0.0f;
//Draws
the 3D scene
void
drawScene() {
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f);
//Add ambient light
GLfloat ambientColor[] = {0.2f,
0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
GLfloat light_ambient[] = {1.0,
1.0,1.0,1.0};//producing the color
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,
ambientColor);
GLfloat lightPos0[] = {0.0f, 0.0f,
1.0f, 0.0f}; //Positioned at (4, 0, 8)
glLightfv(GL_LIGHT0, GL_DIFFUSE,
light_ambient);
glLightfv(GL_LIGHT0, GL_POSITION,
lightPos0);
glRotatef(_angle, 1.0f,0.0f, 0.0f);
glColor4f(0.0f, 1.0f, 1.0f, 1.0f);//light
blue
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(0.0f, 1.0f, 0.0f); // Green
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, 1.0f);
// Bottom face (y = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
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, -1.0f);
// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
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, 1.0f);
// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 1.0f); // Yellow
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, -1.0f);
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
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, 1.0f);
// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
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, -1.0f);
glEnd();
// End of drawing color-cube
glutSwapBuffers();
glFlush();
}
void
update(int value) {
_angle += 1.5f;
if (_angle > 360) {
_angle -= 360;
}
glutPostRedisplay();
glutTimerFunc(25, update,0);
}
int
main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |
GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("Lighting");
initRendering();
glutDisplayFunc(drawScene);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0); //Add
a timer
glutMainLoop();
return 0;
}
No comments:
Post a Comment