How To Draw A Point In Opengl
Lab 2: Points, Primitives and 2D Art
Highlights of this lab:
This lab is an introduction to Fundamental OpenGL Functions
- Lab 1 Follow-upwardly
- Bones OpenGL Rendering Functions
- Setting Upwardly a Shader Plan
- Vertices
- Points
- Lines
- Triangles
- Specifying Colours
- Setting Upwardly a Shader Plan
- Setting Upwardly 2D Rendering
- Immigration the window
- The Camera
Assignment:
After the lab lecture, you lot have one week to:
- Use the Glut framework and Core profile commands to create your own 2D picture using OpenGL primitives
- Upload your results to URCourses
On-line OpenGL Manual
- This link goes to the OpenGL 3.3 transmission, only y'all should be able to use information technology without modification in virtually cases.
- Provided for your convenience. Use this manual to await up OpenGL functions in the transmission when you use them.
- Sometimes the notes will provide a link to the human page for an OpenGL office when it is first introduced, simply you lot will not meet the navigation certificate frame.
Seminar Notes
Earlier you begin this seminar, create a new projection like you did in the first lab, but don't add together the two shader files or change annihilation in the basic Glut plan. Y'all will complete the project by post-obit the instructions in this lab's notes instead.
A. Lab ane Follow-up
OpenGL is an operating organization and hardware platform independent graphics library designed to exist hands portable yet rapidly executable. Unlike Direct3D, which is just available on PC, Xbox 360 and Windows Mobile Os, OpenGL is bachelor on a wide diversity of hardware platforms and operating systems including Unix/X11 (Linux, Irix, BSD, Solaris), Mac OS 10, and Microsoft Windows 98 to Windows eight. The embedded version, OpenGL ES, is available on many hand held devices including iPhone OS and Android OS devices. A Javascript version of OpenGL ES 2.0 called WebGL is an official part of the HTML 5 specification.
The basic steps to employ OpenGL in a program are:
- Get a Device Context (DC) for the rendering location
- Select and set a pixel format (feature set) for the DC
- Create a Rendering Context (RC) associated with the DC
- Load and compile shaders
- Load geometry into buffers
- Connect buffers to the shaders
- Draw the geometry as needed
- Release the rendering context
- Release the device context
What are DCs and RCs?
- A DC is just a information structure that keeps the state of the electric current settings and routes calls to the appropriate device.
- An RC is a data construction that keeps the country variables for OpenGL. It is besides a portal through which OpenGL calls are sent to the device.
- You can think of both DC and RC every bit a data structure that keeps the state of current OpenGL settings and routes calls to the proper device.
What is a pixel format?
- Pixel formats define the translation between OpenGL calls (such as an OpenGL phone call to depict a pixel with an RGB triad value of [128,120,135]) and the operation that the rendering system actually performs (the pixel might exist drawn with a translated RGB value of [128,128,128]). They are as well a clarification of all the hardware features you desire OpenGL to provide to your plan. The selected pixel format describes such things as how colours are displayed, the depth of field resolution, and what additional capabilities are supported by the rendering context you accept created.
- Click hither for a more involved discussion of pixel formats in Windows.
- Click here for details on DCs (Renderers) and pixel formats (Buffer Attributes) in Mac OS 10
B. OpenGL Rendering Functions
Your graphics hardware has limited ability to represent geometry. Virtually hardware merely understands triangle rendering primitives. Everything else is built upwardly using triangles. Older versions of OpenGL included some other shapes that were supported by some specialized hardware, such as convex polygons and quadrilaterals, but that support was removed in Cadre profile. Below is a diagram showing OpenGLs dissimilar primitive types or modes:
Drawing with 1 of these types is controlled past a glDrawArrays function. The glDrawArrays function tells your rendering context to brainstorm drawing a certain number of elements from a list of vertex data that has already been loaded into an array buffer object and connected to appropriate shader inputs. And then, to exist able to draw you will need to know how to load vertex information into a buffer, and how to attach information technology to a shader. Eventually you will also learn how to quickly switch between buffers of drawable objects with Vertex Array Objects (VAOs)
Setting Up a Shader Programme
Before you can do whatever cartoon you demand to tell OpenGL what to do with the things you tell information technology to describe. You practise this with shader programs. OpenGL 3.ii Core Profile uses GLSL one.5 every bit its shader programming language. Shader programs consist of a minimum of two parts: a vertex shader and a fragment shader.
You may also accept heard of two other shader types: geometry shaders and tesselation shaders. Geometry shaders were introduced in OpenGL 3.2 Core Profile and tesselation shaders were introduced in OpenGL 4.0. They are optional and will non be covered in these labs.
Vertex Shader
You will ship lists of vertex information into a vertex shader. This information comes in through variables labelled with the in modifier. This information represents attributes of the vertex that can change from ane vertex to the next such as colour and position. Vertex shader inputs are as well known equally attributes.
When we are done manipulating and creating shader properties, nosotros laissez passer the results along to the fragment shader through outputs labelled with the out modifier. If the vertex shader outputs are different for the vertices in the same primitive, they will be interpolated across the archaic - their values will vary. Vertex shader inputs are also known equally varyings.
Below is our first vertex shader: Add a new file called vshader.glsl to your project and paste in the vertex shader code:
vshader.glsl
#version 150in vec2 vPosition; //receives incoming vertex positions
out vec4 colour; //passes the colour of this vertex to the fragment shadervoid main()
{ //Add default z and w coordinates to the incoming position and laissez passer it on.
gl_Position = vec4(vPosition, 0.0, 1.0);//Colour every vertex ruby-red
color = vec4(1.0, 0.0, 0.0, 1.0); //colour channes are red, green, blueish and blastoff
}
This vertex shader only has 1 input which represents a second coordinate for the vertex. This coordinate is in a two component vector which has a base type of float, a 32-fleck IEEE floating point value. Vertices can be moved around in infinite, coloured, and lit past the vertex shader. You will explore many of these things later. For now, our vertex programme will only provide a colour for the vertex. This color is hard coded and volition exist the aforementioned for all vertices. You will learn how to change this color programmatically later on on.
Our start vertex shader has two outputs as well. You can see the announcement for a 4 component vector, vec4, for colour, and we use the built-in output gl_Position, which is also a vec4, which is why the shader adds two more components to vPosition when we assign it to gl_Position.
Fragment Shader
The fragment shader gets data that is blended from each vertex that makes upwards the primitive being drawn. This could be a position somewhere betwixt each vertex, a texture lookup coordinate, or a composite colour. For now our shader will ignore gl_FragCoord, the fragment position, and simply re-create the incoming colour to the screen.
Add a new file called fshader.glsl to your project and paste in the vertex shader that matches your computer's capabilities:
fshader.glsl
#version 150in vec4 colour; //The composite fragment colour from the vertex shader.
//Proper name must match an output in the vertex shader.
out vec4 fragColour; //Define a proper noun for the colour outputvoid principal()
{
fragColour = colour;
}
This fragment has one input for the interpolated colour. It is important that names for the inputs you create in a fragment shader friction match the name of an output you create in the vertex shader.
There is no built-in output for fragment colour in GLSL one.5, then we create one ourselves. There used to be one called gl_FragColour in GLSL one.two. The proper noun chosen here reflects the old proper noun, merely information technology could be anything.
Loading, Compiling and Using the Shader Programme
Compiling shaders is an involved procedure. You accept to get the shader lawmaking as null terminated C-strings, compile the individual pieces correctly, and so link them. You are besides responsible for checking for compile and link errors and reporting them. Dr. Angel has provided a role that does all this for you. I strongly suggest that you use information technology. His function is called InitShader(). You should call InitShader from within your init function, and use the result as the active shader like this:// Load and compile shaders, then use the resulting shader program
GLuint program = InitShader( "vshader.glsl", "fshader.glsl" );
glUseProgram( plan );
Vertices
Defining Vertex Data
Basic OpenGL rendering primitives are made up of lists of vertices. Vertex data can be two, 3 of four dimensional. An extra dimension is sometimes necessary to properly move vertices around in space. Vertex data is near often represented with the vec2, vec3, and vec4 data types in the shader. These are ii, iii and 4 component floating point structures. You lot should represent this data with arrays of the GLfloat data type in your plan. For example, the following ii dimensional array gives the 2nd coordinates for the iii vertices in a triangle:
//Triangle positions
GLfloat points[3][ii] =
{
{ 0.9f, 0.9f},
{ 0.9f, 0.0f},
{ 0.0f, 0.9f}
};
The number of coordinates provided per vertex should friction match the vec blazon specified on the position input of the shader you lot are using.
Dr. Angel'south OpenGL framework defines C++ classes for the vec2, vec3 and vec4 information types. The following code is identical to to the array above, but uses the vec2 grade:
Make this assortment global
//Triangle positions
vec2 points[] =
{
vec2( 0.9f, 0.9f),
vec2( 0.9f, 0.0f),
vec2( 0.0f, 0.9f)
};
Yous can employ either form interchangeably, but I prefer to use Dr. Affections's vec* classes because they provide many convenient features that are hard to reproduce with simple arrays.
Loading Vertex Data into Buffers
Once you have some vertex information, yous need to load it into buffers. Each assortment tin can exist loaded into a divide buffer, or all the arrays tin be packed into the same buffer. You volition find examples of both in various code samples in your textbook. For at present, nosotros volition utilise separate buffers for position and colour data.
To create a buffer, you lot employ the glGenBuffers command. glGenBuffers() creates valid buffer names which you must bind to piece of work with and load with buffer information.
void glGenBuffers(GLsizei n, GLuint * buffers);
Where n specifies how many names to generate, and buffers is a reference to enough memory for the n names.
Once you have a buffer name, you lot bind it with glBindBuffer. A buffer is not allocated until you bind it the first time.
void glBindBuffer(GLenum target, GLuint buffer);
Where target indicates what type of information the buffer holds, and buffer is a valid buffer name generated with glGenBuffers().
Yous will apply the target type GL_ARRAY_BUFFER for storing all vertex data in these labs.
With the buffer bound, y'all are ready to load data into it with glBufferData.
void glBufferData(GLenum target, GLsizeiptr size, const GLvoid * information, GLenum usage);
Where:
- target indicates what type of data you are sending (you volition always utilize GL_ARRAY_BUFFER),
- size is the size of the incoming data in bytes,
- data is a pointer to the commencement of your data array,
- usage indicates how the data will be used.
Since you lot will probable utilise your buffers for drawing elementary geometric objects, you lot volition generally specify the GL_STATIC_DRAW usage type. If y'all program to update the buffer ofttimes, you might want to specify GL_DYNAMIC_DRAW. If you plan to use the buffer infrequently you should specify GL_STREAM_DRAW. A buffer's data may exist updated with some other phone call to glBufferData(). If you plan to update simply a portion of a buffer's data, consider using glBufferSubData.
Hither is how we would load our sample triangle position information. Since we will only load the data once, place this code in init afterward the shader loading code:
Identify this code in init after the shader loading lawmaking:
//*** Position buffer **********************
// Create a buffer for vertex positions, make it active, and copy data to information technology
GLuint positionBuffer;
glGenBuffers( ane, &positionBuffer );
glBindBuffer( GL_ARRAY_BUFFER, positionBuffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW );
glBufferData( GL_ARRAY_BUFFER, 3*sizeof(vec2), points, GL_STATIC_DRAW );
Attaching Buffers to Shader Programs
In one case a buffer is loaded with information, information technology must be attached to the right input in your shader programme. To do this, you enquire for the input by name, enable it, then attach your information in the currently jump buffer to the input with a description of how the data is formatted.First, though, you lot need a vertex array object. Vertex array objects, or VAOs, manage the connections betwixt buffers and your shader. If you desire to have multiple sets of buffers each describing separate objects, you tin chop-chop switch betwixt them by using one VAO for each. You configure them with the connections, then just before drawing you bind the 1 that y'all desire and it switches what you will describe.
For at present we will brand just one VAO. Y'all may see that some of the labs use multiple VAOs.
To create a VAO, you lot employ the glGenVertexArrays command. I works a lot similar glGenBuffers
void glGenVertexArrays(GLsizei north, GLuint *arrays);
Where due north specifies how many arrays to generate, and buffers is a reference to enough memory for the north names.
To arrive active and ready to configure or describe with, y'all use glBindVertexArray
void glBindVertexArray(GLuint array);
Where array is a valid assortment name generated with glGenVertexArrays
Here is how you lot would ready your vertex assortment to manage buffer/shader connections. It is important that your vertex assortment announced before the glVertexAttribPointer part. Dr. Angel seems to prefer to identify it before generating buffers, so put it there.
Identify this code in init betwixt the shader loading and buffer loading blocks:
//*** Vertex Array Object ****************** GLuint vao; glGenVertexArrays(i, &vao); glBindVertexArray(vao);
Now nosotros're ready to connect the buffers to the shader inputs.
To get a reference to a shader input you utilise glGetAttribLocation.
GLint glGetAttribLocation(GLuint programme, const GLchar * name);
Where program is a valid, compiled shader program, and proper name is a graphic symbol string containing the name of the desired shader input.
If name does not refer to a valid input in the specified shader plan, the returned effect will be -1.
To enable the shader input you utilise glEnableVertexAttribArray.
void glEnableVertexAttribArray(GLuint index);
Where index is a value returned from glGetAttribLocation.
To attach the currently jump buffer to a shader input you use glVertexAttribPointer.
void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid * arrow);
Where:
- alphabetize is a value returned from glGetAttribLocation.
- size is the number of components being sent per vertex. Must be 1, ii, 3, or 4.
- blazon is the information type of the components. Yous will exist using GL_FLOAT.
- normalized is for fixed signal data and doesn't apply to the type GL_FLOAT. Use GL_FALSE.
- pace indicates the number of bytes betwixt values that utilise to this shader input. This allows u.s. to send interleaved data. If your values are tightly packed, employ 0 equally your argument.
- pointer is the offset of the start relevant component in your buffer. If yous are using separate buffers for each vertex aspect (color, position, texture coordinate, etc), this should be 0.
- Notice that this is a void, or untyped, pointer. OpenGL is a C library that doesn't support polymorphism. A void pointer is a C programming mechanism that is sometimes used to laissez passer information of different types to a function. The type is usually stated by one of the arguments to the function. Here the type is specified with the type statement.
The purpose of the size and type arguments is to describe the data being sent to the shader. If the original information doesn't lucifer what's asked for in the shader, information technology will converted for y'all. In fact, all vertex attributes are converted to size 4. If y or z are missing, they get 0, and if w is missing information technology becomes 1. You can then define an in in the shader of a dissimilar size depending on your demand.
Here is how we will adhere the sample triangle position buffer to the "vPosition" input of the shader:
Place this code in init after the buffer creation code
//Enable the shader'south vertex position input and attach the active buffer
GLuint vPosition = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( vPosition );
glVertexAttribPointer( vPosition, 2, GL_FLOAT, GL_FALSE, 0, 0 );
Finally, to draw things, use glDrawArrays.
To draw the sample triangle place this code in the describe role before the glutSwapBuffers control:
void glDrawArrays(GLenum way, GLint first, GLsizei count);
Where:
- style is what primitive to describe with
- first is what vertex to start at in the array you loaded
- count is how many vertices to draw.
Place this lawmaking in the depict function before the glutSwapBuffers control:
glClear( GL_COLOR_BUFFER_BIT ); glDrawArrays( GL_TRIANGLES, 0, 3 );
If you have done everything to this signal y'all should meet a red triangle in the upper correct corner of an otherwise black rendering window. Now its time to experiment with dissimilar drawing modes.
Points
Only one type of bespeak tin can be fatigued:- GL_POINTS
draws a point for each vertex
Lines
Three different line primitives can be created:
- GL_LINES — draws a line segment for each pair of vertices.
- GL_LINE_STRIP — draws a connected group of line segments from vertex v0 to vn connecting a line between each vertex and the side by side in the gild given.
- GL_LINE_LOOP — similar to GL_LINE_STRIP, except it closes the line from vn to v0, defining a loop.
Some OpenGL implementations let you control the width of lines with glLineWidth(). On most Macs, the range of line widths is 1.0 to 1.0, which is the minimum divers in the standard. You may find that your PC allows more.
Triangles
- Front faces, back faces and rendering modes
- glCullFace() — specify whether forepart- or dorsum-facing triangles should be drawn
- glFrontFace() — specify whether clockwise or counter-clockwise point drawing social club means a triangle is facing front.
- glPolygonMode() — select a polygon rasterization mode.
- Triangle Types
- GL_TRIANGLES — draws a series of split three-sided polygons
- GL_TRIANGLE_STRIP — draws a strip of connected triangles. The commencement iii vertices define a complete triangle. Each subsequent vertex completes a triangle with the previous two.
- GL_TRIANGLE_FAN — draws a strip of triangles continued about a common origin. The outset three vertices define a complete triangle. Each subsequent vertex completes a triangle with the previous and the showtime vertex.
Try this points array with each of the above triangle types:
//TriangleInformation technology may be hard to meet why you become the results you observe. Consider the guild the points are defined and how triangles are divers for each triangle blazon. If you are nevertheless confused, try using glPolygonMode to switch to outline mode rather than fill way.
vec2 points[] =
{
vec2( 0.0f, 0.0f ),
vec2( 0.5f, 0.0f ),
vec2( 0.5f, 0.5f ),
vec2(-0.5f, 0.5f ),
vec2(-i.0f, 0.0f ),
vec2(-0.5f,-0.5f )};
Specifying Colours
So far our shader has used a hard coded colour. You can change this color in a running programme in one of two means: compatible colours, and colour arrays. These are explained below.
All our colours will be in RGBA format - Reddish, Green, Blue, Alpha. Blastoff is an actress term used in blending operations. You can call back of it as "transparency", but information technology can do more that. The alpha channel will be ignored in our programs this week.
Uniform Colours
A compatible is a shader value that has a constant value during a depict operation, but can be changed between draw operations tin exist changed with OpenGL commands.In your shader code, a uniform is declared next to other inputs like this:
compatible type uniformName;//eg: a 4 component color uniform
compatible vec4 uColour; //re-create this to your colour output
Y'all become access to a uniform in much the same way as a vertex array input, but you utilise glGetUniformLocation:
GLint uniformLocation = glGetUniformLocation(shaderProgram, "uniformName");You lot modify the value of a uniform with glUniform*() type functions. The * represents the format of the uniform you are changing and has ii or three parts://eg: get the colour from the example in a higher place for apply in lab sample code
GLint uColour; //Getting uniforms can be boring. Make this global?
uColour = glGetUniformLocation(programme, "uColour");
- the number of components (1, 2 3, or four)
- the data type (f for float, i for integer, ui for unsigned integer)
- (optional) a v to betoken that you lot are passing in an array of values.
glUniform4f( uColour, ane.0f, 1.0f, 0.0f, 1.0f ); //YellowGLfloat yellow[4] = { one.0f, 1.0f, 0.0f, 1.0f }; //Yellow
glUniform4fv( uColour, 1, yellow);
Vertex Colour Arrays
These piece of work only like vertex position arrays. You will demand to ready up a second array input to your vertex shader, create a color assortment, load it into a buffer and attach it to your shader. Here are samples of all three:
The following lawmaking defines an attribute input called vColour. It is similar to the code used for vPosition. Y'all should assign the value in vColour to the colour output:
Add this line to your vertex shader, side by side to the vPosition input, and modify your colour output value appropriately:
in vec4 vColour; // Per vertex color input
Add the advisable triangle colours to chief.cpp virtually to the points array
//for initial triangle
vec4 colours[] =
{
vec4(1.0f, 0.0f, 0.0f, i.0f), //Cherry-red
vec4(0.0f, 1.0f, 0.0f, one.0f), //Green
vec4(0.0f, 0.0f, i.0f, 1.0f), //Bluish
};//for later triangle types case
vec4 colours[] =
{
vec4(1.0f, 0.0f, 0.0f, 1.0f), //Scarlet
vec4(0.0f, 1.0f, 0.0f, 1.0f), //Greenish
vec4(0.0f, 0.0f, 1.0f, 1.0f), //Blue
vec4(1.0f, i.0f, 0.0f, i.0f), //Yellow
vec4(0.0f, 1.0f, one.0f, one.0f), //Cyan
vec4(1.0f, 0.0f, 1.0f, one.0f), //Magenta
};
Then copy the colour information to a buffer, similar this:
Place this below your position buffer code in init
//*** Colour buffer ********************** // Create a buffer for color positions, go far agile, and copy information to it GLuint colourBuffer; glGenBuffers( i, &colourBuffer ); glBindBuffer( GL_ARRAY_BUFFER, colourBuffer ); glBufferData( GL_ARRAY_BUFFER, sizeof(colours), colours, GL_STATIC_DRAW ); //Enable the shader's vertex color input and attach the active buffer GLuint vColour = glGetAttribLocation( program, "vColour" ); glEnableVertexAttribArray( vColour ); glVertexAttribPointer( vColour, 4, GL_FLOAT, GL_FALSE, 0, 0 );
The procedure is very similar to the position buffer set up. I have highlighted the differences in red.
Setting Upwards second Rendering
Clearing the rendering window
The colour buffer and depth buffer are usually cleared each time you begin drawing to the OpenGL window. The values you use to clear with rarely change, and then they are often fix in the initialisation step with the glClearColor and glClearDepth functions:
glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); //articulate colour is blackThe bodily clearing happens just before you draw. In your main describe routine, you specify which buffers to clear with the glClear function:
glClearDepth(ane.0f); //Clear to maximum distance
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
The Camera
In this lab you volition be drawing 2D objects. When you draw in 2d (or you are doing 3D CAD work) you should utilize a special geometry transformation that does not cause shape or size distortion. This transformation is called orthographic projection. In the last lab we wanted a 3D outcome with foreshortening and then nosotros used perspective projection. Perspective transformation makes information technology hard to identify things precisely on the screen. Shapes are distorted toward the edges and corners, and their apparent size varies with their distance from the camera. With orthographic projection you can precisely command how coordinates map to the drawing area, and objects render the aforementioned way regardless of distance.
This week, nosotros will use only simple normalized device coordinates - our cartoon space will lie betwixt (-1,-i) in the lower left corner and (1,1) in the upper right. If you are using 3D coordinates, so -1 is the nearest possible Z coordinate, and 1 is the farthest. Things do not announced smaller with distance. Side by side week, when y'all learn to do perspective projection and other transformations, yous will likewise see Dr. Angel'due south Ortho() or glOrtho2D() functions which give you control over how coordinates are mapped to the window when you don't practice perspective.
Depth testing
In the concluding two sections we've discussed how to articulate the depth buffer, and the default range of depth values. Perhaps y'all'd too like to know how to specify 3D vertices and exercise depth testing.
Without depth testing, objects appear on the screen in the social club you depict them. If you desire to draw something behind another thing you have already drawn, y'all demand to plough on depth testing, supply depth values with your vertex coordinates, and clear the depth buffer each fourth dimension you lot start drawing.
In more than detail:- Articulate the depth buffer along with the colour buffer as described higher up.
- Turn on depth testing with glEnable like this:
Identify this code anywhere in your init:
glEnable(GL_DEPTH_TEST);
- Supply a not-zero depth to the vertex shader by making sure that the vPosition input is a vec3 or vec4. Then brand certain you adjust how it is copied to the gl_Position built-in output.
- Modify your coordinate information arrays to base blazon vec3, then supply a depth, or z, value to each vertex in your data arrays. For example you could specify ii overlapping triangles like this:
//Triangle vec3 points[] = { vec3( 0.0f, 0.0f,-0.5f ), vec3( 0.5f, 0.0f,-0.5f ), vec3( 0.5f, 0.5f,-0.5f ), vec3( 0.0f, 1.0f, 0.0f ), vec3( 0.0f,-i.0f, 0.0f ), vec3( 1.0f, 0.0f, 0.0f ) }; vec4 colours[] = { vec4( 1.0f, 0.0f, 0.0f, 1.0f ), // Triangle 1 is red vec4( 1.0f, 0.0f, 0.0f, i.0f ), vec4( 1.0f, 0.0f, 0.0f, i.0f ), vec4( 0.0f, 1.0f, 1.0f, 1.0f ), // Triangle 2 is cyan vec4( 0.0f, 1.0f, 1.0f, 1.0f ), vec4( 0.0f, 1.0f, 1.0f, 1.0f ) };
- Make certain you are set upwardly to utilise a colour input attribute as discussed earlier.
- Make sure you are drawing the half-dozen points specified.
- Modify the size component of the glVertexAttribPointer phone call for your position buffer to match the points array. Information technology was 2, it should exist 3 now.
If everything works, the cyan triangle in this example appears behind the red, even though it is drawn second. In the default coordinate system, larger z values are farther away. With depth testing off, the cyan triangle would be in forepart of the cherry ane.
Shader Tip
Dr. Affections's shader loader HATES Windows (CR-LF) line endings. Catechumen your shader files to UNIX with Notepad++ or a file transfer program if they just wont work.
In Visual Studio try using Save As, click the arrow next to Save, and select Salvage With Encoding... to observe a save with UNIX line endings option.
Assignment
Goals of this assignment:
Get comfy drawing with vertex buffers and shaders by:- drawing several things with separate draw commands
- using different primitives
- irresolute default line and signal sizes
- changing colours on the fly
For starters:
Information technology is proficient to go a feeling for where you tin can put points on the scene.
The following instructions are meant to get yous started from one of the template projects provided on the lab schedule. Your lab instructor volition probably do a. through c. during the lab demo:
- Become the correct template for the platform you're working on from the lab schedule.
- Create a main.cpp file for it and add the GLUT template code from lab 1.
- Add shader files to the project equally described in the lab ii notes.
- Add code to init as indicated by the following comments:
//Explicitly prepare articulate colour to black or a color you lot like
//Load, compile and use a shader
//Load the elementary triangle position information most the top of the notes into a buffer
//Bind the buffer to your shader's vPosition input
- Confirm that you tin can draw the triangle with the original shader code. Use the beginning glDrawArrays() command found in the notes, and place it in the draw() office as described there.
- Add either a uniform or an array input to your vertex shader to allow yous to change colours.
- Add together these vertices to your points array:
vec2( 0.99, 0.99f),
vec2(-0.99, 0.99f),
vec2(-0.99,-0.99f),
vec2( 0.99,-0.99f),
- Add this glDrawArrays() control to draw the 4 new points:
glDrawArrays(GL_LINE_LOOP, iii, 4); // First at the quaternary vertex, depict four vertices
- Colour the triangle and rectangle with the colours of your choice. Add glUniform*() commands, or gear up up and load a colours array as appropriate to accomplish this task.
- Y'all may remove the glDrawArrays() command that draws the triangle. Please leave the rectangle border in place.
- Consult the marking scheme to see what else to do.
Marker scheme and details of consignment:
- must use GL_POINTS
- must use at least i line type: either GL_LINES, GL_LINE_STRIP, or GL_LINE_LOOP (non including the line loop in the instructions above)
- must use at least one polygon type: either GL_TRIANGES, GL_TRIANGLE_STRIP, or GL_TRIANGLE_FAN (non including whatsoever geometry used as an in-lab demo)
/vi - Draw a flick that contains at least three of the various OpenGL primitives. It should await very different from any in-lab demonstrations.
/3 - Employ at least 3 dissimilar colours. Do this with a uniform shader variable or a vertex colour array input.
/2 - Employ at least 2 point sizes.
/5 - Artistic impression - your drawing should resemble something and reflect some effort.
Samples of previous work. Your piece of work will cease up in the gallery, and then pay attending to artistic impression.
Deliverables
What I want in zipped format:- Your code (main.cpp) + whatsoever supporting files (for instance, if you put your coordinates and colours in your .h file)
- Your shaders (ii files)
- An image of your final product
- BONUS: Add at least one extra useful compatible to your program. You lot can add uniforms to a shader program, like the one shown for irresolute point colour, that accept ane, 2, 3 or 4 arguments. Yous could use such uniforms to adapt the position, sizeor rotation of objects you depict with draw commands. Try something. I like being surprised.
Source: https://www.cs.uregina.ca/Links/class-info/315/WWW/Lab2/
Posted by: martineztrallese.blogspot.com
0 Response to "How To Draw A Point In Opengl"
Post a Comment