Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

167 linhas
7.0 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-2024 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) || defined(PLATFORM_DESKTOP_SDL)
  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. #define GL_SILENCE_DEPRECATION // Silence Opengl API deprecation warnings
  37. #include <OpenGL/gl3.h> // OpenGL 3 library for OSX
  38. #include <OpenGL/gl3ext.h> // OpenGL 3 extensions library for OSX
  39. #else
  40. #include "glad.h" // Required for: OpenGL functionality
  41. #endif
  42. #define GLSL_VERSION 330
  43. #endif
  44. #else // PLATFORM_ANDROID, PLATFORM_WEB
  45. #define GLSL_VERSION 100
  46. #endif
  47. #include "rlgl.h" // Required for: rlDrawRenderBatchActive(), rlGetMatrixModelview(), rlGetMatrixProjection()
  48. #include "raymath.h" // Required for: MatrixMultiply(), MatrixToFloat()
  49. #define MAX_PARTICLES 1000
  50. // Particle type
  51. typedef struct Particle {
  52. float x;
  53. float y;
  54. float period;
  55. } Particle;
  56. //------------------------------------------------------------------------------------
  57. // Program main entry point
  58. //------------------------------------------------------------------------------------
  59. int main(void)
  60. {
  61. // Initialization
  62. //--------------------------------------------------------------------------------------
  63. const int screenWidth = 800;
  64. const int screenHeight = 450;
  65. InitWindow(screenWidth, screenHeight, "raylib - point particles");
  66. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/point_particle.vs", GLSL_VERSION),
  67. TextFormat("resources/shaders/glsl%i/point_particle.fs", GLSL_VERSION));
  68. int currentTimeLoc = GetShaderLocation(shader, "currentTime");
  69. int colorLoc = GetShaderLocation(shader, "color");
  70. // Initialize the vertex buffer for the particles and assign each particle random values
  71. Particle particles[MAX_PARTICLES] = { 0 };
  72. for (int i = 0; i < MAX_PARTICLES; i++)
  73. {
  74. particles[i].x = (float)GetRandomValue(20, screenWidth - 20);
  75. particles[i].y = (float)GetRandomValue(50, screenHeight - 20);
  76. // Give each particle a slightly different period. But don't spread it to much.
  77. // This way the particles line up every so often and you get a glimps of what is going on.
  78. particles[i].period = (float)GetRandomValue(10, 30)/10.0f;
  79. }
  80. // Create a plain OpenGL vertex buffer with the data and an vertex array object
  81. // that feeds the data from the buffer into the vertexPosition shader attribute.
  82. GLuint vao = 0;
  83. GLuint vbo = 0;
  84. glGenVertexArrays(1, &vao);
  85. glBindVertexArray(vao);
  86. glGenBuffers(1, &vbo);
  87. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  88. glBufferData(GL_ARRAY_BUFFER, MAX_PARTICLES*sizeof(Particle), particles, GL_STATIC_DRAW);
  89. // Note: LoadShader() automatically fetches the attribute index of "vertexPosition" and saves it in shader.locs[SHADER_LOC_VERTEX_POSITION]
  90. glVertexAttribPointer(shader.locs[SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, GL_FALSE, 0, 0);
  91. glEnableVertexAttribArray(0);
  92. glBindBuffer(GL_ARRAY_BUFFER, 0);
  93. glBindVertexArray(0);
  94. // Allows the vertex shader to set the point size of each particle individually
  95. #ifndef GRAPHICS_API_OPENGL_ES2
  96. glEnable(GL_PROGRAM_POINT_SIZE);
  97. #endif
  98. SetTargetFPS(60);
  99. //--------------------------------------------------------------------------------------
  100. // Main game loop
  101. while (!WindowShouldClose()) // Detect window close button or ESC key
  102. {
  103. // Draw
  104. //----------------------------------------------------------------------------------
  105. BeginDrawing();
  106. ClearBackground(WHITE);
  107. DrawRectangle(10, 10, 210, 30, MAROON);
  108. DrawText(TextFormat("%zu particles in one vertex buffer", MAX_PARTICLES), 20, 20, 10, RAYWHITE);
  109. rlDrawRenderBatchActive(); // Draw iternal buffers data (previous draw calls)
  110. // Switch to plain OpenGL
  111. //------------------------------------------------------------------------------
  112. glUseProgram(shader.id);
  113. glUniform1f(currentTimeLoc, GetTime());
  114. Vector4 color = ColorNormalize((Color){ 255, 0, 0, 128 });
  115. glUniform4fv(colorLoc, 1, (float *)&color);
  116. // Get the current modelview and projection matrix so the particle system is displayed and transformed
  117. Matrix modelViewProjection = MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection());
  118. glUniformMatrix4fv(shader.locs[SHADER_LOC_MATRIX_MVP], 1, false, MatrixToFloat(modelViewProjection));
  119. glBindVertexArray(vao);
  120. glDrawArrays(GL_POINTS, 0, MAX_PARTICLES);
  121. glBindVertexArray(0);
  122. glUseProgram(0);
  123. //------------------------------------------------------------------------------
  124. DrawFPS(screenWidth - 100, 10);
  125. EndDrawing();
  126. //----------------------------------------------------------------------------------
  127. }
  128. // De-Initialization
  129. //--------------------------------------------------------------------------------------
  130. glDeleteBuffers(1, &vbo);
  131. glDeleteVertexArrays(1, &vao);
  132. UnloadShader(shader); // Unload shader
  133. CloseWindow(); // Close window and OpenGL context
  134. //--------------------------------------------------------------------------------------
  135. return 0;
  136. }