OpenGL Programming: Understanding Matrix Transformations and Rendering a Triangle, Study notes of Computer Graphics

An introduction to opengl programming, focusing on matrix transformations and rendering a triangle using the glut library. It covers the use of functions such as glmatrixmode, glloadidentity, gltranslated, glrotatef, gluperspective, and glrotated, as well as the meaning and parameters of each. The document also explains the role of functions like glclearcolor, glclear, and glbegin/glend in the rendering process.

Typology: Study notes

2011/2012

Uploaded on 08/04/2012

parnashi
parnashi 🇮🇳

4.4

(49)

71 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture No.34 OpenGL Programming - II
#include "GL/glut.h"
#include <stdlib.h>
static void reshape( int w, int h )
{
glViewport( 0, 0, w, h );
}
static void keyboard( unsigned char key, int x, int y )
{
switch( key )
{
case 27: //Escape
exit(0);
break;
}
}
static void display()
{
}
static void idle()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
///////////////////////////////////////////////////////
// glRotatef(0.5f,0.0f,1.0f,0.0);
glBegin(GL_TRIANGLES);
glVertex3f(0.5f,0.2f,0.0f);
glVertex3f(0.5f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glEnd();
glutSwapBuffers();
}
static void initGL()
{
float ratio= (float)640 /480 ;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download OpenGL Programming: Understanding Matrix Transformations and Rendering a Triangle and more Study notes Computer Graphics in PDF only on Docsity!

Lecture No.34 OpenGL Programming - II

#include "GL/glut.h" #include <stdlib.h>

static void reshape( int w, int h ) { glViewport( 0, 0, w, h ); }

static void keyboard( unsigned char key, int x, int y ) { switch( key ) { case 27: //Escape exit(0); break; } }

static void display() { }

static void idle() { glClearColor(0.0f,0.0f,0.0f,0.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); /////////////////////////////////////////////////////// // glRotatef(0.5f,0.0f,1.0f,0.0);

glBegin(GL_TRIANGLES);

glVertex3f(0.5f,0.2f,0.0f); glVertex3f(0.5f,0.0f,0.0f); glVertex3f(0.0f,0.0f,0.0f);

glEnd();

glutSwapBuffers(); }

static void initGL() { float ratio= (float)640 /480 ;

glMatrixMode( GL_PROJECTION ); glLoadIdentity();

docsity.com

gluPerspective( 45.0,(float) 640 /480 , 1.0, 100.0 ); glMatrixMode(GL_MODELVIEW);

glLoadIdentity(); glTranslated(0.0, 0.0, -5.0 );

} int main( int argc, char* argv[] ) {

//Initialize the GLUT library. glutInit( &argc, argv );

//Set up the OpenGL rendering context. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );

//Create a window and set its width and height. glutCreateWindow( "Deform" ); glutReshapeWindow( 640, 480 );

//Initialize our data structures. initGL();

//The keyboard function gets called whenever we hit a key. glutKeyboardFunc( keyboard );

//The display function gets called whenever the window //requires an update or when we explicitly call //glutPostRedisplay() glutDisplayFunc( display );

//The reshape function gets called whenever the window changes //shape. glutReshapeFunc( reshape );

//The idle function gets called when there are no window //events to process. glutIdleFunc( idle );

//Get the ball rolling! glutMainLoop();

return 0; }

//eof

In the above program we have first set perspective projection matrix and then rendered a triangle in idle function.

docsity.com

but in some cases it is more efficient.

The following functions retrieve information related to glLoadIdentity :

Error Codes

The following is the error code and its condition.

Error code Condition

GL_INVALID_OPERATION glLoadIdentity was called between a call to glBegin and the corresponding call to glEnd.

glTranslated, glTranslatef

The glTranslated and glTranslatef functions multiply the current matrix by a translation matrix.

void glTranslated( GLdouble x , GLdouble y , GLdouble z );

void glTranslatef( GLfloat x , GLfloat y , GLfloat z ); Parameters x, y, z The x , y , and z coordinates of a translation vector.

Remarks

The glTranslate function produces the translation specified by ( x , y , z ). The translation vector is used to compute a 4x4 translation matrix:

The current matrix (see glMatrixMode ) is multiplied by this translation matrix, with the product replacing the current matrix. That is, if M is the current matrix and T is the translation matrix, then M is replaced with M•T.

If the matrix mode is either GL_MODELVIEW or GL_PROJECTION, all objects drawn after glTranslate is called are translated. Use glPushMatrix and glPopMatrix to save and restore the untranslated coordinate system.

The following functions retrieve information related to glTranslated and glTranslatef :

Error Codes

docsity.com

The following is the error code and its condition.

Error code Condition

GL_INVALID_OPERATION glTranslate was called between a call to glBegin and the corresponding call to glEnd.

gluPerspective

The gluPerspective is the function of gl utility library. This function sets up a perspective projection matrix.

void gluPerspective( GLdouble fovy , GLdouble aspect , GLdouble zNear , GLdouble zFar ); Parameters fovy The field of view angle, in degrees, in the y-direction. aspect The aspect ratio that determines the field of view in the x-direction. The aspect ratio is the ratio of x (width) to y (height). zNear The distance from the viewer to the near clipping plane (always positive). zFar The distance from the viewer to the far clipping plane (always positive). Remarks

The gluPerspective function specifies a viewing frustum into the world coordinate system. In general, the aspect ratio in gluPerspective should match the aspect ratio of the associated viewport. For example, aspect = 2.0 means the viewer's angle of view is twice as wide in x as it is in y. If the viewport is twice as wide as it is tall, it displays the image without distortion.

The matrix generated by gluPerspective is multiplied by the current matrix, just as if glMultMatrix were called with the generated matrix. To load the perspective matrix onto the current matrix stack instead, precede the call to gluPerspective with a call to glLoadIdentity.

glRotated, glRotatef

The glRotated and glRotatef functions multiply the current matrix by a rotation matrix.

void glRotated( GLdouble angle , GLdouble x , GLdouble y , GLdouble z ); void glRotatef( GLfloat angle ,

docsity.com

GL_INVALID_OPERATION glClearColor was called between a call to glBegin and the corresponding call to glEnd. glColor

These functions set the current color.

glColor3b, glColor3d, glColor3f, glColor3i, glColor3s, glColor3ub, glColor3ui, glColor3us, glColor4b, glColor4d, glColor4f, glColor4i, glColor4s, glColor4ub, glColor4ui, glColor4us, glColor3bv, glColor3dv, glColor3fv, glColor3iv, glColor3sv, glColor3ubv, glColor3uiv, glColor3usv, glColor4bv, glColor4dv, glColor4fv, glColor4iv, glColor4sv, glColor4ubv, glColor4uiv, glColor4usv.

void glColor3b( GLbyte red , GLbyte green , GLbyte blue ); void glColor3f( GLfloat red , GLfloat green , GLfloat blue ); Consult yourself for the documentation of rest of the functions of this type. Parameters red, green, blue New red, green, and blue values for the current color. alpha A new alpha value for the current color. Included only in the four-argument glColor4 function. Remarks

OpenGL stores both a current single-valued color index and a current four-valued RGBA color. The glColor function sets a new four-valued RGBA color.

There are two major variants to glColor :

 The glColor3 variants specify new red, green, and blue values explicitly, and set

the current alpha value to 1.0 implicitly.

 The glColor4 variants specify all four color components explicitly.

The glColor3b , glColor4b , glColor3s , glColor4s , glColor3i , and glColor4i functions take three or four signed byte, short, or long integers as arguments. When you append v to the name, the color functions can take a pointer to an array of such values.

Current color values are stored in floating-point format, with unspecified mantissa and exponent sizes. Unsigned integer color components, when specified, are linearly mapped to floating-point values such that the largest representable value maps to 1.0 (full intensity), and zero maps to 0.0 (zero intensity). Signed integer color components, when specified, are linearly mapped to floating-point values such that the most positive representable value maps to 1.0, and the most negative representable value maps to 1.0. Floating-point values are mapped directly.

docsity.com

Neither floating-point nor signed integer values are clamped to the range [0,1] before updating the current color. However, color components are clamped to this range before they are interpolated or written into a color buffer.

You can update the current color at any time. In particular, you can call glColor between a call to glBegin and the corresponding call to glEnd.

glClear

The glClear function clears buffers to preset values.

void glClear( GLbitfield mask ); Parameters mask Bitwise OR operators of masks that indicate the buffers to be cleared. The four masks are as follows. Mask Buffer to be Cleared

GL_COLOR_BUFFER_BIT The buffers currently enabled for color writing.

GL_DEPTH_BUFFER_BIT The depth buffer.

GL_ACCUM_BUFFER_BIT The accumulation buffer.

GL_STENCIL_BUFFER_BIT The stencil buffer.

Remarks

The glClear function sets the bitplane area of the window to values previously selected by glClearColor , glClearIndex , glClearDepth , glClearStencil , and glClearAccum. You can clear multiple color buffers simultaneously by selecting more than one buffer at a time using glDrawBuffer.

The pixel-ownership test, the scissor test, dithering, and the buffer writemasks affect the operation of glClear. The scissor box bounds the cleared region. The glClear function ignores the alpha function, blend function, logical operation, stenciling, texture mapping, and z -buffering.

The glClear function takes a single argument ( mask ) that is the bitwise OR of several values indicating which buffer is to be cleared.

The value to which each buffer is cleared depends on the setting of the clear value for that buffer.

If a buffer is not present, a glClear call directed at that buffer has no effect.

Error Codes

The following are the error codes generated and their conditions.

docsity.com

GL_QUAD_STRIP

Draws a connected group of quadrilaterals. One quadrilateral is defined for each pair of vertices presented after the first pair. Vertices 2n 1 , 2n , 2n + 2 , and 2n + 1 define quadrilateral n. N quadrilaterals are drawn. Note that the order in which vertices are used to construct a quadrilateral from strip data is different from that used with independent data. GL_POLYGON Draws a single, convex polygon. Vertices 1 through N define this polygon. Remarks

The glBegin and glEnd functions delimit the vertices that define a primitive or a group of like primitives. The glBegin function accepts a single argument that specifies which of ten primitives the vertices compose. Taking n as an integer count starting at one, and N as the total number of vertices specified, the interpretations are as follows:

 You can use only a subset of OpenGL functions between glBegin and glEnd. The

functions you can use are:

glVertex glColor glIndex glNormal glMaterial

You can also use glCallList or glCallLists to execute display lists that include only the preceding functions. If any other OpenGL function is called between glBegin and glEnd , the error flag is set and the function is ignored.

 Regardless of the value chosen for mode in glBegin , there is no limit to the

number of vertices you can define between glBegin and glEnd. Lines, triangles, quadrilaterals, and polygons that are incompletely specified are not drawn. Incomplete specification results when either too few vertices are provided to specify even a single primitive or when an incorrect multiple of vertices is specified. The incomplete primitive is ignored; the complete primitives are drawn.

 The minimum specification of vertices for each primitive is:

Minimum number of vertices Type of primitive

1 Point

2 Line

3 Triangle

4 Quadrilateral

3 Polygon

docsity.com

Modes that require a certain multiple of vertices are GL_LINES (2), GL_TRIANGLES

(3), GL_QUADS (4), and GL_QUAD_STRIP (2).

Error Codes

The following are the error codes generated and their conditions.

Error code Condition

GL_INVALID_ENUM mode was set to an unaccepted value.

GL_INVALID_OPERATION A function other than glVertex , glColor , glIndex , glNormal , glTexCoord , glEvalCoord , glEvalPoint , glMaterial , glEdgeFlag , glCallList , or glCallLists was called between glBegin and the corresponding glEnd. The function glEnd was called before the corresponding glBegin was called, or glBegin was called within a glBegin / glEnd sequence.

glVertex

These functions specify a vertex.

glVertex2d, glVertex2f, glVertex2i, glVertex2s, glVertex3d, glVertex3f, glVertex3i, glVertex3s, glVertex4d, glVertex4f, glVertex4i, glVertex4s, glVertex2dv, glVertex2fv, glVertex2iv, glVertex2sv, glVertex3dv, glVertex3fv, glVertex3iv, glVertex3sv, glVertex4dv, glVertex4fv, glVertex4iv, glVertex4sv

void glVertex3f( GLfloat x , GLfloat y , GLfloat z ); Consult the documentation to yourself for the functions of the same nature. Parameters x, y, z, w The x , y , z , and w coordinates of a vertex. Not all parameters are present in all forms of the command. Remarks

The glVertex function commands are used within glBegin / glEnd pairs to specify point, line, and polygon vertices. The current color, normal, and texture coordinates are associated with the vertex when glVertex is called.

When only x and y are specified, z defaults to 0.0 and w defaults to 1.0. When x, y, and z are specified, w defaults to 1.0.

Invoking glVertex outside of a glBegin / glEnd pair results in undefined behavior.

docsity.com