vec4 vec4MakeWithPoints(vec4 start, vec4 end) { vec4 ret; ret.x = end.x - start.x; ret.y = end.y - start.y; ret.z = end.z - start.z; ret.w = end.w - start.w; vec4Normalize(&ret); return ret; }
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); }
ray Mouse::getRay(const Camera & camera, float win_width, float win_height) { ray myRay; float x = (2.0f * *mouseX) / win_width - 1.0f; float y = 1.0f - (2.0f * *mouseY) / win_height; float z = -1.0f; vec rayNds = getVec(x, y, z); float rayClip[] = {rayNds.x, rayNds.y, z, 1.0}; float rayEye[4]; float tempInverse[16]; float rayWorld[4]; matInverse(&tempInverse[0]); matMultVec4fUtil(rayEye, rayClip, &tempInverse[0]); rayEye[2] = z; rayEye[3] = 0.0; matMultVec4fUtil(rayWorld, rayEye, &tempInverse[0]); vec4 rayWor = getVec4(rayWorld[0], rayWorld[1], rayWorld[2], rayWorld[3]); // don't forget to normalise the vector at some point rayWor = vec4Normalize(rayWor); myRay.origin.x = camera.position.x; myRay.origin.y = camera.position.y; myRay.origin.z = camera.position.z; myRay.direction.x = rayWor.x; myRay.direction.y = rayWor.y; myRay.direction.z = rayWor.z; myRay.direction.w = rayWor.w; return myRay; }
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); }