Monday, 28 March 2016

Computer Graphics Hands-On Lab: Mastering Circle Drawing Algorithms in C++

Computer Graphics Hands-On Lab-2: Mastering Circle Drawing Algorithms in C++
By: Dr. Zeeshan Bhatti 

Focus Keyword: Circle Drawing Algorithms

Master the art of drawing perfect circles in computer graphics! This lab project by Dr. Zeeshan Bhatti guides you through Polynomial and Bresenham's Circle Algorithms in C++, with 7 practical tasks to build your skills.

Bresenham Circle Drawing Algorithm in Computer Graphics | Circle Drawing using Algorithm 🎮07 Part-1

Computer Graphics Hands-On Lab: Mastering Line Drawing Algorithms in C++ - DDA vs. Bresenham

Computer Graphics Hands-On Lab-1 : Mastering Line Drawing Algorithms in C++ - DDA vs. Bresenham

Focus Keyword: Line Drawing Algorithms

Instructor: Dr. Zeeshan Bhatti

Polynomial Method Line Drawing in Computer Graphics | How to Draw Line using DDA algorithm in CG 🎮04

Lab Objectives

Welcome to your first computer graphics lab! This foundational session introduces you to the core problem of raster graphics: how to draw a straight line on a pixel-based display. By the end of this lab, you will:

Sunday, 27 March 2016

Computer Graphics using Java: Java Graphics Programming Lab: Mastering 2D Shapes and Geometric Primitives

Computer Graphics using Java: Java Graphics Programming: Mastering 2D Shapes and Geometric Primitives using JAva 

By: Dr. Zeeshan Bhatti

Basics of Computer Graphics | What is Computer Graphics | Computer Graphics full Course Urdu  🎮02

The Java 2D™ API provides several classes that define common geometric objects such as points, lines, curves, and rectangles. These geometry classes are part of the java.awt.geom package.

Saturday, 19 March 2016

Autodesk Maya Tutorial: Mastering Set Driven Key for Advanced Rigging

Autodesk Maya Tutorial: Mastering Set Driven Key for Advanced Rigging


Welcome back, animators and riggers! I'm Prof. Dr. Zeeshan Bhatti, and today on Zeeshan Academy, we're diving into one of the most powerful and versatile tools in the Maya rigging artist's toolkit: Set Driven Key (SDK).

If you've ever wanted to create complex, intuitive relationships between attributes—like having a character's brow furrow automatically when they raise their eyelids, or building a clean control panel for facial expressions—then this tutorial is for you. We'll not only learn how to use SDKs to build a simple facial rig but also how to create a custom GUI to control it all, using the very MEL scripts you provided.

What is a Set Driven Key?

Forget the traditional concept of keyframing animation over time. A Set Driven Key establishes a relationship where the value of one attribute (the "Driver") directly controls the value of another attribute (the "Driven").

Think of it like a gear system:

  • Driver: The gear you turn.

  • Driven: The gear that turns as a result.

In our case, we'll create a slider that drives the rotation of a character's jaw bone. This allows for non-linear, customizable control that is essential for sophisticated character rigs.

Part 1: Building a Jaw Rig with Set Driven Key

Let's start with a practical example: opening and closing a character's mouth.

Step 1: Create the Control and the Geometry

  1. Create a simple polygon cube. Scale it to resemble a head. This is our "Head_Geo".

  2. Create a NURBS circle (Create > NURBS Primitives > Circle). This will be our control. Name it "Jaw_CTRL".

  3. Place the Jaw_CTRL in front of the face. Freeze its transformations (Modify > Freeze Transformations).

Step 2: Create the Jaw Joint

  1. Switch to the Rigging menu set.

  2. Create a joint (Skeleton > Create Joints). Click once for the root (at the pivot of the head) and again near the bottom of the "mouth" for the jaw tip. Press Enter. Name it "Jaw_Joint".

  3. Parent the Jaw_Joint to the Jaw_CTRL (Select Jaw_Joint, then Shift-select Jaw_CTRL, press p).

Step 3: Skin the Geometry

  1. Select the Jaw_Joint, then Shift-select the Head_Geo.

  2. Go to Skin > Bind Skin > Smooth Bind. Now, when you rotate the joint, the geometry should deform.

