How to Use OpenGL/ES on Native Activity
Noritsuna Imamura
noritsuna@siprop.org

©SIProp Project, 2006-2008

1
Agenda
What’s OpenGL?
Kind of OpenGL
Vertions

Hello 3D World
World
Polygon
Projection
Lighting
Texture

Hands on
Use OpenGL/ES 2.0
Shader Programming
©SIProp Project, 2006-2008

2
What’s OpenGL?
OpenGL is 3D Graphic API.
Made by SGI
Since: 1992
Current Version: 4.4
Architecture: PIPE Line Process

©SIProp Project, 2006-2008

3
What’s OpenGL/ES?
OpenGL is 3D Graphic API for Embedded.
Subset from OpenGL
Since: 2003
Current Version: 3.0

Android Version

Android API
Level

OpenGL/ES

1.0(Apple Pie)
2.2(Froyo)
4.3(JerryBeans)

1
8
18

1.0, 1.1
2.0
3.0
©SIProp Project, 2006-2008

4
Bible of OpenGL/ES
OpenGL/ES 2.0 Programming Guide
http://www.amazon.com/dp/0321502795/

OpenGL/ES 3.0 Programming Guide
http://www.amazon.com/dp/0321933885/

©SIProp Project, 2006-2008

5
Function

1.0/1.1

2.0

3.0

Compatibility

ー

ー

2.0

Programmable Shader

ー

GLSL ES 1.0

GLSL ES 3.0

Transform Feedback

ー

ー

Supported

Geometry

glNormal
glVertexPointer
glNormalPointer
glTexCordPointe

glVertexAttribPoint
er

glVertexAttribPoint
er

Matrix

glMatrixMode
glLoadIdentity
glPushMatrix

ー

ー

Lighting

glLight/glMaterial

ー

ー

Fog

glFog

ー

ー

Alpha Test

glAlphaFunc

ー

ー

Point Stripe

glTexEnv

ー

ー

User Clip

glClipPlane

ー

ー

Logic Operation

glLogicOp

ー

ー

FrameBuffer Object

Option

Supported

Supported
Multi-Buffer
©SIProp Project, 2006-2008

6
Hello 3D World!!!

©SIProp Project, 2006-2008

7
What’s 3D World?
Frame of Reference
World
Model(Object)

Coordinates
Vertex
Index
Source Site: http://tech-sketch.jp/2011/10/3d1.html
©SIProp Project, 2006-2008

8
How to Draw Polygon?
TRIANGLE_STRIP
Joint 2 Points

TRIANGLES
Draw each Triangle

TRIANGLE_FAN
Joint 2 Points, but
MUST use 1st Point

QUAD_STRIP
Joint 2 Points

QUAD
Draw each Square

POLYGON
Draw as Polygon

©SIProp Project, 2006-2008

9
How to Set in the World?
After Making Model, Set it to the World Frame.
Affine Transform

©SIProp Project, 2006-2008

10
Where is your Viewpoint(Viewport)?
Perspective Projection
zNear
Minimum Viewpoint of Z-Axis

zFar
Maximum Viewpoint of Z-Axis

fovy
Angle of view of Y-Axis

aspect
Width/height of RealWindow

©SIProp Project, 2006-2008

11
Can you see them?
Z-Buffer
ON => Draw by ZAxis
OFF => Draw by Sort
of Drawing

Alpha Blending
ON => Color is
Blended with Back
Model
OFF => No See

©SIProp Project, 2006-2008

12
There is Dark…
Positional Light
Radiate in all directions

Directional Light
Parallel in all directions

©SIProp Project, 2006-2008

13
Bad Surface…
Put Texture Image to Model.
UV-Mapping (U-horizontal, V-vertical)
Set Relation Between Texture Pixel and Model Vertex.

©SIProp Project, 2006-2008

14
How to Programing?
Use Fixed Function Pipeline in OpenGL/ES 1.1
glScale(x, y, z),
glRotate(Angle, x_moment, y_moment, z_moment)
glTranslate(x, y, z)

©SIProp Project, 2006-2008

15
How to Programing in 2.0or3.0?
Use Programmable Pipeline
VertexShader
FragmentShader

