Home > Articles > Programming
- How To Draw Triangle In Dev C 2017
- How To Draw Triangle In Dev C 4
- How To Draw Triangle In Dev C Online
Jan 17, 2018 Draw Rectangle in C graphics rectangle is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. Left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y.
␡- Graphics Essentials
Page 1 of 6Next >
Learn how to use C++ code to create basic animated graphics in this chapter from Beginning Game Programming by Michael Morrison.
This chapter is from the book Beginning Game Programming
This chapter is from the book
This chapter is from the book
NOTE
1979 was a critical milestone in the evolution of video games because it brought us Asteroids, which is one of the most enduring games ever. Created by Atari, Asteroids was one of the first vector graphics games, which means that it relied solely on lines to draw graphics, as opposed to little square pixels. Asteroids demonstrated that a very simple concept, with even simpler graphics and a soundtrack that gradually builds tension, creates a mix for success. In fact, Asteroids was so successful that arcade operators had to build larger coin boxes for the games—not a bad problem to have!
A computer game consists of many different pieces, all of which must come together to form a unique entertainment experience for the player. By far, the most important pieces of any game are the graphics. Graphics are used to represent the characters and creatures in a game, as well as background worlds and other interesting objects that factor into the overall game design. Granted, games have certainly done well because of factors outside of graphics, such as game play and sound quality, but those games are very rare. Besides, nowadays game players expect to see high-quality graphics just as we all expect to see high-quality visual effects in Hollywood movies. So, it's important to develop a solid understanding of graphics programming and how to use graphics wisely in your games.
In this chapter, you'll learn
- The basics of drawing graphics using the Windows Graphics Device Interface
- What a device context is and why it's important to GDI graphics
- How to paint text and primitive graphics in Windows
- How to create a sample program that demonstrates GDI graphics in the context of the game engine
Graphics Essentials
Before jumping into the details of how graphics work in Windows and how they are applied to games, it's important to establish some ground rules and gain an understanding of how computer graphics work in general. More specifically, you need to have a solid grasp on what a graphics coordinate system is, as well as how color is represented in computer graphics. The next couple of sections provide you with this knowledge, which you'll put to practical use a little later in the chapter.
Understanding the Graphics Coordinate System
All graphical computing systems use some sort of graphics coordinate system to specify how points are arranged in a window or on the screen. Graphics coordinate systems typically spell out the origin (0, 0) of the system, as well as the axes and directions of increasing value for each of the axes. If you're not a big math person, this simply means that a coordinate system describes how to pinpoint any location on the screen as an XY value. The traditional mathematical coordinate system familiar to most of us is shown in Figure 3.1.
Figure 3.1 The traditional XY coordinate system is commonly used in math.
Windows graphics relies on a similar coordinate system to specify how and where drawing operations take place. Because all drawing in Windows takes place within the confines of a window, the Windows coordinate system is applied relative to a particular window. The Windows coordinate system has an origin that is located in the upper-left corner of the window, with positive X values increasing to the right and positive Y values increasing down. All values in the Windows coordinate system are positive integers. Figure 3.2 shows how this coordinate system looks.
Figure 3.2 The Windows XY coordinate system is similar to the traditional coordinate system except that it applies to the client area of windows.
NOTE
When I talk about drawing graphics in a window, I'm actually referring to the client area of a window, which doesn't include the title bar, menus, scrollbars, and so on. In the case of games, you can think of the client area of the main game window as the game screen. You learn more about the client area of a window later in this chapter in the section titled, 'Painting Windows.'
If the Windows graphics coordinate system sounds a little complicated, just think of it in terms of a classic game of Battleship. In Battleship, you try to sink enemy ships by firing torpedoes at specific locations on a grid. Battleship uses its own coordinate system to allow you to specify locations on the grid where ships might be located. Similarly, when you draw graphics in Windows, you specify locations in the client area of a window, which is really just a grid of little squares called pixels.
Learning the Basics of Color
A topic that impacts almost every area of game graphics is color. Fortunately, most computer systems take a similar approach to representing color. The main function of color in a computer system is to accurately reflect the physical nature of color within the confines of a computer. This physical nature isn't hard to figure out; anyone who has experienced the joy of Play-Doh can tell you that colors react in different ways when they are combined with each other. Like Play-Doh, a computer color system needs to be capable of mixing colors with accurate, predictable results.
Color computer monitors provide possibly the most useful insight into how software systems implement color. A color monitor has three electron guns: red, green, and blue. The output from these three guns converges on each pixel on the screen, stimulating phosphors to produce the appropriate color. The combined intensities of each gun determine the resulting pixel color. This convergence of different colors from the monitor guns is very similar to the convergence of different colored Play-Doh.
NOTE
Technically speaking, the result of combining colors on a monitor is different from that of combining similarly colored Play-Doh. The reason for this is that color combinations on a monitor are additive, meaning that mixed colors are added together to become white; Play-Doh color combinations are subtractive, meaning that mixed colors are subtracted from each other to become black. Whether the color combination is additive or subtractive depends on the physical properties of the particular medium involved.
The Windows color system is very similar to the physical system used by color monitors; it forms unique colors by using varying intensities of the colors red, green, and blue. Therefore, Windows colors are represented by the combination of the numeric intensities of the primary colors (red, green, and blue). This color system is known as RGB (Red Green Blue) and is standard across most graphical computer systems.
NOTE
RGB isn't the only color system used by computers. Another color system used heavily in desktop publishing applications is CMYK, which stands for Cyan-Magenta-Yellow-Black. Colors in the CMYK color system are expressed in terms of the color components cyan, magenta, yellow, and black, as opposed to red, green, and blue in RGB. The CMYK color system is used heavily in printing because CMYK printing inks are subtractive in nature, making it easier to print using a four-color ink combination (cyan, magenta, yellow, and black); hence the term four-color printing.
Table 3.1 shows the numeric values for the red, green, and blue components of some basic colors. Notice that the intensities of each color component range from 0 to 255 in value.
Table 3.1 Numeric RGB Color Component Values for Commonly Used Colors
Color | Red | Green | Blue |
White | 255 | 255 | 255 |
Black | 0 | 0 | 0 |
Light Gray | 192 | 192 | 192 |
Medium Gray | 128 | 128 | 128 |
Dark Gray | 64 | 64 | 64 |
Red | 255 | 0 | 0 |
Nord lead vst mac download. Green | 0 | 255 | 0 |
Blue | 0 | 0 | 255 |
Yellow | 255 | 255 | 0 |
Purple | 255 | 0 | 255 |
The Win32 API defines a structure named COLORREF that combines the red, green, and blue components of an RGB color into a single value. The COLORREF structure is important because it is used throughout the Win32 API to represent RGB colors. To create a color as a COLORREF structure, you use the RGB() macro, which accepts red, green, and blue color components as arguments. Here is an example of creating a solid green color using RGB():
The color created in this line of code is green because the green component (the middle argument) is specified as 255, whereas the red and blue components are specified as 0. Changing the values of these three arguments alters the mix of the color—with lower numbers resulting in darker colors and higher numbers resulting in brighter colors.
You can experiment with RGB color combinations in the standard Paint program that comes with Windows. Double-click one of the colors in the color palette in the lower left corner of the Paint window. Then, click the Define Custom Colors button in the Edit Colors dialog box. You can then either type numbers into the Red, Green, and Blue edit fields or click to select a color and intensity (see Figure 3.3).
Figure 3.3 The standard Windows Paint program allows you to specify colors via RGB values.
Related Resources
- Book $35.99
- Book $39.99
- Book $27.99
- Shaders
This will be another long tutorial. Precision auto tune killeen tx phone number.
OpenGL 3 makes it easy to write complicated stuff, but at the expense that drawing a simple triangle is actually quite difficult.
Don’t forget to cut’n paste the code on a regular basis.
If the program crashes at startup, you’re probably running from the wrong directory. Read CAREFULLY the first tutorial and the FAQ on how to configure Visual Studio !
I won’t dig into details now, but you need to create a Vertex Array Object and set it as the current one :
Do this once your window is created (= after the OpenGL Context creation) and before any other OpenGL call.
If you really want to know more about VAOs, there are a few other tutorials out there, but this is not very important.
A triangle is defined by three points. When talking about “points” in 3D graphics, we usually use the word “vertex” ( “vertices” on the plural ). A vertex has 3 coordinates : X, Y and Z. You can think about these three coordinates in the following way :
- X in on your right
- Y is up
- Z is towards your back (yes, behind, not in front of you)
But here is a better way to visualize this : use the Right Hand Rule
- X is your thumb
- Y is your index
- Z is your middle finger. If you put your thumb to the right and your index to the sky, it will point to your back, too.
Having the Z in this direction is weird, so why is it so ? Short answer : because 100 years of Right Hand Rule Math will give you lots of useful tools. The only downside is an unintuitive Z.
On a side note, notice that you can move your hand freely : your X, Y and Z will be moving, too. More on this later.
So we need three 3D points in order to make a triangle ; let’s go :
The first vertex is (-1,-1,0). This means that unless we transform it in some way, it will be displayed at (-1,-1) on the screen. What does this mean ? The screen origin is in the middle, X is on the right, as usual, and Y is up. This is what it gives on a wide screen :
This is something you can’t change, it’s built in your graphics card. So (-1,-1) is the bottom left corner of your screen. (1,-1) is the bottom right, and (0,1) is the middle top. So this triangle should take most of the screen.
The next step is to give this triangle to OpenGL. We do this by creating a buffer:
This needs to be done only once.
Now, in our main loop, where we used to draw “nothing”, we can draw our magnificent triangle :
If you’re lucky, you can see the result in white. (Don’t panic if you don’t some systems require a shader to show anything) :
Now this is some boring white. Let’s see how we can improve it by painting it in red. This is done by using something called shaders.
Shader Compilation
In the simplest possible configuration, you will need two shaders : one called Vertex Shader, which will be executed for each vertex, and one called Fragment Shader, which will be executed for each sample. And since we use 4x antialising, we have 4 samples in each pixel.
Shaders are programmed in a language called GLSL : GL Shader Language, which is part of OpenGL. Unlike C or Java, GLSL has to be compiled at run time, which means that each and every time you launch your application, all your shaders are recompiled.
The two shaders are usually in separate files. In this example, we have SimpleFragmentShader.fragmentshader and SimpleVertexShader.vertexshader . The extension is irrelevant, it could be .txt or .glsl .
So here’s the code. It’s not very important to fully understand it, since you often do this only once in a program, so comments should be enough. Since this function will be used by all other tutorials, it is placed in a separate file : common/loadShader.cpp . Notice that just as buffers, shaders are not directly accessible : we just have an ID. The actual implementation is hidden inside the driver.
Our Vertex Shader
Let’s write our vertex shader first.The first line tells the compiler that we will use OpenGL 3’s syntax.
The second line declares the input data :
Let’s explain this line in detail :
How To Draw Triangle In Dev C 2017
- “vec3” is a vector of 3 components in GLSL. It is similar (but different) to the glm::vec3 we used to declare our triangle. The important thing is that if we use 3 components in C++, we use 3 components in GLSL too.
- “layout(location = 0)” refers to the buffer we use to feed the vertexPosition_modelspace attribute. Each vertex can have numerous attributes : A position, one or several colours, one or several texture coordinates, lots of other things. OpenGL doesn’t know what a colour is : it just sees a vec3. So we have to tell him which buffer corresponds to which input. We do that by setting the layout to the same value as the first parameter to glVertexAttribPointer. The value “0” is not important, it could be 12 (but no more than glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &v) ), the important thing is that it’s the same number on both sides.
- “vertexPosition_modelspace” could have any other name. It will contain the position of the vertex for each run of the vertex shader.
- “in” means that this is some input data. Soon we’ll see the “out” keyword.
The function that is called for each vertex is called main, just as in C :
Auto tune evo vst c sharp. Our main function will merely set the vertex’ position to whatever was in the buffer. So if we gave (1,1), the triangle would have one of its vertices at the top right corner of the screen. We’ll see in the next tutorial how to do some more interesting computations on the input position.
How To Draw Triangle In Dev C 4
gl_Position is one of the few built-in variables : you *have *to assign some value to it. Everything else is optional; we’ll see what “everything else” means in Tutorial 4.
Our Fragment Shader
How To Draw Triangle In Dev C Online
For our first fragment shader, we will do something really simple : set the color of each fragment to red. (Remember, there are 4 fragment in a pixel because we use 4x AA)
So yeah, vec3(1,0,0) means red. This is because on computer screens, colour is represented by a Red, Green, and Blue triplet, in this order. So (1,0,0) means Full Red, no green and no blue.
Import our LoadShaders function as the last include:
Before the main loop, call our LoadShaders function:
Now inside the main loop, first clear the screen. This will change the background color to dark blue because of the previous glClearColor(0.0f, 0.0f, 0.4f, 0.0f) call:
and then tell OpenGL that you want to use your shader:
… and presto, here’s your red triangle !
In the next tutorial we’ll learn transformations : How to setup your camera, move your objects, etc.