1 of 28

Nima Kalantari

CSCE 441 - Computer Graphics

OpenGL

2 of 28

General code structure

int main()

{

Init();

while (WindowIsOpen() == true)

{

glClear(GL_COLOR_BUFFER_BIT);

Display();

UpdateWindowAndEvents();

}

return 0;

}

Drawing primitives

3 of 28

Outline

  • Transformations
  • OpenGL primitives
  • Drawing in old OpenGL
  • Drawing in modern OpenGL

4 of 28

Transformations in OpenGL

  • Transformation consists of three parts
    • Object positioning in world: model matrix
    • World to camera: view matrix
    • Project to 2D: projection matrix
  • Old OpenGL, two matrix stacks
    • GL_MODELVIEW_MATRIX, GL_PROJECTION_MATRIX
    • Could push and pop matrices onto stacks

5 of 28

Transformations in OpenGL

  • New OpenGL: Use own matrix stack
    • e.g., stack<mat4> modelview; modelview.push(mat4(1.0));
    • GLM libraries replace many deprecated commands
  • In old OpenGL and GLM, matrices are column-major and right-multiply top of stack
    • Last transform in code is the first applied

6 of 28

Example

void Display()

{

matrixStack.loadIdentity();

// View and projection transformations

matrixStack.pushMatrix();

matrixStack.Perspective(…);

matrixStack.LookAt(…);

// Model transformation

matrixStack.pushMatrix();

matrixStack.translate(-2.0f, 2.0f, 0.0f);

matrixStack.rotateY(glm::radians(45.0f));

matrixStack.scale(0.8);

DrawCube(matrixStack.topMatrix());

matrixStack.popMatrix();

matrixStack.popMatrix();

}

7 of 28

Outline

  • Transformations
  • OpenGL primitives
  • Drawing in old OpenGL
  • Drawing in modern OpenGL

8 of 28

OpenGL Primitives

Points

Lines (also strips, loops)

Polygon

Triangle

Quad

Quad Strip

Triangle Strip

Triangle Fan

Slide from Ravi Ramamoorthi

9 of 28

Geometry

  • Points
    • GL_POINTS
  • Line segments
    • GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP
  • Triangles
    • GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN

10 of 28

Outline

  • Transformations
  • OpenGL primitives
  • Drawing in old OpenGL
  • Drawing in modern OpenGL

11 of 28

Drawing in Old OpenGL

  • Put vertices between glBegin() and glEnd()

12 of 28

Example

void Display()

{

glBegin(GL_TRIANGLES);

// Triangle 1

glVertex3f(+0.0f, +0.0f, +0.0f);

glVertex3f(+1.0f, +1.0f, +0.0f);

glVertex3f(-1.0f, +1.0f, +0.0f);

// Triangle 2

glVertex3f(+0.0f, +0.0f, +0.0f);

glVertex3f(-1.0f, -1.0f, +0.0f);

glVertex3f(+1.0f, -1.0f, +0.0f);

glEnd();

}

13 of 28

Example

void Display()

{

glBegin(GL_TRIANGLES);

// Triangle 1

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(+0.0f, +0.0f, +0.0f);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3f(+1.0f, +1.0f, +0.0f);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(-1.0f, +1.0f, +0.0f);

// Triangle 2

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(+0.0f, +0.0f, +0.0f);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3f(-1.0f, -1.0f, +0.0f);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(+1.0f, -1.0f, +0.0f);

glEnd();

}

14 of 28

Example

void Display()

{

glBegin(GL_TRIANGLE_STRIP);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(-1.0f, +1.0f, +0.0f);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3f(+1.0f, +1.0f, +0.0f);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(+0.0f, +0.0f, +0.0f);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(+1.0f, -1.0f, +0.0f);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3f(-1.0f, -1.0f, +0.0f);

glEnd();

}

15 of 28

Drawing in Old OpenGL

  • Put vertices between glBegin() and glEnd()
    • glVertex*
    • glColor*
    • glNormal*
    • glTexCoord*

16 of 28

Problem

void Display()

{

glBegin(GL_TRIANGLES);

// Triangle 1

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(+0.0f, +0.0f, +0.0f);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3f(+1.0f, +1.0f, +0.0f);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(-1.0f, +1.0f, +0.0f);

// Triangle 2

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(+0.0f, +0.0f, +0.0f);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3f(-1.0f, -1.0f, +0.0f);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(+1.0f, -1.0f, +0.0f);

glEnd();

}

17 of 28

General code structure

int main()

{

Init();

while (WindowIsOpen() == true)

{

glClear(GL_COLOR_BUFFER_BIT);

Display();

UpdateWindowAndEvents();

}

return 0;

}

18 of 28

Slow!

void Display()

