/*---------------------------------------------------------------------------* Project: Horizon File: ClipEquation.cpp Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 47228 $ *---------------------------------------------------------------------------*/ /* *------------------------------------------------------------ * Copyright(c) 2009-2010 by Digital Media Professionals Inc. * All rights reserved. *------------------------------------------------------------ * This source code is the confidential and proprietary * of Digital Media Professionals Inc. *------------------------------------------------------------ */ /* * Comments * -------- * * User clipping plane (fragment). * This sample displays a cube with lower left vertex (-10,-10,-10) and upper right at (10,10,10) * The clipping plane is enabled and rotates around the z axis * The clipping plane is defined by the equation -1.x + 0.y + 0.z + 5 = 0 */ #include #include #include #include #include #include #include #include "demo.h" /* program id */ GLuint s_PgID; /* shader id */ GLuint s_ShID; GLuint s_QuadindexID; GLuint s_QuadvertID; GLuint s_QuadcolID; /* ExpHeap for app. */ nn::fnd::ExpHeap s_AppHeap; uptr s_HeapForGx; const u32 s_GxHeapSize = 0x400000; demo::RenderSystem s_RenderSystem; /* load objects */ static void LoadObjects(void) { const float vertex[24] = { -10.0f, -10.0f, -10.0f, -10.0f, 10.0f, -10.0f, 10.0f, 10.0f, -10.0f, 10.0f, -10.0f, -10.0f, -10.0f, -10.0f, 10.0f, -10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f, -10.0f, 10.0f }; const float color[32] = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }; const GLushort s_QuadIndex[36] = { 0, 1, 2, /* back face 1 */ 0, 2, 3, /* back face 2 */ 4, 5, 6, /* front face 1 */ 4, 6, 7, /* front face 2 */ 4, 0, 3, /* bottom face 1 */ 4, 3, 7, /* bottom face 2 */ 5, 1, 2, /* top face 1 */ 5, 2, 6, /* top face 2 */ 4, 5, 1, /* left face 1 */ 4, 1, 0, /* left face 2 */ 7, 6, 2, /* right face 1 */ 7, 2, 3, /* right face 2 */ }; glGenBuffers(1, &s_QuadindexID) ; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s_QuadindexID); glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(GLushort), &s_QuadIndex, GL_STATIC_DRAW); glGenBuffers(1, &s_QuadvertID); glBindBuffer(GL_ARRAY_BUFFER, s_QuadvertID); glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), &vertex, GL_STATIC_DRAW); glGenBuffers(1, &s_QuadcolID); glBindBuffer(GL_ARRAY_BUFFER, s_QuadcolID); glBufferData(GL_ARRAY_BUFFER, 32 * sizeof(GLfloat), &color, GL_STATIC_DRAW); } int DrawFrame(void) { static int f = 0; s_RenderSystem.SetRenderTarget(NN_GX_DISPLAY0); s_RenderSystem.Clear(); /* simple disable culling to see properly clipped and non clipped faces clipped by clipping plane */ glDisable(GL_CULL_FACE); nn::math::Matrix44 proj; nn::math::MTX44Perspective(&proj, 50.0f, static_cast(nn::gx::DISPLAY0_WIDTH) / static_cast(nn::gx::DISPLAY0_HEIGHT), 1.0f, 2000.0f); glUniformMatrix4fv(glGetUniformLocation(s_PgID, "uProjection"), 1, GL_TRUE, static_cast(proj)); nn::math::Matrix34 eye; nn::math::Vector3 camPos(0.0f, 0.0f, 50.0f); nn::math::Vector3 camUp(0.0f, 1.0f, 0.0f); nn::math::Vector3 target(0.0f, 0.0f, 0.0f); nn::math::MTX34Identity(&eye); nn::math::MTX34LookAt(&eye, &camPos, &camUp, &target); nn::math::Matrix44 mv(eye); glUniformMatrix4fv(glGetUniformLocation(s_PgID, "uModelView"), 1, GL_TRUE, static_cast(mv)); /* rotation of the clipping plane */ nn::math::Matrix34 rot; nn::math::MTX34RotXYZDeg(&rot, 0.f, 0.f, static_cast(f)); nn::math::MTX34Mult(&eye, &eye, &rot); nn::math::Vector4 eq(1.0f, 0.0f, 0.0f, -5.0f); nn::math::Matrix44 mmp(proj); nn::math::Matrix44 eye44(eye); nn::math::MTX44Mult(&mmp, &mmp, &eye44); nn::math::MTX44Inverse(&mmp, &mmp); nn::math::MTX44Transpose(&mmp, &mmp); nn::math::Vector4 mmpVec0(mmp.m[0][0], mmp.m[0][1], mmp.m[0][2], mmp.m[0][3]); nn::math::Vector4 mmpVec1(mmp.m[1][0], mmp.m[1][1], mmp.m[1][2], mmp.m[1][3]); nn::math::Vector4 mmpVec2(mmp.m[2][0], mmp.m[2][1], mmp.m[2][2], mmp.m[2][3]); nn::math::Vector4 mmpVec3(mmp.m[3][0], mmp.m[3][1], mmp.m[3][2], mmp.m[3][3]); nn::math::Vector4 p = nn::math::Vector4( nn::math::VEC4Dot(&mmpVec0, &eq), nn::math::VEC4Dot(&mmpVec1, &eq), nn::math::VEC4Dot(&mmpVec2, &eq), nn::math::VEC4Dot(&mmpVec3, &eq) ); glUniform4fv(glGetUniformLocation(s_PgID, "dmp_FragOperation.clippingPlane"), 1, static_cast(p)); glUniform1i(glGetUniformLocation(s_PgID, "dmp_FragOperation.enableClippingPlane"), GL_TRUE); glBindBuffer(GL_ARRAY_BUFFER, s_QuadvertID); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, s_QuadcolID); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s_QuadindexID); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0); s_RenderSystem.SwapBuffers(); s_RenderSystem.SetRenderTarget(NN_GX_DISPLAY1); s_RenderSystem.Clear(); s_RenderSystem.SwapBuffers(); s_RenderSystem.WaitVsync(NN_GX_DISPLAY_BOTH); f++; return !glGetError(); } /* initialization */ static int Initialize(void) { // fs initialization nn::fs::Initialize(); const size_t ROMFS_BUFFER_SIZE = 1024 * 64; static char buffer[ROMFS_BUFFER_SIZE]; NN_UTIL_PANIC_IF_FAILED( nn::fs::MountRom(16, 16, buffer, ROMFS_BUFFER_SIZE)); s_AppHeap.Initialize(nn::os::GetDeviceMemoryAddress(), nn::os::GetDeviceMemorySize() ); s_HeapForGx = reinterpret_cast(s_AppHeap.Allocate(s_GxHeapSize)); /* Initialize display */ s_RenderSystem.Initialize(s_HeapForGx, s_GxHeapSize); glClearColor(0.2f, 0.4f, 0.6f, 1.0f); glClearDepthf(1.f); glViewport(0, 0, nn::gx::DISPLAY0_WIDTH, nn::gx::DISPLAY0_HEIGHT); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_CULL_FACE); glFrontFace(GL_CCW); glCullFace(GL_BACK); /* create program and load & attach vertex shader */ s_PgID = glCreateProgram(); s_ShID = glCreateShader(GL_VERTEX_SHADER); nn::fs::FileReader file(L"rom:/shader.shbin"); size_t fileSize = file.GetSize(); void* buf = s_AppHeap.Allocate(fileSize); s32 read = file.Read(buf, fileSize); glShaderBinary(1, &s_ShID, GL_PLATFORM_BINARY_DMP, buf, read); file.Finalize(); s_AppHeap.Free(buf); glAttachShader(s_PgID, s_ShID); /* attach fixed-function fragment shader */ glAttachShader(s_PgID, GL_DMP_FRAGMENT_SHADER_DMP); glBindAttribLocation(s_PgID, 0, "aPosition"); glBindAttribLocation(s_PgID, 1, "aColor"); glLinkProgram(s_PgID); glValidateProgram(s_PgID); /* set program as current one to enable setting its uniforms */ glUseProgram(s_PgID); LoadObjects(); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glUniform3i(glGetUniformLocation(s_PgID, "dmp_TexEnv[0].srcRgb"), GL_PRIMARY_COLOR, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR); glUniform3i(glGetUniformLocation(s_PgID, "dmp_TexEnv[0].srcAlpha"), GL_PRIMARY_COLOR, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR); return 0; } void nnMain(void) { // Call only nn::applet::Enable to also allow execution from the HOME Menu // HOME Menu transitions, POWER Button presses, and sleep are all unsupported nn::applet::Enable(); /* initialization */ if (Initialize() >= 0) { /* Enter loop */ while (1) { DrawFrame(); } } /* shutdown_display */ s_RenderSystem.Finalize(); s_AppHeap.Free(reinterpret_cast(s_HeapForGx)); s_AppHeap.Finalize(); }