©SIProp Project, 2006-2008

16
Function

1.0/1.1

2.0

3.0

Compatibility

ー

ー

2.0

Programmable Shader

ー

GLSL ES 1.0

GLSL ES 3.0

Transform Feedback

ー

ー

Supported

Geometry

glNormal
glVertexPointer
glNormalPointer
glTexCordPointe

glVertexAttribPoint
er

glVertexAttribPoint
er

Matrix

glMatrixMode
glLoadIdentity
glPushMatrix

ー

ー

Lighting

glLight/glMaterial

ー

ー

Fog

glFog

ー

ー

Alpha Test

glAlphaFunc

ー

ー

Point Stripe

glTexEnv

ー

ー

User Clip

glClipPlane

ー

ー

Logic Operation

glLogicOp

ー

ー

FrameBuffer Object

Option

Supported

Supported
Multi-Buffer
©SIProp Project, 2006-2008

17
What’s Programmable Shader?
Shader(Renderer) that use GLSL (OpenGL
Shading Language).
Vertex Shader
Convert Model Information to World Frame

Geometry Shader (Primitive Shader)
Convert Primitive Data

Pixel Shader (Fragment Shader)
Effect to Each Pixels
Ex. Fogging, Shadowing, …

©SIProp Project, 2006-2008

18
Hello 3D Programming World!!!

©SIProp Project, 2006-2008

19
Today’s Target

©SIProp Project, 2006-2008

20
Naming Rule of Functions
glXXX

yyXXXf

OpenGL Func

eglXXX

float

yyXXXd

OpenGL/ES Func

gluXXX

double

yyXXXi

GUI Tools of Func

int

AGL, GLX, WGL
glutXXX
GLUT Func
GLUT is GUI Tools
of 3rd Party

yyXXX2d
2D of Double

yyXXX3d
3D of Double
©SIProp Project, 2006-2008

21
Setting Files
AndroidManifest.xml
1.

<uses-feature android:glEsVersion="0x00020000"/>

jni/Android.mk
1.

LOCAL_LDLIBS
lEGL -lGLESv2

+= -lm -llog -lc -ldl -lz -landroid -

©SIProp Project, 2006-2008

22
Init EGL World(System) 1/2
Configuration
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

const EGLint attribs[] = {
// Uset Open GL ES 2.0
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
// Set Surface Type as Double Buffer
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
// Minimum Frame Size of Blue
EGL_BLUE_SIZE, 8,
// Minimum Frame Size of Green
EGL_GREEN_SIZE, 8,
// Minimum Frame Size of Red
EGL_RED_SIZE, 8,
// Size of Depth Buffer
EGL_DEPTH_SIZE, 16,
// Terminator
EGL_NONE
©SIProp Project, 2006-2008
};

23
Init EGL World(System) 2/2
Init & Create
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

15.
16.
17.

// Get EGL Display Connection
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
// Init Display
eglInitialize(display, 0, 0);
// Choose Frame Buffer List
eglChooseConfig(display, attribs, &config, 1, &numConfigs);
// Get Frame Buffer Setting
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
// Set Frame Buffer to NativeActivity
ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);
// Get Window Surface
surface = eglCreateWindowSurface(display, config, engine->app->window,
NULL);
// Get Rendering Context
const EGLint attrib_list[] = { EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE };
context = eglCreateContext(display, config, NULL, attrib_list);
// Attach Rendering Context to Window Surface
eglMakeCurrent(display, surface, surface, context);
©SIProp Project, 2006-2008

24
Init Cube Data
Create Programmable Shader!
glMyModel.cpp -> initCube()
1.
2.

// Create Shader Program
glProgram_Cube =
createProgram(gVertexShader_cube,
gFragmentShader_cube);

3.

// Bind Vertex to "Position" in VertexShader
Program
glBindAttribLocation(glProgram_Cube,
ATTRIB_VERTEX, "Position");
// Bind Color to "SourceColor" in VertexShader
Program
glBindAttribLocation(glProgram_Cube,
ATTRIB_COLOR, "SourceColor");

4.

5.
6.

7.
8.

// Set program
glUseProgram(glProgram_Cube);

