Advertisement
Guest User

C code for OpenGL 3.3 and GLSL 3.3

a guest
Apr 14th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. //MAIN SOURCE
  2.  
  3. #include <SDL.h>
  4. #include <GL/glew.h>
  5. #include <SDL_opengl.h>
  6. #include <stdio.h>
  7. #include <conio.h>
  8.  
  9.  
  10. #define SCREEN_HEIGHT 480
  11. #define SCREEN_WIDTH 640
  12.  
  13.  
  14. SDL_Renderer *mainrenderer;
  15. SDL_Window *mainwindow;
  16. SDL_Event event;
  17. bool running = true;
  18.  
  19. const char *GetStringFromFile(const char *stringfilename)
  20. {
  21.  
  22. FILE *file;
  23. errno_t err;
  24. err = fopen_s(&file, stringfilename, "ab"); // open the file at the end and binary
  25. if (err != 0 )
  26. {
  27. printf("can't be opened");
  28. }
  29.  
  30.  
  31. long length = ftell(file);// Ask the size of the file
  32. printf("file length is %ld \n", length);
  33. fseek(file, 0, SEEK_SET); // return to the beginning of the file
  34. char *buffer = calloc(length + 1, sizeof(char)); // alloc the buffer with the end 0
  35. fread(buffer, sizeof(char), length, file); // read the file
  36. fclose(file);
  37. printf(buffer);
  38.  
  39. //FILE *stringfile = fopen_s(&stringfile, stringfilename, "ab"); // open the file at the end and binary
  40. //int length = ftell(stringfile); // Ask the size of the file
  41. //fseek(stringfile, 0, SEEK_SET); // return to the beginning of the file
  42. //char *buffer = calloc(length + 1, sizeof(char)); // alloc the buffer with the end 0
  43. //fread(buffer, sizeof(char), length, stringfile); // read the file
  44. //fclose(stringfile);
  45. //return buffer;
  46. //printf(buffer);
  47. //return buffer;
  48. };
  49. GLuint LoadShaders(const char *fragshaderfn, const char *vertshaderfn, GLuint vertshader, GLuint fragshader)
  50. {
  51. const char * vertfilesource = GetStringFromFile(vertshaderfn);
  52.  
  53. int vertlenofstr = strlen(vertfilesource);
  54. printf("\n\n\n\n this is the length -> %d", vertlenofstr);
  55.  
  56. glShaderSource(vertshader, 1,
  57. &vertfilesource, &vertlenofstr);
  58.  
  59. glCompileShader(vertshader);
  60.  
  61. const char *vertshaderinfolog[256] = { 0 };
  62. int vertlenofils = strlen(vertshaderinfolog);
  63.  
  64. glGetShaderInfoLog(vertshader, sizeof(char) * 256, vertlenofils, vertshaderinfolog);
  65.  
  66. printf(vertshaderinfolog);
  67.  
  68. GLuint vertprogram = glCreateProgram();
  69.  
  70. glAttachShader(vertprogram, vertshader);
  71.  
  72. glLinkProgram(vertprogram);
  73.  
  74. const char *vertproginfolog[256] = { 0 };
  75. int vertlenoffp = strlen(vertproginfolog);
  76. glGetProgramInfoLog(vertprogram, sizeof(char) * 256, vertlenoffp, vertproginfolog);
  77.  
  78. printf(vertproginfolog);
  79.  
  80. glUseProgram(vertprogram);
  81.  
  82.  
  83. const char * fragfilesource = GetStringFromFile(fragshaderfn);
  84.  
  85. int fraglenofstr = strlen(fragfilesource);
  86. printf("\n\n\n\n this is the length -> %d", fraglenofstr);
  87.  
  88.  
  89. glShaderSource(fragshader,1,
  90. &fragfilesource, &fraglenofstr);
  91.  
  92. glCompileShader(fragshader);
  93.  
  94. const char *fragshaderinfolog[256] = { 0 };
  95. int fraglenofils = strlen(fragshaderinfolog);
  96.  
  97. glGetShaderInfoLog(fragshader, sizeof(char) * 256, fraglenofils, fragshaderinfolog);
  98.  
  99. printf("\n\n\n\n FRAG SHADER INFO LOG", fragshaderinfolog);
  100.  
  101. GLuint fragprogram = glCreateProgram();
  102.  
  103. glAttachShader(fragprogram, fragshader);
  104.  
  105. glLinkProgram(fragprogram);
  106.  
  107.  
  108. const char *fragproginfolog[256] = { 0 };
  109. int fraglenoffp = strlen(fragproginfolog);
  110. glGetProgramInfoLog(fragprogram, sizeof(char) * 256, fraglenoffp, fragproginfolog);
  111.  
  112. printf("\n\n\n\n FRAG PROG INFO LOG",fragproginfolog);
  113.  
  114. glUseProgram(fragprogram);
  115.  
  116. return fragshader;
  117. return vertshader;
  118. };
  119.  
  120.  
  121. int main(int argc , char **argv)
  122. {
  123. SDL_Init(SDL_INIT_EVERYTHING);
  124.  
  125. mainwindow = SDL_CreateWindow(
  126. "C OpenGL Engine",
  127. 300, 300, 640, 480,
  128. SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL );
  129.  
  130. SDL_GLContext maincontext = SDL_GL_CreateContext(mainwindow);
  131. SDL_GL_MakeCurrent(mainwindow, maincontext);
  132.  
  133. glewExperimental = GL_TRUE;
  134. GLenum err = glewInit();
  135. GLuint vertshaderhandle = glCreateShader(GL_VERTEX_SHADER),
  136. fragshaderhandle = glCreateShader(GL_FRAGMENT_SHADER);
  137. if (GLEW_OK != err)
  138. {
  139. /* Problem: glewInit failed, something is seriously wrong. */
  140. fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  141. }
  142. printf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  143.  
  144. if (SDL_GL_SetSwapInterval(1) < 0)
  145. {
  146. printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
  147. }
  148.  
  149. //LoadShaders("cunt.frag", "cunter.vert", vertshaderhandle, fragshaderhandle);
  150. GetStringFromFile("cunt.frag");
  151.  
  152. glClearColor(0.0, 0.0, 0.0, 1.0);
  153.  
  154. GLuint projmat = glGetUniformLocation(vertshaderhandle, "projection");
  155. //printf("\n\n\n\n\n\n\n Projmat : %d",projmat);
  156. while (running == true)
  157. {
  158. while (SDL_PollEvent(&event))
  159. {
  160. switch (event.type)
  161. {
  162. case SDL_QUIT:
  163. SDL_GL_DeleteContext(maincontext);
  164. SDL_Quit();
  165.  
  166. return 0;
  167. break;
  168. }
  169. }
  170.  
  171. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  172.  
  173. SDL_GL_SwapWindow(mainwindow);
  174. }
  175.  
  176. SDL_GL_DeleteContext(maincontext);
  177. SDL_DestroyWindow(mainwindow);
  178. SDL_Quit();
  179. return 0;
  180. }
  181.  
  182.  
  183. /*int errorflag = glGetError();
  184. if (errorflag == GL_NO_ERROR)
  185. {
  186. printf("something happend maybe?");
  187. }*/
  188. /*char fragfilecontents[500] = { 0 };
  189. char fragshadercompilelogcontents[500] = { 0 };
  190. char fragshaderlength[500] = { 0 };
  191. char fragshadercompilestatus[500] = { 0 };
  192. while ((fragfilecontents[0] = fgetc(fragfile)) != EOF)
  193. {
  194. printf(fragfilecontents);
  195.  
  196. }
  197.  
  198. GLuint fragshader = glCreateShader(GL_FRAGMENT_SHADER);
  199.  
  200. glShaderSource(fragshader, 0,
  201. (const GLchar **)&fragfilecontents, NULL);*/
  202.  
  203. //glCompileShader(fragshader);
  204. //GLint compile_status;
  205. //glGetShaderiv(fragshader, GL_COMPILE_STATUS, &fragshadercompilestatus);
  206. //printf("%s",fragshadercompilestatus);
  207. // glGetShaderiv(fragshader, GL_INFO_LOG_LENGTH, &fragshaderlength);
  208. //glGetShaderInfoLog(fragshader, 256, &fragshaderlength, fragshadercompilelogcontents[0]);
  209. //printf("%s",fragshaderlength[0]);
  210.  
  211. //PROPER READ AND WRITE :
  212. //const char *getStringFromFile(char const *path) {
  213. // FILE *file = fopen(path, "ab"); // open the file at the end and binary
  214. // int length = ftell(file); // Ask the size of the file
  215. // fseek(file, 0, SEEK_SET); // return to the beginning of the file
  216. // char *buffer = calloc(length + 1, sizeof(char)); // alloc the buffer with the end 0
  217. // fread(buffer, sizeof(char), length, file); // read the file
  218. // fclose(file);
  219. // return buffer;
  220. //}
  221.  
  222.  
  223. //CUNT.FRAG
  224. #version 330
  225.  
  226. out vec4 color;
  227.  
  228. void main()
  229. {
  230. color = vec4(1.0, 0.0, 0.0, 1.0);
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement