Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 <math.h>
- #include <gx2/texture.h>
- #include <gx2/sampler.h>
- #include <malloc.h>
- #include <cglm/cglm.h>
- #include <coreinit/systeminfo.h>
- #include <coreinit/thread.h>
- #include <coreinit/time.h>
- #define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
- #define radToDeg(angleInRadians) ((angleInRadians) * 180.0 / M_PI)
- #define FOV 70
- #define NEAR_PLANE 0.1f
- #define FAR_PLANE 1000
- static const float sPositionData[] = {
- -0.5f, 0.5f, 0.0f, 1.0f, // top
- -0.5f, -0.5f, 0.0f, 1.0f, // bottom left
- 0.5f, -0.5f, 0.0f, 1.0f, // bottom right
- -0.5f, 0.5f, 0.0f, 1.0f, // top
- 0.5f, -0.5f, 0.0f, 1.0f, // bottom right
- 0.5f, 0.5f, 0.0f, 1.0f
- };
- static const float sTexCoordData[] = {
- 0.0f, 1.0f,
- 0.0f, 0.0f,
- 1.0f, 0.0f,
- 0.0f, 1.0f,
- 1.0f, 0.0f,
- 1.0f, 1.0f
- };
- float color[] = {
- 0.0f, 0.0f, 1.0f, 1.0f
- };
- uint32_t texData[] = {
- 0xFF0000FF, 0x00FF00FF, 0x0000FFFF,
- 0xFFFF00FF, 0x00FFFFFF, 0xFF00FFFF,
- 0xFFFFFFFF, 0x808080FF, 0x8080FFFF
- };
- float t;
- mat4 projection;
- mat4 view;
- mat4 model;
- vec3 modelPos;
- GX2Texture tex;
- GX2Sampler sampler;
- int main(int argc, char **argv) {
- GX2RBuffer positionBuffer = { 0 };
- GX2RBuffer texCoordBuffer = { 0 };
- WHBGfxShaderGroup group = { 0 };
- void *buffer = NULL;
- char *gshFileData = NULL;
- char *sdRootPath = NULL;
- char path[256];
- int result = 0;
- WHBLogUdpInit();
- WHBProcInit();
- WHBGfxInit();
- if (!WHBMountSdCard()) {
- result = -1;
- goto exit;
- }
- sdRootPath = WHBGetSdCardMountPath();
- sprintf(path, "%s/shaderStuffTEMP/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, "aTexCoords", 1, 0, GX2_ATTRIB_FORMAT_FLOAT_32_32);
- WHBGfxInitFetchShader(&group);
- WHBFreeWholeFile(gshFileData);
- gshFileData = NULL;
- // Set vertex position
- positionBuffer.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER |
- GX2R_RESOURCE_USAGE_CPU_READ |
- GX2R_RESOURCE_USAGE_CPU_WRITE |
- GX2R_RESOURCE_USAGE_GPU_READ;
- positionBuffer.elemSize = 4 * 4;
- positionBuffer.elemCount = 6;
- GX2RCreateBuffer(&positionBuffer);
- buffer = GX2RLockBufferEx(&positionBuffer, 0);
- memcpy(buffer, sPositionData, positionBuffer.elemSize * positionBuffer.elemCount);
- GX2RUnlockBufferEx(&positionBuffer, 0);
- // Set vertex colour
- texCoordBuffer.flags = GX2R_RESOURCE_BIND_VERTEX_BUFFER |
- GX2R_RESOURCE_USAGE_CPU_READ |
- GX2R_RESOURCE_USAGE_CPU_WRITE |
- GX2R_RESOURCE_USAGE_GPU_READ;
- texCoordBuffer.elemSize = 2 * 4;
- texCoordBuffer.elemCount = 6;
- GX2RCreateBuffer(&texCoordBuffer);
- buffer = GX2RLockBufferEx(&texCoordBuffer, 0);
- memcpy(buffer, sTexCoordData, texCoordBuffer.elemSize * texCoordBuffer.elemCount);
- GX2RUnlockBufferEx(&texCoordBuffer, 0);
- GX2InitSampler(&sampler, GX2_TEX_CLAMP_MODE_CLAMP, GX2_TEX_XY_FILTER_MODE_LINEAR);
- tex.surface.dim = GX2_SURFACE_DIM_TEXTURE_2D;
- tex.surface.width = 1024;
- tex.surface.height = 1024;
- tex.surface.depth = 1;
- tex.surface.mipLevels = 1;
- tex.surface.format = GX2_SURFACE_FORMAT_UINT_R8_G8_B8_A8;
- tex.surface.aa = GX2_AA_MODE1X;
- tex.surface.use = GX2_SURFACE_USE_TEXTURE;
- tex.surface.imageSize = 0;
- tex.surface.image = NULL;
- tex.surface.mipmapSize = 0;
- tex.surface.mipmaps = NULL;
- tex.surface.tileMode = GX2_TILE_MODE_DEFAULT;
- tex.surface.swizzle = 0;
- tex.surface.alignment = 0;
- tex.surface.pitch = 0;
- for(int i = 0; i < 13; i++) tex.surface.mipLevelOffset[i] = 0;
- tex.viewFirstMip = 0;
- tex.viewNumMips = 1;
- tex.viewFirstSlice = 0;
- tex.viewNumSlices = 1;
- tex.compMap = 0x00010203;
- for(int i = 0; i < 5; i++) tex.regs[i] = 0;
- GX2CalcSurfaceSizeAndAlignment(&tex.surface);
- GX2InitTextureRegs(&tex);
- tex.surface.image = memalign(tex.surface.alignment, tex.surface.imageSize);
- memcpy(tex.surface.image, main, tex.surface.imageSize);
- WHBLogPrintf("Begin rendering...");
- while (WHBProcIsRunning()) {
- t += 0.05f;
- //glm_mat4_identity(projection);
- //glm_perspective(degToRad(45.0f), (float)854 / (float)480, 0.1f, 100.0f, projection);
- //glm_mat4_transpose(projection);
- float aspectRatio = (float)854 / (float)480;
- float y_scale = (float)((1.0f / tan(degToRad(FOV / 2.0f))));
- float x_scale = y_scale / aspectRatio;
- float frustum_length = FAR_PLANE - NEAR_PLANE;
- projection[0][0] = x_scale;
- projection[1][1] = y_scale;
- projection[2][2] = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
- projection[2][3] = -1;
- projection[3][2] = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);
- projection[3][3] = 0;
- view[0][0] = 1;
- view[1][1] = 1;
- view[2][2] = 1;
- view[3][3] = 1;
- model[0][0] = 2;
- model[1][1] = 2;
- model[2][2] = 1;
- model[3][3] = 1;
- model[2][3] = -10;
- color[0] = (float)(1 * sin(t));
- color[1] = (float)(1 * sin(t / 2));
- color[2] = (float)(1 * sin(t / 4));
- float* vertices = (float *)GX2RLockBufferEx(&positionBuffer, 0);
- vertices[0] = -0.5 + (0.125 * sin(t * 2));
- vertices[12] = -0.5 + (0.125 * sin(t * 2));
- vertices[20] = 0.5 + (0.125 * sin(t * 2));
- vertices[1] = 0.5 + (0.125 * -cos(t * 4));
- vertices[13] = 0.5 + (0.125 * -cos(t * 4));
- vertices[21] = 0.5 + (0.125 * -cos(t * 4));
- GX2RUnlockBufferEx(&positionBuffer, 0);
- // Render!
- WHBGfxBeginRender();
- WHBGfxBeginRenderTV();
- WHBGfxClearColor(0.5f, 0.5f, 1.0f, 1.0f);
- GX2SetFetchShader(&group.fetchShader);
- GX2SetVertexShader(group.vertexShader);
- GX2SetPixelShader(group.pixelShader);
- GX2RSetAttributeBuffer(&positionBuffer, 0, positionBuffer.elemSize, 0);
- GX2RSetAttributeBuffer(&texCoordBuffer, 1, texCoordBuffer.elemSize, 0);
- GX2SetPixelTexture(&tex, 0);
- GX2SetPixelSampler(&sampler, group.pixelShader->samplerVars[0].location);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[0].offset, 4, (uint32_t *)&color);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[1].offset, 16, (uint32_t *)&projection[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[2].offset, 16, (uint32_t *)&view[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[3].offset, 16, (uint32_t *)&model[0][0]);
- GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 6, 0, 1);
- WHBGfxFinishRenderTV();
- WHBGfxBeginRenderDRC();
- WHBGfxClearColor(0.5f, 0.5f, 1.0f, 1.0f);
- GX2SetFetchShader(&group.fetchShader);
- GX2SetVertexShader(group.vertexShader);
- GX2SetPixelShader(group.pixelShader);
- GX2RSetAttributeBuffer(&positionBuffer, 0, positionBuffer.elemSize, 0);
- GX2RSetAttributeBuffer(&texCoordBuffer, 1, texCoordBuffer.elemSize, 0);
- GX2SetPixelTexture(&tex, 0);
- GX2SetPixelSampler(&sampler, group.pixelShader->samplerVars[0].location);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[0].offset, 4, (uint32_t *)&color);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[1].offset, 16, (uint32_t *)&projection[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[2].offset, 16, (uint32_t *)&view[0][0]);
- GX2SetVertexUniformReg(group.vertexShader->uniformVars[3].offset, 16, (uint32_t *)&model[0][0]);
- GX2DrawEx(GX2_PRIMITIVE_MODE_TRIANGLES, 6, 0, 1);
- WHBGfxFinishRenderDRC();
- WHBGfxFinishRender();
- }
- exit:
- WHBLogPrintf("Exiting...");
- GX2RDestroyBufferEx(&positionBuffer, 0);
- GX2RDestroyBufferEx(&texCoordBuffer, 0);
- WHBUnmountSdCard();
- WHBGfxShutdown();
- WHBProcShutdown();
- WHBLogUdpDeinit();
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement