|
@@ -18,30 +18,23 @@
|
|
*
|
|
*
|
|
********************************************************************************************/
|
|
********************************************************************************************/
|
|
|
|
|
|
-#define GLAD_IMPLEMENTATION
|
|
|
|
#include "glad.h" // Extensions loading library
|
|
#include "glad.h" // Extensions loading library
|
|
#include <GLFW/glfw3.h> // Windows/Context and inputs management
|
|
#include <GLFW/glfw3.h> // Windows/Context and inputs management
|
|
|
|
|
|
#define RLGL_STANDALONE
|
|
#define RLGL_STANDALONE
|
|
-#include "rlgl.h"
|
|
|
|
|
|
+#include "rlgl.h" // rlgl library: OpenGL 1.1 immediate-mode style coding
|
|
|
|
|
|
-#include <stdlib.h>
|
|
|
|
-#include <stdio.h>
|
|
|
|
-#include <stdarg.h>
|
|
|
|
|
|
|
|
#define RED (Color){ 230, 41, 55, 255 } // Red
|
|
#define RED (Color){ 230, 41, 55, 255 } // Red
|
|
#define MAROON (Color){ 190, 33, 55, 255 } // Maroon
|
|
#define MAROON (Color){ 190, 33, 55, 255 } // Maroon
|
|
#define RAYWHITE (Color){ 245, 245, 245, 255 } // My own White (raylib logo)
|
|
#define RAYWHITE (Color){ 245, 245, 245, 255 } // My own White (raylib logo)
|
|
#define DARKGRAY (Color){ 80, 80, 80, 255 } // Dark Gray
|
|
#define DARKGRAY (Color){ 80, 80, 80, 255 } // Dark Gray
|
|
-//----------------------------------------------------------------------------------
|
|
|
|
-typedef enum { LOG_INFO = 0, LOG_ERROR, LOG_WARNING, LOG_DEBUG, LOG_OTHER } TraceLogType;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
//----------------------------------------------------------------------------------
|
|
// Module specific Functions Declaration
|
|
// Module specific Functions Declaration
|
|
//----------------------------------------------------------------------------------
|
|
//----------------------------------------------------------------------------------
|
|
static void ErrorCallback(int error, const char* description);
|
|
static void ErrorCallback(int error, const char* description);
|
|
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
|
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
|
-static void TraceLog(int msgType, const char *text, ...);
|
|
|
|
|
|
|
|
// Drawing functions (uses rlgl functionality)
|
|
// Drawing functions (uses rlgl functionality)
|
|
static void DrawGrid(int slices, float spacing);
|
|
static void DrawGrid(int slices, float spacing);
|
|
@@ -66,10 +59,10 @@ int main(void)
|
|
|
|
|
|
if (!glfwInit())
|
|
if (!glfwInit())
|
|
{
|
|
{
|
|
- TraceLog(LOG_WARNING, "GLFW3: Can not initialize GLFW");
|
|
|
|
- exit(EXIT_FAILURE);
|
|
|
|
|
|
+ TraceLog(WARNING, "GLFW3: Can not initialize GLFW");
|
|
|
|
+ return 1;
|
|
}
|
|
}
|
|
- else TraceLog(LOG_INFO, "GLFW3: GLFW initialized successfully");
|
|
|
|
|
|
+ else TraceLog(INFO, "GLFW3: GLFW initialized successfully");
|
|
|
|
|
|
glfwWindowHint(GLFW_SAMPLES, 4);
|
|
glfwWindowHint(GLFW_SAMPLES, 4);
|
|
glfwWindowHint(GLFW_DEPTH_BITS, 16);
|
|
glfwWindowHint(GLFW_DEPTH_BITS, 16);
|
|
@@ -83,9 +76,9 @@ int main(void)
|
|
if (!window)
|
|
if (!window)
|
|
{
|
|
{
|
|
glfwTerminate();
|
|
glfwTerminate();
|
|
- exit(EXIT_FAILURE);
|
|
|
|
|
|
+ return 2;
|
|
}
|
|
}
|
|
- else TraceLog(LOG_INFO, "GLFW3: Window created successfully");
|
|
|
|
|
|
+ else TraceLog(INFO, "GLFW3: Window created successfully");
|
|
|
|
|
|
glfwSetKeyCallback(window, KeyCallback);
|
|
glfwSetKeyCallback(window, KeyCallback);
|
|
|
|
|
|
@@ -95,10 +88,10 @@ int main(void)
|
|
// Load OpenGL 3.3 extensions
|
|
// Load OpenGL 3.3 extensions
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
|
{
|
|
{
|
|
- TraceLog(LOG_WARNING, "GLAD: Cannot load OpenGL extensions");
|
|
|
|
- exit(1);
|
|
|
|
|
|
+ TraceLog(WARNING, "GLAD: Cannot load OpenGL extensions");
|
|
|
|
+ return 3;
|
|
}
|
|
}
|
|
- else TraceLog(LOG_INFO, "GLAD: OpenGL extensions loaded successfully");
|
|
|
|
|
|
+ else TraceLog(INFO, "GLAD: OpenGL extensions loaded successfully");
|
|
//--------------------------------------------------------
|
|
//--------------------------------------------------------
|
|
|
|
|
|
// Initialize rlgl internal buffers and OpenGL state
|
|
// Initialize rlgl internal buffers and OpenGL state
|
|
@@ -107,7 +100,6 @@ int main(void)
|
|
rlClearColor(245, 245, 245, 255); // Define clear color
|
|
rlClearColor(245, 245, 245, 255); // Define clear color
|
|
rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
|
|
rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
|
|
|
|
|
|
- Vector2 size = { 200, 200 };
|
|
|
|
Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
|
|
Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
|
|
|
|
|
|
Camera camera;
|
|
Camera camera;
|
|
@@ -128,29 +120,45 @@ int main(void)
|
|
// Draw
|
|
// Draw
|
|
//----------------------------------------------------------------------------------
|
|
//----------------------------------------------------------------------------------
|
|
rlClearScreenBuffers(); // Clear current framebuffer
|
|
rlClearScreenBuffers(); // Clear current framebuffer
|
|
|
|
+
|
|
// Calculate projection matrix (from perspective) and view matrix from camera look at
|
|
// Calculate projection matrix (from perspective) and view matrix from camera look at
|
|
Matrix matProj = MatrixPerspective(camera.fovy, (double)screenWidth/(double)screenHeight, 0.01, 1000.0);
|
|
Matrix matProj = MatrixPerspective(camera.fovy, (double)screenWidth/(double)screenHeight, 0.01, 1000.0);
|
|
MatrixTranspose(&matProj);
|
|
MatrixTranspose(&matProj);
|
|
Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
|
|
Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
|
|
- Matrix mvp = MatrixMultiply(matView, matProj);
|
|
|
|
|
|
+
|
|
|
|
+ SetMatrixModelview(matView); // Replace internal modelview matrix by a custom one
|
|
|
|
+ SetMatrixProjection(matProj); // Replace internal projection matrix by a custom one
|
|
|
|
|
|
DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
|
|
DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
|
|
DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, RAYWHITE);
|
|
DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, RAYWHITE);
|
|
DrawGrid(10, 1.0f);
|
|
DrawGrid(10, 1.0f);
|
|
|
|
|
|
// NOTE: Internal buffers drawing (3D data)
|
|
// NOTE: Internal buffers drawing (3D data)
|
|
- rlglDraw(mvp);
|
|
|
|
|
|
+ rlglDraw();
|
|
|
|
|
|
|
|
+ // Draw '2D' elements in the scene (GUI)
|
|
|
|
+#define RLGL_CREATE_MATRIX_MANUALLY
|
|
|
|
+#if defined(RLGL_CREATE_MATRIX_MANUALLY)
|
|
|
|
+
|
|
matProj = MatrixOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0);
|
|
matProj = MatrixOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0);
|
|
MatrixTranspose(&matProj);
|
|
MatrixTranspose(&matProj);
|
|
matView = MatrixIdentity();
|
|
matView = MatrixIdentity();
|
|
- mvp = MatrixMultiply(matView, matProj);
|
|
|
|
|
|
|
|
- // TODO: 2D drawing on Oculus Rift: requires an ovrLayerQuad layer
|
|
|
|
- DrawRectangleV((Vector2){ 10.0f, 10.0f }, (Vector2){ 300.0f, 20.0f }, DARKGRAY);
|
|
|
|
|
|
+ SetMatrixModelview(matView); // Replace internal modelview matrix by a custom one
|
|
|
|
+ SetMatrixProjection(matProj); // Replace internal projection matrix by a custom one
|
|
|
|
+
|
|
|
|
+#else // Let rlgl generate and multiply matrix internally
|
|
|
|
+
|
|
|
|
+ rlMatrixMode(RL_PROJECTION); // Enable internal projection matrix
|
|
|
|
+ rlLoadIdentity(); // Reset internal projection matrix
|
|
|
|
+ rlOrtho(0.0, screenWidth, screenHeight, 0.0, 0.0, 1.0); // Recalculate internal projection matrix
|
|
|
|
+ rlMatrixMode(RL_MODELVIEW); // Enable internal modelview matrix
|
|
|
|
+ rlLoadIdentity(); // Reset internal modelview matrix
|
|
|
|
+#endif
|
|
|
|
+ DrawRectangleV((Vector2){ 10.0f, 10.0f }, (Vector2){ 600.0f, 20.0f }, DARKGRAY);
|
|
|
|
|
|
// NOTE: Internal buffers drawing (2D data)
|
|
// NOTE: Internal buffers drawing (2D data)
|
|
- rlglDraw(mvp);
|
|
|
|
|
|
+ rlglDraw();
|
|
|
|
|
|
glfwSwapBuffers(window);
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
glfwPollEvents();
|
|
@@ -163,7 +171,6 @@ int main(void)
|
|
|
|
|
|
glfwDestroyWindow(window);
|
|
glfwDestroyWindow(window);
|
|
glfwTerminate();
|
|
glfwTerminate();
|
|
-
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
@@ -176,7 +183,7 @@ int main(void)
|
|
// GLFW3: Error callback
|
|
// GLFW3: Error callback
|
|
static void ErrorCallback(int error, const char* description)
|
|
static void ErrorCallback(int error, const char* description)
|
|
{
|
|
{
|
|
- TraceLog(LOG_ERROR, description);
|
|
|
|
|
|
+ TraceLog(ERROR, description);
|
|
}
|
|
}
|
|
|
|
|
|
// GLFW3: Keyboard callback
|
|
// GLFW3: Keyboard callback
|
|
@@ -188,29 +195,6 @@ static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, i
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-// Output a trace log message
|
|
|
|
-static void TraceLog(int msgType, const char *text, ...)
|
|
|
|
-{
|
|
|
|
- va_list args;
|
|
|
|
- va_start(args, text);
|
|
|
|
-
|
|
|
|
- switch(msgType)
|
|
|
|
- {
|
|
|
|
- case LOG_INFO: fprintf(stdout, "INFO: "); break;
|
|
|
|
- case LOG_ERROR: fprintf(stdout, "ERROR: "); break;
|
|
|
|
- case LOG_WARNING: fprintf(stdout, "WARNING: "); break;
|
|
|
|
- case LOG_DEBUG: fprintf(stdout, "DEBUG: "); break;
|
|
|
|
- default: break;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- vfprintf(stdout, text, args);
|
|
|
|
- fprintf(stdout, "\n");
|
|
|
|
-
|
|
|
|
- va_end(args);
|
|
|
|
-
|
|
|
|
- //if (msgType == LOG_ERROR) exit(1);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
// Draw rectangle using rlgl OpenGL 1.1 style coding (translated to OpenGL 3.3 internally)
|
|
// Draw rectangle using rlgl OpenGL 1.1 style coding (translated to OpenGL 3.3 internally)
|
|
static void DrawRectangleV(Vector2 position, Vector2 size, Color color)
|
|
static void DrawRectangleV(Vector2 position, Vector2 size, Color color)
|
|
{
|
|
{
|