Skip to content

Instantly share code, notes, and snippets.

@CreeperMario
Created June 9, 2018 06:59
Show Gist options
  • Save CreeperMario/ed343a8399a23cf234a522531f321972 to your computer and use it in GitHub Desktop.
Save CreeperMario/ed343a8399a23cf234a522531f321972 to your computer and use it in GitHub Desktop.
My first attempt at "seriously" using GX2.
cmake_minimum_required(VERSION 3.2)
project(glitchy-rectangle C)
include("${WUT_ROOT}/share/wut.cmake" REQUIRED)
add_executable(glitchy-rectangle
glitchy-rectangle.c)
target_link_libraries(glitchy-rectangle
whb
gfd
gx2
proc_ui
nsysnet
sysapp)
wut_create_rpx(glitchy-rectangle.rpx glitchy-rectangle)
#include <gfd.h>
#include <gx2/draw.h>
#include <gx2/shaders.h>
#include <gx2/mem.h>
#include <gx2/registers.h>
#include <gx2r/draw.h>
#include <gx2r/buffer.h>
#include <string.h>
#include <stdio.h>
#include <whb/file.h>
#include <whb/proc.h>
#include <whb/sdcard.h>
#include <whb/gfx.h>
#include <whb/log.h>
#include <whb/log_udp.h>
#include <coreinit/systeminfo.h>
#include <coreinit/thread.h>
#include <coreinit/time.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
GX2RBuffer positionBuffer1 = { 0 };
GX2RBuffer positionBuffer2 = { 0 };
GX2RBuffer colourBuffer1 = { 0 };
GX2RBuffer colourBuffer2 = { 0 };
WHBGfxShaderGroup group = { 0 };
void *buffer = NULL;
char *gshFileData = NULL;
char *sdRootPath = NULL;
char path[256];
int result = 0;
srand(OSGetTick());
WHBLogUdpInit();
WHBProcInit();
WHBGfxInit();
if (!WHBMountSdCard()) {
result = -1;
goto exit;
}
sdRootPath = WHBGetSdCardMountPath();
sprintf(path, "%s/wut/content/pos_col_shader.gsh", sdRootPath);
gshFileData = WHBReadWholeFile(path, NULL);
if (!gshFileData) {
result = -1;
WHBLogPrintf("WHBReadWholeFile(%s) returned NULL", path);
goto exit;
}
if (!WHBGfxLoadGFDShaderGroup(&group, 0, gshFileData)) {
result = -1;
WHBLogPrintf("WHBGfxLoadGFDShaderGroup returned FALSE");
goto exit;
}
WHBGfxInitShaderAttribute(&group, "aPosition", 0, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32_32_32);
WHBGfxInitShaderAttribute(&group, "aColour", 1, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32_32_32);
WHBGfxInitFetchShader(&group);
WHBFreeWholeFile(gshFileData);
gshFileData = NULL;
// Set vertex position
positionBuffer1.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER |
GX2R_RESOURCE_USAGE_CPU_READ |
GX2R_RESOURCE_USAGE_CPU_WRITE |
GX2R_RESOURCE_USAGE_GPU_READ;
positionBuffer1.elemSize = 4 * 4;
positionBuffer1.elemCount = 3;
GX2RCreateBuffer(&positionBuffer1);
positionBuffer2.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER |
GX2R_RESOURCE_USAGE_CPU_READ |
GX2R_RESOURCE_USAGE_CPU_WRITE |
GX2R_RESOURCE_USAGE_GPU_READ;
positionBuffer2.elemSize = 4 * 4;
positionBuffer2.elemCount = 3;
GX2RCreateBuffer(&positionBuffer2);
// Set vertex colour
colourBuffer1.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER |
GX2R_RESOURCE_USAGE_CPU_READ |
GX2R_RESOURCE_USAGE_CPU_WRITE |
GX2R_RESOURCE_USAGE_GPU_READ;
colourBuffer1.elemSize = 4 * 4;
colourBuffer1.elemCount = 3;
GX2RCreateBuffer(&colourBuffer1);
colourBuffer2.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER |
GX2R_RESOURCE_USAGE_CPU_READ |
GX2R_RESOURCE_USAGE_CPU_WRITE |
GX2R_RESOURCE_USAGE_GPU_READ;
colourBuffer2.elemSize = 4 * 4;
colourBuffer2.elemCount = 3;
GX2RCreateBuffer(&colourBuffer2);
WHBLogPrintf("Begin rendering...");
while (WHBProcIsRunning()) {
float topLeftCornerX = -0.4f - ((rand() % 900) / 10000.0f);
float topLeftCornerY = 0.4f + ((rand() % 900) / 10000.0f);
float topRightCornerX = 0.4f + ((rand() % 900) / 10000.0f);
float topRightCornerY = 0.4f + ((rand() % 900) / 10000.0f);
float bottomRightCornerX = 0.4f + ((rand() % 900) / 10000.0f);
float bottomRightCornerY = -0.4f - ((rand() % 900) / 10000.0f);
float bottomLeftCornerX = -0.4f - ((rand() % 900) / 10000.0f);
float bottomLeftCornerY = -0.4f - ((rand() % 900) / 10000.0f);
float topLeftCornerR = 1.0f - ((rand() % 30) / 100.0f);
float topLeftCornerG = 0.0f + ((rand() % 30) / 100.0f);
float topLeftCornerB = 0.0f + ((rand() % 30) / 100.0f);
float topRightCornerR = 0.0f + ((rand() % 30) / 100.0f);
float topRightCornerG = 1.0f - ((rand() % 30) / 100.0f);
float topRightCornerB = 0.0f + ((rand() % 30) / 100.0f);
float bottomRightCornerR = 0.0f + ((rand() % 30) / 100.0f);
float bottomRightCornerG = 0.0f + ((rand() % 30) / 100.0f);
float bottomRightCornerB = 1.0f - ((rand() % 30) / 100.0f);
float bottomLeftCornerR = 0.0f + ((rand() % 30) / 100.0f);
float bottomLeftCornerG = 1.0f - ((rand() % 30) / 100.0f);
float bottomLeftCornerB = 0.0f + ((rand() % 30) / 100.0f);
float * pos1 = (float *)GX2RLockBufferEx(&positionBuffer1, 0);
pos1[0] = topLeftCornerX;
pos1[1] = topLeftCornerY;
pos1[2] = 0.0f;
pos1[3] = 1.0f;
pos1[4] = topRightCornerX;
pos1[5] = topRightCornerY;
pos1[6] = 0.0f;
pos1[7] = 1.0f;
pos1[8] = bottomRightCornerX;
pos1[9] = bottomRightCornerY;
pos1[10] = 0.0f;
pos1[11] = 1.0f;
GX2RUnlockBufferEx(&positionBuffer1, 0);
float * pos2 = (float *)GX2RLockBufferEx(&positionBuffer2, 0);
pos2[0] = bottomRightCornerX;
pos2[1] = bottomRightCornerY;
pos2[2] = 0.0f;
pos2[3] = 1.0f;
pos2[4] = bottomLeftCornerX;
pos2[5] = bottomLeftCornerY;
pos2[6] = 0.0f;
pos2[7] = 1.0f;
pos2[8] = topLeftCornerX;
pos2[9] = topLeftCornerY;
pos2[10] = 0.0f;
pos2[11] = 1.0f;
GX2RUnlockBufferEx(&positionBuffer2, 0);
float * col1 = (float *)GX2RLockBufferEx(&colourBuffer1, 0);
col1[0] = topLeftCornerR;
col1[1] = topLeftCornerG;
col1[2] = topLeftCornerB;
col1[3] = 1.0f;
col1[4] = topRightCornerR;
col1[5] = topRightCornerG;
col1[6] = topRightCornerB;
col1[7] = 1.0f;
col1[8] = bottomRightCornerR;
col1[9] = bottomRightCornerG;
col1[10] = bottomRightCornerB;
col1[11] = 1.0f;
GX2RUnlockBufferEx(&colourBuffer1, 0);
float * col2 = (float *)GX2RLockBufferEx(&colourBuffer2, 0);
col2[0] = bottomRightCornerR;
col2[1] = bottomRightCornerG;
col2[2] = bottomRightCornerB;
col2[3] = 1.0f;
col2[4] = bottomLeftCornerR;
col2[5] = bottomLeftCornerG;
col2[6] = bottomLeftCornerB;
col2[7] = 1.0f;
col2[8] = topLeftCornerR;
col2[9] = topLeftCornerG;
col2[10] = topLeftCornerB;
col2[11] = 1.0f;
GX2RUnlockBufferEx(&colourBuffer2, 0);
// Render!
WHBGfxBeginRender();
WHBGfxBeginRenderTV();
WHBGfxClearColor(1.0f, 1.0f, 1.0f, 1.0f);
GX2SetFetchShader(&group.fetchShader);
GX2SetVertexShader(group.vertexShader);
GX2SetPixelShader(group.pixelShader);
GX2RSetAttributeBuffer(&positionBuffer1, 0, positionBuffer1.elemSize, 0);
GX2RSetAttributeBuffer(&colourBuffer1, 1, colourBuffer1.elemSize, 0);
GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 3, 0, 1);
GX2RSetAttributeBuffer(&positionBuffer2, 0, positionBuffer2.elemSize, 0);
GX2RSetAttributeBuffer(&colourBuffer2, 1, colourBuffer2.elemSize, 0);
GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 3, 0, 1);
WHBGfxFinishRenderTV();
WHBGfxBeginRenderDRC();
WHBGfxClearColor(1.0f, 1.0f, 1.0f, 1.0f);
GX2SetFetchShader(&group.fetchShader);
GX2SetVertexShader(group.vertexShader);
GX2SetPixelShader(group.pixelShader);
GX2RSetAttributeBuffer(&positionBuffer1, 0, positionBuffer1.elemSize, 0);
GX2RSetAttributeBuffer(&colourBuffer1, 1, colourBuffer1.elemSize, 0);
GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 3, 0, 1);
GX2RSetAttributeBuffer(&positionBuffer2, 0, positionBuffer2.elemSize, 0);
GX2RSetAttributeBuffer(&colourBuffer2, 1, colourBuffer2.elemSize, 0);
GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 3, 0, 1);
WHBGfxFinishRenderDRC();
WHBGfxFinishRender();
}
exit:
WHBLogPrintf("Exiting...");
GX2RDestroyBufferEx(&positionBuffer1, 0);
GX2RDestroyBufferEx(&positionBuffer2, 0);
GX2RDestroyBufferEx(&colourBuffer1, 0);
GX2RDestroyBufferEx(&colourBuffer2, 0);
WHBUnmountSdCard();
WHBGfxShutdown();
WHBProcShutdown();
WHBLogUdpDeinit();
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment