Professional Documents
Culture Documents
Chapter 1
Chapter 1
Chapter 1
INTRODUCTION
1.1 Introduction to Computer Graphics
Computer graphics started with the display of data on hardcopy plotters and Cathode Ray Tube (CRT)
screens soon after the Introduction of computers themselves. It has grown to include the Creation,
Storage and Manipulation of Models and Images of objects. These models come from a diverse and
expanding set of fields, and include physical, mathematical, engineering, architectural and even
conceptual structures, natural phenomenon and so on. Computer graphics today is largely interactive:
the user controls the contents, structure, and appearance of objects and of their displayed images by
using input devices, such as a keyboard, mouse, or touch sensitive panel on the screen. Because of the
close relationship between the input devices and the display, the handling of such devices is included in
the study of computer graphics.
1.3 ADVANTAGES
Scientific visualization
Information visualization
Computer vision
Image processing
Computational geometry
Computational topology
Applied mathematics
#include<GL/glut.h>
OR
#include<GLUT/glut.h>
GLUT is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL programs.
It implements a simple windowing application programming interface (API) for OpenGL. GLUT makes it
considerably easier to learn about and explore OpenGL programming. GLUT provides a portable API so
you can write a single OpenGL program that works across all PC and workstation OS platforms.
GLUT is designed for constructing small to medium sized OpenGL programs. While GLUT is well-suited to
learning OpenGL and developing simple OpenGL applications, GLUT is not a full-featured toolkit so large
applications requiring sophisticated user interfaces are better off using native window system toolkits.
GLUT is simple, easy, and small.
The GLUT library has C, C++ (same as C), FORTRAN, and ADA programming bindings. The GLUT source
code distribution is portable to nearly all OpenGL implementations and platforms. The current version is
3.7. Additional releases of the library are not anticipated. GLUT is not open source.
Chapter 2
LITERATURE SURVEY
The basic functions like glColor3f(…); glRotatef(..), glTranslatef(..) etc that are most commonly used in
the code are taken from the prescribed VTU Text book “INTERACTIVE COMPUTER GRAPHICS” 5th edition
by Edward Angel.[1].
The lab programs in the syllabus also serve as a basic template for creating a project. The usage of colors
and specifications are taken from the various programs that were taught in the lab.[1].
The VTU prescribed text book serves as a huge database of functions and they are used in the project.
CHAPTER 3
REQUIREMENTS AND SPECIFICATIONS
System Requirements
System requirements are intended to communicate in precise way, the functions that the system must
provide. To reduce ambiguity, they may be written in a structured form of natural language
supplemented by tables and system models.
CHAPTER 4
IMPLEMENTATION
4.1 FUNCTIONS USED
Void glColor3f(float red, float green, float blue); This function is used to mention the color in which the
pixel should appear. The number 3 specifies the number of arguments that the function would take. ‘f ’
gives the type that is float. The arguments are in the order RGB(Red, Green, Blue). The color of the pixel
can be specified as the combination of these 3 primary colors.
Void glClearColor(int red, int green, int blue, int alpha); This function is used to clear the color of the
screen. The 4 values that are passed as arguments for this function are (RED, GREEN, BLUE, ALPHA)
where the red green and blue components are taken to set the background color and alpha is a value
that specifies depth of the window. It is used for 3D images.
Void glutKeyboardFunc(); void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y)); where
func is the new keyboard callback function. glutKeyboardFunc sets the keyboard callback for the current
window. When a user types into the window, each key press generating an ASCII character will generate
a keyboard callback. The key callback parameter is the generated ASCII character. The x and y callback
parameters indicate the mouse location in window relative coordinates when the key was pressed.
When a new window is created, no keyboard callback is initially registered, and ASCII key strokes in the
window are ignored. Passing NULL to glutKeyboardFunc disables the generation of keyboard callbacks.
Void glFlush(); Different GL implementations buffer commands in several different locations, including
network buffers and the graphics accelerator itself. glFlush empties all of these buffers, causing all
issued commands to be executed as quickly as they are accepted by the actual rendering engine. Though
this execution may not be completed in any particular time period, it does complete in finite time. Void
glMatrixMode(GLenum mode); where mode Specifies which matrix stack is the target for subsequent
matrix operations. Three values are accepted: GL_MODELVIEW, GL_PROJECTION, and GL_TEXTURE. The
initial value is GL_MODELVIEW.
glMatrixMode sets the current matrix mode. Mode can assume one of three values:
GL_MODELVIEW Applies subsequent matrix operations to the model view matrix stack.
GL_PROJECTION applies subsequent matrix operations to the projection matrix stack.
void glViewport(GLint x, GLint y, GLsizei width, GLsizei height) where x, y Specify the lower left corner
of the viewport rectangle, in pixels. The initial value is (0, 0). width, height Specify the width and height
of the viewport. When a GL context is first attached to a surface (e.g. window), width and height are set
to the dimensions of that surface. glViewport specifies the affine transformation of x and y from
normalized device coordinates to window coordinates. Let (xnd, ynd) be normalized device coordinates.
Then the window coordinates (xw, yw) are computed as follows:
xw = ( xnd + 1 ) width/2 + x
yw = ( ynd + 1 ) height/2 + y
Viewport width and height are silently clamped to a range that depends on the implementation. To
query this range, call glGetInteger with argument GL_MAX_VIEWPORT_DIMS.
void glutInit(int *argcp, char **argv); glutInit will initialize the GLUT library and negotiate a session with
the window system. During this process, glutInit may cause the termination of the GLUT program with
an error message to the user if GLUT cannot be properly initialized. Examples of this situation include
the failure to connect to the window system, the lack of window system support for OpenGL, and invalid
command line options. glutInit also processes command line options, but the specific options parse are
window system dependent.
void glutReshapeFunc(void (*func)(int width, int height)); glutReshapeFunc sets the reshape callback
for the current window. The reshape callback is triggered when a window is reshaped. A reshape
callback is also triggered immediately before a window's first display callback after a window is created
or whenever an overlay for the window is established. The width and height parameters of the callback
specify the new window size in pixels. Before the callback, the current window is set to the window that
has been reshaped. If a reshape callback is not registered for a window or NULL is passed to
glutReshapeFunc (to deregister a previously registered callback), the default reshape callback is used.
This default callback will simply.
void glutMainLoop(void); glutMainLoop enters the GLUT event processing loop. This routine should be
called at most once in a GLUT program. Once called, this routine will never .
glutPostRedisplay(): glutPostRedisplay, glutPostWindowRedisplay — marks the current or specified
window as needing to be redisplayed.
4.2 ALGORITHM
int minimax( char _board[9] )
{
short int i;
int bestValue = +INFINITY, index = 0;
char bestMoves[9] = {0};
for( i = 0; i < 9; i++ )
{
if( _board[i] == empty )
{
_board[i] = o;
int value = maxSearch( _board );
if( value < bestValue )
{
bestValue = value; index = 0;
bestMoves[index] = i;
}
else if( value == bestValue )
bestMoves[index++] = i;
_board[i] = empty;
}
Chapter 5
5.1 Objectives
Our project name is Tic-Tac-Toe game. This game is very popular and is fairly simple by itself. It is
actually a two player game. In this game, there is a board with n x n squares. In our game, it is 3 x 3
squares. The goal of Tic-Tac-Toe is to be one of the players to get three same symbols in a row -
horizontally, vertically or diagonally - on a 3 x 3 grid.
5.2 Overview
This game can be played in a 3x3 grid (shown in the figure 5.1) .
The game can be played by two players.
There are two options for players:
Figure 5.1
5.2.1 Players:
For the option human, both the players are human and for the option computer, the first player is
human and the second player is computer.
A player marks any of the 3x3 squares with his symbol (may be “X” or “O”) and his aim is to create a
straight line horizontally or vertically or diagonally with two intensions:
a) Create a straight line before his opponent to win the game.
b) Restrict his opponent from creating a straight line first.
In case logically no one can create a straight line with his own symbol, the game results a tie. Hence
there are only three possible results – a player wins, his opponent (human or computer) wins or it’s a
tie.
Figure 5.2
If any player is able to draw three Xs or three Os in the following combinations then that player wins.
The combinations are:
a) 1, 2, 3 b) 4, 5, 6
c) 7, 8, 9 d) 1, 4, 7
e) 2, 5, 8 f) 3, 6, 9
h) 1, 5, 9 i) 3, 5, 7
If any situation arises like the above figure then the computer sets its symbol any one of the position
among 2, 4, 6 and 8.
If any situation arises like the above figure’s then the computer sets its symbol at any position among 4
and 6.
If any situation arises like the above figure’s then the computer sets its symbol at any position among 2
and 8.
If any situation arises like the above figure’s then the computer sets its symbol at any position among 1,
3, 7 and 9.
5.2.7 Classes:
There are two classes in our program .One class is Main.java and another is NewGame.java.
RESULTS
Snapshots :
Snap shot showing instructions before starting :
Methods:
The methods we used in our program are as follows:
private int checkRow (int rownum, String rowname): To check the combination whether any two
symbols(X or O) are same for winning or blocking .
private int checkColumn(int columnnum, String columnname): To check the combination whether any
two symbols(X or O) are same for winning or blocking .
private Boolean checkCorner(String cornername, int count1): To check the combination whether any
two symbols(X or O) are same for winning or blocking.
private void setValue(int number, int position): To set the symbols in the right place for winning and
blocking .
private void setCorner(): To create scope for computer to win in the third move.
Limitations:
1. GUI is not so attractive.
2. Only mouse interface is implemented, keyboard is not activated in the game.
Code / Appendix
#include <GL/glut.h> // glut (gl utility toolkit) basic windows functions, keyboard, mouse.
#include <string.h>
// lighting
int abc = 0;
// player or computer; 1 = X, -1 = O
//
// 0 | 1 | 2
// 3 | 4 | 5
// 6 | 7 | 8
//
static int box[8][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
int box_map[9];
int object_map[9][2] = {{-6, 6}, {0, 6}, {6, 6}, {-6, 0}, {0, 0}, {6, 0}, {-6, -6}, {0, -6}, {6, -6}}; // quadric pointer for build our X
GLUquadricObj *Cylinder;
void init_game(void)
int i;
box_map[i] = 0;
win = 0;
start_game = 1;
int check_move(void)
int i, t = 0;
if ((t == 3) || (t == -3))
spinboxes = i;
return (1);
t = 0;
if (t == 24)
return (2);
return (0);
int blocking_win(void)
int i, t;
if ((t == 2) || (t == -2))
// Find empty
if (box_map[box[i][0]] == 0)
box_map[box[i][0]] = computer;
if (box_map[box[i][1]] == 0)
box_map[box[i][1]] = computer;
if (box_map[box[i][2]] == 0)
box_map[box[i][2]] = computer;
return (1);
return (0);
if (blocking_win() == 1)
return (1);
if (check_row() == 1)
return (1);
if (check_corner() == 1)
return (1);
return (0);
int l, i;
int l, i;
void init(void)
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
start_game = 0;
win = 0;
Cylinder = gluNewQuadric();
gluQuadricDrawStyle(Cylinder, GLU_FILL);
gluQuadricNormals(Cylinder, GLU_SMOOTH);
gluQuadricOrientation(Cylinder, GLU_OUTSIDE);
glPushMatrix();
glTranslatef(x, y, z);
glRotatef(a, 1, 0, 0);
glPopMatrix();
glPushMatrix();
glPopMatrix();
glPushMatrix();
glRotatef(a, 1, 0, 0);
glRotatef(90, 0, 1, 0);
glRotatef(315, 1, 0, 0);
glTranslatef(0, 0, -2);
glPopMatrix();
Sprint(-7, 2, "Overview");
SPrint(-7, 1, "Tic Tac Toe is a two-player game where the first player to");
SPrint(-7, 0, "connect a line of pieces from one side or corner of the board to the other wins.");
SPrint(-7, -5, "3.A player wins by being the first to connect a line of friendly");
SPrint(-7, -6, " pieces from one side or corner of the board to the other.");
SPrint(-7, -8, " for a player to win (in which case the result is a draw).");
glutSwapBuffers();
else if (abc == 0)
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHTING);
j = 0;
if (abs(win) == 1)
switch (key)
case 27:
break;
default:
break;
if (start_game == 0)
player = 1;
computer = -1;
init_game();
computer_move();
return;
player = -1;
computer = 1;
init_game();
return;
if (start_game == 1)
if (win == 0)
if (box_map[object_select] == 0)
box_map[object_select] = player;
win = check_move();
if (win == 1)
start_game = 0;
return;
computer_move();
win = check_move();
if (win == 1)
win = -1;
start_game = 0;
if (win == 2)
start_game = 0;
switch (choice)
case 1:
abc = 1;
glutMouseFunc(mouse);
break;
case 2:
abc = 3;
glutMouseFunc(mouse);
break;
exit(0);
break;
// Main program
glutInit(&argc, argv);
glutInitWindowSize(850, 600);
glutInitWindowPosition(10, 10);
glutCreateWindow(argv[0]);
init();
glutCreateMenu(menu);
glutAddMenuEntry("Help", 2);
glutAddMenuEntry("Quit", 3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
//glutMouseFunc(mouse);
glutMainLoop();
return 0;
A player can play perfect tic-tac-toe (win or draw) given they move according to the highest possible
moves:
1. Win: If the player has two in a row, play the third to get three in a row.
2. Block: If the opponent has two in a row, play the third to block them.
3. Fork: Create an opportunity where you can win in two ways.
4. Block opponent's fork:
Option 1: Create two in a row to force the opponent into defending, as long as it doesn't result
in them creating a fork or winning. For example, if "X" has a corner, "O" has the center, and "X"
has the opposite corner as well, "O" must not play a corner in order to win. (Playing a corner in
this scenario creates a fork for "X" to win.)
Option 2: If there is a configuration where the opponent can fork, block that fork. 5. Center:
Play the center. 6. Opposite corner: If the opponent is in the corner, play the opposite corner. 7.
Empty corner: Play in a corner square. 8. Empty side: Play in a middle square on any of the 4
sides.
BIBLIOGRAPHY
1. Reference from Interactive Computer Graphics by EDWARD ANGEL
2. Internet source
www.angelfire.com
Wikipedia.org.
Google.
Opengl.org.