0% found this document useful (0 votes)
22 views26 pages

OpenGL Setup for Computer Graphics Lab

Uploaded by

Nardos Tesema
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views26 pages

OpenGL Setup for Computer Graphics Lab

Uploaded by

Nardos Tesema
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Computer Graphics

Lab 1
Open GL Configuration
1. Install DevC++
2. Download graphics package (graphics.h,winbgim.h …)
3. Copy "graphics.h" and "winbgim.h" files to "include" folder
4. Copy "libbgi.a" to file to "lib" folder
5. Copy “[Link]” to c:\windows\system and system32 folder
6. Copy “glut.h” to file to “C:\Program Files (x86)\Dev-Cpp\MinGW32\include\
GL” folder
7. Copy “libglut32.a” to file to “C:\Program Files (x86)\Dev-Cpp\MinGW32\lib”
folder
8. Go to tools->compile option and add “ -lglu32 -lglut32 -lopengl32” on
compile option
9. Go to tools->compile option and add “-lbgi -lgdi32 -lcomdlg32 -luuid -
loleaut32 -lole32” on linker option
First Example
#include <graphics.h>
main(){
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode,"");
}
Initializing C++ Graphics Mode:
• The computer display system must be initialized into graphics mode
before calling the graphics function.

• The “initgraph” function is used to initialize the display into graphics


mode.
• This function is a part of the “graphics.h” header file.
• initgraph(&driver, &mode, “path”);
Driver:
• Represents the graphics driver installed on the computer.
• It may be an integer variable or an integer constant identifier,
• e.g. CGA, EGA, SVGA, etc.
• The-graphics driver can also be automatically detected by using the
keyword “DETECT”.
int driver, mode;
driver = DETECT;
Mode
• Represents output resolution on the computer screen.
• The normal mode for VGA is VGAHI. It gives the highest resolution
• If the driver is on auto-detected, then its use is optional.
• The computer automatically detects the driver as well as the mode.
• & represents the addresses of constant numerical identifiers of driver
and mode.
• If constants (e.g., VGA, VGAHI) are used, then “&” operator is not
used as shown below:
• initgraph (VGA, VGAHI, “path”);
path
• Represents the path of graphic drivers.
• It is the directory on the system where the BGI files are located.
Suppose the BGI files are stored in “C:\TC\BGI”, then the complete
path is written as:
• initgraph (VGA, VGAHI, “C:\TC\\BGI”);
• If the BGI files are in the current directory, then the path is written as:
• initgraph (VGA, VGAHI, “ ”);
Second example (dda line drawing)
dx = abs(x2-x1);
#include <iostream> dy = abs(y2-y1);
if(dx>=dy) length = dx;
#include <graphics.h>
else length = dy;
#include <math.h> dx = dx/length;
dy = dy/length;
using namespace std; x=x1;
main(){ y=y1;
i=1;
float x,y,dx,dy; while(i<=length){
int x1,y1,x2,y2,i,length; putpixel(x,y,RED);
x = x + dx;
int gdriver = DETECT, gmode; y = y + dy;
initgraph(&gdriver,&gmode,""); i++;
}
cout<<"Enter co-ordinates of point 1:"; getch();
cin>>x1>>y1; closegraph();
}
cout<<"Enter co-ordinates of point 2: ";
Second example output
putpixel()
The header file graphics.h contains putpixel() function which plots a pixel at
location (x, y) of specified color.

Syntax :
void putpixel(int x, int y, int color);
where, (x, y) is the location at which pixel is to be
put , and color specifies the color of the pixel.
Example 3 Bresenham’s Line
while(x<x1) int main()
#include<iostream> { {
if(p>=0) int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
#include<graphics.h> { initgraph(&gdriver, &gmode, "");
using namespace std; putpixel(x,y,7); cout<<"Enter co-ordinates of first point: ";
void drawline(int x0, int y0, int x1, int y1) y=y+1; cin>>x0>>y0;
p=p+2*dy-2*dx; cout<<"Enter co-ordinates of second point: ";
{ } cin>>x1>>y1;
int dx, dy, p, x, y; else drawline(x0, y0, x1, y1);
{ getch();
dx=x1-x0;
putpixel(x,y,7); return 0;
dy=y1-y0; p=p+2*dy; }
x=x0; }
x=x+1;
y=y0; }
p=2*dy-dx; }
Bresenham’s Line output
Example 4
// C++ Implementation for drawing line
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
// line for x1, y1, x2, y2
line(150, 150, 450, 150);
// line for x1, y1, x2, y2
line(150, 200, 450, 200);
// line for x1, y1, x2, y2
line(150, 250, 450, 250);
getch();
closegraph();
}
void line();
• Declaration : void line(int x1, int y1, int x2, int y2);
• line function is used to draw a line from a point(x1,y1) to point(x2,y2)
i.e. (x1,y1) and (x2,y2) are end points of the line.
Some graphics functiones
Example 5
#include<graphics.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
setcolor(5);
rectangle(60,80,150,200); // rectangle(int left, int top, int right, int bottom);
rectangle(95,140,120,200);
line(60,80,100,15);
line(100,15,150,80);
circle(100,60,10); //circle(x, y, radius);
getch();
closegraph();
}
Output
Exercise 1: write c++ code to display the
following output
Exercise 2
What is the output of the following code
#include<graphics.h>
main() line(150,185,150,300);
{ line(150,200,120,230);
int gd=DETECT,gm; line(150,200,180,230);
initgraph(&gd,&gm,""); line(150,300,120,330);
setcolor(9); line(150,300,180,330);
circle(150,150,35); outtextxy(230,350,"Computer
circle(130,150,5); Graphics Lab Example");
circle(170,150,5); getch();
}
line(150,160,150,140);
line(125,140,150,140);
line(150,160,175,160);
Exercise 3
• Draw Ethiopian National Flag
Example
#include<iostream>
setfillstyle(SOLID_FILL, WHITE);
#include<graphics.h>
rectangle(50,60,150,40);
main() floodfill(100,50,WHITE);
{
setfillstyle(SOLID_FILL,GREEN);
int gd=DETECT,gm; rectangle(50,40,150,20);
detectgraph(&gd,&gm); floodfill(100,30,WHITE);

line(150,60,150,200);
initgraph(&gd,&gm,""); getch();
closegraph();
setfillstyle(SOLID_FILL,RED); }
rectangle(50,80,150,60);
floodfill(100,70,WHITE);
Rectangle ,Bar and Circle
#include<graphics.h>
setfillstyle(SOLID_FILL,GREEN);
main()
bar(50,40,150,20);
{
int gd=DETECT,gm; setfillstyle(SOLID_FILL,WHITE);
detectgraph(&gd,&gm); bar(50,18,55,200);
getch();
initgraph(&gd,&gm,"");
closegraph();
setfillstyle(SOLID_FILL,RED); }
bar(50,80,150,60);
setfillstyle(SOLID_FILL,WHITE);
bar(50,60,150,40);
floodfill(252, 158, 15); floodfill(252, 158,
WHITE);
Lab Assignment

Common questions

Powered by AI

The 'getch()' function, primarily used in console applications, pauses the program until a key is pressed, facilitating the observation of output in graphics programming by preventing immediate closure of the output window. It is crucial for allowing users to view the graphical output until they're ready to exit, especially in applications that open a graphical window that would otherwise close immediately upon completion of execution .

Bresenham’s line algorithm optimizes the line drawing process by using integer arithmetic instead of floating-point calculations, which makes it more efficient and faster than the DDA algorithm. By calculating a decision variable 'p' that determines if the next pixel should be above or below the previously drawn one, the algorithm avoids using divisions or multiplications repeatedly, focusing on additions and subtractions. Thus, it is preferred for its performance, especially on systems with limited computational power .

In the 'initgraph' function, the 'driver' parameter specifies the graphics driver to be used, which can be an integer variable, constant, or 'DETECT' for auto-detection. The 'mode' parameter defines the resolution or mode of the graphics display, like VGAHI for high resolution. The 'path' parameter indicates where the necessary BGI files for graphics operation are located, allowing the system to access these files correctly. These parameters collectively help in setting up the graphics environment for rendering graphical elements .

To set up an OpenGL environment in DevC++, first install DevC++. Then download the graphics package, which includes files such as "graphics.h" and "winbgim.h", and place these in the "include" folder. Copy "libbgi.a" to the "lib" folder. Place "glut32.dll" in both the c:\windows\system and system32 folders. Copy "glut.h" to the "C:\Program Files (x86)\Dev-Cpp\MinGW32\include\GL" folder and "libglut32.a" to the "C:\Program Files (x86)\Dev-Cpp\MinGW32\lib" folder. Adjust the compiler options by adding "-lglu32 -lglut32 -lopengl32" and the linker options with "-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32" .

Both 'rectangle()' and 'bar()' functions are used to draw shapes on the graphics screen in C++. The 'rectangle()' function draws an outline of a rectangle, defined by its top-left and bottom-right corners. In contrast, the 'bar()' function draws a filled rectangle, creating a solid shape between the top-left and bottom-right points. The primary difference is in the additional filling feature of the 'bar()' function, which utilizes 'setfillstyle()' to specify the fill pattern and color. This makes 'bar()' useful for drawing solid blocks, whereas 'rectangle()' is used for outlines .

'setfillstyle()' in C++ graphics programming sets the pattern and color used for filling shapes. It is used with functions like 'bar()' to fill rectangles with patterns (such as solid fill) and colors specified by its parameters. In conjunction with 'bar()', it defines how the filled rectangle's interior appears. While 'rectangle()' does not inherently use 'setfillstyle()', if used with 'floodfill()', it can also fill a 'rectangle' with the desired styles inside its boundaries .

The Ethiopian National Flag can be drawn using rectangles and filled with the colors green, yellow, and red using 'setfillstyle()' and 'rectangle()'. Starting at specific coordinates to ensure correct positions, each color band is drawn by changing the fill style and using 'fillfill()' to apply color. The correct color order, band dimensions, and fill method must be considered to ensure an accurate representation, respecting proportions and the arrangement of colors, typically green on top, followed by yellow, and then red as seen in traditional flag layouts .

The DDA (Digital Differential Analyzer) line drawing algorithm calculates intermediate points using floating-point arithmetic to draw lines between two points. The process involves calculating the differences in the x and y coordinates (dx, dy), determining the larger of the two as the length of the line, and incrementing x and y by a factor of dx/length and dy/length, respectively. The primary advantage of DDA over algorithms like Bresenham's is its simplicity and efficiency for lines with a gentle slope, as it produces smooth and as accurate lines, although it may struggle with performance due to floating-point calculations .

The 'initgraph()' function initializes the display system into graphics mode, which is necessary before calling any graphics functions in C++. It is a part of "graphics.h" and requires three parameters: a pointer to the graphics driver, a pointer to the graphics mode, and the path to the graphics drivers. The function can automatically detect the best graphics driver using the keyword 'DETECT'. The mode can represent different resolutions, such as VGAHI, which provides the highest resolution for VGA .

The 'putpixel()' function in C++ is used to draw a single pixel at a specified (x, y) coordinate in a defined color on the screen. It is part of "graphics.h" with the syntax 'void putpixel(int x, int y, int color);'. Here, (x, y) specifies the position of the pixel, and 'color' determines the pixel's color. The function is essential for implementing detailed graphics work, such as drawing lines or shapes pixel by pixel .

You might also like