unis cs297 graphics with java and opengl state management and drawing primative geometric objects

37
UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

Upload: daniella-stokes

Post on 17-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

UniS

CS297 Graphics with Java and OpenGL

State Management and Drawing Primative Geometric Objects

Page 2: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

2

UniS

Specifying Vertices

• A vertex is a point in 3D space, that can be specified as either a pair, triple or quadruple of values.

• E.G. valid definitions for vertices :– gl.glVertex2f(-0.5f, -0.5f);– gl.glVertex2f(-0.5f, 0.5f);– gl.glVertex2f(0.5f, 0.5f);– FloatBuffer vec0 =

FloatBuffer.wrap( new float[] {0.5f, -0.5f});gl.glVertex2fv(vec0);

Page 3: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

3

UniS

Specifying Vertices

• gl.glVertex2f(-0.5f, -0.5f);defines the 3D point (-0.5, -0.5, 0.0)

• in general gl.glVertex2f(Xf, Yf);specifies (X, Y, 0)

• note the 2f in glVertex2f denotes two values that are both floating point

• In general OpenGL commands of the form gl.glFooNt specify a command gl.glFoo with N parameters of type N.

Page 4: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

4

UniS

OpenGL Geometric Drawing Primitives

gl.glBegin(GL.GL_POLYGON); gl.glVertex2f(-0.5f, -0.5f); gl.glVertex2f(-0.5f, 0.5f); gl.glVertex2f(0.5f, 0.5f); gl.glVertex2fv(0.5f, -0.5f);gl.glEnd();

Page 5: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

5

UniS

OpenGL Geometric Drawing Primitives

gl.glBegin(GL.GL_LINES); gl.glVertex2f(-0.5f, -0.5f); gl.glVertex2f(-0.5f, 0.5f); gl.glVertex2f(0.5f, 0.5f); gl.glVertex2fv(0.5f, -0.5f);gl.glEnd();

first line

second line

Page 6: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

6

UniS

OpenGL Geometric Drawing Primitives

gl.glBegin(GL.GL_LINE_LOOP); gl.glVertex2f(-0.5f, -0.5f); gl.glVertex2f(-0.5f, 0.5f); gl.glVertex2f(0.5f, 0.5f); gl.glVertex2fv(0.5f, -0.5f);gl.glEnd();

Page 7: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

7

UniS

Parameters for gl.glBegin()

• GL_POINTS individual points• GL_LINES pairs of vertices interpreted as individual line

segments• GL_LINE_STRIP series of connected line segments• GL_LINE_LOOP same as above, with a segment added

between last and first vertices• GL_TRIANGLES triples of vertices interpreted as triangles• GL_TRIANGLE_STRIP linked strip of triangles• GL_TRIANGLE_FAN linked fan of triangles• GL_QUADS quadruples of vertices interpreted as four-sided

polygons• GL_QUAD_STRIP linked strip of quadrilaterals• GL_POLYGON boundary of a simple, convex polygon

Page 8: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

8

UniS

OpenGL commands allowed between gl.glBegin and gl.glEnd

Chapters here refer to the RedBook chapters• glVertex*() set vertex coordinates Chapter 2• glColor*() set current color Chapter 4• glIndex*() set current color index Chapter 4• glNormal*() set normal vector coordinates Chapter 2• glTexCoord*() set texture coordinates Chapter 9• glEdgeFlag*() control drawing of edges Chapter 2• glMaterial*() set material properties Chapter 5• glArrayElement() extract vertex array data Chapter 2• glEvalCoord*(), glEvalPoint*() generate coordinates

Chapter 12• glCallList(), glCallLists() execute display list(s)

Chapter 7

Page 9: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

9

UniS

OpenGL commands allowed between gl.glBegin and gl.glEnd

• No other OpenGL commands are valid between a glBegin() and glEnd() pair, and making most other OpenGL calls generates an error.

• Some vertex array commands, such as glEnableClientState() and glVertexPointer(), when called between glBegin() and glEnd(), have undefined behavior but do not necessarily generate an error.

Page 10: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

10

UniS

Other commands allowed between gl.glBegin and gl.glEnd

gl.glBegin(GL.GL_LINE_LOOP);

