Color Combination
CONTENTS
SL.NO particulars PAGE.NO
1 Abstract 2
2 System Specifications 3
3 Introduction to openGL 4
5 Implementation 7
6 Interaction 9
7 Source Code 10
8 Output 27
9 Conclusion 29
10 Bibliography 30
1 Dept. of Computer Science & Engineering.
Color Combination
Abstract
Main aim of this Mini Project is to illustrate the concepts of color
combination and usage of OpenGL library.
Our project demonstrates what happens when the primary colors i.e
Red, Green and Blue are combined.
All the permutations and combinations of colors are taken care of.
We have used input devices like mouse and key board to interact with
program
System specifications
2 Dept. of Computer Science & Engineering.
Color Combination
SOFTWARE REQUIREMENTS :
MICROSOFT VISUAL C++
OPENGL
HARDWARE REQUIREMENT :
GRAPHICS SYSTEM,
Pentium P4 with 256 of Ram(Min)
Introduction to openGL
As a software interface for graphics hardware, OpenGL's main purpose is to
render two- and three-dimensional objects into a frame buffer.
These objects are described as sequences of vertices or pixels.
OpenGL performs several processing steps on this data to convert it to pixels to
3 Dept. of Computer Science & Engineering.
Color Combination
form the final desired image in the frame buffer.
OpenGL Fundamentals
This section explains some of the concepts inherent in OpenGL.
Primitives and Commands
OpenGL draws primitives—points, line segments, or polygons—subject to several
selectable modes.
You can control modes independently of each other; that is, setting one mode
doesn't affect whether other modes are set .Primitives are specified, modes are
set, and other OpenGL operations are described by issuing commands in the form
of function calls.
Primitives are defined by a group of one or more vertices. A vertex defines a
point, an endpoint of a line, or a corner of a polygon where two edges meet. Data
is associated with a vertex, and each vertex and its associated data are processed
independently, in order, and in the same way. The type of clipping depends on
which primitive the group of vertices represents.
Commands are always processed in the order in which they are received,
although there may be an indeterminate delay before a command takes effect.
This means that each primitive is drawn completely before any subsequent
command takes effect. It also means that state-querying commands return data
that's consistent with complete execution of all previously issued OpenGL
commands.
Basic OpenGL Operation
The figure shown below gives an abstract, high-level block diagram of how
OpenGL processes data. In the diagram, commands enter from the left and
proceed through what can be thought of as a processing pipeline. Some
commands specify geometric objects to be drawn, and others control how the
objects are handled during the various processing stages.
Figure . OpenGL Block Diagram
4 Dept. of Computer Science & Engineering.
Color Combination
As shown by the first block in the diagram, rather than having all commands
proceed immediately through the pipeline, you can choose to accumulate some
of them in a display list for processing at a later time.
Rasterization produces a series of frame buffer addresses and associated values
using a two-dimensional description of a point, line segment, or polygon.
Each fragment so produced is fed into the last stage,
per-fragment operations, which performs the final operations on the data before
it's stored as pixels in the frame buffer. These operations include conditional
updates to the frame buffer based on incoming and previously stored z-value s
(for z-buffering) and blending of incoming pixel colors with stored colors, as well
as masking and other logical operations on pixel values.
All elements of OpenGL state, including the contents of the texture memory and
even of the frame buffer, can be obtained by an OpenGL application.
5 Dept. of Computer Science & Engineering.
Color Combination
Implementation
This program is implemented using various openGL functions which are
shown below.
Various functions used in this program.
glutInit() : interaction between the windowing system and OPENGL is
initiated
glutInitDisplayMode() : used when double buffering is required and depth
information is required
6 Dept. of Computer Science & Engineering.
Color Combination
glutCreateWindow() : this opens the OPENGL window and displays the title
at top of the window
glutInitWindowSize() : specifies the size of the window
glutInitWindowPosition() : specifies the position of the window in screen
co-ordinates
glutKeyboardFunc() : handles normal ascii symbols
glutSpecialFunc() : handles special keyboard keys
glutReshapeFunc() : sets up the callback function for reshaping the window
glutIdleFunc() : this handles the processing of the background
glutDisplayFunc() : this handles redrawing of the window
glutMainLoop() : this starts the main loop, it never returns
glViewport() : used to set up the viewport
glVertex3fv() : used to set up the points or vertices in three dimensions
glColor3fv() : used to render color to faces
glFlush() : used to flush the pipeline
7 Dept. of Computer Science & Engineering.
Color Combination
glutPostRedisplay() : used to trigger an automatic redrawal of the object
glMatrixMode() : used to set up the required mode of the matrix
glLoadIdentity() : used to load or initialize to the identity matrix
glTranslatef() : used to translate or move the rotation centre from one
point to another in three dimensions
glRotatef() : used to rotate an object through a specified rotation angle
Interaction with program
This program includes interaction through keyboard.
S Start the Project
R Toggle Red Light
G Toggle Green Light
B Toggle Blue Light
Q-> Quit
8 Dept. of Computer Science & Engineering.
Color Combination
Source Code
/*An Interactive Program to create 3d objects*/
#include <windows.h>
#include<string.h>
#include<stdarg.h>
#include<stdio.h>
#include <glut.h>
static double x=0.0;
static float red1=0;
9 Dept. of Computer Science & Engineering.
Color Combination
static float green1=0;
static float blue1=0;
static float help1=1;
void stroke_output(GLfloat x, GLfloat y, char *format,...)
{
va_list args;
char buffer[200], *p;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
glPushMatrix();
glTranslatef(-2.5, y, 0);
glScaled(0.003, 0.005, 0.005);
for (p = buffer; *p; p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
glPopMatrix();
}
10 Dept. of Computer Science & Engineering.
Color Combination
void doInit()
{
/* Background and foreground color */
glClearColor(0.5,0.5,0.5,0.0);
glColor3f(.0,1.0,1.0);
glViewport(0,0,640,480);
/* Select the projection matrix and reset it then
setup our view perspective */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0f,(GLfloat)640/(GLfloat)480,0.1f,200.0f);
/* Select the modelview matrix, which we alter with rotatef() */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearDepth(2.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glDepthFunc(GL_LEQUAL);
}
11 Dept. of Computer Science & Engineering.
Color Combination
void torch(){
glPushMatrix();
glutSolidSphere(0.8,50,50);
glPopMatrix();
glPushMatrix();
glRotatef(90,0,1,0);
glScaled(0.5,0.5,3);
glColor3f(0,1,1);
glutSolidTorus(0.4,1.5,50,50);
glPopMatrix();
glPushMatrix();
glTranslatef(-1.5,0,0);
glRotatef(90,0,1,0);
glScaled(0.7,0.7,1.5);
12 Dept. of Computer Science & Engineering.
Color Combination
glutSolidTorus(0.4,1.5,50,50);
glPopMatrix();
void help(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScaled(0.7,0.7,0.7);
stroke_output(-2,3,"H -----> Toggle Help");
stroke_output(-2,2,"R -----> Toggle Red Light");
stroke_output(-2,1,"G -----> Toggle Green Light");
stroke_output(-2,0,"B -----> Toggle Blue Light");
glPopMatrix();
13 Dept. of Computer Science & Engineering.
Color Combination
glFlush();
glutSwapBuffers();
}
void draw(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-13.0f);
glPushMatrix();
glTranslatef(0,0,-15);
glScaled(1,1,0.1);
glColor3f(0.3,0.3,0);
glutSolidCube(9);
glPopMatrix();
14 Dept. of Computer Science & Engineering.
Color Combination
glPushMatrix();
glColor3f(1,0,1);
glutSolidCone(4.5,15,40,40);
glPopMatrix();
// Color Sphere
glPushMatrix();
glTranslatef(0,0,-14);
glScaled(1,1,0.1);
glColor3f(red1,green1,blue1);
glutSolidSphere(2,30,30);
glPopMatrix();
// red Torch
glPushMatrix();
glTranslatef(-1.5,-2,2);
15 Dept. of Computer Science & Engineering.
Color Combination
glRotatef(-20,0,0,1);
glPushMatrix();
glRotatef(90,0,1,0);
glScaled(0.3,0.3,0.3);
glColor3f(1,0,0);
torch();
glPopMatrix();
glPopMatrix();
if(red1){
glPushMatrix();
glRotatef(50,1,0,1);
glRotatef(-55,0,1,1);
glColor3f(1,0,0);
glutWireCone(1.0,3,10,10);
glPopMatrix();
}
16 Dept. of Computer Science & Engineering.
Color Combination
// Green Torch
glPushMatrix();
glTranslatef(0,-2,2);
glRotatef(-20,0,0,1);
glPushMatrix();
glRotatef(90,0,1,0);
glScaled(0.3,0.3,0.3);
glColor3f(0,1,0);
torch();
glPopMatrix();
glPopMatrix();
if(green1){
glPushMatrix();
glTranslatef(0,-0.5,-3);
glRotatef(10,1,0,1);
glColor3f(0,1,0);
glutWireCone(1.0,7,10,10);
glPopMatrix();
}
17 Dept. of Computer Science & Engineering.
Color Combination
// Blue Torch
glPushMatrix();
glTranslatef(1.5,-2,2);
glRotatef(-20,0,0,1);
glPushMatrix();
glRotatef(90,0,1,0);
glScaled(0.3,0.3,0.3);
glColor3f(0,0,1);
torch();
glPopMatrix();
glPopMatrix();
if(blue1){
glPushMatrix();
glRotatef(90,1,0,1);
glColor3f(0,0,1);
glutWireCone(1.0,3,10,10);
glPopMatrix();
}
18 Dept. of Computer Science & Engineering.
Color Combination
glFlush();
glutSwapBuffers();
}
void doDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-13.0f);
// Write your Own Code Here
if(help1)
help();
else
draw();
19 Dept. of Computer Science & Engineering.
Color Combination
GLfloat mat_ambient[]={0.0f,1.0f,2.0f,1.0f};
GLfloat mat_diffuse[]={0.0f,1.5f,.5f,1.0f};
GLfloat mat_specular[]={5.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[]={100.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
/*GLfloat lightIntensity[]={3.7f,0.7f,0.7f,1.0f}; Orange
GLfloat light_position[]={2.0f,5.0f,3.0f,1.0f};*/
/*light source properties*/
GLfloat lightIntensity[]={1.7f,1.7f,1.7f,1.0f};
GLfloat light_position[]={2.0f,0.0f,0.0f,0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
GLfloat light_position2[]={0.0f,0.0f,8.0f,0.0f};
20 Dept. of Computer Science & Engineering.
Color Combination
glLightfv(GL_LIGHT0,GL_POSITION,light_position2);
GLfloat light_position3[]={6.0f,0.0f,5.0f,0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,light_position3);
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightIntensity);
glFlush();
glutSwapBuffers();
void mykey(unsigned char key,int x,int y)
{
if(key=='q'||key=='Q')
{
exit(0);
}
21 Dept. of Computer Science & Engineering.
Color Combination
if(key=='h'||key=='H')
{
help1=!help1;
glutPostRedisplay();
}
if(key=='s' || key=='S'){
glutIdleFunc(draw);
if(key=='r'||key=='R')
{
red1=!red1 ;
glutPostRedisplay();
22 Dept. of Computer Science & Engineering.
Color Combination
if(key=='g'||key=='G')
{
green1=!green1;
glutPostRedisplay();
if(key=='b'||key=='B')
{
blue1=!blue1;
glutPostRedisplay();
int main(int argc, char *argv[])
{
23 Dept. of Computer Science & Engineering.
Color Combination
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow("Basic Structures Orientation");
glutDisplayFunc(doDisplay);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glutKeyboardFunc(mykey);
doInit();
glutMainLoop();
return 0;
}
OUTPUT OF THE PROGRAM
24 Dept. of Computer Science & Engineering.
Color Combination
25 Dept. of Computer Science & Engineering.
Color Combination
26 Dept. of Computer Science & Engineering.
Color Combination
Conclusions
The project “Color Combination” clearly demonstrates the use of OpenGL
functions and what happens when primary colors are mixed.
Finally we conclude that this program clearly illustrate the color combination
using openGL and has been completed successfully and is ready to be
demonstrated.
Bibliography
WE HAVE OBTAINED INFORMATION FROM MANY RESOURCES TO DESIGN AND
IMPLEMENT OUR PROJECT SUCCESSIVELY. WE HAVE ACQUIRED MOST OF THE
KNOWLEDGE FROM RELATED WEBSITES. THE FOLLOWING ARE SOME OF THE
27 Dept. of Computer Science & Engineering.
Color Combination
RESOURCES :
TEXT BOOKS :
INTERACTIVE COMPUTER GRAPHICS A TOP-DOWN APPROACH
-By Edward Angel.
COMPUTER GRAPHICS,PRINCIPLES & PRACTICES
- Foley van dam
- Feiner hughes
WEB REFERENCES:
http://jerome.jouvie.free.fr/OpenGl/Lessons/Lesson3.php
http://google.com
http://opengl.org
28 Dept. of Computer Science & Engineering.