From 4c7ce44cdba484990142f3a3091c02200ea076bf Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 6 Apr 2012 17:54:16 +0000 Subject: [PATCH] tutorial: port the rotating cube tutorial to the Xbox 360 and tweak the triangle tutorial to make it shorter. --- test/tutorial/tut01.cpp | 36 ++++++++---------- test/tutorial/tut02.cpp | 82 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 91 insertions(+), 27 deletions(-) diff --git a/test/tutorial/tut01.cpp b/test/tutorial/tut01.cpp index 71c37629..3c452c3f 100644 --- a/test/tutorial/tut01.cpp +++ b/test/tutorial/tut01.cpp @@ -38,13 +38,8 @@ public: Triangle() { m_vertices[0] = vec2( 0.0, 0.8); -#if defined _XBOX - m_vertices[2] = vec2(-0.8, -0.8); - m_vertices[1] = vec2( 0.8, -0.8); -#else m_vertices[1] = vec2(-0.8, -0.8); m_vertices[2] = vec2( 0.8, -0.8); -#endif m_ready = false; } @@ -57,33 +52,29 @@ public: m_shader = Shader::Create( #if !defined __CELLOS_LV2__ && !defined _XBOX "#version 120\n" - "attribute vec2 coord2d;" + "attribute vec2 in_Position;" "void main(void) {" - " gl_Position = vec4(coord2d, 0.0, 1.0);" + " gl_Position = vec4(in_Position, 0.0, 1.0);" "}", "#version 120\n" "void main(void) {" - " gl_FragColor.r = 1.0;" - " gl_FragColor.g = 1.0;" - " gl_FragColor.b = 0.0;" - " gl_FragColor.a = 1.0;" - "}" + " gl_FragColor = vec4(0.7, 0.5, 0.2, 1.0);" + "}"); #else - "void main(float2 coord2d : POSITION," + "void main(float2 in_Position : POSITION," " out float4 out_Position : POSITION) {" - " out_Position = float4(coord2d, 0.0, 1.0);" + " out_Position = float4(in_Position, 0.0, 1.0);" "}", "void main(out float4 out_FragColor : COLOR) {" - " out_FragColor.r = 1.0;" - " out_FragColor.g = 1.0;" - " out_FragColor.b = 0.0;" - " out_FragColor.a = 0.0;" + " out_FragColor = float4(0.7, 0.5, 0.2, 1.0);" "}" #endif ); - m_attrib = m_shader->GetAttribLocation("coord2d"); +#if !defined _XBOX + m_attrib = m_shader->GetAttribLocation("in_Position"); +#endif m_ready = true; #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX @@ -119,6 +110,7 @@ public: m_shader->Bind(); #if defined _XBOX extern D3DDevice *g_d3ddevice; + g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); g_d3ddevice->SetVertexDeclaration(m_vdecl); g_d3ddevice->SetStreamSource(0, m_vbo, 0, sizeof(*m_vertices)); #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ @@ -159,10 +151,12 @@ private: #if defined _XBOX D3DVertexDeclaration *m_vdecl; D3DVertexBuffer *m_vbo; -#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ +#else + int m_attrib; +# if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ GLuint m_vbo; +# endif #endif - int m_attrib; bool m_ready; }; diff --git a/test/tutorial/tut02.cpp b/test/tutorial/tut02.cpp index 8edababf..a4570211 100644 --- a/test/tutorial/tut02.cpp +++ b/test/tutorial/tut02.cpp @@ -92,6 +92,7 @@ public: if (!m_ready) { m_shader = Shader::Create( +#if !defined __CELLOS_LV2__ && !defined _XBOX "#version 120\n" "attribute vec3 in_Vertex;" "attribute vec3 in_Color;" @@ -108,13 +109,31 @@ public: "" "void main(void) {" " gl_FragColor = vec4(pass_Color, 1.0);" - "}"); + "}" +#else + "void main(float3 in_Vertex : POSITION," + " float3 in_Color : COLOR," + " uniform float4x4 in_Matrix," + " out float4 out_Position : POSITION," + " out float3 pass_Color : COLOR) {" + " pass_Color = in_Color;" + " out_Position = mul(in_Matrix, float4(in_Vertex, 1.0));" + "}", + + "void main(float3 pass_Color : COLOR," + " out float4 out_FragColor : COLOR) {" + " out_FragColor = float4(pass_Color, 1.0);" + "}" +#endif + ); +#if !defined _XBOX m_coord = m_shader->GetAttribLocation("in_Vertex"); m_color = m_shader->GetAttribLocation("in_Color"); +#endif m_mvp = m_shader->GetUniformLocation("in_Matrix"); m_ready = true; -#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ +#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX /* Method 1: store vertex buffer on the GPU memory */ glGenBuffers(1, &m_vbo); glBindBuffer(GL_ARRAY_BUFFER, m_vbo); @@ -128,7 +147,43 @@ public: glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_indices), m_indices, GL_STATIC_DRAW); +#elif defined _XBOX + extern D3DDevice *g_d3ddevice; + D3DVERTEXELEMENT9 const elements[] = + { + { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, + { 1, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 }, + D3DDECL_END() + }; + g_d3ddevice->CreateVertexDeclaration(elements, &m_vdecl); + + if (FAILED(g_d3ddevice->CreateVertexBuffer(sizeof(m_vertices), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &m_vbo, NULL))) + exit(0); + + vec3 *vertices; + if (FAILED(m_vbo->Lock(0, 0, (void **)&vertices, 0))) + exit(0); + memcpy(vertices, m_vertices, sizeof(m_vertices)); + m_vbo->Unlock(); + + if (FAILED(g_d3ddevice->CreateVertexBuffer(sizeof(m_colors), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &m_cbo, NULL))) + exit(0); + + vec3 *colors; + if (FAILED(m_cbo->Lock(0, 0, (void **)&colors, 0))) + exit(0); + memcpy(colors, m_colors, sizeof(m_colors)); + m_cbo->Unlock(); + + int16_t *indices; + if (FAILED(g_d3ddevice->CreateIndexBuffer(sizeof(m_indices), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ibo, NULL))) + exit(0); + if (FAILED(m_ibo->Lock(0, 0, (void **)&indices, 0))) + exit(0); + memcpy(indices, m_indices, sizeof(m_indices)); + m_ibo->Unlock(); #else + /* TODO */ #endif /* FIXME: this object never cleans up */ @@ -136,7 +191,15 @@ public: m_shader->Bind(); m_shader->SetUniform(m_mvp, m_matrix); -#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ +#if defined _XBOX + extern D3DDevice *g_d3ddevice; + g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); + g_d3ddevice->SetVertexDeclaration(m_vdecl); + g_d3ddevice->SetStreamSource(0, m_vbo, 0, sizeof(*m_vertices)); + g_d3ddevice->SetStreamSource(1, m_cbo, 0, sizeof(*m_colors)); + g_d3ddevice->SetIndices(m_ibo); + g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 0, 0, sizeof(m_indices) / sizeof(*m_indices)); +#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ glEnableVertexAttribArray(m_coord); glBindBuffer(GL_ARRAY_BUFFER, m_vbo); glVertexAttribPointer(m_coord, 3, GL_FLOAT, GL_FALSE, 0, 0); @@ -169,10 +232,17 @@ private: vec3 m_colors[8]; i16vec3 m_indices[12]; Shader *m_shader; +#if defined _XBOX + D3DVertexDeclaration *m_vdecl; + D3DVertexBuffer *m_vbo, *m_cbo; + D3DIndexBuffer *m_ibo; +#else + int m_coord, m_color; #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ GLuint m_vbo, m_cbo, m_ibo; #endif - int m_coord, m_color, m_mvp; +#endif + int m_mvp; bool m_ready; }; @@ -180,9 +250,9 @@ int main(int argc, char **argv) { Application app("Tutorial 2: Cube", ivec2(640, 480), 60.0f); -#if defined _MSC_VER +#if defined _MSC_VER && !defined _XBOX _chdir(".."); -#elif defined _WIN32 +#elif defined _WIN32 && !defined _XBOX _chdir("../.."); #endif