for (int i = 0; i < 8; i++) {

double angle = (2*Math.PI*i)/8;

gl.glVertex2f((float)Math.cos(angle), (float)Math.sin(angle));

}

gl.glEnd();

Page 11: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

11

UniS

Basic State management

• An object may be rendered with lighting, texturing, hidden surface removal, fog, or some other states affecting its appearance.

• By default, most of these states are initially inactive. • These states may be costly to activate; for example,

turning on texture mapping will almost certainly slow down the speed of rendering a primitive.

• To turn on and off many of these states, use these two simple commands:– gl.glEnable(GL.GL_Constant);– gl.glDisable(GL.GL_Constant);

• glEnable() turns on a capability determined by GL.GL_Constant, and glDisable() turns it off.

Page 12: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

12

UniS

• You can also check if a state is currently enabled or disabled.

• boolean glIsEnabled(GL.GL_Constant)

Page 13: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

13

UniS

Basic Polygons

• Polygons drawn by filling in pixels enclosed within boundary

• Can also draw them as outlined polygons or simply as points at the vertices.

• Filled polygons drawn in such a way that if adjacent polygons share an edge or vertex, the pixels making up the edge or vertex are drawn exactly once they're included in only one of the polygons.

• Hence transparent polygons don't have their edges drawn twice, which would make those edges appear darker (or brighter, depending on what colour you're drawing with).

Page 14: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

14

UniS

Polygons as Points, Outlines, or Solids

• A polygon has two sides - front and back - and might be rendered differently depending on which side is facing the viewer.

• This allows an obvious distinction between the parts that are inside and those that are outside.

• By default, both front and back faces are drawn in the same way. To change this, or to draw only outlines or vertices, use glPolygonMode().

Page 15: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

15

UniS

Polygons as Points, Outlines, or Solids

• glPolygonMode(GL_FRONT, GL_FILL);

• glPolygonMode(GL_BACK, GL_LINE);

Page 16: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

16

UniS

Polygons as Points, Outlines, or Solids

gl.glPolygonMode(GL.GL_FRONT, GL.GL_FILL); gl.glBegin(GL.GL_POLYGON); for (int i = 0; i < 8; i++) { double angle = 2*Math.PI*i/8; gl.glVertex2f((float)Math.cos(angle), (float)Math.sin(angle)); } gl.glEnd();

Page 17: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

17

UniS

Polygons as Points, Outlines, or Solids

gl.glPolygonMode(GL.GL_FRONT, GL.GL_LINE); gl.glBegin(GL.GL_POLYGON); for (int i = 0; i < 8; i++) { double angle = 2*Math.PI*i/8; gl.glVertex2f((float)Math.cos(angle), (float)Math.sin(angle)); } gl.glEnd();

Page 18: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

18

UniS

Reversing and Culling Polygon Faces

• By convention, polygons whose vertices appear in counterclockwise order on the screen are called front-facing.

• You can construct the surface of any "reasonable" solid - from polygons of consistent orientation. In other words, you can use all clockwise polygons, or all counterclockwise

• such a surface is an orientable manifold • spheres, donuts, and teapots are orientable; Klein

bottles and Möbius strips aren't orientable polygons. • Suppose you've consistently described a model of an

orientable surface but that you happen to have the clockwise orientation on the outside.

• You can swap what OpenGL considers the back face by using the function glFrontFace(), supplying the desired orientation for front-facing polygons

Page 19: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

19

UniS

Reversing and Culling Polygon Faces

v0

v1

v2v3

v4

v5v6

v7

Vertices drawn counter clockwise hence you are lookingat the front of the polygon.

When does it matter whichis the front and which is theback?

Lighting, shading, texture mapping

Page 20: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

20

UniS

Reversing and Culling Polygon Faces

• To instruct OpenGL to discard front or back-facing polygons, use the command – gl.glCullFace() with parameter GL_FRONT,

GL_BACK, or GL_FRONT_AND_BACK– and enable culling with

gl.glEnable(GL.GL_CULL_FACE)

Page 21: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

21

UniS

Normal Vectors

• A normal vector points in a direction that's perpendicular to a surface.

• For a flat surface, one perpendicular direction is the same for every point on the surface.

• For a general curved surface, the normal direction might be different at each point on the surface.

• With OpenGL, you can specify a normal for each polygon or for each vertex.

• Vertices of the same polygon might share the same normal (for a flat surface) or have different normals (for a curved surface).

• Can't assign normals anywhere other than at the vertices.

• Must have normals to light models.

Page 22: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

22

UniS

Normal Vectors

Often, each vertex has a different normal: gl.glBegin (GL.GL_POLYGON);

gl.glNormal3fv(n0); gl.glVertex3fv(v0);gl.glNormal3fv(n1);gl.glVertex3fv(v1);gl.glNormal3fv(n2);gl.glVertex3fv(v2);gl.glNormal3fv(n3);gl.glVertex3fv(v3);

gl.glEnd();You have to calculate values for normals, OpenGL does not have any other way of calculating them.

Page 23: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

23

UniS

Vertex Arrays

• OpenGL requires many function calls to render geometric primitives.

• Drawing a 20-sided polygon requires 22 function calls: – one call to glBegin(),

– one call for each of the vertices,

– and a final call to glEnd().

• Additional information for polygon boundary, edge flags or surface normals adds function calls for each vertex. This can quickly double or triple the number of function calls required for one geometric object.

Page 24: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

24

UniS

Vertex Arrays

• An additional problem is the redundant processing of vertices that are shared between adjacent polygons.

• For example, a cube has six faces and eight shared vertices.

• Using the standard method of describing this object, each vertex would have to be specified three times: once for every face that uses it.

• So 24 vertices would be processed, even though eight would be enough.

Page 25: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

25

UniS

Vertex Arrays

• OpenGL has vertex array routines that allow you to specify a lot of vertex-related data with just a few arrays and to access that data with equally few function calls.

• Using vertex array routines, all 20 vertices in a 20-sided polygon could be put into one array and called with one function.

• If each vertex also had a surface normal, all 20 surface normals could be put into another array and also called with one function.

Page 26: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

26

UniS

Vertex Arrays

• Using vertex arrays reduces the number of function calls, which improves performance.

• Using vertex arrays may allow non-redundant processing of shared vertices.

• Vertex arrays are standard in version 1.1 of OpenGL but were not part of the OpenGL 1.0 specification. With OpenGL 1.0, some vendors have implemented vertex arrays as an extension.

Page 27: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

27

UniS

Three steps to using vertex arrays to render geometry

• Can use up to six arrays, each to store a different type of data: – vertex coordinates, – RGBA colors, – color indices, – surface normals,– texture coordinates, – or polygon edge flags.

Page 28: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

28

UniS

Three steps to using vertex arrays to render geometry

1. Activate the arrays that are needed.

2. Put data into the array or arrays. The arrays are accessed by the addresses of their memory locations. In the client-server model, this data is stored in the client's address space.

3. Draw geometry with the data. In the client-server model, the data is transferred to the server's address space. There are three ways to do this:

– Accessing individual array elements (randomly hopping around)

– Creating a list of individual array elements (methodically hopping around)

– Processing sequential array elements

Page 29: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

29

UniS

Step 1: Enabling Arrays

• Use glEnableClientState() to enabe a particular array.

• For example gl.glEnableClientState(GL.GL_VERTEX_ARRAY);enables the vertex array.

• Other valid parameters are:GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_INDEX_ARRAY, GL_NORMAL_ARRAY, GL_TEXTURE_COORD_ARRAY, and GL_EDGE_FLAG_ARRAY

Page 30: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

30

UniS

Normal vector arrays for lighting effects

• If you use lighting, you may want to define a surface normal for every vertex.

• To use vertex arrays for that case, you activate both the surface normal and vertex coordinate arrays:– gl.glEnableClientState

(GL.GL_NORMAL_ARRAY);– gl.glEnableClientState

(GL.GL_VERTEX_ARRAY);• Where arrays are not being used they can be

disabled via, for example,gl.glDisableClientState(GL.GL_NORMAL_ARRAY);

Page 31: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

31

UniS

Step 2: Specifying Data for the Arrays

• void gl.glVertexPointer (int size, int type, int stride, Buffer ptr)– Specifies where spatial coordinate data can be

accessed. – type specifies the data type (GL_SHORT, GL_INT,

GL_FLOAT, or GL_DOUBLE) of each coordinate in the array.

– size is the number of coordinates per vertex, which must be 2, 3, or 4.

