Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include GLEW_INCLUDE
  2. #include <SDL.h>
  3.  
  4. int w=800, h=600;
  5.  
  6. float vertexData[] = {
  7. 0, 1, 0, 0, 0, 1, 0, 1, 0, 0,
  8. 1, 0, 0, 0, 0, 1, 1, 0, 0, 0,
  9. 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
  10. };
  11.  
  12. int indexData[] = {
  13. 0, 1, 2
  14. };
  15.  
  16. const char* vertSrc =
  17. "#version 400\n"
  18. "in vec3 os_Position;\n"
  19. "void main() {\n"
  20. " gl_Position = vec4(os_Position, 1.0);\n"
  21. "}\n";
  22.  
  23. const char* fragSrc =
  24. "#version 400\n"
  25. "out vec4 Output0;\n"
  26. "void main() {\n"
  27. " Output0 = vec4(1.0);\n"
  28. "}\n";
  29.  
  30. int main() {
  31. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
  32. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
  33. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  34. SDL_Window* win = SDL_CreateWindow("SSCCE", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_OPENGL);
  35. SDL_GLContext ctx = SDL_GL_CreateContext(win);
  36. glewInit();
  37.  
  38. unsigned prog = glCreateProgram();
  39.  
  40. unsigned vert = glCreateShader(GL_VERTEX_SHADER);
  41. int vertSrcLen = strlen(vertSrc);
  42. glShaderSource(vert, 1, (const GLchar**)&vertSrc, &vertSrcLen);
  43. glCompileShader(vert);
  44. glAttachShader(prog, vert);
  45.  
  46. unsigned frag = glCreateShader(GL_FRAGMENT_SHADER);
  47. int fragSrcLen = strlen(fragSrc);
  48. glShaderSource(frag, 1, (const GLchar**)&fragSrc, &fragSrcLen);
  49. glCompileShader(frag);
  50. glAttachShader(prog, frag);
  51.  
  52. glBindFragDataLocation(prog, 0, "Output0");
  53. glLinkProgram(prog);
  54. glUseProgram(prog);
  55.  
  56. glViewport(0, 0, w, h);
  57. glClearColor(0, 0, 0, 0);
  58. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  59.  
  60. unsigned vao = 0;
  61. glGenVertexArrays(1, &vao);
  62. glBindVertexArray(vao);
  63.  
  64. unsigned vbo = 0;
  65. glGenBuffers(1, &vbo);
  66. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  67. glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
  68.  
  69. int stride = sizeof(float) * (3 + 3 + 4);
  70.  
  71. int attrib = glGetAttribLocation(prog, "os_Position");
  72. printf("attrib = %d\n", attrib);
  73. glEnableVertexAttribArray(attrib);
  74. glVertexAttribPointer(attrib, 3, GL_FLOAT, true, stride, (void*)0);
  75.  
  76. unsigned ibo = 0;
  77. glGenBuffers(1, &ibo);
  78. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  79. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexData), indexData, GL_STATIC_DRAW);
  80. glDrawElements(GL_TRIANGLES, sizeof(indexData) / sizeof(indexData[0]), GL_UNSIGNED_INT, 0);
  81.  
  82. SDL_GL_SwapWindow(win);
  83. SDL_Delay(1000);
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement