Draw a rotating cube: Difference between revisions
Content added Content deleted
m (BASIC256 moved to the BASIC section.) |
(→{{header|Evaldraw}}: add evaldraw solution for Draw a Rotating Cube) |
||
Line 938: | Line 938: | ||
call draw |
call draw |
||
. |
. |
||
</syntaxhighlight> |
|||
=={{header|Evaldraw}}== |
|||
Based on the solution in draw cuboid. |
|||
Draws a filled cube with a texture on each face. Uses default since no texture file set. |
|||
<syntaxhighlight lang="C"> |
|||
// We can define our own vec3 struct |
|||
struct vec3{x,y,z;} |
|||
static modelMatrix[9]; |
|||
() { |
|||
cls(0x828282); // clear screen |
|||
clz(1e32); // clear depth buffer |
|||
setcam(0,0,-3,0,0); // set camera some units back |
|||
// create two local arrays to hold rotation matrices |
|||
double roty[9], rotz[9]; |
|||
static otim; |
|||
tim=klock(0); dt=tim-otim; otim=tim; |
|||
static degrees = 0; |
|||
degrees+=200*dt; |
|||
rads = degrees/180*pi; |
|||
rotateZ( rotz, rads ); |
|||
rotateY( roty, rads ); |
|||
// evaldraw does support some GL-like drawing |
|||
// modes, but any transformations must be done by hand |
|||
// Here we use a global model matrix that |
|||
// transforms vertices created by the myVertex function |
|||
mult(modelMatrix, roty, rotz); |
|||
drawcuboid(0,0,0,1,1,1); |
|||
} |
|||
drawcuboid(x,y,z,sx,sy,sz) { |
|||
glBegin(GL_QUADS); |
|||
setcol(192,32,32); |
|||
glTexCoord(0,0); myVertex(x-sx,y-sy,z-sz); |
|||
glTexCoord(1,0); myVertex(x+sx,y-sy,z-sz); |
|||
glTexCoord(1,1); myVertex(x+sx,y+sy,z-sz); |
|||
glTexCoord(0,1); myVertex(x-sx,y+sy,z-sz); |
|||
setcol(32,192,32); |
|||
glTexCoord(0,0); myVertex(x-sx,y-sy,z+sz); |
|||
glTexCoord(1,0); myVertex(x-sx,y-sy,z-sz); |
|||
glTexCoord(1,1); myVertex(x-sx,y+sy,z-sz); |
|||
glTexCoord(0,1); myVertex(x-sx,y+sy,z+sz); |
|||
setcol(32,32,192); |
|||
glTexCoord(0,0); myVertex(x+sx,y-sy,z+sz); |
|||
glTexCoord(1,0); myVertex(x-sx,y-sy,z+sz); |
|||
glTexCoord(1,1); myVertex(x-sx,y+sy,z+sz); |
|||
glTexCoord(0,1); myVertex(x+sx,y+sy,z+sz); |
|||
setcol(192,192,32); |
|||
glTexCoord(0,0); myVertex(x+sx,y-sy,z-sz); |
|||
glTexCoord(1,0); myVertex(x+sx,y-sy,z+sz); |
|||
glTexCoord(1,1); myVertex(x+sx,y+sy,z+sz); |
|||
glTexCoord(0,1); myVertex(x+sx,y+sy,z-sz); |
|||
setcol(192,32,192); |
|||
glTexCoord(0,0); myVertex(x-sx,y-sy,z+sz); |
|||
glTexCoord(1,0); myVertex(x+sx,y-sy,z+sz); |
|||
glTexCoord(1,1); myVertex(x+sx,y-sy,z-sz); |
|||
glTexCoord(0,1); myVertex(x-sx,y-sy,z-sz); |
|||
setcol(32,192,192); |
|||
glTexCoord(0,0); myVertex(x-sx,y+sy,z-sz); |
|||
glTexCoord(1,0); myVertex(x+sx,y+sy,z-sz); |
|||
glTexCoord(1,1); myVertex(x+sx,y+sy,z+sz); |
|||
glTexCoord(0,1); myVertex(x-sx,y+sy,z+sz); |
|||
glEnd(); |
|||
} |
|||
myVertex(x,y,z) { |
|||
// Initialize a struct value |
|||
vec3 v = {x,y,z}; |
|||
// Apply global model matrix transformation |
|||
transformPoint(v, modelMatrix); |
|||
// Submit the vertex to draw list |
|||
glVertex(v.x, v.y, v.z); |
|||
} |
|||
rotateY(m[9], r) { |
|||
c = cos(r); s=sin(r); |
|||
m[0] = c; m[1] = 0; m[2] = s; |
|||
m[3] = 0; m[4] = 1; m[5] = 0; |
|||
m[6] = -s; m[7] = 0; m[8] = c; |
|||
} |
|||
rotateZ(m[9], r) { |
|||
c = cos(r); s=sin(r); |
|||
m[0] = c; m[1] = -s; m[2] = 0; |
|||
m[3] = s; m[4] = c; m[5] = 0; |
|||
m[6] = 0; m[7] = 0; m[8] = 1; |
|||
} |
|||
transformPoint(vec3 v, m[9]) { |
|||
x2 = v.x * m[0] + v.y * m[1] + v.z * m[2]; |
|||
y2 = v.x * m[3] + v.y * m[4] + v.z * m[5]; |
|||
z2 = v.x * m[6] + v.y * m[7] + v.z * m[8]; |
|||
// Mutate the struct v with new values |
|||
v.x=x2; v.y=y2; v.z=z2; |
|||
} |
|||
mult(c[9],a[9],b[9]) { // C = AB |
|||
// multiply a row in A with a column in B |
|||
for(i=0; i<3; i++) |
|||
for(j=0; j<3; j++) { |
|||
sum = 0.0; |
|||
for(k=0; k<3; k++) { |
|||
sum += A[k*3+i] * B[k*3+j]; |
|||
} |
|||
C[i*3+j] = sum; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||