– stride is the byte offset between consecutive vertexes. If stride is 0, the vertices are understood to be tightly packed in the array.

Page 32: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

32

UniS

Step 2: Specifying Data for the Arrays

Command Sizes Parameter values

glVertexPointer 2, 3, 4 GL_SHORT, GL_INT, GL_FLOAT, GL_DOUBLE

glNormalPointer 3 GL_BYTE, GL_SHORT, GL_INT, GL_FLOAT, GL_DOUBLE

glColorPointer 3,4 GL_BYTE, GL_UNSIGNED_BYTE,

GL_SHORT, GL_UNSIGNED_SHORT,

GL_INT, GL_UNSIGNED_INT, GL_FLOAT,

GL_DOUBLE

glIndexPointer 1 GL_UNSIGNED_BYTE, GL_SHORT, GL_INT, GL_FLOAT, GL_DOUBLE

glTexCoordPointer 1, 2, 3, 4

GL_SHORT, GL_INT, GL_FLOAT, GL_DOUBLE

glEdgeFlagPointer 1 none

Page 33: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

33

UniS

Step 2: Specifying Data for the Arrays

IntBuffer vertices =

IntBuffer.wrap( new int[ ] {25, 25, 100, 325, 175, 25, 175, 325, 250, 25, 325, 325});

gl.glEnableClientState (GL.GL_VERTEX_ARRAY); gl.glVertexPointer (2, GL.GL_INT, 0, vertices);

Each pair of integerswill be regarded as a vertex in glVertexPointer

Page 34: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

34

UniS

FloatBuffer intertwined = FloatBuffer.wrap(new float[ ] = {1.0f, 0.2f, 1.0f, 100.0f, 100.0f, 0.0f,1.0f, 0.2f, 0.2f, 0.0f, 200.0f, 0.0f,1.0f, 1.0f, 0.2f, 100.0f, 300.0f, 0.0f,0.2f, 1.0f, 0.2f, 200.0f, 300.0f, 0.0f,0.2f, 1.0f, 1.0f, 300.0f, 200.0f, 0.0f,0.2f, 0.2f, 1.0f, 200.0f, 100.0f, 0.0f};

For example, to reference only the color values in the intertwined array, following starts from beginning of array and jumps ahead 6, which is the size of both the color and vertex coordinate values.

This jump is enough to get to the beginning of the data for the next vertex. Following command will pick red triples of values as the colour values.glColorPointer (3, GL_FLOAT, 6, intertwined);

Page 35: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

35

UniS

FloatBuffer intertwined = FloatBuffer.wrap(new float[ ] = {1.0f, 0.2f, 1.0f, 100.0f, 100.0f, 0.0f,1.0f, 0.2f, 0.2f, 0.0f, 200.0f, 0.0f,1.0f, 1.0f, 0.2f, 100.0f, 300.0f, 0.0f,0.2f, 1.0f, 0.2f, 200.0f, 300.0f, 0.0f,0.2f, 1.0f, 1.0f, 300.0f, 200.0f, 0.0f,0.2f, 0.2f, 1.0f, 200.0f, 100.0f, 0.0f};

To reference the vertices values in the intertwined array, following resets start of array to index 3. Then glVertexPointer picks up all the black triples as vertices.

intertwined.position(3);glVertexPointer (3, GL_FLOAT, 6, intertwined);

Page 36: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

36

UniS

Dereference a Single Array Element

• public void glArrayElement(int i)

• Obtains the data of the i-th vertex for all currently enabled arrays.

Page 37: UniS CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

37

UniS

Dereference a Single Array Element

glEnableClientState (GL_COLOR_ARRAY);

glEnableClientState (GL_VERTEX_ARRAY);

glColorPointer (3, GL_FLOAT, 0, colors);

glVertexPointer (2, GL_INT, 0, vertices);

glBegin(GL_TRIANGLES);glArrayElement (2);glArrayElement (3);glArrayElement (5);glEnd();

When executed, the latter five lines of codehas the same effect as

glBegin(GL_TRIANGLES);glColor3fv(colors[2*3]);glVertex3fv(vertices[2*2]);glColor3fv(colors[3*3]);glVertex3fv(vertices[3*2]);glColor3fv(colors[5*3]);glVertex3fv(vertices[5*2]);glEnd();