You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
6.9 KiB

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