{

glBegin(GL_TRIANGLES);

// Triangle 1

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(+0.0f, +0.0f, +0.0f);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3f(+1.0f, +1.0f, +0.0f);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(-1.0f, +1.0f, +0.0f);

// Triangle 2

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(+0.0f, +0.0f, +0.0f);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3f(-1.0f, -1.0f, +0.0f);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(+1.0f, -1.0f, +0.0f);

glEnd();

}

19 of 28

Outline

  • Transformations
  • OpenGL primitives
  • Drawing in old OpenGL
  • Drawing in modern OpenGL

20 of 28

Drawing in Modern OpenGL

  • Initialization
    • Send the data to the GPU
  • Drawing
    • Point to location of data on the GPU
    • Draw

21 of 28

Example – Without Color

GLuint vpbufferID;

void SendData()

{

GLfloat vertsPos[] =

{

+0.0f, +0.0f, +0.0f,

+1.0f, +1.0f, +0.0f,

-1.0f, +1.0f, +0.0f,

+0.0f, +0.0f, +0.0f,

-1.0f, -1.0f, +0.0f,

+1.0f, -1.0f, +0.0f

};

glGenBuffers(1, &vpbufferID);

glBindBuffer(GL_ARRAY_BUFFER, vpbufferID);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertsPos), vertsPos, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);

}

22 of 28

Example – Without Color

void Display()

{

glBindBuffer(GL_ARRAY_BUFFER, vpbufferID);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, 6);

}

Attribute ID

Number of

Elements

Type

Normalization

Stride

Offset

Primitive

Offset

Number of

vertices

23 of 28

Example – With Color

GLuint vpbufferID;

GLuint vcbufferID;

void SendData()

{

GLfloat vertsPos[] = { // vertices };

glGenBuffers(1, &vpbufferID);

glBindBuffer(GL_ARRAY_BUFFER, vpbufferID);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertsPos), vertsPos, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);

GLfloat vertsCol[] = { // colors };

glGenBuffers(1, &vcbufferID);

glBindBuffer(GL_ARRAY_BUFFER, vcbufferID);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertsCol), vertsCol, GL_STATIC_DRAW);

glEnableVertexAttribArray(1);

}

24 of 28

Example – With Color

void Display()

{

glBindBuffer(GL_ARRAY_BUFFER, vpbufferID);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, vcbufferID);

glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, 6);

}

25 of 28

Example – Combining Color & Position Arrays

GLuint vbufferID;

void SendData()

{

GLfloat verts[] =

{

// x , y , z , r , g , b

+0.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f,

+1.0f, +1.0f, +0.0f, +0.0f, +0.0f, +1.0f,

-1.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f,

+0.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f,

-1.0f, -1.0f, +0.0f, +0.0f, +0.0f, +1.0f,

+1.0f, -1.0f, +0.0f, +0.0f, +1.0f, +0.0f

};

glGenBuffers(1, &vbufferID);

glBindBuffer(GL_ARRAY_BUFFER, vbufferID);

glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);

glEnableVertexAttribArray(1);

}

26 of 28

Example – Combining Color & Position Arrays

void Display()

{

glBindBuffer(GL_ARRAY_BUFFER, vbufferID);

glVertexAttribPointer(0, 3, …,sizeof(float)*6, 0);

glVertexAttribPointer(1, 3, …,sizeof(float)*6, (void*)(sizeof(float)*3));

glDrawArrays(GL_TRIANGLES, 0, 6);

}

Stride

Offset

Number of Elements

27 of 28

Example – glDrawElements

GLuint vbufferID;

GLuint ibufferID;

void SendData()

{

GLfloat verts[] =

{

+0.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f,

+1.0f, +1.0f, +0.0f, +0.0f, +0.0f, +1.0f,

-1.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f,

-1.0f, -1.0f, +0.0f, +0.0f, +0.0f, +1.0f,

+1.0f, -1.0f, +0.0f, +0.0f, +1.0f, +0.0f

};

glGenBuffers(1, &vbufferID);

glBindBuffer(GL_ARRAY_BUFFER, vbufferID);

glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);

glEnableVertexAttribArray(1);

GLuint inds[] = { 0, 1, 2, 0, 3, 4 };

glGenBuffers(1, &ibufferID);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibufferID);

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(inds), inds, GL_STATIC_DRAW);

}

28 of 28

Example – glDrawElements

void Display()

{

glBindBuffer(GL_ARRAY_BUFFER, vbufferID);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibufferID);

glVertexAttribPointer(0, …, sizeof(float) * 6, 0);

glVertexAttribPointer(1, …, sizeof(float) * 6, (void*)(sizeof(float) * 3));

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

}

Primitive

Offset

Number of

Vertices

Type of

Indices