©SIProp Project, 2006-2008

25
Setting of Programmable Shader
Write Shader Program using GLSL ES
Vertex Shader Source Code
1.
2.
3.
4.
5.
6.
7.
8.
9.

// Vertex Shader Program
static const char gVertexShader_cube[] =
"attribute vec4 Position;
n"
"attribute vec4 SourceColor;
n"
"varying vec4 DestinationColor;n"
"uniform mat4 Projection;
n"
"uniform mat4 Modelview;
n"
"void main() {n"
"
gl_Position = Projection * Modelview *
Position; n"
10.
"
DestinationColor = SourceColor; n"
11.
"}n";
©SIProp Project, 2006-2008

26
Setting of Programmable Shader
Fragment Shader Source Code

1.
2.
3.
4.
5.
6.

// FragmentShader of Color
static const char gFragmentShader_cube[] =
"varying lowp vec4 DestinationColor; n"
"void main() {n"
"gl_FragColor = DestinationColor;n"
"}n";

©SIProp Project, 2006-2008

27
About GLSL: Type
Type

Meaning

void

Same in C Lang

bool

Same in C++ Lang

int

Same in C Lang

float

Same in C Lang

vecX

Vector Type

bvecX

Logical Vector Type

ivecX

Int Vector Type

matX

Matrix Type

samplerXD

Handler of Texture

sampleCube

Handler of Cubic Texture

samplerXDShadow

Handler of Depth Texture

http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.clean.pdf
©SIProp Project, 2006-2008

28
About GLSL: Value
Value

Meaning

const

Same in C Lang

uniform

Global Value

in(Varying)

Input Value

out(Varying)

Output Value

inout(Varying)

Input/Output Value

©SIProp Project, 2006-2008

29
About GLSL: Built-in Function
Function Name

Meaning

radians()

degrees to radians

degrees()

radians to degrees

sin()

Sine

cos()

Cosine

tan()

Tangent

texture()

Set Texture

texture3D()

Set 3D Texture

imageLoad()

Load Image File

imageStore()

Store Image File

©SIProp Project, 2006-2008

30
About GLSL: Built-in Value
Value Name

Type

Meaning

gl_Vertex

vec4

Vertex Coordinates

gl_Normal

vec3

Vertex Normal vector

gl_Color

Vec4

Vertex Color

gl_ModelViewMatrix

mat4

ModelView Matrix

gl_ProjectionMatrix

mat4

Projection Matrix

gl_FrontColor

Vec4

Front Color

gl_BackColor

Vec4

Back Color

gl_TextCoord[]

Vec4

Texture Coordinates

gl_Position

Vec4

Vertex Position

gl_FragColor

Vec4

Color Frag

gl_FlagDepth

float

Depth Frag

©SIProp Project, 2006-2008

31
Check createProgram() Func
glPGShaderTools.cpp
NOT built-in Function. Everyone write same way.

1.

GLuint createProgram(const char* pVertexSource,
const char* pFragmentSource) {

2.

GLuint vertexShader =
loadShader(GL_VERTEX_SHADER, pVertexSource);
if (!vertexShader)
return 0;

3.
4.

5.

GLuint pixelShader =
loadShader(GL_FRAGMENT_SHADER,
pFragmentSource);

6.
©SIProp Project, 2006-2008

32
Set Projection
glPGShaderTools.cpp -> prepareFrame()
getPerspective() is NOT built-in…
1.
2.

3.

GLint projectionUniform =
glGetUniformLocation(glProgram, "Projection");
mat4 projectionMatrix = getPerspective(45, (float)
width / height, 0.5f, 500);
glUniformMatrix4fv(projectionUniform, 1, 0,
projectionMatrix.Pointer());

OpenGL/ES Version… All Built-in…
1.
2.
3.

glMatrixMode(GL_PROJECTON);
glLoadIdentity();
gluPerspective(45, (float)width / height, 0.5f, 500);
©SIProp Project, 2006-2008

33
Where is your Viewpoint(Viewport)?
Perspective Projection
zNear
Minimum Viewpoint of Z-Axis

zFar
Maximum Viewpoint of Z-Axis

fovy
Angle of view of Y-Axis

aspect
Width/height of RealWindow

©SIProp Project, 2006-2008

34
Draw Cube! 1/2
glMyModels.cpp -> drawCube()
Check Matrix.hpp & Vertex.hpp
They have “Affine Transform” Code…
Affine Transform Built-in Func is in OpenGL/ES 1.1…
1.
2.
3.
4.
5.
6.
7.

// Pose for Cube
mat4 rotationX = mat4::RotateX(engine->angleX);
mat4 rotationY = mat4::RotateY(engine->angleY);
mat4 rotationZ = mat4::RotateZ(engine->angleZ);
mat4 scale = mat4::Scale(1.0);
mat4 translation = mat4::Translate(0, 0, -10);
mat4 modelviewMatrix =
scale * rotationX * rotationY *
rotationZ *translation;

8.
9.

// Set pose
int modelviewUniform = glGetUniformLocation(glProgram_Cube,
"Modelview");
glUniformMatrix4fv(modelviewUniform, 1, 0,
modelviewMatrix.Pointer());

10.

©SIProp Project, 2006-2008

35
Draw Cube! 2/2
Draw Cube Polygon
1.
2.
3.
4.
5.
6.
7.
8.
9.

// Set Vertex Array
glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE,
0, cubeVertices);
// Set Color Array
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE,
0, cubeColors);
//Enabled
glEnableVertexAttribArray(ATTRIB_VERTEX);
glEnableVertexAttribArray(ATTRIB_COLOR);
// Draw
glDrawElements(GL_TRIANGLE_STRIP, 14, GL_UNSIGNED_SHORT,
cubeIndices);

©SIProp Project, 2006-2008

36
Vertex of Cube
Draw Cube Polygon
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