Step 4: The Magic - Setting the Driven Key
Now, we'll make the control's translate Y drive the jaw's rotation.

  1. Open the Set Driven Key window: Animate > Set Driven Key > Set...

  2. Load the Driver:

    • Select the Jaw_CTRL.

    • In the SDK window, click Load Driver.

    • On the right, you'll see its attributes. Select translateY.

  3. Load the Driven:

    • Select the Jaw_Joint.

    • In the SDK window, click Load Driven.

    • On the right, select the rotateX attribute (assuming this is the axis that opens the mouth).

  4. Set the First Key (Mouth Closed):

    • Ensure the Jaw_CTRL is at its original position (translateY = 0).

    • Ensure the Jaw_Joint is not rotated (rotateX = 0). This is the closed mouth state.

    • In the SDK window, click Key. You've set the first relationship: translateY 0 drives rotateX 0.

  5. Set the Second Key (Mouth Open):

    • Move the Jaw_CTRL upward on the Y-axis (e.g., translateY = 5).

    • Rotate the Jaw_Joint on the X-axis to open the mouth (e.g., rotateX = 45).

    • Click Key again.

Step 5: Test Your Rig!
Now, slowly move the Jaw_CTRL up and down. The jaw should open and close smoothly. You've just created your first Set Driven Key rig!

Part 2: Building a Custom Control GUI with MEL

Manipulating controls in the viewport is great, but for a clean, professional rig, a custom GUI is unbeatable. Let's use your scripts to build one.

Step 1: Create the Main Window
We'll start by creating the window and its basic layout.

mel
// Create the main window for our facial rig
window -title "Zeeshan Academy - Facial Rig" -widthHeight 300 400;
// Define a simple column layout to stack our controls
columnLayout -adjustableColumn true;

// Add a title text
text -label "Facial Control Panel" -font "boldLabelFont";

Step 2: Add Control Sliders
Let's add a slider for our jaw control. The attrFieldSliderGrp command is perfect as it gives us a slider, a field, and a label.

mel
// Create a slider for the Jaw control
attrFieldSliderGrp -min 0 -max 10
                   -columnWidth3 20 70 100
                   -attribute "Jaw_CTRL.translateY"
                   -label "Jaw Open"
                   jawSlider;
  • -min/-max: Defines the slider's range.

  • -columnWidth3: Sets the width for the number, slider, and field.

  • -attribute: This directly links the GUI element to our control's attribute in the scene! This is the most powerful part.

Step 3: Add Functional Buttons
Now, let's add buttons to select controls quickly.

mel
// Add some space
text -label "";

// A regular button to select the jaw control
button -width 130 -height 25 -label "Select Jaw CTRL" -annotation "Selects the Jaw Control" -command "select -r Jaw_CTRL";

// A symbol button for a hand control (using a placeholder image)
symbolButton -image "sphere.xpm" -command "select -r Hand_CTRL" -annotation "Select Hand CTRL";
  • The -command flag runs a MEL command when pressed. Here, it's a simple select command.

Step 4: Add a Checkbox for System Switching
For more complex rigs (like IK/FK switching), a checkbox is ideal.

mel
// Add more space
text -label "";

// A checkbox for an IK/FK switch
checkBox -label "IK / FK Switch" -width 130 -height 19 -annotation "Switch between IK and FK controls"
         -onCommand "setAttr IK_FK_CTRL.switch 1" // Command when checked
         -ofc "setAttr IK_FK_CTRL.switch 0";      // Command when unchecked

Step 5: Show the Window
Finally, we need to display the window we've built.

mel
// Show the completed window
showWindow;

Putting It All Together: Your Final MEL Script

Here is the complete script you can paste into the Maya Script Editor (make sure to be in the MEL tab) and execute.

mel
// Check if window exists and delete it to avoid errors
if (`window -exists facialRigWindow`) deleteUI facialRigWindow;

// Create the main window for our facial rig
window -title "Zeeshan Academy - Facial Rig" -widthHeight 300 400 facialRigWindow;
// Define a simple column layout to stack our controls
columnLayout -adjustableColumn true;

// Add a title text
text -label "Facial Control Panel" -font "boldLabelFont";

