CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018....

22
LAB (1) : OpenGL Tutorial (2) 2021. 03. 15 CS 380 Introduction to Computer Graphics Reference : Foundations of 3D Computer Graphics, Steven J. Gortler

Transcript of CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018....

Page 1: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

LAB (1) : OpenGL Tutorial (2)

2021. 03. 15

CS 380

Introduction to Computer Graphics

Reference : Foundations of 3D Computer Graphics, Steven J. Gortler

Page 2: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Goals

• Set up a project for OpenGL programs.

• Understand texture mapping.

• Practice with shader code.

2

Page 3: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Upgrade Visual Studio Compiler

3

Click OK

Page 4: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Library Setup

4

• Your project should include lib folder contains glew, glut

Page 5: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Include Header Files

5

Page 6: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Include Library Directory

6

Page 7: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Add Library Dependencies

7

Page 8: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

About PPM File

• Image file format such as jpg, png.

• You can open ppm file in Photoshop or Gimp.

• The skeleton code includes simple ppm reader (ppm.cpp)

8

Page 9: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

First Screen

9

Page 10: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

gl2 & gl3 Shaders

10

Page 11: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Texture Binding

• Read image file

• Load an image to GPU

• Setup texture parameters

11

Texture ID (int)

Page 12: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Uniform Variables

12

• Find location of uniform variables in the c++ code.

• Set the values of uniform variables.

• Accept the texture with sampler2D in shader.

Pass texture ID for texture uniform value

Notice that we pass integer value to uTexUnit0, but shader accept the value as sampler2D

Page 13: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Uniform Functions Examples

• glUniform1i : set integer value

• glUniform1f : set float value

• glUniform3i : set 3d integer vector

• glUniform3f : set 3d float vector

• glUniformMatrix3fv: set 3×3 float matrix

• All functions are listed in – https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glUniform.xhtml

13

Page 14: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Homework 1

• Geometric overview

(0.5,0.5)

(0.5,-0.5)(-0.5,-0.5)

(-0.5,0.5)Center point of screen: (0, 0)

Total size of screen: 2 × 2

Page 15: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Reshaping

• glViewport sets the width and height of the viewing box.

• glutPostRedisplay() calls the display callback function.

Detect changing window size Call reshape function

Page 16: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Texture mapping

• texture(uTexUnitX, vTexCoordX) ➔ Color

u

v

(1,1)

(1,0)(0,0)

(0,1)

uTexUnit1

uTexUnit0

vTexCoord1

vTexCoord0

(0,1)

(1,0)

(1,1)

(1,1)

(1,0)(0,0)

(0,1)

Page 17: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

TexCoord and Sampler2D

• vTexCoord (vec2)

– A texture coordinate of each vertex or fragment

• uTexUnit (sampler2d)

– An image we pass to the shaders.

17

(0,1)

(1,0)

(1,1)

(0,0)

Page 18: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Texture mapping

• In a vertex shader

– Set a texture coordinate for each vertex

• In a fragment shader

– As an input of the fragment shader, we get an interpolated texture coordinate for each pixel.

18

Rasterization

(0,1)

(0,0)

(1,0)

(1/3,1/3)

Fragment shader

Vertex shader

Page 19: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Expected Result

19

• The smile image should maintain half size of shorter direction (horizontal, vertical).

Correct Wrong cases

Page 20: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Constraints & Tip for HW1

• Your solution cannot change the vertex coordinate

• Your solution cannot modify the call to glViewport

– Do not change the following line

• Modify shader codes

• Modify uniform variables in your C code

20

Page 21: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

Visual Studio Tips

21

• If you click mouse right button, you can see Go To Definition which leads you to where the variable or function defined.

Page 22: CS 380 Introduction to Computer Graphics - KAISTvclab.kaist.ac.kr/cs380/cs380_lab_02.pdf · 2018. 3. 12. · LAB (1) : OpenGL Tutorial (2) 2018. 03. 12 CS 380 Introduction to Computer

HW1 Submission

• Homework due

– 3/21 (SUN) 23:59

• Submission

– Zip your code folder and upload it on KLMS

– Zip file name: hw1_20161234_Name.zip

• You can submit today if you finish the task.

22