// Vertex List of Cube
const GLfloat cubeVertices[] = {
/*
x
y
-1.0,
-1.0,
1.0,
1.0,
-1.0,
1.0,
-1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
-1.0,
-1.0,
-1.0,
1.0,
-1.0,
-1.0,
-1.0,
1.0,
-1.0,
1.0,
1.0,
-1.0,
};

z

*/

//0
//1
//2
//3
//4
//5
//6
//7

©SIProp Project, 2006-2008

37
How to Draw Polygon?
TRIANGLE_STRIP
Joint 2 Points

TRIANGLES
Draw each Triangle

TRIANGLE_FAN
Joint 2 Points, but
MUST use 1st Point

QUAD_STRIP
Joint 2 Points

QUAD
Draw each Square

POLYGON
Draw as Polygon

©SIProp Project, 2006-2008

38
Today’s Target

©SIProp Project, 2006-2008

39

How to Use OpenGL/ES on Native Activity

  • 1.
    How to UseOpenGL/ES on Native Activity Noritsuna Imamura noritsuna@siprop.org ©SIProp Project, 2006-2008 1
  • 2.
    Agenda What’s OpenGL? Kind ofOpenGL Vertions Hello 3D World World Polygon Projection Lighting Texture Hands on Use OpenGL/ES 2.0 Shader Programming ©SIProp Project, 2006-2008 2
  • 3.
    What’s OpenGL? OpenGL is3D Graphic API. Made by SGI Since: 1992 Current Version: 4.4 Architecture: PIPE Line Process ©SIProp Project, 2006-2008 3
  • 4.
    What’s OpenGL/ES? OpenGL is3D Graphic API for Embedded. Subset from OpenGL Since: 2003 Current Version: 3.0 Android Version Android API Level OpenGL/ES 1.0(Apple Pie) 2.2(Froyo) 4.3(JerryBeans) 1 8 18 1.0, 1.1 2.0 3.0 ©SIProp Project, 2006-2008 4
  • 5.
    Bible of OpenGL/ES OpenGL/ES2.0 Programming Guide http://www.amazon.com/dp/0321502795/ OpenGL/ES 3.0 Programming Guide http://www.amazon.com/dp/0321933885/ ©SIProp Project, 2006-2008 5
  • 6.
    Function 1.0/1.1 2.0 3.0 Compatibility ー ー 2.0 Programmable Shader ー GLSL ES1.0 GLSL ES 3.0 Transform Feedback ー ー Supported Geometry glNormal glVertexPointer glNormalPointer glTexCordPointe glVertexAttribPoint er glVertexAttribPoint er Matrix glMatrixMode glLoadIdentity glPushMatrix ー ー Lighting glLight/glMaterial ー ー Fog glFog ー ー Alpha Test glAlphaFunc ー ー Point Stripe glTexEnv ー ー User Clip glClipPlane ー ー Logic Operation glLogicOp ー ー FrameBuffer Object Option Supported Supported Multi-Buffer ©SIProp Project, 2006-2008 6
  • 7.
    Hello 3D World!!! ©SIPropProject, 2006-2008 7
  • 8.
    What’s 3D World? Frameof Reference World Model(Object) Coordinates Vertex Index Source Site: http://tech-sketch.jp/2011/10/3d1.html ©SIProp Project, 2006-2008 8
  • 9.
    How to DrawPolygon? TRIANGLE_STRIP Joint 2 Points TRIANGLES Draw each Triangle TRIANGLE_FAN Joint 2 Points, but MUST use 1st Point QUAD_STRIP Joint 2 Points QUAD Draw each Square POLYGON Draw as Polygon ©SIProp Project, 2006-2008 9
  • 10.
    How to Setin the World? After Making Model, Set it to the World Frame. Affine Transform ©SIProp Project, 2006-2008 10
  • 11.
    Where is yourViewpoint(Viewport)? Perspective Projection zNear Minimum Viewpoint of Z-Axis zFar Maximum Viewpoint of Z-Axis fovy Angle of view of Y-Axis aspect Width/height of RealWindow ©SIProp Project, 2006-2008 11
  • 12.
    Can you seethem? Z-Buffer ON => Draw by ZAxis OFF => Draw by Sort of Drawing Alpha Blending ON => Color is Blended with Back Model OFF => No See ©SIProp Project, 2006-2008 12
  • 13.
    There is Dark… PositionalLight Radiate in all directions Directional Light Parallel in all directions ©SIProp Project, 2006-2008 13
  • 14.
    Bad Surface… Put TextureImage to Model. UV-Mapping (U-horizontal, V-vertical) Set Relation Between Texture Pixel and Model Vertex. ©SIProp Project, 2006-2008 14
  • 15.
    How to Programing? UseFixed Function Pipeline in OpenGL/ES 1.1 glScale(x, y, z), glRotate(Angle, x_moment, y_moment, z_moment) glTranslate(x, y, z) ©SIProp Project, 2006-2008 15
  • 16.
    How to Programingin 2.0or3.0? Use Programmable Pipeline VertexShader FragmentShader ©SIProp Project, 2006-2008 16
  • 17.
    Function 1.0/1.1 2.0 3.0 Compatibility ー ー 2.0 Programmable Shader ー GLSL ES1.0 GLSL ES 3.0 Transform Feedback ー ー Supported Geometry glNormal glVertexPointer glNormalPointer glTexCordPointe glVertexAttribPoint er glVertexAttribPoint er Matrix glMatrixMode glLoadIdentity glPushMatrix ー ー Lighting glLight/glMaterial ー ー Fog glFog ー ー Alpha Test glAlphaFunc ー ー Point Stripe glTexEnv ー ー User Clip glClipPlane ー ー Logic Operation glLogicOp ー ー FrameBuffer Object Option Supported Supported Multi-Buffer ©SIProp Project, 2006-2008 17
  • 18.
    What’s Programmable Shader? Shader(Renderer)that use GLSL (OpenGL Shading Language). Vertex Shader Convert Model Information to World Frame Geometry Shader (Primitive Shader) Convert Primitive Data Pixel Shader (Fragment Shader) Effect to Each Pixels Ex. Fogging, Shadowing, … ©SIProp Project, 2006-2008 18
  • 19.
    Hello 3D ProgrammingWorld!!! ©SIProp Project, 2006-2008 19
  • 20.
  • 21.
    Naming Rule ofFunctions glXXX yyXXXf OpenGL Func eglXXX float yyXXXd OpenGL/ES Func gluXXX double yyXXXi GUI Tools of Func int AGL, GLX, WGL glutXXX GLUT Func GLUT is GUI Tools of 3rd Party yyXXX2d 2D of Double yyXXX3d 3D of Double ©SIProp Project, 2006-2008 21
  • 22.
  • 23.
    Init EGL World(System)1/2 Configuration 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. const EGLint attribs[] = { // Uset Open GL ES 2.0 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, // Set Surface Type as Double Buffer EGL_SURFACE_TYPE, EGL_WINDOW_BIT, // Minimum Frame Size of Blue EGL_BLUE_SIZE, 8, // Minimum Frame Size of Green EGL_GREEN_SIZE, 8, // Minimum Frame Size of Red EGL_RED_SIZE, 8, // Size of Depth Buffer EGL_DEPTH_SIZE, 16, // Terminator EGL_NONE ©SIProp Project, 2006-2008 }; 23
  • 24.
    Init EGL World(System)2/2 Init & Create 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. // Get EGL Display Connection EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); // Init Display eglInitialize(display, 0, 0); // Choose Frame Buffer List eglChooseConfig(display, attribs, &config, 1, &numConfigs); // Get Frame Buffer Setting eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format); // Set Frame Buffer to NativeActivity ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format); // Get Window Surface surface = eglCreateWindowSurface(display, config, engine->app->window, NULL); // Get Rendering Context const EGLint attrib_list[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; context = eglCreateContext(display, config, NULL, attrib_list); // Attach Rendering Context to Window Surface eglMakeCurrent(display, surface, surface, context); ©SIProp Project, 2006-2008 24
  • 25.
    Init Cube Data CreateProgrammable Shader! glMyModel.cpp -> initCube() 1. 2. // Create Shader Program glProgram_Cube = createProgram(gVertexShader_cube, gFragmentShader_cube); 3. // Bind Vertex to "Position" in VertexShader Program glBindAttribLocation(glProgram_Cube, ATTRIB_VERTEX, "Position"); // Bind Color to "SourceColor" in VertexShader Program glBindAttribLocation(glProgram_Cube, ATTRIB_COLOR, "SourceColor"); 4. 5. 6. 7. 8. // Set program glUseProgram(glProgram_Cube); ©SIProp Project, 2006-2008 25
  • 26.
    Setting of ProgrammableShader Write Shader Program using GLSL ES Vertex Shader Source Code 1. 2. 3. 4. 5. 6. 7. 8. 9. // Vertex Shader Program static const char gVertexShader_cube[] = "attribute vec4 Position; n" "attribute vec4 SourceColor; n" "varying vec4 DestinationColor;n" "uniform mat4 Projection; n" "uniform mat4 Modelview; n" "void main() {n" " gl_Position = Projection * Modelview * Position; n" 10. " DestinationColor = SourceColor; n" 11. "}n"; ©SIProp Project, 2006-2008 26
  • 27.
    Setting of ProgrammableShader Fragment Shader Source Code 1. 2. 3. 4. 5. 6. // FragmentShader of Color static const char gFragmentShader_cube[] = "varying lowp vec4 DestinationColor; n" "void main() {n" "gl_FragColor = DestinationColor;n" "}n"; ©SIProp Project, 2006-2008 27
  • 28.
    About GLSL: Type Type Meaning void Samein C Lang bool Same in C++ Lang int Same in C Lang float Same in C Lang vecX Vector Type bvecX Logical Vector Type ivecX Int Vector Type matX Matrix Type samplerXD Handler of Texture sampleCube Handler of Cubic Texture samplerXDShadow Handler of Depth Texture http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.clean.pdf ©SIProp Project, 2006-2008 28
  • 29.
    About GLSL: Value Value Meaning const Samein C Lang uniform Global Value in(Varying) Input Value out(Varying) Output Value inout(Varying) Input/Output Value ©SIProp Project, 2006-2008 29
  • 30.
    About GLSL: Built-inFunction Function Name Meaning radians() degrees to radians degrees() radians to degrees sin() Sine cos() Cosine tan() Tangent texture() Set Texture texture3D() Set 3D Texture imageLoad() Load Image File imageStore() Store Image File ©SIProp Project, 2006-2008 30
  • 31.
    About GLSL: Built-inValue Value Name Type Meaning gl_Vertex vec4 Vertex Coordinates gl_Normal vec3 Vertex Normal vector gl_Color Vec4 Vertex Color gl_ModelViewMatrix mat4 ModelView Matrix gl_ProjectionMatrix mat4 Projection Matrix gl_FrontColor Vec4 Front Color gl_BackColor Vec4 Back Color gl_TextCoord[] Vec4 Texture Coordinates gl_Position Vec4 Vertex Position gl_FragColor Vec4 Color Frag gl_FlagDepth float Depth Frag ©SIProp Project, 2006-2008 31
  • 32.
    Check createProgram() Func glPGShaderTools.cpp NOTbuilt-in Function. Everyone write same way. 1. GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) { 2. GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource); if (!vertexShader) return 0; 3. 4. 5. GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource); 6. ©SIProp Project, 2006-2008 32
  • 33.
    Set Projection glPGShaderTools.cpp ->prepareFrame() getPerspective() is NOT built-in… 1. 2. 3. GLint projectionUniform = glGetUniformLocation(glProgram, "Projection"); mat4 projectionMatrix = getPerspective(45, (float) width / height, 0.5f, 500); glUniformMatrix4fv(projectionUniform, 1, 0, projectionMatrix.Pointer()); OpenGL/ES Version… All Built-in… 1. 2. 3. glMatrixMode(GL_PROJECTON); glLoadIdentity(); gluPerspective(45, (float)width / height, 0.5f, 500); ©SIProp Project, 2006-2008 33
  • 34.
    Where is yourViewpoint(Viewport)? Perspective Projection zNear Minimum Viewpoint of Z-Axis zFar Maximum Viewpoint of Z-Axis fovy Angle of view of Y-Axis aspect Width/height of RealWindow ©SIProp Project, 2006-2008 34
  • 35.
    Draw Cube! 1/2 glMyModels.cpp-> drawCube() Check Matrix.hpp & Vertex.hpp They have “Affine Transform” Code… Affine Transform Built-in Func is in OpenGL/ES 1.1… 1. 2. 3. 4. 5. 6. 7. // Pose for Cube mat4 rotationX = mat4::RotateX(engine->angleX); mat4 rotationY = mat4::RotateY(engine->angleY); mat4 rotationZ = mat4::RotateZ(engine->angleZ); mat4 scale = mat4::Scale(1.0); mat4 translation = mat4::Translate(0, 0, -10); mat4 modelviewMatrix = scale * rotationX * rotationY * rotationZ *translation; 8. 9. // Set pose int modelviewUniform = glGetUniformLocation(glProgram_Cube, "Modelview"); glUniformMatrix4fv(modelviewUniform, 1, 0, modelviewMatrix.Pointer()); 10. ©SIProp Project, 2006-2008 35
  • 36.
    Draw Cube! 2/2 DrawCube Polygon 1. 2. 3. 4. 5. 6. 7. 8. 9. // Set Vertex Array glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, 0, cubeVertices); // Set Color Array glVertexAttribPointer(ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, cubeColors); //Enabled glEnableVertexAttribArray(ATTRIB_VERTEX); glEnableVertexAttribArray(ATTRIB_COLOR); // Draw glDrawElements(GL_TRIANGLE_STRIP, 14, GL_UNSIGNED_SHORT, cubeIndices); ©SIProp Project, 2006-2008 36
  • 37.
    Vertex of Cube DrawCube Polygon 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. // Vertex List of Cube const GLfloat cubeVertices[] = { /* x y -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, }; z */ //0 //1 //2 //3 //4 //5 //6 //7 ©SIProp Project, 2006-2008 37
  • 38.
    How to DrawPolygon? TRIANGLE_STRIP Joint 2 Points TRIANGLES Draw each Triangle TRIANGLE_FAN Joint 2 Points, but MUST use 1st Point QUAD_STRIP Joint 2 Points QUAD Draw each Square POLYGON Draw as Polygon ©SIProp Project, 2006-2008 38
  • 39.