// Create a slider for the Jaw control
attrFieldSliderGrp -min 0 -max 10 -columnWidth3 20 70 100 -at "Jaw_CTRL.translateY" -l "Jaw Open" jawSlider;

// Add some space
text -label "";

// A regular button to select the jaw control
button -w 130 -h 25 -label "Select Jaw CTRL" -ann "Selects the Jaw Control" -c "select -r Jaw_CTRL";

// A symbol button for a hand control
symbolButton -image "sphere.xpm" -c "select -r Hand_CTRL" -ann "Select Hand CTRL";

// Add more space
text -label "";

// A checkbox for an IK/FK switch
checkBox -label "IK / FK Switch" -w 130 -h 19 -ann "Switch between IK and FK controls" -onc "setAttr IK_FK_CTRL.switch 1" -ofc "setAttr IK_FK_CTRL.switch 0";

// Show the completed window
showWindow;

Conclusion: You Are Now a Driver Master!

Congratulations! You've not only learned how to create intuitive rigging relationships with Set Driven Key but also how to build a professional-looking control panel using MEL. This combination is the foundation of creating robust, user-friendly character rigs that animators will love.

The principles you've learned here can be expanded to rig entire faces—where one slider could control the corners of the mouth, the cheeks, and even create nose wrinkles all at once.

For a visual walkthrough of this entire process, don't forget to check out the detailed video tutorial on the Zeeshan Academy YouTube channel!

Now, go forth and drive some keys!

Prof. Dr. Zeeshan Bhatti
Zeeshan Academy - https://www.youtube.com/@ZeeshanAcademy

Following are few quick scipts to create MEL GUI components:


create a window
 window;

  // define the layout of controls added 
  // to the window.
  columnLayout;

   // create some text
   text -label "hello world";

 // show the window we last created
 showWindow;

Slider:
attrFieldSliderGrp -min -10 -max 10 -cw 1 20 -cw 2 1 -cw 3 70
                              -at RightLeg_CTRL.AnkelRotate
                              -l aud AFD1;

Button:
button -w 70 -h 20 -label "Select All" -ann "Select all Controls" -c selAll

Symbol Button

// create window
window;
columnLayout;
   
  // create three symbol buttons with related mel command
  symbolButton -image "circle.xpm" -command "circle";
  symbolButton -image "sphere.xpm" -command "sphere";
  symbolButton -image "cube.xpm" -command "polyCube";
showWindow;


Checkbox:
checkBox -label "IK_FK" -w 30 -h 19 -ann "Switch IK FK control"
                 -onc "l_showIKFK(\"1\")"
                 -ofc "l_showIKFK(\"0\")"
                 ik_fk_switch;

example:
// create a checkbox
$c = `checkBox -label "thingy" -onCommand "on_func" -offCommand "off_func"`;
// to get the current value of the checkBox, use the -query flag
$value = `checkBox -query -value $c`;
print("check_box value = "+ $value +"\n");


Button with IMAGE:
iconTextButton -style "iconOnly" -mw 0 -mh 0 -ann "Select Hand CTRL"
                          -image1 "bbutton.bmp" -w 20 -h 20
                          -command "SelectHandCTRL(\"Left\")" 
                           selHand_Ctrl;

               

Friday, 18 March 2016

Computer Graphcis using C/C++ - Lab Handout


Computer Graphics using C/C++ -  Lab Handout 

Computer Graphics (ITEC-613 & SENG-613)

BS(IT) P-IV & BS(SW) P-IV First Semester 2016

Lab Handout: 1

By: Dr. Zeeshan Bhatti

Turbo C/C++ has a good collection of graphics libraries. If you know the basics of C/C++, you can easily learn graphics programming. To start programming, let us write a small program that displays a circle on the screen.

Simple Graphics Program 1:
/* simple.c
*/
#include<graphics.h>
#include<conio.h>

void main()
{
int gd=DETECT, gm;
clrscr();
initgraph(&gd, &gm, "c:\\tc\\bgi " );
circle(200,100,150);

getch();
closegraph();
}

To run this program, you need graphics.h header file, graphics.lib library file and Graphics driver (BGI file) in the program folder. These files are part of Turbo C package. In all our programs we used 640x480 VGA monitor. So all the programs are according to that specification. For VGA monitor, graphics driver used is EGAVGA.BGI.

