OpenGL - Systemintegration

Inhaltsverzeichnisseite

Java3D&gl4java

 

Beispiel fuer ein Vertexprogramm

 

Dieser Beispielcode und der Screenshot entstammen einem kleinen OpenGL

Programm aus dem Cg-Toolkit von Nvidia, welches auch den hier abgebildeten

Assmblercode zur Programmierung des Vertexshader nutzt.

 

Hier der Triceratops mit einer verdrehten Wirbelsaeule.

 

Und hier der dafuer verantwortliche Assemblercode fuer den Vertexshader:

 

!!VP1.0 # Twist

MOV    R0, v[OPOS];

 

MUL    R1.x, R0.x, c[61].x;        # frequency

 

# calculate sin(angle) and cos(angle)

ADD    R1.y, R1.x, -c[62].w;       # R1.y = R1.x + PI/2.0

 

# reduce to period of 2*PI

MUL    R2, R1, c[62].x;            # R2 = R1 / 2.0 * PI

EXP    R3.y, R2.x;                 # R2.y = R2.x - floor(R2.x)

MOV    R3.x, R3.y;

EXP    R3.y, R2.y;                 # R2.y = R2.x - floor(R2.x)

MAD    R2, R3, c[62].y, -c[62].z;  # R2 = (R3 * 2.0*PI) - M_PI

 

# R4.x = sin(R2.x);

# R4.y = cos(R2.y);

# parallel taylor series

MUL    R3,    R2, R2;

MAD    R4, -R3, c[63].w, c[63].z;

MAD    R4, R4, -R3, c[63].y;

MAD    R4, R4, -R3, c[63].x;

MUL    R4, R4, R2;

 

# x    y    z    w

# R:

# 1    0    0    0

# 0    c    -s    0

# 0    s    c    0

# 0    0    0    1

 

# c = cos(a)

# s = sin(a)

 

# calculate rotation around X

MOV    R1, R0;

 

MUL    R1.y, R0.y, R4.y;

MAD    R1.y, R0.z, -R4.x, R1.y;    # ny = y*cos(a) - z*sin(a)

 

MUL    R1.z, R0.y, R4.x;

MAD    R1.z, R0.z, R4.y, R1.z;     # nz = y*sin(a) + z*cos(a)

 

DP4    o[HPOS].x, c[0], R1;        # object x MVP -> clip

DP4    o[HPOS].y, c[1], R1;

DP4    o[HPOS].z, c[2], R1;

DP4    o[HPOS].w, c[3], R1;

 

# rotate normal

MOV    R2, v[NRML];

MUL    R2.y, v[NRML].y, R4.y;

MAD    R2.y, v[NRML].z, -R4.x, R2.y;   # ny = y*cos(a) - z*sin(a)

 

MUL    R2.z, v[NRML].y, R4.x;

MAD    R2.z, v[NRML].z, R4.y, R2.z;    # nz = y*sin(a) + z*cos(a)

 

# diffuse lighting

DP3    R1.x, c[4], R2;             # normal x MV-1T -> lighting normal

DP3    R1.y, c[5], R2;

DP3    R1.z, c[6], R2;

 

DP3    R3, c[32], R1;              # light position DOT normal

MUL    o[COL0].xyz, R3, c[35];     # col = ldotn * diffuse

 

MOV   o[TEX0], v[TEX0];

 

END