OpenGL代写:COMP4610 OpenGL Problems

OpenGL相关Lab,实现10个小程序。

OpenGL

Part A

Question A.1

The Star Wars movies start with an opening crawl which scrolls text up the screen providing the audience with a background to the story. Using OpenGL 1.1 create an OpenGL program that is able to provide a similar opening crawl for some fixed text. So the background must be black and the text yellow. The text scrolls up the screen, and it should just start to appear as the application starts. Once the text is off the top of the screen the application remains a black screen. The text must be wide enough to fill over half the screen without going over the edge. Each line in the text must be centered. Noting in the Star Wars crawl the the text gets smaller as it goes up giving a 3D effect, you don’t need to do this just keep it the same size (although it may be something fun to try out). Note also you do not need to adjust for a changing screen size. Just have the screen start and remain at 800x600.

You may make use of the ScreenSaver program and implement your code in either c or Java(using JOGL).

Hints, bitmap fonts don’t scale so use “glutStrokeString”:

1
float l = glut.glutStrokeLengthf(GLUT.STROKE_ROMAN, txt[i]); glut.glutStrokeString(GLUT.STROKE_ROMAN, txt[i]);

Question A.2

Implement a program using OpenGL 1.1 that displays a the text “Hello World” in blue with a white background. The text must fit exactly the width of the window, so the font size is determined by the width of the window. The aspect ratio of the font should remain uneffected so the text neither looks long and thin or squashed. The text must also be vertically centred. You should be able to resize the window and the text will be appropriately scaled and positioned.

You may make use of the ScreenSaver program and implement your code in either c or Java(using JOGL).

In the below image I have run the program 3 times and packed their windows next to each other. This is so you can see how the program should look with different window sizes.

Hints, bitmap fonts don’t scale so use “glutStrokeString”, noting also the height of this font will be 100.0f:

1
float l = glut.glutStrokeLengthf(GLUT.STROKE_ROMAN, "Hello World") glut.glutStrokeString(GLUT.STROKE_ROMAN, "Hello World");

Question A.3

Implement a program using OpenGL 1.1 that displays windmills. Each windmill is made up of 6 right angle triangles. For drawing a windmill use the below design:

All the triangles for drawing the windmill can and must be drawn with the method:

1
2
3
4
5
6
7
8
9
10
private void drawTriangle(GL2 gl) {
gl.glPushMatrix();
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex2d(0.0, 0.0);
gl.glVertex2d(0.0, 40.0);
gl.glVertex2d(5.0, 0.0);
gl.glVertex2d(0.0, 0.0);
gl.glEnd();
gl.glPopMatrix();
}

To draw all the windmills use the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public void display(GLAutoDrawable dr) {
GL2 gl = (GL2) dr.getGL();
GLU glu = new GLU();
GLUT glut = new GLUT();

// clear the screen
gl.glClear(GL.GL_COLOR_BUFFER_BIT);

// draw 10 windmills randomly placed
Random r = new Random(0);
for (int i = 0; i < 10; i++) {
gl.glPushMatrix();
gl.glTranslatef(r.nextInt(800), r.nextInt(500), 0.0);
gl.glColor3f(r.nextFloat(), r.nextFloat(), r.nextFloat());
gl.glScalef(2.0f, 2.0f, 1.0f);
drawWindMill(rot + r.nextInt(360), gl);
gl.glPopMatrix();
}
gl.glFlush();

// move the angle of the sails on for the next frame
rot += rotvel;
if (rot > 360.0f)
rot = 0.0f;
}

So the only method you need to implement is:

1
2
3
4
5
6
// drawWindMill - draw a windmill (0,0) is at the base and centre
private void drawWindMill(float a, GL2 gl) {
gl.glPushMatrix();
// your solution goes here
gl.glPopMatrix();
}

Question A.4

Implement a program using OpenGL 1.1 that displays a spinning chessboard. The chessboard will spin in the centre of the display on its centre. Display starts off with dimension 800:600, and you may assuming the window is not resized. The background must be blue and the squares that make up the chess board are either black or white. Each square is 50x50 and must be drawn with:

1
2
3
4
5
6
7
8
9
10
11
private void drawSquare(GL2 gl) {
gl.glPushMatrix();
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex2d(0.0, 0.0);
gl.glVertex2d(0.0, 50.0);
gl.glVertex2d(50.0, 50.0);
gl.glVertex2d(50.0, 0.0);
gl.glVertex2d(0.0, 0.0);
gl.glEnd();
gl.glPopMatrix();
}

You may make use of the ScreenSaver program and implement your code in either c or Java(using JOGL).

Question A.5

Implement a program using OpenGL 1.1 that displays a spinning spiral. The background must be white and a red line that is 10 wide. The spiral should be in the centre of the window. Note also you do not need to adjust for a changing screen size. Just have the screen start and remain at 800x600.

You may make use of the ScreenSaver program and implement your code in either c or Java(using JOGL).

Hint: the spiral can be inscribed by (5 * a * sin(a), 5 * a *cos(a)) for “a” between 0 and 50. Also you can just use GL_LINE_STRIP to draw the line.

Question A.6

Implement a program using OpenGL 1.1 that displays a spinning colour wheel. The background must be white and with a radius of 200. The wheel should be in the centre of the window. Note also you do not need to adjust for a changing screen size. Just have the screen start and remain at 800x600.

You may make use of the ScreenSaver program and implement your code in either c or Java(using JOGL).

Hints: use a GL_TRIANGLE_FAN of 10 triangles, have the centre vertex’s colour set to white and the surrounding vertices set to a random colour. i.e. gl.glColor3f(r.nextFloat(),r.nextFloat(),r.nextFloat());

Question A.7

Implement a program using OpenGL 1.1 that displays the computer graphics logo with a white background. The logo must fit exactly the width of the window. The aspect ratio of the logo image should remain unchanged. The logo must also be vertically centred. You should be able to resize the window and the logo will be appropriately scaled and positioned.

You may make use of the ScreenSaver program and implement your code in either c or Java(using JOGL).

In the below image I have run the program 3 times and packed their windows next to each other. This is so you can see how the program should look with different window sizes.

Hints, below are a few methods (in no particular order) that may help in your solution:

1
2
3
4
5
6
7
8
9
10
cgtexture.bind(gl);
gl.glEnable(GL2.GL_TEXTURE_2D);
gl.glTexCoord2d(0.0, 0.0);
gl.glDisable(GL2.GL_TEXTURE_2D);
try {
cgtexture = TextureIO.newTexture(new File("compgraphicslogo"));
cgtexture.enable(gl);
} catch (GLException | IOException e) {
e.printStackTrace();
}

Question A.8

Implement a program using OpenGL 1.1 that displays a spinning chessboard by adding a texture image to a single large square. The chessboard will spin in the centre of the display on its centre. The display starts off with dimension 800:600, and you may assuming the window is not resized. The background must be green and use the below image to add a texture to a single polygon.

This image was created with question 4 so there is a bit of blue on the boarder, this is and must be evident in your solution.

You may make use of the ScreenSaver program and implement your code in either c or Java(using JOGL).

Part B

Still under construction. I need to make the staring code.

Question B.1

Make a spinning chessboard that is visually identical to Question A4, however use a shader approach rather than OpenGL 1.1. In this implementation you must only have 2 triangles for the entire chess board. Thus the checker pattern will need to be done within the fragment shader.

Question B.2

Make a spinning chessboard that is visually identical to question A4, however use a shader approach rather than OpenGL 1.1. In this implementation you must have 2 triangles for each of the checker patterns. So overall there will be 128 triangles that makes up the scene.