Here, initgraph() function initializes the graphics mode and clears the screen.

initgraph function:


Initializes the graphics system.

Declaration:
initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);

Remarks: To start the graphics system, you must first call initgraph.

initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver) then putting the system into graphics mode.

initgraph also resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.
Arguments:

*graphdriver: Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics drivers enumeration type.

*graphmode : Integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver. You can give *graphmode a value using a constant of the graphics_modes enumeration type.

pathtodriver : Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first. If they're not there, initgraph looks in the current directory. If pathtodriver is null, the driver files must be in the current directory. This is also the path settextstyle searches for the stroked character font files (*.CHR).

closegraph() function:

closegraph() function switches back the screen from graphcs mode to text mode. It clears the screen also. A graphics program should have a closegraph function at the end of graphics. Otherwise DOS screen will not go to text mode after running the program. Here, closegraph() is called after getch() since screen should not clear until user hits a key.

If you have the BGI file in the same folder of your program, you can just leave it as "" only. you need not mention *graphmode if you give *graphdriver as DETECT.

To get details of different graphics modes and graphics drivers, view appendix.

In graphics mode, all the screen co-ordinates are mentioned in terms of pixels. Number of pixels in the screen decides resolution of the screen. In the example 1.0, circle is drawn with x-coordinate of the center 200, y-coordinate 100 and radius 150 pixels. All the coordinates are mentioned with respect to top-left corner of the screen.

Basic Shapes and Colors:


Now let us write a program to draw some basic shapes.
Simple Graphics Program 2:

/*
shapes.c
*/

#include<graphics.h>
#include<conio.h>

void main()
{
int gd=DETECT, gm;
int poly[12]={350,450, 350,410, 430,400, 350,350, 300,430, 350,450 };
initgraph(&gd, &gm, "");
circle(100,100,50);
outtextxy(75,170, "Circle");
rectangle(200,50,350,150);
outtextxy(240, 170, "Rectangle");
ellipse(500, 100,0,360, 100,50);
outtextxy(480, 170, "Ellipse");
line(100,250,540,250);
outtextxy(300,260,"Line");

sector(150, 400, 30, 300, 100,50);
outtextxy(120, 460, "Sector");
drawpoly(6, poly);
outtextxy(340, 460, "Polygon");
getch();
closegraph();
}


Figure 1: Here is the screenshot of output.


Here, circle() function takes x, y coordinates of the circle with respect to left top of the screen and radius of the circle in terms of pixels as arguments. Not that, in graphics, almost all the screen parameters are measured in terms of pixels.

Function outtextxy() displays a string in graphical mode. You can use different fonts, text sizes, alignments, colors and directions of the text that we will study later. Parameters passed are x and y coordinates of the position on the screen where text is to be displayed. There is another function outtext() that displayes a text in the current position. Current position is the place where last drawing is ended. These functions are declared as follows:

void far outtextxy(int x, int y, char *text);
void far outtext(char *text);

Another basic shape that we come across is a rectangle. To draw a border, use rectangle with the coordinates of outline, to draw a square use rectangle with same height and width. drawpoly() and fillpoly() are two functions useful to draw any polygons. To use these functions, store coordinates of the shape in an array and pass the address of array as an argument to the function. By looking at the output of the previous program, you can understand what drawpoly is. fillpoly is similar except that it fills in the shape with current fill color.

Declaration:

void far rectangle(int x1, int y1, int x2, int y2);

void far drawpoly(int numpoints, int far *polypoints);

void far fillpoly(int numpoints, int far *polypoints);

Remarks:

rectangle draws a rectangle in the current line style, thickness, and drawing color.

drawpoly draws a polygon using the current line style and color.

fillpoly draws the outline of a polygon using the current line style and color, then fills the polygon using the current fill pattern and fill color.

Arguments:

(x1,y1) is the upper left corner of the rectangle, and (x2,y2) is its lower right corner.

numpoints: Specifies number of points

*polypoints: Points to a sequence of (numpoints x 2) integers. Each pair of integers gives the x and y coordinates of a point on the polygon.

To draw a closed polygon with N points, numpoints should be N+1 and the array polypoints[] should contain 2(N+1) integers with first 2 integers equal to last 2 integers.

Colors :

Here is some idea about colors. There are 16 colors declared in graphics.h as listed bellow.

CPP ObjectsCPP Basic ShapesBLACK: 0
BLUE: 1
GREEN: 2
CYAN: 3
RED: 4
MAGENTA: 5
BROWN: 6
LIGHTGRAY: 7
DARKGRAY: 8
LIGHTBLUE: 9
LIGHTGREEN: 10
LIGHTCYAN: 11
LIGHTRED: 12
LIGHTMAGENTA: 13
YELLOW: 14
WHITE: 15

To use these colors, use functions setcolor(), setbkcolor() and setfillstyle(). setcolor() function sets the current drawing color. If we use setcolor(RED); and draw any shape, line or text after that, the drawing will be in red color. You can either use color as defined above or number like setcolor(4);. setbkcolor() sets background color for drawing. Setfillstyle sets fill pattern and fill colors. After calling setfillstyle, if we use functions like floodfill, fillpoly, bar etc, shpes will be filled with fill color and pattern set using setfillstyle. These function declarations are as follows

Declaration:
void far setfillstyle(int pattern, int color);
void far setcolor(int color);
void far setbkcolor(int color);

Remarks:
setfillstyle sets the current fill pattern and fill color.
setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor.
setbkcolor sets the background to the color specified by color.

The parameter pattern in setfillstyle is as follows:
Names
Value
Means Fill With...
EMPTY_FILL
0
Background color
SOLID_FILL
1
Solid fill
LINE_FILL
2
---
LTSLASH_FILL
3
///
SLASH_FILL
4
///, thick lines
BKSLASH_FILL
5
\\\, thick lines
LTBKSLASH_FILL
6
\\\
HATCH_FILL
7
Light hatch
XHATCH_FILL
8
Heavy crosshatch
INTERLEAVE_FILL
9
Interleaving lines
WIDE_DOT_FILL
10
Widely spaced dots
CLOSE_DOT_FILL
11
Closely spaced dots
USER_FILL
12
User-defin


Here is an example program with colors, pixels, bar,
Simple Graphics Program 3:

/*
random.c
some graphics effects using random numbers.
*/

#include "graphics.h"
#include "conio.h"
#include "stdlib.h"

void main()
{
int gd,gm;
gd=DETECT;

initgraph(&gd, &gm, "");
setcolor(3);
setfillstyle(SOLID_FILL,RED);
bar(50, 50, 590, 430);
setfillstyle(1, 14);
bar(100, 100, 540, 380);

while(!kbhit())
{
putpixel(random(439)+101, random(279)+101,random(16));
setcolor(random(16));
circle(320,240,random(100));
}
getch();
closegraph();
}

Summary List of Graphics Functions

  1. rectangle(x1, y1, x2 , y2);
  2. line(x1, y1, x2 , y2);
  3. bar (x1, y1, x2 , y2);
  4. 3dbar(x1, y1, x2 , y2 , depth, topFlag);
  5. circle(x, y, radius);
  6. putpixel(x, y);
  7. ellipse(x, y, start, end, xRadius, yRadius);
  8. arc(x1, y1, x2, y2, radius);
  9. outtext(“Text”);
  10. outtextxy(x, y, “Text”);
  11. settextstyle(fontStyle, Direction, CharSize); // Direction = 0 or 1
ie: settextstyle(1,0,7);
  1. setfillstyle(pattern, color);
  2. floodfill(x, y, boarderColor);
  3. setbkcolor(colorNo); // Cgange the background color of the dos window
  4. setColor(colorNo); // set the color of the text or objects
  5. setlinestyle(type, pattern, thickness); // type = 1 – 5, thickness= 1-3
  6. kbhit(); // keyboard hit: any key pressed from the keyboard, like getche();

Featured post

🐍 PYTHON DEVS RAGE: Can Vibe Coding Replace Your Next 100 Lines of Code? (Full Tutorial)

  🐍 PYTHON DEVS RAGE: Can Vibe Coding Replace Your Next 100 Lines of Code? (Full Tutorial) Replace boilerplate Python code effortlessly ...