Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 arrays of vertices vertex arrays display lists drawing text

Similar presentations


Presentation on theme: "Chapter 3 arrays of vertices vertex arrays display lists drawing text"— Presentation transcript:

1 Chapter 3 arrays of vertices vertex arrays display lists drawing text
mouse buttons

2 Arrays of vertices (and colors) squareAnnulus2.cpp
vertices and colors in global arrays vertices accessed through pointers glVertex3fv(pointer to vertex) colors accessed through pointers glColor3fv(pointer to color) Look at the code.

3 Accessing an Array of Vertices with pointers
// Draw square annulus. glBegin(GL_TRIANGLE_STRIP); for(int i = 0; i < 10; ++i) { glColor3fv(colors[i%8]); glVertex3fv(vertices[i%8]); } glEnd();

4 ARRAY OF VERTICES vs VERTEX ARRAY
ARRAY OF VERTICES // Vertex co-ordinate vectors. static float vertices[8][3] = { {30.0, 30.0, 0.0}, {10.0, 10.0, 0.0}, {70.0, 30.0, 0.0}, {90.0, 10.0, 0.0}, {70.0, 70.0, 0.0}, {90.0, 90.0, 0.0}, {30.0, 70.0, 0.0}, {10.0, 90.0, 0.0} }; VERTEX ARRAY // Vertex co-ordinate vectors. static float vertices[] = { 30.0, 30.0, 0.0, 10.0, 10.0, 0.0, 70.0, 30.0, 0.0, 90.0, 10.0, 0.0, 70.0, 70.0, 0.0, 90.0, 90.0, 0.0, 30.0, 70.0, 0.0, 10.0, 90.0, 0.0 };

5 roomStripe.cpp Room is built with a list of vertices
Figure out where the room is in world coords. Decide where you want the stripe to be, get 14 points Stripe is drawn using a Vertex Array.

6 roomStripe.cpp Color can be solid or use a Color Array
Need to set up and enable these arrays. Discuss input from a file in C++ Discuss changing Frustum/Ortho

7 Vertex Arrays squareAnnulus3.cpp
vertices in one dimensional vertex array colors in one dimensional color array vertex and color are accessed together glArrayElement(i%8); glArrayElement(location of vertex); Need info to know where that is!

8 To use vertex arrays you need to
say you are using a vertex array and a color array. glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); say where to find the data glVertexPointer(3, GL_FLOAT, 0, vertices); glColorPointer(3, GL_FLOAT, 0, colors); (size, type, stride, *pointer) Look at code. (squareAnnulus3.cpp or roomStripe.cpp)

9 squareAnnulus4.cpp In addition to vertex and color arrays (1D) there is an index array static unsigned char stripIndices[] = {0, 1, 2, 3, 4, 5, 6, 7, 0, 1}; One draw statement glDrawElements(GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_BYTE, stripIndices); (primitive, count, type, *indices) Look at code.

10 OpenGL redefines types. Use GL types as parameters to GL functions
OpenGL redefines types. Use GL types as parameters to GL functions. Sometimes you will need to declare the item using the GL type, eg. GLfloat length;

11 Display Lists compiled once can save to machine running the display
can be invoked by a single command Uses the values of variables at the time it compiles, not at the time it is displayed.

12 helixList.cpp // Globals. static unsigned int aHelix; // List index.
in setup(): aHelix = glGenLists(1); // Return a list index. // Begin create a display list. glNewList(aHelix, GL_COMPILE); // Draw a helix. glBegin(GL_LINE_STRIP); for(t = -10 * PI; t <= 10 * PI; t += PI/20.0) glVertex3f(20 * cos(t), 20 * sin(t), t); glEnd(); glEndList(); // End create a display list.

13 helixList.cpp glCallList(aHelix); // Execute display list.
in drawScene(): glCallList(aHelix); // Execute display list.

14 see the text for a discussion of executing multiple display lists with one call.
Got to slide 12

15 Drawing Text - Bitmap use a glut function for drawing a char
Need to go to the right pixel: glRasterPos3f(p,q,r);//object coordinates call a function to draw the string, like writeBitmapString(font, string) // Routine to draw a bitmap character string. void writeBitmapString(void *font, char *string) { char *c; for (c = string; *c != '\0'; c++) glutBitmapCharacter(font, *c); } use a glut function for drawing a char glutBitmapCharacter(font, *c);

16 Sample Letters

17 Available Bitmap Fonts
GLUT_BITMAP_8_BY_13 GLUT_BITMAP_9_BY_15 GLUT_BITMAP_TIMES_ROMAN_10 GLUT_BITMAP_TIMES_ROMAN_24 GLUT_BITMAP_HELVETICA_10 GLUT_BITMAP_HELVETICA_12 GLUT_BITMAP_HELVETICA_18

18 See CircularAnuluses.c for an example

19 Drawing Text - Stroke Works like drawing lines. Uses color, linewidth,... Assumes at the origin, use glTranslate to move to the right spot, glScale to make the right size

20 Available Stroke Fonts
GLUT_STROKE_ROMAN GLUT_STROKE_MONO_ROMAN Run fonts.cpp

21 Stroke text cont. Use a glut function to draw the char
Use the canonical function // Routine to draw a stroke character string. void writeStrokeString(void *font, char *string) { char *c; for (c = string; *c != '\0'; c++) glutStrokeCharacter(font, *c); } Use a glut function to draw the char glutStrokeCharacter(font, *c);

22 Bitmaps don't scale. Stroke scale, rotate.

23 Programming the mouse clicking a button - register with
glutMouseFunc(mouseButton) dragging with a button down - register with glutMotionFunc(mouseMotion) dragging with no button down - register with glutPassiveMotionFunc(mousePassive)

24 void mouseButton(int button, int state, int x, int y);
button can be GLUT_LEFT_BUTTON GLUT_RIGHT_BUTTON GLUT_MIDDLE_BUTTON state can be GLUT_UP GLUT_DOWN

25 int x, int y (x,y) is the location in the OpenGL window, measured in pixels, with origin in the upper left corner.

26 mouse.cpp Run. Notice the use of STL vector.
Look at the mouse control function. Look at the resize function, and the resize behavior.

27 STL vector Point is user defined type. vector<Point> points;
// Iterator to traverse a Point array. vector<Point>::iterator pointsIterator; // Store the clicked point in the points array after correcting // from event to OpenGL co-ordinates. points.push_back( Point(x, height - y) );

28 drawing the points // Loop through the points array drawing each point. pointsIterator = points.begin(); while(pointsIterator != points.end() ) { pointsIterator->drawPoint(); pointsIterator++; }

29 mouseMotion.cpp modified point size and color for demo
point is created when button is pressed. coordinates of point are modified as mouse is moved with the button down point is added to the vector when button is released currentPoint Look at code.


Download ppt "Chapter 3 arrays of vertices vertex arrays display lists drawing text"

Similar presentations


Ads by Google