raylib_opengl_interop.c 6.2 KB

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