Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

C version of pyopengl problem

By: a guest on Feb 22nd, 2012  |  syntax: C  |  size: 3.39 KB  |  views: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <GL/gl.h>
  2. #include <GL/glut.h>
  3. #include <stdlib.h>
  4. #include <stddef.h>
  5. #include <math.h>
  6. #include <stdio.h>
  7.  
  8. GLchar *file_contents(const char *filename, GLint *length)
  9. {
  10.     FILE *f = fopen(filename, "r");
  11.     GLchar *buffer;
  12.  
  13.     if (!f) {
  14.         fprintf(stderr, "Unable to open %s for reading\n", filename);
  15.         return NULL;
  16.     }
  17.  
  18.     fseek(f, 0, SEEK_END);
  19.     *length = ftell(f);
  20.     fseek(f, 0, SEEK_SET);
  21.  
  22.     buffer = (GLchar*) malloc(*length+1);
  23.     *length = fread(buffer, 1, *length, f);
  24.     fclose(f);
  25.     ((char*)buffer)[*length] = '\0';
  26.  
  27.     return buffer;
  28. }
  29.  
  30. GLuint make_shader(GLenum type, const char *filename)
  31. {
  32.     GLint length;
  33.     GLchar *source = file_contents(filename, &length);
  34.     GLuint shader;
  35.     GLint shader_ok;
  36.  
  37.     if (!source)
  38.         return 0;
  39.  
  40.     shader = glCreateShader(type);
  41.     glShaderSource(shader, 1, (const GLchar**)&source, &length);
  42.     free(source);
  43.     glCompileShader(shader);
  44.  
  45.     glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);
  46.     if (!shader_ok) {
  47.         fprintf(stderr, "Failed to compile %s:\n", filename);
  48.         glDeleteShader(shader);
  49.         return 0;
  50.     }
  51.     return shader;
  52. }
  53.  
  54. GLuint make_program(GLuint vertex_shader, GLuint fragment_shader)
  55. {
  56.     GLint program_ok;
  57.  
  58.     GLuint program = glCreateProgram();
  59.  
  60.     glAttachShader(program, vertex_shader);
  61.     glAttachShader(program, fragment_shader);
  62.     glLinkProgram(program);
  63.  
  64.     glGetProgramiv(program, GL_LINK_STATUS, &program_ok);
  65.     if (!program_ok) {
  66.         fprintf(stderr, "Failed to link shader program:\n");
  67.         glDeleteProgram(program);
  68.         return 0;
  69.     }
  70.     return program;
  71. }
  72.  
  73. static const GLfloat verts[] = {
  74.     -1.0f, -1.0f,
  75.      1.0f, -1.0f,
  76.     -1.0f,  1.0f,
  77.      1.0f,  1.0f
  78. };
  79. static const GLushort polys[] = { 0, 1, 2, 2,1,3 };
  80. GLuint program, posidx, vbuf, ebuf;
  81.  
  82. void init_gl() {
  83.     glEnable(GL_BLEND);
  84.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  85.     glClearColor(0.5,0.5,0.5,1);
  86.     glClearDepth(1.0);
  87.     glDepthFunc(GL_LESS);
  88.     glEnable(GL_DEPTH_TEST);
  89.     glEnable(GL_TEXTURE_2D);
  90.     glViewport(0,0,800,600);
  91.  
  92.     program = make_program(make_shader(GL_VERTEX_SHADER, "stupid.v.glsl"), make_shader(GL_FRAGMENT_SHADER, "stupid.f.glsl"));
  93.     posidx = glGetAttribLocation(program, "position");
  94.  
  95.     glGenBuffers(1, &vbuf);
  96.     glGenBuffers(1, &ebuf);
  97.     glBindBuffer(GL_ARRAY_BUFFER, vbuf);
  98.     glBufferData(GL_ARRAY_BUFFER, 4*8, verts, GL_STATIC_DRAW);
  99.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebuf);
  100.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2*6, polys, GL_STATIC_DRAW);
  101. }
  102.  
  103.  
  104. void render() {
  105.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  106.     glUseProgram(program);
  107.  
  108.     glEnableVertexAttribArray(posidx);
  109.     glBindBuffer(GL_ARRAY_BUFFER, vbuf);
  110.     glVertexAttribPointer( posidx, 2, GL_FLOAT, GL_FALSE, 4*2, 0);
  111.  
  112.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebuf);
  113.     glDrawElements(
  114.         GL_TRIANGLES,          
  115.         6,          
  116.         GL_UNSIGNED_SHORT,      
  117.         0              
  118.     );
  119.  
  120.     glDisableVertexAttribArray(posidx);
  121.     glutSwapBuffers();
  122. }
  123.  
  124. int main(int argc, char* argv[])
  125. {
  126.     glutInit(&argc, argv);
  127.     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  128.     glutInitWindowSize(800, 600);
  129.     glutCreateWindow("Flag");
  130.     glutDisplayFunc(&render);
  131.  
  132.     init_gl();
  133.  
  134.     glutMainLoop();
  135.     return 0;
  136. }