Chapter 3 arrays of vertices vertex arrays display lists drawing text mouse buttons
4 ways to give the vertices List the vertices between glBegin and glEnd arrays of vertices (still uses glBegin and glEnd) Vertex arrays Index arrays
SquareAnnulus1.cpp as we have seen so far glBegin(GL_TRIANGLE_STRIP); glColor3f(0.0, 0.0, 0.0); glVertex3f(30.0, 30.0, 0.0); glColor3f(1.0, 0.0, 0.0); glVertex3f(10.0, 10.0, 0.0);
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.
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();
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 };
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!
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)
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.
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;
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.
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.
helixList.cpp glCallList(aHelix); // Execute display list. in drawScene(): glCallList(aHelix); // Execute display list.
see the text for a discussion of executing multiple display lists with one call. Got to slide 12
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);
Sample Letters
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
See CircularAnnuluses.cpp for an example
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
Available Stroke Fonts GLUT_STROKE_ROMAN GLUT_STROKE_MONO_ROMAN Run fonts.cpp
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, const char *string) { const char *c; for (c = string; *c != '\0'; c++) glutStrokeCharacter(font, *c); } Use a glut function to draw the char glutStrokeCharacter(font, *c);
Bitmaps don't scale. Stroke scale, rotate.
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)
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
int x, int y (x,y) is the location in the OpenGL window, measured in pixels, with origin in the upper left corner.
mouse.cpp Run. Notice the use of STL vector. Look at the mouse control function. Look at the resize function, and the resize behavior.
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) );
drawing the points // Loop through the points array // drawing each point. pointsIterator = points.begin(); while(pointsIterator != points.end() ) { pointsIterator->drawPoint(); pointsIterator++; } Got to here class 5
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.
Using the keyboard glutKeyboardFunc(keyboardInput) - to register ordinary keyboard input glutSpecialFunc(specialkeyInput) - to register F keys, arrow keys, page up and down.
keyboard functions keyboardInput(unsigned char key, int x, int y); specialkeyInput(int key, int x, int y); Lots of values for key for example: GLUT_KEY_F4 GLUT_KEY_DOWN GLUT_KEY_HOME
menus (popup) Run menus.cpp glutCreateMenu(menuFunction) - registers menuFunction as a menu function and returns an ID number for that menu. (Must be after the glutCreateWindow.) prototype void menuFunction(int value) value is a number corresponding to the user's menu choice.
glutAddMenuEntry(tag, returnedValue); tag is what it will say returnedValue in the value that menuFunction will get as its argument. eg: glutAddMenuEntry("Quit",1);
glutAddSubMenu(tag, sub_menu) tag is what it will say sub_menu is the ID of the (sub)menu you want to pop up. Awkward: need to create the submenu before the menu that calls it.
glutAttachMenu(button) - says which button brings up this menu Run and look at code of menus.cpp Don't forget to PostRedisplay() in the menuFunction!
A state that needs to be enabled. Line Stipples A state that needs to be enabled. glEnable(GL_LINE_STIPPLE); glDisable(GL_LINE_STIPPLE); Applies to all lines until you disable it.
glLineStipple(factor, pattern) factor is a positive integer pattern is the pattern, in hexidecimal eg: - - - --- - - Reverse - - --- - - - 1001011100101010 0x972A Here factor is 1.
glLineStipple(factor, pattern) If factor is 2, instead of 1 get - - - --- - - -- -- -- ------ -- --
glut solids glutSolidSphere(radius, slices, stacks) glutWireSphere(radius, slices, stacks)
all glut solids Run glutObjects.cpp
Equation of a plane ax + by + cz + d = 0 n⋅(p-p0)=0, d=-(ax0 + by0 + cz0) Modified, from mathworld.wolfram.com/Plane.html
clipping planes glClipPlane(GL_CLIP_PLANEi, *equation) equation is an array of a,b,c,d glEnable(GL_CLIP_PLANEi); glDisable(GL_CLIP_PLANEi); See (x,y,z) only if ax+by+cz+d >=0 if drawn while enabled. Run and look at clippingPlanes.cpp
gluPerspective(fovy,aspect, near, far) like glFrustum fovy - field of view angle aspect ratio - width/height near and far like glFrustum symmetric about the z axis
gluPerspective(fovy,aspect,near,far)
glViewPort(x,y,w,h); (x,y) lower left corner of viewport, in pixels, within the window (w,h) width and height of viewport. origin of window at lower left glOrtho or glFrustum or ... apply to each viewport. default viewport is the whole window run viewports.cpp
canvas.cpp put it all together!