程序代写案例-COMP3170

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
COMP3170 – Week 1 workshop
Discussion questions

Question 1
A simplified graphics pipeline consists of the following steps:
1. Vertex specification
2. Vertex shader
3. Vertex post-processing
4. Primitive assembly
5. Rasterization
6. Fragment Shader
7. Per-Sample operations

Where would the following operations take place in the pipeline:
• Applying textures (i.e. images) to the surfaces of objects.
• Projecting 3D vertices into 2D camera coordinates.
• Clipping parts of the image that don’t need to be drawn.
• Blending transparent pixels with those behind it.
• Modelling an object as a mesh of vertices.
• Shading an object based on how much light is falling on it.
• Converting lines & triangles into pixels.
• Deciding whether or not pixels are visible based on their depth.
• Transforming 3D vertices based on the position and rotation of the model.
• Joining vertices into lines or triangles.


Question 2
The computers in the 127 lab contain NVIDIA GeForce GTX1080 graphics cards with 2560
cores:



Suppose you were to use this to render the following different scenes:
1. A 1920x1080 image of a single blue triangle on a yellow background.
2. A 1024x768 image of a cube textured with an image of the Mona Lisa.
3. An 800x600 image of an untextured 43,000 vertex model of a T-Rex.

Which of these is likely to take the longest to render? What considerations would affect the
result?

Prac Exercise

OpenGL has been described as “an engine for drawing triangles very fast”. As a low-level
graphics API, it contains very little built-in support for achieving particular graphics effects
like lighting, shadows, or even 3D. Instead it is designed to provide an interface to allow you
to program your own effects on the GPU. The disadvantage of this is there is no simple “turn
on lighting” API call. The advantage, however, is that you’re not stuck with one default
lighting model, you can program lights to work however you want, to add extra layers of
realism or to achieve weird effects of your own.

JOGL is the Java API for OpenGL. As it is just a thin wrapper to the C OpenGL libraries, it can
be a fairly unpleasant library to work with. You are not expected to memorise the OpenGL
function calls. Instead, we will provide you with a Java framework to avoid you having to
deal with the more unpleasant parts, and focus on writing interesting graphics effects in
shaders.

We will be using the latest version of JOGL (2.4.0) for this unit. Installing it can be tricky, so
we have set up an Eclipse project containing all the necessary files to run JOGL on either a
Mac or Windows PC.

Task 1: Fork and clone the comp3170-jogl-2.4.0 project from GitHub Classroom

1. Follow this link to the project in GitHub Classroom:
https://classroom.github.com/a/7bZ-ZARe
2. Press the Accept this assignment button.
3. This should automatically create a copy of the repository for you called
https://github.com/MQGames/comp3170-21s1-jogl-240-YOURNAME
4. Open Eclipse and go to the workspace.
5. Select File > Import … from the menu. A dialogue like this should appear:

6. Select Projects from Git (with smart import) and press Next.
7. You will be prompted to select a repository source. Select Clone URI and press Next.
8. Copy the URL of your GitHub repository and paste in the the URI field.
9. Enter your GitHub username and password in the Authentication box.
10. You will be prompted to select a branch. Make sure master is selected and press
Next.
11. You will be prompted for a local destination for your repository. This can be
anywhere you want, but make sure the name of the repo is comp3170-21s1-jogl-240
(i.e. remove the “-YOURNAME” from the end). Press Next.
12. Press Finish.

This should create a new project called comp3170-21s1-jogl-240. If you look inside the
project you should see all the JOGL and JOML libraries:




Task 2: Create a new JOGL project

You can now create new Eclipse projects which refer to the project to import these libraries.
1. In Eclipse select File > New > Java Project.
2. Fill in the Project name and press Finish.
3. You will be prompted whether to create a module-info.java file. Press Don’t Create.
4. Select the newly created project in the Package Explorer and select Project >
Properties from the menu.
5. Select Java Build Path > Projects:

6. Select Classpath and press Add…
7. Select comp3170-21s1-jogl-240 from the list of projects and press OK. This adds the
project to your classpath, so Java knows where to find the JOGL libraries.
8. You are now ready to start programming with JOGL.

Task 3: Check out the demo code
For starters you will experiment with the shader example presented in this week’s lecture.
1. Fork this classroom assignment:
https://classroom.github.com/a/rG5wQskQ
2. Following the same steps as above, clone this repository into an Eclipse project.
3. The project should already be set up to recognise your JOGL project and find the
JOGL libraries (as long as you named it correctly).
4. In the package explorer, find the find Week1.java:

5. Select Run > Run as … > Java Application.
6. If all goes well, you should see a window containing a red circle:



Task 3: Edit the shader.
Have a browse through the code in the project. In particular look at the file fragment.glsl. It
contains the code:

fragment.glsl
#version 410

uniform vec3 u_colour; // colour as a 3D vector (r,g,b)
uniform vec2 u_screenSize; // screen dimensions in pixels

layout(location = 0) out vec4 o_colour; // output to colour
buffer

void main() {
// scale p into range (0,0) to (1,1)
vec2 p = gl_FragCoord.xy / u_screenSize;

// calculate distance to midpoint
float d = distance(p, vec2(0.5, 0.5));

if (d < 0.5) {
o_colour = vec4(u_colour, 1);
}
else {
o_colour = vec4(0,0,0,1); // BLACK
}
}

This is the code that draws the circle. This code is run for every fragment (i.e. pixel) in the
window, to decide what colour the pixel should be. Not the following
1) gl_FragCoord is a built-in variable which contains the screen position of the
fragment.
2) u_screenSize is a uniform variable provided by the Java code which contains the
width and height of the window in pixels, as a 2d vector. This is set in Week1.java
lines 138-139.
3) u_colour is a uniform variable provided by the Java code which contains the a
colour as an (r,g,b) vector. The value is set to (1,0,0) which is red. This is set in
Week1.java lines 134-135.
4) o_colour is the output colour which is written to the screen (i.e. buffer 0). This is a
4d (r,g,b,a) vector. We set the ‘a’ (alpha) value to 1.

Experiment with the code to make sure you understand why it works. Things to try:
1) Change the colours of the circle and the background.
2) Change the centre and diameter of the circle.




3) Try changing line 10 to:

vec2 v = abs(p - vec2(0.5, 0.5));
float d = max(v.x, v.y);

What shape is drawn? Why?
What happens if you change max() to min(). Why?
Why is the abs() function needed here? What happens if you remove it?

4) How could you change the code to draw a diamond like this:


Notice that if you change the size of the window, the shape is stretched.
5) Why is the happening?
6) Challenge: How could you change the code to prevent this?



Syntax Highlighting
There is an Eclipse Shaders extension which does GLSL syntax highlighting available here:
https://sourceforge.net/projects/glshaders/

Download this and follow the installation instructions to make Eclipse show your shader
code more prettily.

欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468