Draw a wireframe polygon using bresenham algorithm

Assignment Help Computer Graphics
Reference no: EM13909386

Part I- Description:

In this project, you will implement a program that will do three things:

1. Allows the user to draw a wireframe polygon (i.e., series of lines) using Bresenham's line drawing algorithm.

2. Allows the user to change the current drawing color (for items already drawn and items to be drawn).

3. Draws circles (using the midpoint circle algorithm) automatically at each vertex of the polygon.

For the line-drawing part, here Is what your program should be able to do:

1. When the user first presses and holds down the left mouse button, a line should be drawn from the initial point where the mouse button was pressed to the current location of the mouse. This should be updated dynamically as the user continues to move the mouse and hold down the left mouse button.

2. When the user releases the left mouse button, the line should be drawn on the screen, and the endpoint of the line should stop following the mouse position.

3. If the user presses and holds the left mouse button again, a new line should be drawn from the endpoint of the last line to the current mouse position.

4. If the user right-clicks, a line should be drawn connecting the LAST vertex drawn with the FIRST vertex. Note that NO NEW VERTEX should be created on a right-click.

5. If the user clicks the left mouse button again to start drawing AFTER a right-click, the current polygon should be cleared (as well as any circles drawn), and a new polygon started.

For the color-changing part, the user should be able to hit a key on the keyboard to change to one of 4 color chokes: white, red, green, and blue. (So, "1" = white, "2" = red, etc.)

This should update ALL colors in your color buffer to be the same color; so, if the user hits "red", all lines drawn and all lines to be drawn will draw in red.

For the circle-drawing part, a circle with a radius of 50 pixels should be drawn on each vertex of the polygon (including the one being dragged around by the user.

You must use Bresenham's algorithm to draw lines. This means you will use OpenGL to draw points only (GL_POINTS). Do NOT draw the lines with GL_LINES!

You must deal with all 8 octants for line drawing.

You may use the following code in this project:

-Any of the sample code for this class
-The Bresenham's line drawing code in the Hearn-Baker book (pg. 98-99 in the 3rd edition) as a starting point (you will need to adapt it for all 8 octants)
-The circle midpoint code found in the Hearn-Raker book (pg. 108-109 in the 3rd edition)

However, you may NOT use code from elsewhere (online or offline) for ANY part of this assignment. You also CANNOT share code amongst yourselves. These are INDIVIDUAL projects.

NO CREDIT will be given for the Bresenham code if it only handles 2 octants (i.e., what the Hearn-Baker code handles).

The current line must be updated dynamically as the user moves the mouse while pressing the left mouse button (and again, a circle must be drawn as well).

The program MUST redraw the image while Idle, so you must pass In your drawing function to glutldleFunc as well as glutDisplayFunc.
You must use OpenGL, GLUT, GLEW, and GLM for this project.

You must use OpenGL 3.0+ (so do NOT use the fixed-function pipeline, glElegin()/glEnd(), etc.). That said, just use the simple shaders from the sample code, since you should only have to modify the C++ code.

You will need vertex buffers for pixel positions as well as the color for each pixel (but the color can be set to the same value).

Bonus:

For bonus credit, when the user right-clicks and closes the polygon, instead of drawing full circles at each vertex, only draw the part of the circle segment that goes from one edge to another:

1114_img.png

You want the Image on the right, NOT the left.

In some cases, the circle will loop around the "outside"; this is still acceptable:

1790_img1.png

To do this, you will need to:

1. Loop through each set of three vertices → (i -1), i, (i + 1)

2. Check the angle for the two lines →(0) to (i -1), and (i) to (i + 1)

a. You will probably use the atan2() function, which Is in radians, and returns from -PI to Pl. To make all angles positive: if(angle <0) angle = 2.0*PI + angle.

3. Pick the smaller of the two angles as your "start angle" and the other as your "end angle"

4. When you go to plot each pixel in the midpoint circle algorithm, check whether the vector formed by the pixel coordinates has an angle between the start and end angles

5. If it is, draw that pixel. If not, don't.

Hints:

I would STRONGLY suggest creating a simple struct for holding 3D points:

struct Vec3 {
GLfloat x,y,z;
};

I would also add some constructors in there for ease of use.

I would suggest keeping track of the start and end points for the lines In a C++ vector (e.g., myLines); then, use your Bresenham function to fill ANOTHER vector (e.g., myPixels) with the actual pixels to draw. Finally, before you draw the lines, update the vertex buffer with the pixel data (e.g., use glilufferData() to copy the data from myPixels to the vertex buffer).

Ideally, you should create a struct for your current polygon that contains a vector with the vertices, a vector with the color for each vertex, and finally whether the polygon is "finished" or "not finished" (i.e., closed or not closed).

You should also consider making a struct for circles as well (i.e., center and radius).

Regular Version Screenshot:

896_img2.png

Part II- Description

In this project, you will allow the user to:

-To draw and close a polyline as in Part I; HOWEVER, there will be some key differences:

The line/fill color will be chosen based on which polygon is being drawn:

• First polygon → red
• Second polygon → green
• Third polygon → blue
• ...then repeat color pattern

After the first polygon, the alpha value should be set to 0.5

• You will use the following formula to write to the buffer:
• Dst = Src*Alpha + (1 - Alpha)*Dst

The screen should NOT be cleared after a polygon is closed; you will instead add a new polygon to draw on top of the previous polygons

-Once the polygon is closed (right-click), the polygon should be filled using the general scan-line filling algorithm.
-A new left click should start a new polygon BUT leave the previous polygons alone

You MUST use the "Render to Texture" sample as your starting point:

You will draw lines and fill polygons using "Case 2" from that sample (that is, you will write to your own color buffer and then copy that buffer to a texture).

You do not have to fill the polygon until it is closed.

You MUST meet the following requirements:

-You MUST use the "Render to Texture" sample as your starting point AND you must use "Case 2"

-You MUST use Bresenham's algorithm for lines (do NOT use GL LINES)

-You MUST use the general scan-line filling algorithm (do NOT use GL POLYGON, GL TRIANGLE, GL_TRIANGLE_STRIP,

GL_TRIANGLE_FAN, GL QUADS, or GL_QUAD_STRIP)

-You MUST use OpenGL 3.0+ (do NOT use (g1Begin()/glEnd(), gluPerspective(), glMatrixMode(), etc.); using legacy will give you a grade of ZERO

-You MUST use OpenGL, GLUT, GLEW, and GLM for this project

-You MUST deal with what to when crossing vertices (either shortening the edges or adding vertices)

-You MUST change the color with each new polygon using the pattern described above; color changes should ONLY affect new polygons (old polygons should keep their existing colors)

-You MUST do ALPHA BLENDING for all polygons after the first one! (Alpha of first polygon = 1.0, alpha of all other polygons = 0.5)

-Your code MUST compile and run! Code that does NOT compile will be given a ZERO

You must also include a README (in plain text). This should include:

-Title of project and class: so, "CS 548: Assignment 2"
- Your name and email
-OpenGL version: also include what version your shaders are using (the ttversion part)
-OS and Development environment: For example, Windows 7 and Visual Studio 2013
-Basic description of any controls: If you have any additional controls you have added, mention these.

The README will be graded on clarity, neatness, and content, and it's worth 10% of your grade.

Hints:

NOTE: You will NOT be storing the pixels in vertex and color buffers and then drawing with GL POINTS like last time. This time, you are changing pixel values directly on your own buffer, and then you're copying the buffer to a texture.

I would strongly recommend you have the following structs/classes:

-Vertex3 (points)
- Color4 (color)
- Line (stores two Vertex3)
-Edge (used in sorted edge list and active edge list)
-Polygon (contains list of lines AND the sorted edge list (which will be a list of lists)

If you use C++ stdlib vectors for your lists (which I would recommend), remember you can insert the contents of one vector onto the end of another vector using:

A.insert(A.end(), B.begin(), B.end());

Also, you will have to sort edges by x values. You can use functions from the stdlib to do this. You will have to create a function to do the sort. Let's assume you have an Edge struct with x in it:

int compareEdges(const Edge a, const Edge b){
return (int)(a.x < b.x);
}
...

// To do the sort

sort(A.beginQ, A.end(), compareEdges);

This will sort from the smallest to largest values of x.

Each edge's current x value can be a float (so feel free to just add the inverse slope and use floor() to get the new pixels in the FILLING algorithm).

You can build the sorted edge list for each polygon ONCE and save it in each polygon after it's complete. The active edge list will still need to be built every time the polygon is drawn.

REMEMBER: You should only add/shorten edges if the vertices around it are MONOTONICALLY increasing/decreasing in y!
NOTE: Do NOT add edges to the sorted edge list that are horizontal!

ALSO REMEMBER: You do NOT need a destination alpha channel for this to work! (You only need the source alpha.)

Screenshot:

2080_img3.png

Attachment:- RenderToTexture.zip

Reference no: EM13909386

Questions Cloud

Simple interest on its savings account balances : First City Bank pays 7 percent simple interest on its savings account balances, whereas Second City Bank pays 7 percent interest compounded annually. If you made a $6,000 deposit in each bank, how much more money would you earn from your Second City ..
A jeans maker is designing a new line : A jeans maker is designing a new line of jeans called the Slims.
Discount rate-calculate the net present value and payback : Using a 4.5% discount rate, calculate the Net Present Value, Payback, Profitability Index, and IRR for each of the investment projects below (note, the inflows are for each year). Based on your calculations rank the projects and support you answer.
Calculate value of trade receivables under existing scheme : Calculate the approximate equivalent annual percentage cost of a discount of 2%, which reduces the time taken by credit customers to pay from 70 days to 30 days.
Draw a wireframe polygon using bresenham algorithm : In this project, you will implement a program that will do three things: Allows the user to draw a wireframe polygon (i.e., series of lines) using Bresenham's line drawing algorithm. Allows the user to change the current drawing color (for items alre..
The dividends are expected to grow at a constant rate : The Jackson–Timberlake Wardrobe Co. just paid a dividend of $1.60 per share on its stock. The dividends are expected to grow at a constant rate of 6 percent per year indefinitely. Investors require a return of 10 percent on the company's stock
Firm needs a computerized machine tool lathe : Your firm needs a computerized machine tool lathe which costs $50,000 and requires $12,000 in maintenance for each year of its 3-year life. After three years, this machine will be replaced. The machine falls into the MACRS 3-year class life category...
Implications or consequences and intellectual standards : Using the 8 elements of reasoning that were outlined in week 2 (Purpose, problem, information, concepts, assumptions, inferences, points of view, implications or consequences) choose a news article and break it down according to those elements..You w..
Evaluate result for the case in which x is an exponential rv : Evaluate your result for the case in which X is an exponential rv (you already know what the result should be in this case). Evaluate your result for a case in which E [X]  ∞ and E rX2l = ∞.

Reviews

Write a Review

Computer Graphics Questions & Answers

  Show the matrix and the starting points

you do not need to show matrix math calculations, but you should show the matrix and the starting points and the results for each geometric transformation.

  About color memorization

Color Memorization

  1 identify the 5 elements of strategy for home depot2 do a

1. identify the 5 elements of strategy for home depot.2. do a swot analysis for home depot and provide recommendations

  Question about educational technology

Educational technology helps to create workers's training cheaper, more individual and more effective. Based on your experience, describe a method that exploits

  Make a list of eight shots in picture composition

What are the specific instructions that the director gives to the camera operator to perform each operation Make a list of eight shots in picture composition

  Write program to animate two circles bouncing around window

Write a program to animate two circles bouncing around a window. The basic idea is to start the two circles somewhere in the interior of the window.

  Find the normal to the triangle

Find the normal to the triangle. Assume that the triangle is given clockwise. Normalize the vector. Draw a figure showing that your normal is correct.

  Design a program that demonstrate passing argument

I need to design a program that demonstrate passing arguments ByVal and then ByRef. I just need help with the coding.

  Steps in the student lab manual

In this lab you will determine the shell that you are running, correct mistakes on the command line, and use filename generation. You will also learn about help that the system provides in the form of man pages, the --help option, and the bash hel..

  What type of animation is produced by the line

what type of animation is produced by the line y=mx+c

  Analyze security requirements and develop a security policy

analyze security requirements and develop a security policy that fully addresses them. the project will enable the

  Calculate and plot projection of rectangle on image plane

There is a rectangle with vertices at (-6,-3,6)T, (3,-3,6)T, (3,3,9)T, and (-6,3,9)T. What is the 3x4 perspective camera matrix? Calculate and plot the projection of the rectangle on the image plane.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd