raylib_opengl_interop.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*******************************************************************************************
  2. *
  3. * raylib [others] example - OpenGL interoperatibility
  4. *
  5. * Example complexity rating: [★★★★] 4/4
  6. *
  7. * Example originally created with raylib 3.8, last time updated with raylib 4.0
  8. *
  9. * Example contributed by Stephan Soller (@arkanis) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2021-2025 Stephan Soller (@arkanis) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************
  17. *
  18. * Mixes raylib and plain OpenGL code to draw a GL_POINTS based particle system. The
  19. * primary point is to demonstrate raylib and OpenGL interop
  20. *
  21. * rlgl batched draw operations internally so we have to flush the current batch before
  22. * doing our own OpenGL work (rlDrawRenderBatchActive())
  23. *
  24. * The example also demonstrates how to get the current model view projection matrix of
  25. * raylib. That way raylib cameras and so on work as expected
  26. *
  27. ********************************************************************************************/
  28. #include "raylib.h"
  29. #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_DESKTOP_SDL)
  30. #if defined(GRAPHICS_API_OPENGL_ES2)
  31. #include "glad_gles2.h" // Required for: OpenGL functionality
  32. #define glGenVertexArrays glGenVertexArraysOES
  33. #define glBindVertexArray glBindVertexArrayOES
  34. #define glDeleteVertexArrays glDeleteVertexArraysOES
  35. #define GLSL_VERSION 100
  36. #else
  37. #if defined(__APPLE__)
  38. #define GL_SILENCE_DEPRECATION // Silence Opengl API deprecation warnings
  39. #include <OpenGL/gl3.h> // OpenGL 3 library for OSX
  40. #include <OpenGL/gl3ext.h> // OpenGL 3 extensions library for OSX
  41. #else
  42. #include "glad.h" // Required for: OpenGL functionality
  43. #endif
  44. #define GLSL_VERSION 330
  45. #endif
  46. #else // PLATFORM_ANDROID, PLATFORM_WEB
  47. #define GLSL_VERSION 100
  48. #endif
  49. #include "rlgl.h" // Required for: rlDrawRenderBatchActive(), rlGetMatrixModelview(), rlGetMatrixProjection()
  50. #include "raymath.h" // Required for: MatrixMultiply(), MatrixToFloat()
  51. #define MAX_PARTICLES 1000
  52. //------------------------------------------------------------------------------------
  53. // Module Functions Declaration
  54. //------------------------------------------------------------------------------------
  55. // Particle type
  56. typedef struct Particle {
  57. float x;
  58. float y;
  59. float period;
  60. } Particle;
  61. //------------------------------------------------------------------------------------
  62. // Program main entry point
  63. //------------------------------------------------------------------------------------
  64. int main(void)
  65. {
  66. // Initialization
  67. //--------------------------------------------------------------------------------------
  68. const int screenWidth = 800;
  69. const int screenHeight = 450;
  70. InitWindow(screenWidth, screenHeight, "raylib [others] example - OpenGL interoperatibility");
  71. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/point_particle.vs", GLSL_VERSION),
  72. TextFormat("resources/shaders/glsl%i/point_particle.fs", GLSL_VERSION));
  73. int currentTimeLoc = GetShaderLocation(shader, "currentTime");
  74. int colorLoc = GetShaderLocation(shader, "color");
  75. // Initialize the vertex buffer for the particles and assign each particle random values
  76. Particle particles[MAX_PARTICLES] = { 0 };
  77. for (int i = 0; i < MAX_PARTICLES; i++)
  78. {
  79. particles[i].x = (float)GetRandomValue(20, screenWidth - 20);
  80. particles[i].y = (float)GetRandomValue(50, screenHeight - 20);
  81. // Give each particle a slightly different period. But don't spread it to much
  82. // This way the particles line up every so often and you get a glimps of what is going on
  83. particles[i].period = (float)GetRandomValue(10, 30)/10.0f;
  84. }
  85. // Create a plain OpenGL vertex buffer with the data and an vertex array object
  86. // that feeds the data from the buffer into the vertexPosition shader attribute
  87. GLuint vao = 0;
  88. GLuint vbo = 0;
  89. glGenVertexArrays(1, &vao);
  90. glBindVertexArray(vao);
  91. glGenBuffers(1, &vbo);
  92. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  93. glBufferData(GL_ARRAY_BUFFER, MAX_PARTICLES*sizeof(Particle), particles, GL_STATIC_DRAW);
  94. // Note: LoadShader() automatically fetches the attribute index of "vertexPosition" and saves it in shader.locs[SHADER_LOC_VERTEX_POSITION]
  95. glVertexAttribPointer(shader.locs[SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, GL_FALSE, 0, 0);
  96. glEnableVertexAttribArray(0);
  97. glBindBuffer(GL_ARRAY_BUFFER, 0);
  98. glBindVertexArray(0);
  99. // Allows the vertex shader to set the point size of each particle individually
  100. #ifndef GRAPHICS_API_OPENGL_ES2
  101. glEnable(GL_PROGRAM_POINT_SIZE);
  102. #endif
  103. SetTargetFPS(60);
  104. //--------------------------------------------------------------------------------------
  105. // Main game loop
  106. while (!WindowShouldClose()) // Detect window close button or ESC key
  107. {
  108. // Draw
  109. //----------------------------------------------------------------------------------
  110. BeginDrawing();
  111. ClearBackground(WHITE);
  112. DrawRectangle(10, 10, 210, 30, MAROON);
  113. DrawText(TextFormat("%zu particles in one vertex buffer", MAX_PARTICLES), 20, 20, 10, RAYWHITE);
  114. rlDrawRenderBatchActive(); // Draw iternal buffers data (previous draw calls)
  115. // Switch to plain OpenGL
  116. //------------------------------------------------------------------------------
  117. glUseProgram(shader.id);
  118. glUniform1f(currentTimeLoc, GetTime());
  119. Vector4 color = ColorNormalize((Color){ 255, 0, 0, 128 });
  120. glUniform4fv(colorLoc, 1, (float *)&color);
  121. // Get the current modelview and projection matrix so the particle system is displayed and transformed
  122. Matrix modelViewProjection = MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection());
  123. glUniformMatrix4fv(shader.locs[SHADER_LOC_MATRIX_MVP], 1, false, MatrixToFloat(modelViewProjection));
  124. glBindVertexArray(vao);
  125. glDrawArrays(GL_POINTS, 0, MAX_PARTICLES);
  126. glBindVertexArray(0);
  127. glUseProgram(0);
  128. //------------------------------------------------------------------------------
  129. DrawFPS(screenWidth - 100, 10);
  130. EndDrawing();
  131. //----------------------------------------------------------------------------------
  132. }
  133. // De-Initialization
  134. //--------------------------------------------------------------------------------------
  135. glDeleteBuffers(1, &vbo);
  136. glDeleteVertexArrays(1, &vao);
  137. UnloadShader(shader); // Unload shader
  138. CloseWindow(); // Close window and OpenGL context
  139. //--------------------------------------------------------------------------------------
  140. return 0;
  141. }