1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     gx.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46365 $
14  *---------------------------------------------------------------------------*/
15 
16 /*
17  *------------------------------------------------------------
18  * Copyright(c) 2009-2010 by Digital Media Professionals Inc.
19  * All rights reserved.
20  *------------------------------------------------------------
21  * This source code is the confidential and proprietary
22  * of Digital Media Professionals Inc.
23  *------------------------------------------------------------
24  */
25 
26 #include <nn/gx.h>
27 #include <nn/fs.h>
28 #include "gx.h"
29 
30 extern "C" {
31     extern u8* SHADER_BEGIN[];
32     extern u8* SHADER_END[];
33 }
34 
35 /* buffer id */
36 GLuint s_ArrayBufferID;
37 GLuint s_ElementArrayBufferID;
38 
39 /* program id */
40 GLuint s_PgID;
41 
42 /* shader id */
43 GLuint s_ShID;
44 
45 //void* s_pShader;
46 
47 /* generate simple object */
48 GLfloat s_Coords[] = {
49      0.5f, 0.0f, 0.f, 1.f,
50     -0.5f, 0.5f, 0.f, 1.f,
51     -0.5f,-0.5f, 0.f, 1.f
52 };
53 GLfloat s_Color[] = {
54     1.f, 0.0f, 0.0f,
55     0.f, 1.0f, 0.0f,
56     0.f, 0.0f, 1.0f
57 };
58 GLushort s_Idxs[] = {0, 1, 2};
59 
LoadObjects(void)60 void LoadObjects(void)
61 {
62     glGenBuffers(1, &s_ArrayBufferID);
63     glBindBuffer(GL_ARRAY_BUFFER, s_ArrayBufferID);
64     glBufferData(GL_ARRAY_BUFFER, sizeof(s_Coords) + sizeof(s_Color), 0, GL_STATIC_DRAW);
65     glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(s_Coords), s_Coords);
66     glBufferSubData(GL_ARRAY_BUFFER, sizeof(s_Coords), sizeof(s_Color), s_Color);
67 
68     glGenBuffers(1, &s_ElementArrayBufferID);
69     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s_ElementArrayBufferID);
70     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(s_Idxs), s_Idxs, GL_STATIC_DRAW);
71 
72     glEnableVertexAttribArray(0);
73     glEnableVertexAttribArray(1);
74 
75     glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0) ;
76     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)sizeof(s_Coords));
77 }
78 
UpdateObject(void)79 void UpdateObject(void)
80 {
81     glBindBuffer(GL_ARRAY_BUFFER, s_ArrayBufferID);
82     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s_ElementArrayBufferID);
83     glEnableVertexAttribArray(0);
84     glEnableVertexAttribArray(1);
85     glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0) ;
86     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)sizeof(s_Coords));
87 }
88 
89 
Start()90 void GraphicsDemo::Start()
91 {
92     Device::Start();
93 
94     s_PgID = glCreateProgram();
95     s_ShID = glCreateShader(GL_VERTEX_SHADER);
96 
97     // Shader binary setting
98     glShaderBinary(1, &s_ShID, GL_PLATFORM_BINARY_DMP, SHADER_BEGIN, reinterpret_cast<int>(SHADER_END) - reinterpret_cast<int>(SHADER_BEGIN));
99 
100     glAttachShader(s_PgID, s_ShID);
101     glAttachShader(s_PgID, GL_DMP_FRAGMENT_SHADER_DMP);
102 
103     glBindAttribLocation(s_PgID, 0, "aPosition");
104     glBindAttribLocation(s_PgID, 1, "aColor");
105 
106     glLinkProgram(s_PgID);
107     glValidateProgram(s_PgID);
108     glUseProgram(s_PgID);
109 
110     mp_RenderSystem->SetClearColor(NN_GX_DISPLAY0, 0.36f, 0.42f, 0.5f, 1.0f);
111     mp_RenderSystem->SetClearColor(NN_GX_DISPLAY1, 0.f, 0.f, 0.f, 1.0f);
112 
113     glClearDepthf(1.f);
114 
115     glEnable(GL_DEPTH_TEST);
116     glDepthFunc(GL_LESS);
117     glFrontFace(GL_CCW);
118 
119     LoadObjects();
120 
121     glUniform3i(glGetUniformLocation(s_PgID, "dmp_TexEnv[0].srcRgb"), GL_PRIMARY_COLOR, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR);
122     glUniform3i(glGetUniformLocation(s_PgID, "dmp_TexEnv[0].srcAlpha"), GL_PRIMARY_COLOR, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR);
123 }
124 
End()125 void GraphicsDemo::End()
126 {
127     Device::End();
128 }
129 
130 
DrawFrame(void)131 void GraphicsDemo::DrawFrame(void)
132 {
133     static u32 f = 0;
134 
135     glUseProgram(s_PgID);
136     glBindAttribLocation(s_PgID, 0, "aPosition");
137     glBindAttribLocation(s_PgID, 1, "aColor");
138     UpdateObject();
139 
140     nn::math::Matrix44 proj;
141     nn::math::MTX44Frustum(&proj, -0.02f, 0.02f, -0.02f*nn::gx::DISPLAY0_HEIGHT/nn::gx::DISPLAY0_WIDTH,
142                  0.02f*nn::gx::DISPLAY0_HEIGHT/nn::gx::DISPLAY0_WIDTH, 0.2f, 10.f);
143     glUniformMatrix4fv(glGetUniformLocation(s_PgID, "uProjection"), 1, GL_TRUE, static_cast<f32*>(proj));
144 
145     nn::math::Matrix34 eye, rot;
146     nn::math::Vector3 camPos(0.f, 0.4f, 9.5f);
147     nn::math::Vector3 camUp(0.f, 1.f, 0.f);
148     nn::math::Vector3 target(0.f, 0.f, 0.f);
149     nn::math::Vector3 rotAxis(0.f, 1.f, 0.f);
150 
151     nn::math::MTX34Identity(&eye);
152     nn::math::MTX34LookAt(&eye, &camPos, &camUp, &target);
153     nn::math::MTX34RotAxisDeg(&rot, &rotAxis, -6.f * (f%60));
154 
155     nn::math::MTX34Mult(&eye, &eye, &rot);
156     nn::math::Matrix44 mv(eye);
157     glUniformMatrix4fv(glGetUniformLocation(s_PgID, "uModelView"), 1, GL_TRUE, (f32*)(mv));
158 
159     glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);
160     glFinish();
161 
162     f++;
163 }
164 
165 /*---------------------------------------------------------------------------*
166   End of file
167  *---------------------------------------------------------------------------*/
168