/*---------------------------------------------------------------------------* Project: Horizon File: gx.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: 46365 $ *---------------------------------------------------------------------------*/ /* *------------------------------------------------------------ * 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. *------------------------------------------------------------ */ #include #include #include "gx.h" extern "C" { extern u8* SHADER_BEGIN[]; extern u8* SHADER_END[]; } /* buffer id */ GLuint s_ArrayBufferID; GLuint s_ElementArrayBufferID; /* program id */ GLuint s_PgID; /* shader id */ GLuint s_ShID; //void* s_pShader; /* generate simple object */ GLfloat s_Coords[] = { 0.5f, 0.0f, 0.f, 1.f, -0.5f, 0.5f, 0.f, 1.f, -0.5f,-0.5f, 0.f, 1.f }; GLfloat s_Color[] = { 1.f, 0.0f, 0.0f, 0.f, 1.0f, 0.0f, 0.f, 0.0f, 1.0f }; GLushort s_Idxs[] = {0, 1, 2}; void LoadObjects(void) { glGenBuffers(1, &s_ArrayBufferID); glBindBuffer(GL_ARRAY_BUFFER, s_ArrayBufferID); glBufferData(GL_ARRAY_BUFFER, sizeof(s_Coords) + sizeof(s_Color), 0, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(s_Coords), s_Coords); glBufferSubData(GL_ARRAY_BUFFER, sizeof(s_Coords), sizeof(s_Color), s_Color); glGenBuffers(1, &s_ElementArrayBufferID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s_ElementArrayBufferID); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(s_Idxs), s_Idxs, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0) ; glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)sizeof(s_Coords)); } void UpdateObject(void) { glBindBuffer(GL_ARRAY_BUFFER, s_ArrayBufferID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s_ElementArrayBufferID); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0) ; glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)sizeof(s_Coords)); } void GraphicsDemo::Start() { Device::Start(); s_PgID = glCreateProgram(); s_ShID = glCreateShader(GL_VERTEX_SHADER); // Shader binary setting glShaderBinary(1, &s_ShID, GL_PLATFORM_BINARY_DMP, SHADER_BEGIN, reinterpret_cast(SHADER_END) - reinterpret_cast(SHADER_BEGIN)); glAttachShader(s_PgID, s_ShID); glAttachShader(s_PgID, GL_DMP_FRAGMENT_SHADER_DMP); glBindAttribLocation(s_PgID, 0, "aPosition"); glBindAttribLocation(s_PgID, 1, "aColor"); glLinkProgram(s_PgID); glValidateProgram(s_PgID); glUseProgram(s_PgID); mp_RenderSystem->SetClearColor(NN_GX_DISPLAY0, 0.36f, 0.42f, 0.5f, 1.0f); mp_RenderSystem->SetClearColor(NN_GX_DISPLAY1, 0.f, 0.f, 0.f, 1.0f); glClearDepthf(1.f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glFrontFace(GL_CCW); LoadObjects(); 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); } void GraphicsDemo::End() { Device::End(); } void GraphicsDemo::DrawFrame(void) { static u32 f = 0; glUseProgram(s_PgID); glBindAttribLocation(s_PgID, 0, "aPosition"); glBindAttribLocation(s_PgID, 1, "aColor"); UpdateObject(); nn::math::Matrix44 proj; nn::math::MTX44Frustum(&proj, -0.02f, 0.02f, -0.02f*nn::gx::DISPLAY0_HEIGHT/nn::gx::DISPLAY0_WIDTH, 0.02f*nn::gx::DISPLAY0_HEIGHT/nn::gx::DISPLAY0_WIDTH, 0.2f, 10.f); glUniformMatrix4fv(glGetUniformLocation(s_PgID, "uProjection"), 1, GL_TRUE, static_cast(proj)); nn::math::Matrix34 eye, rot; nn::math::Vector3 camPos(0.f, 0.4f, 9.5f); nn::math::Vector3 camUp(0.f, 1.f, 0.f); nn::math::Vector3 target(0.f, 0.f, 0.f); nn::math::Vector3 rotAxis(0.f, 1.f, 0.f); nn::math::MTX34Identity(&eye); nn::math::MTX34LookAt(&eye, &camPos, &camUp, &target); nn::math::MTX34RotAxisDeg(&rot, &rotAxis, -6.f * (f%60)); nn::math::MTX34Mult(&eye, &eye, &rot); nn::math::Matrix44 mv(eye); glUniformMatrix4fv(glGetUniformLocation(s_PgID, "uModelView"), 1, GL_TRUE, (f32*)(mv)); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0); glFinish(); f++; } /*---------------------------------------------------------------------------* End of file *---------------------------------------------------------------------------*/