void draw() { Matrix4 view; Matrix4 projection; parg_zcam_matrices(&projection, &view); Matrix4 model = M4MakeIdentity(); Matrix4 modelview = M4Mul(view, model); Matrix4 mvp = M4Mul(projection, modelview); parg_draw_clear(); parg_shader_bind(P_SIMPLE); parg_uniform_matrix4f(U_MVP, &mvp); parg_varray_enable( parg_mesh_coord(trimesh), A_POSITION, 2, PARG_FLOAT, 0, 0); parg_draw_triangles(0, parg_mesh_ntriangles(trimesh)); }
void draw() { Matrix4 view; Matrix4 projection; parg_zcam_matrices(&projection, &view); Matrix4 model = M4MakeIdentity(); Matrix4 modelview = M4Mul(view, model); Matrix4 mvp = M4Mul(projection, modelview); parg_draw_clear(); parg_shader_bind(P_SIMPLE); parg_texture_bind(palmstexture, 0); parg_uniform_matrix4f(U_MVP, &mvp); parg_varray_enable( parg_mesh_coord(rectmesh), A_POSITION, 2, PARG_FLOAT, 0, 0); parg_varray_enable(parg_mesh_uv(rectmesh), A_TEXCOORD, 2, PARG_FLOAT, 0, 0); parg_draw_one_quad(); }
void PezUpdate(unsigned int elapsedMicroseconds) { const float RadiansPerMicrosecond = 0.0000005f; static float Theta = 0; Theta += elapsedMicroseconds * RadiansPerMicrosecond; Vector3 offset = V3MakeFromElems(0, 0, 0); Matrix4 model = M4MakeRotationZ(Theta); model = M4Mul(M4MakeTranslation(offset), model); model = M4Mul(model, M4MakeTranslation(V3Neg(offset))); Point3 eyePosition = P3MakeFromElems(0, 10, 0); Point3 targetPosition = P3MakeFromElems(0, 0, 0); Vector3 upVector = V3MakeFromElems(0, 0, 1); Matrix4 view = M4MakeLookAt(eyePosition, targetPosition, upVector); ModelviewMatrix = M4Mul(view, model); }
void SetUniforms() { glUniformMatrix4fv(u("ViewMatrix"), 1, 0, pf(Scene.ViewMatrix)); glUniformMatrix4fv(u("Projection"), 1, 0, pf(Scene.Projection)); Matrix4 spinMatrix = M4MakeRotationY(Scene.Theta); Matrix4 scaleMatrix = M4MakeScale(V3MakeFromScalar(2.0f)); Matrix4 modelMatrix = M4Mul(scaleMatrix, spinMatrix); Matrix4 modelview = M4Mul(Scene.ViewMatrix, modelMatrix); Matrix3 normalMatrix = M4GetUpper3x3(modelview); glUniformMatrix4fv(u("ModelMatrix"), 1, 0, pf(modelMatrix)); glUniformMatrix4fv(u("Modelview"), 1, 0, pf(modelview)); glUniformMatrix3fv(u("NormalMatrix"), 1, 0, pf(normalMatrix)); glUniform1f(u("YScale"), YScale); glUniform1f(u("YOffset"), YOffset); glUniform4f(u("Color"), 1, 1, 1, 1); glUniform1f(u("TessLevelInner"), TessLevel); glUniform1f(u("TessLevelOuter"), TessLevel); }
void PezUpdate(float seconds) { const float RadiansPerSecond = 0.5f; Globals.Theta += seconds * RadiansPerSecond; Globals.Time += seconds; // Create the model-view matrix: Globals.ModelMatrix = M4MakeRotationZYX((Vector3){Globals.Theta, Globals.Theta, Globals.Theta}); Point3 eye = {0, 0, -80}; Point3 target = {0, 0, 0}; Vector3 up = {0, 1, 0}; Globals.ViewMatrix = M4MakeLookAt(eye, target, up); Globals.Modelview = M4Mul(Globals.ViewMatrix, Globals.ModelMatrix); Globals.NormalMatrix = M4GetUpper3x3(Globals.Modelview); }
void PezUpdate(float seconds) { const float RadiansPerSecond = 0.5f; Globals.Theta += seconds * RadiansPerSecond; // Create the model-view matrix: Globals.Transforms.Model = M4MakeRotationY(Globals.Theta); Point3 eye = {0, 0, 4}; Point3 target = {0, 0, 0}; Vector3 up = {0, 1, 0}; Globals.Transforms.View = M4MakeLookAt(eye, target, up); Globals.Transforms.Modelview = M4Mul(Globals.Transforms.View, Globals.Transforms.Model); Globals.Transforms.Normal = M4GetUpper3x3(Globals.Transforms.Modelview); for (int i = 0; i < 9; ++i) { Globals.Transforms.PackedNormal[i] = M3GetElem(Globals.Transforms.Normal, i/3, i%3); } }
void draw() { Matrix4 modelview = view; Matrix4 mvp = M4Mul(projection, view); // Draw the background. parg_draw_clear(); parg_shader_bind(P_TEXTURE); parg_texture_bind(abstract, 0); parg_uniform_matrix4f(U_MVP, &mvp); parg_varray_enable( parg_mesh_coord(backdrop), A_POSITION, 3, PARG_FLOAT, 0, 0); parg_varray_enable(parg_mesh_uv(backdrop), A_TEXCOORD, 2, PARG_FLOAT, 0, 0); parg_varray_bind(parg_mesh_index(backdrop)); parg_draw_triangles_u16(0, parg_mesh_ntriangles(backdrop)); parg_varray_disable(A_TEXCOORD); // Prep for the scene. modelview = M4Mul(view, model); mvp = M4Mul(projection, modelview); parg_texture_bind(kleintex, 0); parg_uniform_matrix4f(U_MVP, &mvp); parg_varray_enable( parg_mesh_coord(kleingeo), A_POSITION, 3, PARG_FLOAT, 0, 0); parg_varray_enable(parg_mesh_uv(kleingeo), A_TEXCOORD, 2, PARG_FLOAT, 0, 0); parg_varray_bind(parg_mesh_index(kleingeo)); // Draw each chart of the Klein bottle, skipping the podium disk. int start = 0; for (int i = 0; i < NCHARTS; i++) { if (i != PODIUM_CHART) { parg_draw_triangles_u16(start, CHARTS[i]); } start += CHARTS[i]; } // Draw it again for the reflection. parg_framebuffer_pushfbo(reflection, 0); parg_draw_clear(); Matrix4 invert = M4MakeScale((Vector3){1, -1, 1}); parg_state_cullfaces(0); Matrix4 flipped = M4Mul(model, invert); mvp = M4Mul(projection, M4Mul(view, flipped)); parg_uniform_matrix4f(U_MVP, &mvp); start = 0; for (int i = 0; i < NCHARTS; i++) { if (i != PODIUM_CHART) { parg_draw_triangles_u16(start, CHARTS[i]); } start += CHARTS[i]; } mvp = M4Mul(projection, modelview); parg_state_cullfaces(1); parg_framebuffer_popfbo(); // Draw the podium. parg_shader_bind(P_PODIUM); parg_texture_bind(rust, 1); parg_framebuffer_bindtex(reflection, 2); parg_uniform1i(U_RUST, 1); parg_uniform1i(U_REFLECTION, 2); parg_uniform2f(U_RESOLUTION, resolution, resolution); parg_uniform_matrix4f(U_MVP, &mvp); start = 0; for (int i = 0; i < NCHARTS; i++) { if (i == PODIUM_CHART) { parg_draw_triangles_u16(start, CHARTS[i]); } start += CHARTS[i]; } parg_varray_disable(A_TEXCOORD); // Draw the walls of the cylinder. #if CYLINDER parg_shader_bind(P_CYLINDER); parg_uniform_matrix4f(U_MVP, &mvp); parg_varray_enable( parg_mesh_coord(cylinder), A_POSITION, 3, PARG_FLOAT, 0, 0); parg_varray_bind(parg_mesh_index(cylinder)); parg_draw_triangles_u16(0, parg_mesh_ntriangles(cylinder)); #endif // Draw the logo billboard. parg_state_blending(1); parg_state_depthtest(0); parg_shader_bind(P_LOGO); parg_uniform_matrix4f(U_MVP, &mvp); parg_texture_bind(logo, 0); parg_varray_enable( parg_mesh_coord(billboard), A_POSITION, 2, PARG_FLOAT, 0, 0); parg_varray_enable( parg_mesh_coord(billboard), A_TEXCOORD, 2, PARG_FLOAT, 0, 0); parg_draw_one_quad(); parg_varray_disable(A_TEXCOORD); parg_state_blending(0); parg_state_depthtest(1); }
void draw() { int mesh = 0, multi = 0, meshcolor = 0; switch (state) { case STATE_GRAY_SOURCE: parg_shader_bind(P_GRAY); parg_texture_bind(graytex, 0); parg_uniform1f(U_ZSCALE, 1); break; case STATE_COLOR_IH: mesh = 1; parg_shader_bind(P_GRAYMESH); parg_uniform1f(U_ZSCALE, 0.3); break; case STATE_COLOR_DHSCSI: mesh = multi = 1; parg_shader_bind(P_GRAYMESH); parg_uniform1f(U_ZSCALE, 0.3); break; case STATE_MULTI_RGBA: case STATE_MULTI_RGB: case STATE_MULTI_DIAGRAM: meshcolor = mesh = multi = 1; parg_shader_bind(P_GRAYMESH); parg_uniform1f(U_ZSCALE, 0.25); break; case STATE_COLOR_DEFAULT: case STATE_GRAY_DEFAULT: case STATE_GRAY_SIMPLIFY: case STATE_GRAY_INVERT: case STATE_GRAY_HEIGHTS: mesh = 1; parg_shader_bind(P_GRAYMESH); parg_uniform1f(U_ZSCALE, 1); break; case STATE_GRAY_MULTI: mesh = multi = 1; parg_shader_bind(P_GRAYMESH); parg_texture_bind(colortex, 0); parg_uniform1f(U_ZSCALE, 0.3); break; case STATE_GRAY_DUAL: mesh = multi = 1; parg_shader_bind(P_GRAYMESH); parg_texture_bind(colortex, 0); parg_uniform1f(U_ZSCALE, 1); break; case STATE_GRAY_DHS: case STATE_GRAY_DHSC: mesh = multi = 1; parg_shader_bind(P_GRAYMESH); parg_texture_bind(colortex, 0); parg_uniform1f(U_ZSCALE, 0.5); break; case STATE_COLOR_SOURCE: parg_texture_bind(colortex, 0); parg_shader_bind(P_COLOR); parg_uniform1f(U_ZSCALE, 1); break; default: break; } if (mesh) { for (int i = 0; i < sizeof(trimesh) / sizeof(trimesh[0]); i++) { parg_mesh_free(trimesh[i]); } memset(trimesh, 0, sizeof(trimesh)); create_mesh(); } Matrix4 model; if (mesh) { model = M4MakeScale(V3MakeFromElems(20, 20, 10)); model = M4Mul(M4MakeTranslation(V3MakeFromElems(-10, -10, 0)), model); } else { model = M4MakeIdentity(); } Matrix4 modelview = M4Mul(view, model); Matrix4 mvp = M4Mul(projection, modelview); parg_uniform_matrix4f(U_MVP, &mvp); parg_draw_clear(); if (mesh) { Vector4 colors[3]; colors[0] = (Vector4){0, 0.6, 0.9, 1}; colors[1] = (Vector4){0, 0.9, 0.6, 1}; colors[2] = (Vector4){0.9, 0.6, 0, 1}; Vector4 black = {0, 0, 0, 1.0}; for (int imesh = 0; imesh < nmeshes; imesh++) { parg_varray_enable(parg_mesh_coord(trimesh[imesh]), A_POSITION, 3, PARG_FLOAT, 0, 0); parg_varray_bind(parg_mesh_index(trimesh[imesh])); if (meshcolor) { unsigned int b = meshcolors[imesh] & 0xff; unsigned int g = (meshcolors[imesh] >> 8) & 0xff; unsigned int r = (meshcolors[imesh] >> 16) & 0xff; unsigned int a = (meshcolors[imesh] >> 24) & 0xff; Vector4 color; color.x = r / 255.0f; color.y = g / 255.0f; color.z = b / 255.0f; color.w = a / 255.0f; parg_uniform4f(U_COLOR, &color); } else { parg_uniform4f(U_COLOR, &colors[imesh]); } parg_draw_triangles_u16(0, parg_mesh_ntriangles(trimesh[imesh])); parg_uniform4f(U_COLOR, &black); parg_draw_wireframe_triangles_u16( 0, parg_mesh_ntriangles(trimesh[imesh])); } } else {
void PezRender() { #define Instances 7 Matrix4 Model[Instances]; Model[0] = M4MakeRotationY(Globals.Theta); Model[1] = M4Mul(M4Mul( M4MakeTranslation((Vector3){0, 0, 0.6}), M4MakeScale(V3MakeFromScalar(0.25))), M4MakeRotationX(Pi/2) ); Model[2] = Model[3] = Model[4] = Model[1]; Model[1] = M4Mul(M4MakeRotationY(Globals.Theta), Model[1]); Model[2] = M4Mul(M4MakeRotationY(Globals.Theta + Pi/2), Model[2]); Model[3] = M4Mul(M4MakeRotationY(Globals.Theta - Pi/2), Model[3]); Model[4] = M4Mul(M4MakeRotationY(Globals.Theta + Pi), Model[4]); Model[5] = M4Mul(M4Mul( M4MakeScale(V3MakeFromScalar(0.5)), M4MakeTranslation((Vector3){0, 1.25, 0})), M4MakeRotationY(-Globals.Theta) ); Model[6] = M4Mul(M4Mul( M4MakeScale(V3MakeFromScalar(0.5)), M4MakeTranslation((Vector3){0, -1.25, 0})), M4MakeRotationY(-Globals.Theta) ); Vector3 LightPosition = {0.5, 0.25, 1.0}; // world space Vector3 EyePosition = {0, 0, 1}; // world space Matrix4 MVP[Instances]; Vector3 Lhat[Instances]; Vector3 Hhat[Instances]; for (int i = 0; i < Instances; i++) { Matrix4 mv = M4Mul(Globals.View, Model[i]); MVP[i] = M4Mul(Globals.Projection, mv); Matrix3 m = M3Transpose(M4GetUpper3x3(Model[i])); Lhat[i] = M3MulV3(m, V3Normalize(LightPosition)); // object space Vector3 Eye = M3MulV3(m, V3Normalize(EyePosition)); // object space Hhat[i] = V3Normalize(V3Add(Lhat[i], Eye)); } int instanceCount = Instances; MeshPod* mesh = &Globals.Cylinder; glBindFramebuffer(GL_FRAMEBUFFER, Globals.FboHandle); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glUseProgram(Globals.LitProgram); glUniform3f(u("SpecularMaterial"), 0.4, 0.4, 0.4); glUniform4f(u("FrontMaterial"), 0, 0, 1, 1); glUniform4f(u("BackMaterial"), 0.5, 0.5, 0, 1); glUniform3fv(u("Hhat"), Instances, &Hhat[0].x); glUniform3fv(u("Lhat"), Instances, &Lhat[0].x); glUniformMatrix4fv(u("ModelviewProjection"), Instances, 0, (float*) &MVP[0]); glBindVertexArray(mesh->FillVao); glDrawElementsInstanced(GL_TRIANGLES, mesh->FillIndexCount, GL_UNSIGNED_SHORT, 0, instanceCount); glUseProgram(Globals.SimpleProgram); glUniform4f(u("Color"), 0, 0, 0, 1); glUniformMatrix4fv(u("ModelviewProjection"), Instances, 0, (float*) &MVP[0]); glDepthMask(GL_FALSE); glBindVertexArray(mesh->LineVao); glDrawElementsInstanced(GL_LINES, mesh->LineIndexCount, GL_UNSIGNED_SHORT, 0, instanceCount); glDepthMask(GL_TRUE); glDisable(GL_DEPTH_TEST); glBindFramebuffer(GL_FRAMEBUFFER, 0); glUseProgram(Globals.QuadProgram); glBindTexture(GL_TEXTURE_2D, Globals.FboTexture); glBindVertexArray(Globals.Grid.FillVao); glDrawElements(GL_TRIANGLES, Globals.Grid.FillIndexCount, GL_UNSIGNED_SHORT, 0); if (1) { glUseProgram(Globals.GridProgram); glBindVertexArray(Globals.Grid.LineVao); glDrawElements(GL_LINES, Globals.Grid.LineIndexCount, GL_UNSIGNED_SHORT, 0); } glBindTexture(GL_TEXTURE_2D, 0); }