1 / 37

CS297 Graphics with Java and OpenGL

CS297 Graphics with Java and OpenGL. State Management and Drawing Primative Geometric Objects. 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);

vinnie
Download Presentation

CS297 Graphics with Java and OpenGL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS297 Graphics with Java and OpenGL State Management and Drawing Primative Geometric Objects

  2. 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);

  3. 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.

  4. 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();

  5. 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

  6. 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();

  7. 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

  8. 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

  9. 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.

  10. 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();

  11. 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.

  12. You can also check if a state is currently enabled or disabled. • boolean glIsEnabled(GL.GL_Constant)

  13. 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).

  14. 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().

  15. Polygons as Points, Outlines, or Solids • glPolygonMode(GL_FRONT, GL_FILL); • glPolygonMode(GL_BACK, GL_LINE);

  16. 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();

  17. 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();

  18. 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

  19. Reversing and Culling Polygon Faces v2 v3 Vertices drawn counter clockwise hence you are lookingat the front of the polygon. When does it matter whichis the front and which is theback? v1 v4 v0 v5 v7 v6 Lighting, shading, texture mapping

  20. 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)

  21. 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.

  22. 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.

  23. 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.

  24. 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.

  25. 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.

  26. 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.

  27. 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.

  28. Three steps to using vertex arrays to render geometry • Activate the arrays that are needed. • 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. • 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

  29. 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

  30. 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);

  31. 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.

  32. Step 2: Specifying Data for the Arrays

  33. Step 2: Specifying Data for the Arrays Each pair of integerswill be regarded as a vertex in glVertexPointer 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);

  34. 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);

  35. 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);

  36. Dereference a Single Array Element • public void glArrayElement(int i) • Obtains the data of the i-th vertex for all currently enabled arrays.

  37. 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();

More Related