// TODO: alot of slow stuff in it b32 cameraIsPointInFrustum(Camera *cam, Vec3 worldPos) { Plane frustumPlanes[6]; getFrustumPlanes(&cam->perspectiveMatrix, frustumPlanes); for(int i = 0; i < 6; i++) { Vec4 thing = vec4(frustumPlanes[i].a,frustumPlanes[i].b,frustumPlanes[i].c,0.0f); Mat4 mat = cameraCalculateInverseViewMatrix(cam); Vec4 res; mat4Vec4Mul(&res, &mat, &thing); frustumPlanes[i] = planeFromVec4(&res); } Vec3 n[6]; r32 d[6]; for(int planeId = 0; planeId < 6; planeId++) { Vec3 idk = vec3(frustumPlanes[planeId].a,frustumPlanes[planeId].b,frustumPlanes[planeId].c); n[planeId] = vec3Normalized(&idk); d[planeId] = frustumPlanes[planeId].d; } // TODO: check for all 6 planes? Vec3 pos; vec3Sub(&pos, &worldPos, &cam->position); r32 res = vec3Dot(&pos, &n[0]) + d[0] + 141.0f; r32 res2 = vec3Dot(&pos, &n[1]) + d[1] + 141.0f; if(res <= 0.f || res2 <= 0.f) return false; return true; }
Vec3 cameraCalculateRightDirection(Camera* camera) { Quaternion q = quaternion(camera->rotation.w,camera->rotation.x,camera->rotation.y,camera->rotation.z); Mat4 m; mat4FromQuaternion(&m, &q); Vec4 dir = vec4(1.f,0.f,0.f,0.f); Vec4 res; mat4Vec4Mul(&res, &m, &dir); Vec3 res3 = vec3(res.x,res.y,res.z); return vec3Normalized(&res3); }
Vec2 cameraWorldToScreenPoint(Camera *cam, Vec3 worldPos) { Vec4 worldCoord = vec4FromVec3AndW(worldPos, 1.0); Mat4 viewProjection; mat4Mul(&viewProjection, &cam->perspectiveMatrix, &cam->transformMatrix); Vec4 ret4; mat4Vec4Mul(&ret4, &viewProjection, &worldCoord); vec4Scale(&ret4, &ret4, 1.0/ret4.w); Vec2 ret; ret.x = ret4.x; ret.y = ret4.y; return ret; }
Vec3 cameraScreenDepthToWorldPoint(Camera *cam, Vec2 screenPos, r32 depth) { Vec4 screenCoord = vec4(screenPos.x, screenPos.y, depth, 1.0); Mat4 mat = cameraCalculateInverseViewMatrix(cam); Mat4 mat2 = invPerspective(&cam->perspectiveMatrix); Mat4 camInvPerspective; mat4Mul(&camInvPerspective, &mat, &mat2); Vec4 ret4; mat4Vec4Mul(&ret4, &camInvPerspective, &screenCoord); vec4Scale(&ret4, &ret4, 1.0/ret4.w); return vec3FromVec4(ret4); }
void transformToView(struct transform *tr, vec3 vin, vec3 vout) { vec4 in, out; in[0] = vin[0]; in[1] = vin[1]; in[2] = vin[2]; in[3] = 0.0; mat4Vec4Mul(tr->view, in, out); vec4Normalize(out, out); vout[0] = out[0]; vout[1] = out[1]; vout[2] = out[2]; // vec3Normalize(vout); }
void draw(struct hypnomix *hyp) { float angle = hyp->var.smooth[0] * 2.0 * M_PI; // * hyp->pr.var[0]; float translate = hyp->var.smooth[1] * hyp->pr.var[1]; float scale = hyp->var.smooth[2] * hyp->pr.var[2]; float facevtx[] = { -1.0, -1.0, 0.0, 1.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0, 1.0, 0.0, }; float facenrm[] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0 }; float faceclr[] = { 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0 }; unsigned int faceidx[] = { 0, 1, 2, 2, 1, 3 }; skyboxDraw(hyp, 0.0, angle, 0.0); transformIdentity(&hyp->tr); // transformModelScale(&hyp->tr, scale, scale, scale); transformModelRotate(&hyp->tr, 0.0, angle, 0.0); // transformViewRotate(&hyp->tr, 0.0, angle, 0.0); transformViewTranslate(&hyp->tr, 0.0, 0.0, -4.0); transformMVP(&hyp->tr); glUseProgram(hyp->pg.id); glEnableVertexAttribArray(hyp->pg.pos); glEnableVertexAttribArray(hyp->pg.clr); glEnableVertexAttribArray(hyp->pg.nrm); glUniformMatrix4fv(hyp->pg.mvp, 1, GL_FALSE, hyp->tr.mvp); glUniformMatrix3fv(hyp->pg.mnormal, 1, GL_FALSE, hyp->tr.mnormal); // transformMVCoordinates(&hyp->tr, &(hyp->lights.dir[0]), lightdir[0]); //lightdir[0][0] = 0.0; //lightdir[0][1] = 0.0; //lightdir[0][2] = 1.0; // glUniform3fv(hyp->pg.lightdir, hyp->lights.nb, lightdir[0]); // transformMVCoordinates(&hyp->tr, &(hyp->lights.pos[0]), lightpos[0]); // glUniform3fv(hyp->pg.lightpos, hyp->lights.nb, lightpos[0]); vec4 lightdir; vec4 tmp; tmp[0] = hyp->lights.dir[0][0]; tmp[1] = hyp->lights.dir[0][1]; tmp[2] = hyp->lights.dir[0][2]; tmp[3] = 0.0; mat4Vec4Mul(hyp->tr.view, tmp, lightdir); vec4Normalize(lightdir, lightdir); /*fprintf(stderr, "LIGHTDIR=%f,%f,%f,%f\n", lightdir[0], lightdir[1], lightdir[2], lightdir[3]); */ glUniform3fv(hyp->pg.lightdir, hyp->lights.nb, lightdir); //glUniform3fv(hyp->pg.lightpos, hyp->lights.nb, hyp->lights.pos); /* glVertexAttribPointer(hyp->pg.pos, 3, GL_FLOAT, GL_FALSE, 0, vtx); glVertexAttribPointer(hyp->pg.nrm, 3, GL_FLOAT, GL_FALSE, 0, nrm); glVertexAttribPointer(hyp->pg.clr, 4, GL_FLOAT, GL_FALSE, 0, clr); */ //glEnable(GL_CULL_FACE); //glCullFace(GL_BACK); glVertexAttribPointer(hyp->pg.pos, 3, GL_FLOAT, GL_FALSE, 0, facevtx); glVertexAttribPointer(hyp->pg.nrm, 3, GL_FLOAT, GL_FALSE, 0, facenrm); glVertexAttribPointer(hyp->pg.clr, 4, GL_FLOAT, GL_FALSE, 0, faceclr); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, faceidx); // glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, idx); //glDisable(GL_CULL_FACE); /* show normals */ float nrmlines[4*6]; float x, y, z; int i; for(i = 0; i < 4; i++) { x = facevtx[i*3]; y = facevtx[i*3+1]; z = facevtx[i*3+2]; nrmlines[i*6] = x; nrmlines[i*6+1] = y; nrmlines[i*6+2] = z; nrmlines[i*6+3] = x + facenrm[i*3] * 0.4; nrmlines[i*6+4] = y + facenrm[i*3+1] * 0.4; nrmlines[i*6+5] = z + facenrm[i*3+2] * 0.4; } glVertexAttribPointer(hyp->pg.pos, 3, GL_FLOAT, GL_FALSE, 0, nrmlines); glDrawArrays(GL_LINES, 0, 8); /* end show normals */ glDisableVertexAttribArray(hyp->pg.nrm); glDisableVertexAttribArray(hyp->pg.clr); glDisableVertexAttribArray(hyp->pg.pos); }