simple shader program for testing purposes
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.

219 lines
4.4KB

  1. #include "gl_2_1.h"
  2. #include "SDL.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. struct renderinfo
  6. {
  7. int ScrW;
  8. int ScrH;
  9. SDL_Window *Window;
  10. SDL_Renderer *Renderer;
  11. SDL_GLContext Context;
  12. GLuint VertShader;
  13. GLuint FragShader;
  14. GLuint Program;
  15. GLuint VBO;
  16. GLuint EBO;
  17. struct
  18. {
  19. GLint Position;
  20. } Attrib;
  21. struct
  22. {
  23. GLint Ticks;
  24. } Uniform;
  25. };
  26. struct renderinfo re;
  27. GLuint R_CreateBuffer(GLenum type, void const *data, GLsizei size)
  28. {
  29. GLuint buffer;
  30. glGenBuffers(1, &buffer);
  31. glBindBuffer(type, buffer);
  32. glBufferData(type, size, data, GL_STATIC_DRAW);
  33. return buffer;
  34. }
  35. GLuint R_CreateShader(GLenum type, char const *src)
  36. {
  37. char log[512];
  38. GLint success;
  39. GLuint shader;
  40. shader = glCreateShader(type);
  41. glShaderSource(shader, 1, &src, NULL);
  42. glCompileShader(shader);
  43. glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
  44. if(!success)
  45. {
  46. glGetShaderInfoLog(shader, 512, NULL, log);
  47. fprintf(stderr, "Shader compile failed!\n%s\n", log);
  48. exit(1);
  49. }
  50. return shader;
  51. }
  52. GLuint R_LinkProgram(GLuint vertShader, GLuint fragShader)
  53. {
  54. char log[512];
  55. GLint success;
  56. GLuint program;
  57. program = glCreateProgram();
  58. glAttachShader(program, vertShader);
  59. glAttachShader(program, fragShader);
  60. glLinkProgram(program);
  61. glGetProgramiv(program, GL_LINK_STATUS, &success);
  62. if(!success)
  63. {
  64. glGetProgramInfoLog(program, 512, NULL, log);
  65. fprintf(stderr, "Failed to link shader program!\n%s\n", log);
  66. exit(1);
  67. }
  68. return program;
  69. }
  70. char const *FXT_ReadFile(char const *fname)
  71. {
  72. FILE *fp;
  73. char *buf;
  74. size_t size;
  75. fp = fopen(fname, "r");
  76. buf = malloc(4096);
  77. size = fread(buf, 1, 4096, fp);
  78. buf[size] = '\0';
  79. return realloc(buf, size + 1);
  80. }
  81. void R_InitGL()
  82. {
  83. GLfloat const vertices[] = {
  84. -1.0, -1.0,
  85. 1.0, -1.0,
  86. -1.0, 1.0,
  87. 1.0, 1.0
  88. };
  89. GLushort const indices[] = { 0, 1, 2, 3 };
  90. // Init viewport.
  91. glViewport(0, 0, re.ScrW, re.ScrH);
  92. glClearColor(0.2, 0.3, 0.3, 1.0);
  93. // Init buffers.
  94. re.VBO = R_CreateBuffer(GL_ARRAY_BUFFER, vertices, sizeof(vertices));
  95. re.EBO = R_CreateBuffer(GL_ELEMENT_ARRAY_BUFFER, indices, sizeof(indices));
  96. // Init shaders.
  97. re.VertShader = R_CreateShader(GL_VERTEX_SHADER, FXT_ReadFile("main.vp.glsl"));
  98. re.FragShader = R_CreateShader(GL_FRAGMENT_SHADER, FXT_ReadFile("main.fp.glsl"));
  99. re.Program = R_LinkProgram(re.VertShader, re.FragShader);
  100. // Init uniform and attribute locations.
  101. re.Uniform.Ticks = glGetUniformLocation(re.Program, "Ticks");
  102. re.Attrib.Position = glGetAttribLocation(re.Program, "Position");
  103. }
  104. void R_Quit()
  105. {
  106. // Delete GL bound stuff.
  107. glDeleteShader(re.VertShader);
  108. glDeleteShader(re.FragShader);
  109. glDeleteProgram(re.Program);
  110. glDeleteBuffers(1, &re.VBO);
  111. // Delete SDL bound stuff.
  112. SDL_DestroyWindow(re.Window);
  113. SDL_Quit();
  114. }
  115. void R_Init()
  116. {
  117. re.ScrW = 640;
  118. re.ScrH = 480;
  119. // Init GL info.
  120. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  121. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
  122. // Init window.
  123. SDL_CreateWindowAndRenderer(re.ScrW, re.ScrH, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN, &re.Window, &re.Renderer);
  124. // Init GL context.
  125. re.Context = SDL_GL_CreateContext(re.Window);
  126. SDL_GL_SetSwapInterval(1);
  127. ogl_LoadFunctions();
  128. R_InitGL();
  129. atexit(R_Quit);
  130. }
  131. void R_Main()
  132. {
  133. glClear(GL_COLOR_BUFFER_BIT);
  134. // Bind shader.
  135. glUseProgram(re.Program);
  136. // Send uniforms.
  137. glUniform1i(re.Uniform.Ticks, SDL_GetTicks());
  138. // Bind vertex buffer.
  139. glBindBuffer(GL_ARRAY_BUFFER, re.VBO);
  140. glVertexAttribPointer(re.Attrib.Position, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, NULL);
  141. glEnableVertexAttribArray(re.Attrib.Position);
  142. // Bind element buffer, draw.
  143. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, re.EBO);
  144. glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, NULL);
  145. // Disable vertex buffer.
  146. glDisableVertexAttribArray(re.Attrib.Position);
  147. // Swap video buffer (send to screen).
  148. SDL_GL_SwapWindow(re.Window);
  149. }
  150. #ifdef _WIN32
  151. #include <windows.h>
  152. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow)
  153. #else
  154. int main(int argc, char **argv)
  155. #endif
  156. {
  157. SDL_Event ev;
  158. SDL_Init(SDL_INIT_EVERYTHING);
  159. R_Init();
  160. for(;;)
  161. {
  162. while(SDL_PollEvent(&ev) != 0)
  163. {
  164. if(ev.type == SDL_QUIT)
  165. {
  166. exit(0);
  167. }
  168. }
  169. R_Main();
  170. }
  171. return 0;
  172. }