fixed spacing with VS17 CTRL + K, CTRL + F

This commit is contained in:
Niko 2019-02-10 23:53:44 -05:00
parent 43772d9466
commit 7b94f48aab

View File

@ -34,34 +34,34 @@ static int s_nxlinkSock = -1;
static void initNxLink() static void initNxLink()
{ {
if (R_FAILED(socketInitializeDefault())) if (R_FAILED(socketInitializeDefault()))
return; return;
s_nxlinkSock = nxlinkStdio(); s_nxlinkSock = nxlinkStdio();
if (s_nxlinkSock >= 0) if (s_nxlinkSock >= 0)
TRACE("printf output now goes to nxlink server"); TRACE("printf output now goes to nxlink server");
else else
socketExit(); socketExit();
} }
static void deinitNxLink() static void deinitNxLink()
{ {
if (s_nxlinkSock >= 0) if (s_nxlinkSock >= 0)
{ {
close(s_nxlinkSock); close(s_nxlinkSock);
socketExit(); socketExit();
s_nxlinkSock = -1; s_nxlinkSock = -1;
} }
} }
extern "C" void userAppInit() extern "C" void userAppInit()
{ {
initNxLink(); initNxLink();
} }
extern "C" void userAppExit() extern "C" void userAppExit()
{ {
deinitNxLink(); deinitNxLink();
} }
#endif #endif
@ -76,100 +76,100 @@ static EGLSurface s_surface;
static bool initEgl(NWindow* win) static bool initEgl(NWindow* win)
{ {
// Connect to the EGL default display // Connect to the EGL default display
s_display = eglGetDisplay(EGL_DEFAULT_DISPLAY); s_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (!s_display) if (!s_display)
{ {
TRACE("Could not connect to display! error: %d", eglGetError()); TRACE("Could not connect to display! error: %d", eglGetError());
goto _fail0; goto _fail0;
} }
// Initialize the EGL display connection // Initialize the EGL display connection
eglInitialize(s_display, nullptr, nullptr); eglInitialize(s_display, nullptr, nullptr);
// Select OpenGL (Core) as the desired graphics API // Select OpenGL (Core) as the desired graphics API
if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE) if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE)
{ {
TRACE("Could not set API! error: %d", eglGetError()); TRACE("Could not set API! error: %d", eglGetError());
goto _fail1; goto _fail1;
} }
// Get an appropriate EGL framebuffer configuration // Get an appropriate EGL framebuffer configuration
EGLConfig config; EGLConfig config;
EGLint numConfigs; EGLint numConfigs;
static const EGLint framebufferAttributeList[] = static const EGLint framebufferAttributeList[] =
{ {
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_RED_SIZE, 8, EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8, EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8, EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8, EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 24, EGL_DEPTH_SIZE, 24,
EGL_STENCIL_SIZE, 8, EGL_STENCIL_SIZE, 8,
EGL_NONE EGL_NONE
}; };
eglChooseConfig(s_display, framebufferAttributeList, &config, 1, &numConfigs); eglChooseConfig(s_display, framebufferAttributeList, &config, 1, &numConfigs);
if (numConfigs == 0) if (numConfigs == 0)
{ {
TRACE("No config found! error: %d", eglGetError()); TRACE("No config found! error: %d", eglGetError());
goto _fail1; goto _fail1;
} }
// Create an EGL window surface // Create an EGL window surface
s_surface = eglCreateWindowSurface(s_display, config, win, nullptr); s_surface = eglCreateWindowSurface(s_display, config, win, nullptr);
if (!s_surface) if (!s_surface)
{ {
TRACE("Surface creation failed! error: %d", eglGetError()); TRACE("Surface creation failed! error: %d", eglGetError());
goto _fail1; goto _fail1;
} }
// Create an EGL rendering context // Create an EGL rendering context
static const EGLint contextAttributeList[] = static const EGLint contextAttributeList[] =
{ {
EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR, EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,
EGL_CONTEXT_MAJOR_VERSION_KHR, 4, EGL_CONTEXT_MAJOR_VERSION_KHR, 4,
EGL_CONTEXT_MINOR_VERSION_KHR, 3, EGL_CONTEXT_MINOR_VERSION_KHR, 3,
EGL_NONE EGL_NONE
}; };
s_context = eglCreateContext(s_display, config, EGL_NO_CONTEXT, contextAttributeList); s_context = eglCreateContext(s_display, config, EGL_NO_CONTEXT, contextAttributeList);
if (!s_context) if (!s_context)
{ {
TRACE("Context creation failed! error: %d", eglGetError()); TRACE("Context creation failed! error: %d", eglGetError());
goto _fail2; goto _fail2;
} }
// Connect the context to the surface // Connect the context to the surface
eglMakeCurrent(s_display, s_surface, s_surface, s_context); eglMakeCurrent(s_display, s_surface, s_surface, s_context);
return true; return true;
_fail2: _fail2:
eglDestroySurface(s_display, s_surface); eglDestroySurface(s_display, s_surface);
s_surface = nullptr; s_surface = nullptr;
_fail1: _fail1:
eglTerminate(s_display); eglTerminate(s_display);
s_display = nullptr; s_display = nullptr;
_fail0: _fail0:
return false; return false;
} }
static void deinitEgl() static void deinitEgl()
{ {
if (s_display) if (s_display)
{ {
eglMakeCurrent(s_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); eglMakeCurrent(s_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (s_context) if (s_context)
{ {
eglDestroyContext(s_display, s_context); eglDestroyContext(s_display, s_context);
s_context = nullptr; s_context = nullptr;
} }
if (s_surface) if (s_surface)
{ {
eglDestroySurface(s_display, s_surface); eglDestroySurface(s_display, s_surface);
s_surface = nullptr; s_surface = nullptr;
} }
eglTerminate(s_display); eglTerminate(s_display);
s_display = nullptr; s_display = nullptr;
} }
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -178,18 +178,18 @@ static void deinitEgl()
static void setMesaConfig() static void setMesaConfig()
{ {
// Uncomment below to disable error checking and save CPU time (useful for production): // Uncomment below to disable error checking and save CPU time (useful for production):
//setenv("MESA_NO_ERROR", "1", 1); //setenv("MESA_NO_ERROR", "1", 1);
// Uncomment below to enable Mesa logging: // Uncomment below to enable Mesa logging:
//setenv("EGL_LOG_LEVEL", "debug", 1); //setenv("EGL_LOG_LEVEL", "debug", 1);
//setenv("MESA_VERBOSE", "all", 1); //setenv("MESA_VERBOSE", "all", 1);
//setenv("NOUVEAU_MESA_DEBUG", "1", 1); //setenv("NOUVEAU_MESA_DEBUG", "1", 1);
// Uncomment below to enable shader debugging in Nouveau: // Uncomment below to enable shader debugging in Nouveau:
//setenv("NV50_PROG_OPTIMIZE", "0", 1); //setenv("NV50_PROG_OPTIMIZE", "0", 1);
//setenv("NV50_PROG_DEBUG", "1", 1); //setenv("NV50_PROG_DEBUG", "1", 1);
//setenv("NV50_PROG_CHIPSET", "0x120", 1); //setenv("NV50_PROG_CHIPSET", "0x120", 1);
} }
static const char* const vertexShaderSource = R"text( static const char* const vertexShaderSource = R"text(
@ -308,35 +308,35 @@ static const char* const fragmentShaderSource = R"text(
static GLuint createAndCompileShader(GLenum type, const char* source) static GLuint createAndCompileShader(GLenum type, const char* source)
{ {
GLint success; GLint success;
GLchar msg[512]; GLchar msg[512];
GLuint handle = glCreateShader(type); GLuint handle = glCreateShader(type);
if (!handle) if (!handle)
{ {
TRACE("%u: cannot create shader", type); TRACE("%u: cannot create shader", type);
return 0; return 0;
} }
glShaderSource(handle, 1, &source, nullptr); glShaderSource(handle, 1, &source, nullptr);
glCompileShader(handle); glCompileShader(handle);
glGetShaderiv(handle, GL_COMPILE_STATUS, &success); glGetShaderiv(handle, GL_COMPILE_STATUS, &success);
if (success == GL_FALSE) if (success == GL_FALSE)
{ {
glGetShaderInfoLog(handle, sizeof(msg), nullptr, msg); glGetShaderInfoLog(handle, sizeof(msg), nullptr, msg);
TRACE("%u: %s\n", type, msg); TRACE("%u: %s\n", type, msg);
glDeleteShader(handle); glDeleteShader(handle);
return 0; return 0;
} }
return handle; return handle;
} }
typedef struct typedef struct
{ {
float position[3]; float position[3];
float texcoord[2]; float texcoord[2];
float normal[3]; float normal[3];
} Vertex; } Vertex;
static GLuint s_program; static GLuint s_program;
@ -352,17 +352,17 @@ static GLint loc_tex_diffuse, loc_tex_specular, loc_tex_ambOcc, loc_tex_normal,
static u64 s_startTicks; static u64 s_startTicks;
void readPNG(const char* path, char** data, int* size) void readPNG(const char* path, char** data, int* size)
{ {
FILE *fp = fopen ( path , "rb" ); FILE *fp = fopen(path, "rb");
fseek( fp , 0L , SEEK_END); fseek(fp, 0L, SEEK_END);
*size = ftell( fp ); *size = ftell(fp);
rewind( fp ); rewind(fp);
*data = (char*)calloc( 1, *size+1 ); *data = (char*)calloc(1, *size + 1);
fread( *data , *size, 1 , fp); fread(*data, *size, 1, fp);
fclose(fp); fclose(fp);
} }
Vertex* gunList; Vertex* gunList;
@ -370,71 +370,71 @@ int gunNumVerts;
void readOBJ(const char* path) void readOBJ(const char* path)
{ {
FILE* f = fopen(path, "r"); FILE* f = fopen(path, "r");
float x[3]; float x[3];
unsigned short y[9]; unsigned short y[9];
std::vector<float>verts; std::vector<float>verts;
std::vector<float>uvs; std::vector<float>uvs;
std::vector<float>norms; std::vector<float>norms;
std::vector<unsigned short>faces; std::vector<unsigned short>faces;
char line[100]; char line[100];
while (fgets(line, sizeof(line), f)) while (fgets(line, sizeof(line), f))
{ {
if (sscanf(line, "v %f %f %f", &x[0], &x[1], &x[2]) == 3) if (sscanf(line, "v %f %f %f", &x[0], &x[1], &x[2]) == 3)
{ {
for(int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
verts.push_back(x[i]); verts.push_back(x[i]);
} }
if (sscanf(line, "vt %f %f", &x[0], &x[1]) == 2) if (sscanf(line, "vt %f %f", &x[0], &x[1]) == 2)
{ {
for(int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
uvs.push_back(x[i]); uvs.push_back(x[i]);
} }
if (sscanf(line, "vn %f %f %f", &x[0], &x[1], &x[2]) == 3) if (sscanf(line, "vn %f %f %f", &x[0], &x[1], &x[2]) == 3)
{ {
for(int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
norms.push_back(x[i]); norms.push_back(x[i]);
} }
if (sscanf(line, "f %hu/%hu/%hu %hu/%hu/%hu %hu/%hu/%hu", &y[0], &y[1], &y[2], &y[3], &y[4], &y[5], &y[6], &y[7], &y[8]) == 9) if (sscanf(line, "f %hu/%hu/%hu %hu/%hu/%hu %hu/%hu/%hu", &y[0], &y[1], &y[2], &y[3], &y[4], &y[5], &y[6], &y[7], &y[8]) == 9)
{ {
for(int i = 0; i < 9; i++) for (int i = 0; i < 9; i++)
faces.push_back(y[i]-1); faces.push_back(y[i] - 1);
}
}
int numFaces = faces.size()/9;
gunNumVerts = numFaces*3;
gunList = new Vertex[gunNumVerts];
for(int i = 0; i < (int)numFaces; i++)
{
for(int j = 0; j < 3; j++)
{
Vertex v;
for(int k = 0; k < 3; k++)
v.position[k] = verts[ 3*faces[9*i+3*j+0] +k];
v.texcoord[0] = uvs[ 2*faces[9*i+3*j+1] +0];
v.texcoord[1] = 1-uvs[ 2*faces[9*i+3*j+1] +1];
for(int k = 0; k < 3; k++)
v.normal[k] = norms[ 3*faces[9*i+3*j+2] +k];
gunList[3*i + j] = v;
} }
} }
fclose(f); int numFaces = faces.size() / 9;
gunNumVerts = numFaces * 3;
gunList = new Vertex[gunNumVerts];
for (int i = 0; i < (int)numFaces; i++)
{
for (int j = 0; j < 3; j++)
{
Vertex v;
for (int k = 0; k < 3; k++)
v.position[k] = verts[3 * faces[9 * i + 3 * j + 0] + k];
v.texcoord[0] = uvs[2 * faces[9 * i + 3 * j + 1] + 0];
v.texcoord[1] = 1 - uvs[2 * faces[9 * i + 3 * j + 1] + 1];
for (int k = 0; k < 3; k++)
v.normal[k] = norms[3 * faces[9 * i + 3 * j + 2] + k];
gunList[3 * i + j] = v;
}
}
fclose(f);
} }
@ -456,230 +456,230 @@ int Specular_png_size;
static void sceneInit() static void sceneInit()
{ {
consoleInit(NULL); consoleInit(NULL);
romfsInit(); romfsInit();
readPNG("romfs:/AmbOcc.png", &AmbOcc_png, &AmbOcc_png_size); readPNG("romfs:/AmbOcc.png", &AmbOcc_png, &AmbOcc_png_size);
readPNG("romfs:/Diffuse.png", &Diffuse_png, &Diffuse_png_size); readPNG("romfs:/Diffuse.png", &Diffuse_png, &Diffuse_png_size);
readPNG("romfs:/Normal.png", &Normal_png, &Normal_png_size); readPNG("romfs:/Normal.png", &Normal_png, &Normal_png_size);
readPNG("romfs:/Rough.png", &Rough_png, &Rough_png_size); readPNG("romfs:/Rough.png", &Rough_png, &Rough_png_size);
readPNG("romfs:/Specular.png", &Specular_png, &Specular_png_size); readPNG("romfs:/Specular.png", &Specular_png, &Specular_png_size);
readOBJ("romfs:/gun.3Dobj"); readOBJ("romfs:/gun.3Dobj");
//readOBJ("romfs:/cube.3Dobj"); //readOBJ("romfs:/cube.3Dobj");
GLint vsh = createAndCompileShader(GL_VERTEX_SHADER, vertexShaderSource);
GLint fsh = createAndCompileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
s_program = glCreateProgram(); GLint vsh = createAndCompileShader(GL_VERTEX_SHADER, vertexShaderSource);
glAttachShader(s_program, vsh); GLint fsh = createAndCompileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
glAttachShader(s_program, fsh);
glLinkProgram(s_program);
GLint success; s_program = glCreateProgram();
glGetProgramiv(s_program, GL_LINK_STATUS, &success); glAttachShader(s_program, vsh);
if (success == GL_FALSE) glAttachShader(s_program, fsh);
{ glLinkProgram(s_program);
char buf[512];
glGetProgramInfoLog(s_program, sizeof(buf), nullptr, buf);
TRACE("Link error: %s", buf);
}
glDeleteShader(vsh);
glDeleteShader(fsh);
loc_mdlvMtx = glGetUniformLocation(s_program, "mdlvMtx"); GLint success;
loc_projMtx = glGetUniformLocation(s_program, "projMtx"); glGetProgramiv(s_program, GL_LINK_STATUS, &success);
loc_lightPos = glGetUniformLocation(s_program, "lightPos"); if (success == GL_FALSE)
loc_ambient = glGetUniformLocation(s_program, "lightAmbient"); {
loc_diffuse = glGetUniformLocation(s_program, "lightDiffuse"); char buf[512];
loc_specular = glGetUniformLocation(s_program, "lightSpecular"); glGetProgramInfoLog(s_program, sizeof(buf), nullptr, buf);
loc_tex_diffuse = glGetUniformLocation(s_program, "tex_diffuse"); TRACE("Link error: %s", buf);
}
glDeleteShader(vsh);
glDeleteShader(fsh);
loc_mdlvMtx = glGetUniformLocation(s_program, "mdlvMtx");
loc_projMtx = glGetUniformLocation(s_program, "projMtx");
loc_lightPos = glGetUniformLocation(s_program, "lightPos");
loc_ambient = glGetUniformLocation(s_program, "lightAmbient");
loc_diffuse = glGetUniformLocation(s_program, "lightDiffuse");
loc_specular = glGetUniformLocation(s_program, "lightSpecular");
loc_tex_diffuse = glGetUniformLocation(s_program, "tex_diffuse");
loc_tex_specular = glGetUniformLocation(s_program, "tex_specular"); loc_tex_specular = glGetUniformLocation(s_program, "tex_specular");
loc_tex_ambOcc = glGetUniformLocation(s_program, "tex_ambOcc"); loc_tex_ambOcc = glGetUniformLocation(s_program, "tex_ambOcc");
loc_tex_normal = glGetUniformLocation(s_program, "tex_normal"); loc_tex_normal = glGetUniformLocation(s_program, "tex_normal");
loc_tex_rough = glGetUniformLocation(s_program, "tex_rough"); loc_tex_rough = glGetUniformLocation(s_program, "tex_rough");
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
glGenVertexArrays(1, &s_vao); glGenVertexArrays(1, &s_vao);
glGenBuffers(1, &s_vbo); glGenBuffers(1, &s_vbo);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(s_vao); glBindVertexArray(s_vao);
glBindBuffer(GL_ARRAY_BUFFER, s_vbo); glBindBuffer(GL_ARRAY_BUFFER, s_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*gunNumVerts, gunList, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*gunNumVerts, gunList, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoord)); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glEnableVertexAttribArray(1); glEnableVertexAttribArray(0);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal)); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoord));
glEnableVertexAttribArray(2); glEnableVertexAttribArray(1);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glBindBuffer(GL_ARRAY_BUFFER, 0); glEnableVertexAttribArray(2);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary. glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
int width, height, nchan; // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0);
int width, height, nchan;
stbi_uc* img; stbi_uc* img;
// Textures
glGenTextures(1, &s_tex_diffuse);
glActiveTexture(GL_TEXTURE0); // activate the texture unit first before binding texture
glBindTexture(GL_TEXTURE_2D, s_tex_diffuse);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img = stbi_load_from_memory((const stbi_uc*)Diffuse_png, Diffuse_png_size, &width, &height, &nchan, 4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
stbi_image_free(img);
// Textures
glGenTextures(1, &s_tex_specular);
glActiveTexture(GL_TEXTURE1); // activate the texture unit first before binding texture
glBindTexture(GL_TEXTURE_2D, s_tex_specular);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img = stbi_load_from_memory((const stbi_uc*)Specular_png, Specular_png_size, &width, &height, &nchan, 4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
stbi_image_free(img);
// Textures // Textures
glGenTextures(1, &s_tex_ambOcc); glGenTextures(1, &s_tex_diffuse);
glActiveTexture(GL_TEXTURE2); // activate the texture unit first before binding texture glActiveTexture(GL_TEXTURE0); // activate the texture unit first before binding texture
glBindTexture(GL_TEXTURE_2D, s_tex_ambOcc); glBindTexture(GL_TEXTURE_2D, s_tex_diffuse);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img = stbi_load_from_memory((const stbi_uc*)AmbOcc_png, AmbOcc_png_size, &width, &height, &nchan, 4); img = stbi_load_from_memory((const stbi_uc*)Diffuse_png, Diffuse_png_size, &width, &height, &nchan, 4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
stbi_image_free(img); stbi_image_free(img);
// Textures
glGenTextures(1, &s_tex_normal);
glActiveTexture(GL_TEXTURE3); // activate the texture unit first before binding texture
glBindTexture(GL_TEXTURE_2D, s_tex_normal);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img = stbi_load_from_memory((const stbi_uc*)Normal_png, Normal_png_size, &width, &height, &nchan, 4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
stbi_image_free(img);
// Textures // Textures
glGenTextures(1, &s_tex_rough); glGenTextures(1, &s_tex_specular);
glActiveTexture(GL_TEXTURE4); // activate the texture unit first before binding texture glActiveTexture(GL_TEXTURE1); // activate the texture unit first before binding texture
glBindTexture(GL_TEXTURE_2D, s_tex_rough); glBindTexture(GL_TEXTURE_2D, s_tex_specular);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img = stbi_load_from_memory((const stbi_uc*)Rough_png, Rough_png_size, &width, &height, &nchan, 4); img = stbi_load_from_memory((const stbi_uc*)Specular_png, Specular_png_size, &width, &height, &nchan, 4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
stbi_image_free(img); stbi_image_free(img);
// Uniforms // Textures
glUseProgram(s_program); glGenTextures(1, &s_tex_ambOcc);
auto projMtx = glm::perspective(40.0f*TAU/360.0f, 1280.0f/720.0f, 0.01f, 1000.0f); glActiveTexture(GL_TEXTURE2); // activate the texture unit first before binding texture
glUniformMatrix4fv(loc_projMtx, 1, GL_FALSE, glm::value_ptr(projMtx)); glBindTexture(GL_TEXTURE_2D, s_tex_ambOcc);
glUniform4f(loc_lightPos, 0.0f, 0.0f, 0.5f, 1.0f); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glUniform3f(loc_ambient, 0.5f, 0.5f, 0.5f); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img = stbi_load_from_memory((const stbi_uc*)AmbOcc_png, AmbOcc_png_size, &width, &height, &nchan, 4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
stbi_image_free(img);
// Textures
glGenTextures(1, &s_tex_normal);
glActiveTexture(GL_TEXTURE3); // activate the texture unit first before binding texture
glBindTexture(GL_TEXTURE_2D, s_tex_normal);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img = stbi_load_from_memory((const stbi_uc*)Normal_png, Normal_png_size, &width, &height, &nchan, 4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
stbi_image_free(img);
// Textures
glGenTextures(1, &s_tex_rough);
glActiveTexture(GL_TEXTURE4); // activate the texture unit first before binding texture
glBindTexture(GL_TEXTURE_2D, s_tex_rough);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img = stbi_load_from_memory((const stbi_uc*)Rough_png, Rough_png_size, &width, &height, &nchan, 4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img);
stbi_image_free(img);
// Uniforms
glUseProgram(s_program);
auto projMtx = glm::perspective(40.0f*TAU / 360.0f, 1280.0f / 720.0f, 0.01f, 1000.0f);
glUniformMatrix4fv(loc_projMtx, 1, GL_FALSE, glm::value_ptr(projMtx));
glUniform4f(loc_lightPos, 0.0f, 0.0f, 0.5f, 1.0f);
glUniform3f(loc_ambient, 0.5f, 0.5f, 0.5f);
glUniform3f(loc_diffuse, 0.2f, 0.2f, 1.0f); glUniform3f(loc_diffuse, 0.2f, 0.2f, 1.0f);
glUniform4f(loc_specular, 0.3f, 0.3f, 0.3f, 16.0f); glUniform4f(loc_specular, 0.3f, 0.3f, 0.3f, 16.0f);
glUniform1i(loc_tex_diffuse, 0); // GL_TEXTURE0make glUniform1i(loc_tex_diffuse, 0); // GL_TEXTURE0make
glUniform1i(loc_tex_specular, 1); // GL_TEXTURE1 glUniform1i(loc_tex_specular, 1); // GL_TEXTURE1
glUniform1i(loc_tex_ambOcc, 2); glUniform1i(loc_tex_ambOcc, 2);
glUniform1i(loc_tex_normal, 3); glUniform1i(loc_tex_normal, 3);
glUniform1i(loc_tex_rough, 4); glUniform1i(loc_tex_rough, 4);
s_startTicks = armGetSystemTick(); s_startTicks = armGetSystemTick();
} }
static float getTime() static float getTime()
{ {
u64 elapsed = armGetSystemTick() - s_startTicks; u64 elapsed = armGetSystemTick() - s_startTicks;
return (elapsed * 625 / 12) / 1000000000.0; return (elapsed * 625 / 12) / 1000000000.0;
} }
static void sceneUpdate() static void sceneUpdate()
{ {
glm::mat4 mdlvMtx{1.0}; glm::mat4 mdlvMtx{ 1.0 };
mdlvMtx = glm::translate(mdlvMtx, glm::vec3{0.0f, 0.0f, -2.0f}); mdlvMtx = glm::translate(mdlvMtx, glm::vec3{ 0.0f, 0.0f, -2.0f });
//mdlvMtx = glm::rotate(mdlvMtx, getTime() * TAU * 0.00234375f, glm::vec3{1.0f, 0.0f, 0.0f}); //mdlvMtx = glm::rotate(mdlvMtx, getTime() * TAU * 0.00234375f, glm::vec3{1.0f, 0.0f, 0.0f});
mdlvMtx = glm::rotate(mdlvMtx, getTime() * TAU * 0.00234375f / 2.0f, glm::vec3{0.0f, 1.0f, 0.0f}); mdlvMtx = glm::rotate(mdlvMtx, getTime() * TAU * 0.00234375f / 2.0f, glm::vec3{ 0.0f, 1.0f, 0.0f });
glUniformMatrix4fv(loc_mdlvMtx, 1, GL_FALSE, glm::value_ptr(mdlvMtx)); glUniformMatrix4fv(loc_mdlvMtx, 1, GL_FALSE, glm::value_ptr(mdlvMtx));
} }
static void sceneRender() static void sceneRender()
{ {
glClearColor(0x68/255.0f, 0xB0/255.0f, 0xD8/255.0f, 1.0f); glClearColor(0x68 / 255.0f, 0xB0 / 255.0f, 0xD8 / 255.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw our textured cube
glBindVertexArray(s_vao); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
// draw our textured cube
glBindVertexArray(s_vao); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
glDrawArrays(GL_TRIANGLES, 0, gunNumVerts); glDrawArrays(GL_TRIANGLES, 0, gunNumVerts);
} }
static void sceneExit() static void sceneExit()
{ {
glDeleteTextures(1, &s_tex_diffuse); glDeleteTextures(1, &s_tex_diffuse);
glDeleteTextures(1, &s_tex_specular); glDeleteTextures(1, &s_tex_specular);
glDeleteTextures(1, &s_tex_ambOcc); glDeleteTextures(1, &s_tex_ambOcc);
glDeleteTextures(1, &s_tex_normal); glDeleteTextures(1, &s_tex_normal);
glDeleteTextures(1, &s_tex_rough); glDeleteTextures(1, &s_tex_rough);
glDeleteBuffers(1, &s_vbo); glDeleteBuffers(1, &s_vbo);
glDeleteVertexArrays(1, &s_vao); glDeleteVertexArrays(1, &s_vao);
glDeleteProgram(s_program); glDeleteProgram(s_program);
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
// Set mesa configuration (useful for debugging) // Set mesa configuration (useful for debugging)
setMesaConfig(); setMesaConfig();
// Initialize EGL on the default window // Initialize EGL on the default window
if (!initEgl(nwindowGetDefault())) if (!initEgl(nwindowGetDefault()))
return EXIT_FAILURE; return EXIT_FAILURE;
// Load OpenGL routines using glad // Load OpenGL routines using glad
gladLoadGL(); gladLoadGL();
// Initialize our scene // Initialize our scene
sceneInit(); sceneInit();
// Main graphics loop // Main graphics loop
while (appletMainLoop()) while (appletMainLoop())
{ {
// Get and process input // Get and process input
hidScanInput(); hidScanInput();
u32 kDown = hidKeysDown(CONTROLLER_P1_AUTO); u32 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) if (kDown & KEY_PLUS)
break; break;
// Update our scene // Update our scene
sceneUpdate(); sceneUpdate();
// Render stuff! // Render stuff!
sceneRender(); sceneRender();
eglSwapBuffers(s_display, s_surface); eglSwapBuffers(s_display, s_surface);
} }
// Deinitialize our scene // Deinitialize our scene
sceneExit(); sceneExit();
// Deinitialize EGL // Deinitialize EGL
deinitEgl(); deinitEgl();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }