t_mat4 mat4_view_lookat(t_vec3 eye, t_vec3 targ, t_vec3 up) { t_mat4 out; t_vec3 zaxis; t_vec3 xaxis; t_vec3 yaxis; zaxis = vec3_normal(vec3_sub(eye, targ)); xaxis = vec3_normal(vec3_cross(up, zaxis)); yaxis = vec3_cross(zaxis, xaxis); out = mat4_ident(); out.m[0] = xaxis.x; out.m[1] = yaxis.x; out.m[2] = zaxis.x; out.m[4] = xaxis.y; out.m[5] = yaxis.y; out.m[6] = zaxis.y; out.m[8] = xaxis.z; out.m[9] = yaxis.z; out.m[10] = zaxis.z; out.m[12] = -vec3_dot(xaxis, eye); out.m[13] = -vec3_dot(yaxis, eye); out.m[14] = -vec3_dot(zaxis, eye); return (out); }
/* Create a new buffer object from an array of vec3s for pts, and vec3s for texture coordinates. Faces is an array of 3 shorts for each face. */ BUFFER_T *buffer_new(float pts[][3], float tex[][3], short faces[][3], int numpts, int numfaces) { int i; float normals[numpts][3]; BUFFER_T *buf; NEW(buf); for(i=0; i<numpts; i++) { normals[i][0]=0; normals[i][1]=0; normals[i][2]=1; } for(i=0; i<numfaces; i++) { int a = faces[i][0]; int b = faces[i][1]; int c = faces[i][2]; float tmp[3]; float tmp2[3]; float n[3]; vec3_sub(tmp,pts[b],pts[a]); vec3_sub(tmp2,pts[c],pts[a]); vec3_cross(n,tmp,tmp2); vec3_normal(n,n); vec3_copy(normals[a],n); vec3_copy(normals[b],n); vec3_copy(normals[c],n); } float B[3*3*numpts]; for(i=0; i<numpts; i++) { for(int j=0; j<3; j++) { B[i*3*3+j]=pts[i][j]; B[i*3*3+3+j]=tex[i][j]; B[i*3*3+6+j]=normals[i][j]; } } buf->ntris=numfaces; glGenBuffers(1,&buf->vbuf); glBindBuffer(GL_ARRAY_BUFFER, buf->vbuf); glBufferData(GL_ARRAY_BUFFER, 36*numpts, B, GL_STATIC_DRAW); buf->vbuf_num = numpts; glGenBuffers(1,&buf->ebuf); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buf->ebuf); glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*numfaces, faces, GL_STATIC_DRAW); buf->ebuf_num = numfaces*3; return buf; }
// ************************************************************ void RTPolygon::GenerateNormal(void) { vNormal = vec3_normal(verts[0], verts[1], verts[2], true); }