0% found this document useful (0 votes)
36 views41 pages

Banda Grafica Programabila

The document discusses programmable graphics bands and 3D APIs. It covers topics like the UPG interface, assembling primitives, rasterization, and frame buffers. It also discusses vertex and fragment shaders. The rest of the document talks about Cg programming, including data types like floats, vectors, and matrices. It covers operators for vectors and matrices. It provides examples of lighting calculations in vertex and fragment shaders, including passing parameters between the shaders.

Uploaded by

Alex Balus
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views41 pages

Banda Grafica Programabila

The document discusses programmable graphics bands and 3D APIs. It covers topics like the UPG interface, assembling primitives, rasterization, and frame buffers. It also discusses vertex and fragment shaders. The rest of the document talks about Cg programming, including data types like floats, vectors, and matrices. It covers operators for vectors and matrices. It provides examples of lighting calculations in vertex and fragment shaders, including passing parameters between the shaders.

Uploaded by

Alex Balus
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

Banda grafica programabila

Aplicatie 3D API 3D

UPG
Interfata UPG Asamblare primitive Rasterizare Operatii raster Frame buffer
1

UCP

Vertex shader

Fragment shader

Cg Tipuri de date
Tipuri de date de baza:

32-bit floating point half 16-bit floating point bool Boolean Nu exista tipul int
float struct

Structuri ca in C/C++
2

Cg Tipuri de date
Tipuri de date vector (dimensiune maxima 4):
float4, float3, float2, float1 bool4, bool3, etc Exemplu:

float4 pos;

Tipuri de date matrice (dimensiune maxima 4x4):


float2x2, float 3x3, float4x4 etc Exemplu: float2x3 m1; //matrice 2x3 -> 6 elemente

float2x4 m2; //matrice 2x4 -> 8 elemente

float4x4 m3; //matrice 4x4 -> 16 elemente

Operatori vectori/matrici
Crearea vectorilor float4 a = {1.0, 1.0, 0.0, 1.0}; Operatorul swizzle: componentelor unui vector: a = b.xxyy; float2 vec2 = b.yx; float scalar = b.w; float3 vec3 = scalar.xxx; pentru replicarea/rearanjarea //b = (2.0, 3.0, 4.0,1.0) //a = (2.0, 2.0, 3.0, 3.0) // vec2 = (3.0, 2.0) // scalar = 1 //vec3 = (1.0, 1.0, 1.0)
4

float4 b = {2.0, 3.0, 4.0, 1.0}

Operatori vectori/matrici
Modificarea componentelor unui vector in mod selectiv float4 v1 = {4.0, -2.0, 5.0, 3.0}; float2 v2 = {-2.0, 4.0}; v1.xw = v2; v1.w = 1.0; //v1= (-2.0, -2.0, 5.0, 4.0) //v1= (-2.0, -2.0, 5.0, 1.0)

Operatori vecori/matrici
Operatii pe vectori si matrici: Comparatii (pe componente): +, -, *, /, <, >, == Inmultirea matricilor: float4x4 M; float4 v; mul(M, v); //matrice-vector -> intoarce un vector mul(v, M); //vector-matrice -> intoarce un vector mul(M, N); //matrice matrice -> intoarce o matrice
6

Operatorul swizzle - matrici

Poate fi folosit pentru a crea vectori / valori scalare: ._m<linie><coloana> Exemplu:


float4x4 m; float scalar; float4 v; scalar = m._m32;// m[3][2] v = m._m00_m11_m22_m33; //diagonala principala
7

Vectori

Vectori de elemente declarate ca in C float x[4], y[3][3], float lightpower[8];

sunt diferiti de vectorii predefiniti in Cg float v[4] != float4 v

Profilele pot restrictiona folosirea vectorilor

void MyVertexProgram( float3 lightcolor[10], ) void MyVertexProgram( float3 lightcolor[10], ) { } { }


8

Transmiterea parametrilor catre vertex shader


void vertex_main( float2 position : POSITION, out float4 oposition : POSITION, out float3 ocolor : COLOR) { oposition = float4(position,0,1); ocolor = float3(0,1,0); }
9

glBegin(GL_POLYGON); glVertex2f(-1.0, 2.0 ); glEnd();

Transmiterea parametrilor catre vertex shader


struct v_Output { float4 position : POSITION; float4 color : COLOR; }; v_Output vertex_main(float2 position : POSITION) { v_Output OUT; OUT.position = float4(position, 0, 1); OUT.color = float3(0,1,0);; return OUT; }
10

Transmiterea parametrilor catre vertex shader


void vertex_main( float2 position : POSITION, uniform float4 constantColor, out float4 oposition : POSITION, out float4 ocolor : COLOR) { oposition = float4(position, 0, 1); ocolor = constantColor; }

glColor3f(1.0, 0.0, 0.0); glBegin(GL_POLYGON); glVertex2f(-1.0, 2.0 ); glEnd();

11

Tipuri parametri vertex shader

Parametri de intrare

Parametri variabili: coordonate varf, vectori normala asociati fiecarui varf Parametri uniformi: matricea modelview

Parametrii de iesire (sunt intotdeauna variabili)

12

Setarea parametrilor uniform


CGparameter param_constantColor; CGProgram* vertexProgram; param_constantColor = cgGetNamedParameter(vertexProgram, "constantColor"); float green[4] = {0.0, 1.0, 0.0, 1.0}; cgSetParameter4fv(param_constantColor, green);

13

Banda grafica programabila


Aplicatie 3D API 3D

UPG
Interfata UPG Asamblare primitive Rasterizare Operatii raster Frame buffer
14

UCP

Vertex shader

Fragment shader

Comunicarea vertex shader fragment shader


Comunicatia se realizeaza prin semantici Se specifica semanticile pentru iesirile vertex shaderului Se specifica semanticile pentru intrarile fragment shaderului

15

Exemplu
void vertex(float2 position: POSITION, out float4 oposition : POSITION, out float4 ocolor: COLOR) { oposition = float4(position, 0, 1); ocolor = float4(0, 1, 0, 1); } void fragment(float4 color: COLOR, out float4 ocolor: COLOR) { ocolor = color; }
16

Transformarea varfurilor in vertex shader


void vertex_transform(float4 position : POSITION, out float4 oPosition : POSITION, out float4 color : COLOR, uniform float4x4 modelViewProj) { // Transforma pozitia din spatiul obiect in spatiul de decupare oPosition = mul(modelViewProj, position); color = float4(0.8, 0.8, 0.8, 1.0); }
17

Calculul luminii
I = ambientala + difuza + speculara = Ip* ka + Ip* kd * cos() + Ip* ks * cos()n
ka: coeficientul de difuzie a luminii ambientale kd: coeficientul de difuzie a luminii incidente ks: coeficientul de reflexie speculara N: normala la suprafata in P IP: intestatea luminii N L n: coeficient de stralucire

R V

P
18

Calculul luminii in OpenGL


Is = ambientala + difuza + speculara + emisiva ambientala = Iambientala_globala* ka difuza = Ip* kd * cos() = Ip* kd * max(N.L, 0) speculara = Ip* ks * tip * cos()n = = Ip* ks * tip * (max(N.H, 0)n emisiva = ke ke: coeficientul luminii emisive
tip: tipul fetei (fata /spate) tip = 1 daca N.L > 0 tip = 0, altfel
L P

R V

H=V+L

19

Iluminare vertex shader

Operatii realizate de vertex shader

Se transforma pozitia din spatiul obiect in viewport 3D Se calculeaza culoarea unui varf folosind componentele luminii: emisiva, ambientala, difuza, speculara Calculul luminii se realizeaza in spatiul obiect -> usor de implementat
20

Iluminare vertex shader


P punctul in care se calcueaza iluminarea (specificat in spatiul obiect) N = normala in P lumina emisiva = Ke lumina ambientala = Ka * ambientala_globala L = normalizare(poz_lumina - P) lumina difuza = Kd * lumina* max((N.L), 0) V = normalizare(poz_observator - P) H = normalizare(L + V) if( (N.L) <= 0) tipF = 0 else tipF = 1 lumina speculara = Ks * lumina * tipF* (max((N.H), 0)

culoare = lumina emisiva + lumina ambientala + lumina difuza + lumina speculara


21

Functii oferite de Cg

Functii matematice

abs(x), pow(x, y), exp(x), sqrt(x) cos(x), sin(x), sincos(x, s, c) round(x), ceil(x) cross(A, B), dot(A, B) mul(M, N), mul(M, v), mul(v, M) distance(p1, p2) length(v) normalize(v)
22

Functii geometrice

Iluminare vertex shader


MVP lumina, ambientala_globala P(x,y,z,w) N(nx,ny,nz) poz_lumina, poz_observator ka,kd,ks,ke,n

variabili

uniformi Vertex shader

P(x,y,z,1) C(r,g,b,a)
23

Iluminare vertex shader


void vertex_Light(float4 position : POSITION, float3 normal : NORMAL, out float4 oPosition : POSITION, out float4 color : COLOR,
uniform float4x4 modelViewProj, uniform float3 ambientala_globala, uniform float3 lumina, uniform float3 poz_lumina, uniform float3 poz_observator, uniform float3 Ke, uniform float3 Ka, uniform float3 Kd, uniform float3 Ks, uniform float n)
24

Iluminare vertex shader


oPosition = mul(modelViewProj, position); float3 P = position.xyz; float3 N = normal; float3 emisiva = Ke; float3 ambientala = Ka * ambientala_globala; float3 L = normalize(poz_lumina - P); float NL = max(dot(N, L), 0); float3 difuza = Kd * lumina * NL; lumina emisiva = Ke lumina ambientala = =Ka * ambientala_globala L = normalizare(poz_lumina - P) lumina difuza = =Kd * lumina* max((N.L), 0)

25

Iluminare vertex shader


V = normalizare(poz_observator - P) H = normalizare(L + V) if( (N.L) <= 0) tipF = 0 else tipF = 1 lumina speculara = Ks * lumina * tipF* (max((N.H), 0) n

float3 V = normalize(eyePosition - P); float3 H = normalize(L + V); float NHn = pow(max(dot(N, H), 0), n); if (dot(N, L)<= 0) NHn = 0; float3 speculara = Ks * lumina * specularLight;

color.xyz = emisiva + ambientala + difuza + speculara; color.w = 1;


26

Parametri de intrare - vertex shader


Vertex shad Varful (x, y, z, w) -> POSITION Normala (x, y, z) -> NORMAL variabili uniform gGetNamedParameter, cgSetParameter{1,2,33,4}{i,f,d}v Ka, Kd, Ks, Ke, n Intensitatea luminii ambientale, intensitatea luminii Pozitia sursei de lumina Pozitia observatorului
27

Iluminare fragment shader


Iluminarea realizata in fragment shader Iluminarea este realizata in spatiul obiect

Parametri de intrare Varful (x, y, z, w) -> POSITION Normala (x, y, z) -> NORMAL
28

Semantici - vertex shader

parametri de intrare

POSITION, NORMAL, COLOR

parametri de iesire

POSITION COLOR TEXCOORD0, TEXCOORD1, TEXCOORD2, TEXCOORD3


29

Semantici - fragment shader

parametri de intrare

COLOR TEXCOORDi, i=0..7

parametri de iesire

COLOR DEPTH

30

Fragment shader

Parametri de intrare

Varful (x, y, z, w) -> (x, y, z) Normala (x, y, z) Varful (x, y, z) -> TEXCOORD0 Normala (x, y, z) -> TEXCOORD1

31

Iluminare fragment shader


P(x,y,z,w) N(nx,ny,nz) MVP

variabili

uniformi Vertex shader

P(x,y,z,1) Po(x,y,z), No(nx,ny,nz)

Se transmit catre fragment-shader

32

Iluminare fragment shader


void vertex_Lighting(float4 position : POSITION, float3 normal : NORMAL, out float4 oPosition : POSITION, out float3 objectPos : TEXCOORD0, out float3 oNormal : TEXCOORD1, uniform float4x4 modelViewProj) { oPosition = mul(modelViewProj, position); objectPos = position.xyz; oNormal = normal; }
33

Iluminare fragment shader


Po(x,y,z,w) No(nx,ny,nz)

lumina, ambientala_globala poz_lumina, poz_observator ka,kd,ks,ke,n uniformi

variabili

Fragment shader

C(r,g,b,a)
34

Iluminare - fragment shader


void fragment_Lighting(float4 position : TEXCOORD0, float3 normal : TEXCOORD1, out float4 color : COLOR,

uniform float3 ambientala_globala, uniform float3 lumina, uniform float3 poz_lumina, uniform float3 poz_observator, uniform float3 Ke, uniform float3 Ka, uniform float3 Kd, uniform float3 Ks, uniform float n)
35

Iluminare - fragment shader


float3 P = position.xyz; float3 N = normalize(normal); // lumina emisiva float3 emissive = Ke; //lumina ambientala float3 ambientala = Ka * ambientala_globala; //lumina difuza float3 L = normalize(poz_lumina - P); float NL = max(dot(L, N), 0); float3 difuza = Kd * lumina * NL;
36

Iluminare - fragment shader


// lumina speculara float3 V = normalize(eyePosition - P); float3 H = normalize(L + V); float NHn = pow(max(dot(H, N), 0), n); if (dot(N, L)<= 0) NHn=0; float3 speculara = Ks * lumina * NHn; color.xyz = emisiva + ambientala + difuza + speculara; color.w = 1; }

37

Compilarea in CG
Cg shader
Translatare

Cg Compiler
Offline / runtime

Cg Runtime (cg.lib)

cgGL.lib

OpenGL

Direct 3D

cgD3D.lib

UPG

38

Legatura Cg - OpenGL
#include <Cg/cg.h> #include <Cg/cgGL.h> Creare context
CGContext* context = cgCreateContext();

Creare program
CGProgram* vertexProg = cgCreateProgramFromFile(context, CG_SOURCE, Vertex.cg, profile, Ex_v, NULL); CGprofile profile = cgGLGetLatestProfile(CG_GL_VERTEX); CGprofile profile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
39

Legatura Cg - OpenGL
Incarcare program
cgGLLoadProgram(vertexProg );

Executie program
cgGLEnableProfile(profile) cgGLBindProgram(vertexProg);

Dezactivare profil
cgGLDisableProfile(profile)

40

Bibliografie

Cg The Cg Tutorial R. Fernando, M. Kilgard http://http.developer.nvidia.com/CgTutorial/ Capitolele 2, 3, 5

CgRefrence Manual, CgUsersManual http://developer.nvidia.com/object/cg_down

41

You might also like