Advertisement
Guest User

Untitled

a guest
Sep 26th, 2012
192
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "SFML/Window.hpp"
  4. #include <GL/glew.h>
  5.  
  6. GLuint program;
  7. GLint attribute_coord2d;
  8.  
  9. int init_resources()
  10. {
  11.   GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
  12.  
  13.   GLuint vs = glCreateShader(GL_VERTEX_SHADER);
  14.   const char *vs_source =
  15.     "#version 120\n"  // OpenGL 2.1
  16.     "attribute vec2 coord2d;                  "
  17.     "void main(void) {                        "
  18.     "  gl_Position = vec4(coord2d, 0.0, 1.0); "
  19.     "}";
  20.   glShaderSource(vs, 1, &vs_source, NULL);
  21.   glCompileShader(vs);
  22.   glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
  23.   if (!compile_ok) {
  24.     fprintf(stderr, "Error in vertex shader\n");
  25.     return 0;
  26.   }
  27.  
  28.   GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
  29.   const char *fs_source =
  30. #ifdef GL_ES_VERSION_2_0
  31.     "#version 100\n"  // OpenGL ES 2.0
  32. #else
  33.     "#version 120\n"  // OpenGL 2.1
  34. #endif
  35.     "void main(void) {        "
  36.     "  gl_FragColor[0] = 0.0; "
  37.     "  gl_FragColor[1] = 0.0; "
  38.     "  gl_FragColor[2] = 1.0; "
  39.     "}";
  40.   glShaderSource(fs, 1, &fs_source, NULL);
  41.   glCompileShader(fs);
  42.   glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
  43.   if (!compile_ok) {
  44.     fprintf(stderr, "Error in fragment shader\n");
  45.     return 0;
  46.   }
  47.  
  48.   program = glCreateProgram();
  49.   glAttachShader(program, vs);
  50.   glAttachShader(program, fs);
  51.   glLinkProgram(program);
  52.   glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
  53.   if (!link_ok) {
  54.     fprintf(stderr, "glLinkProgram:");
  55.     return 0;
  56.   }
  57.  
  58.   const char* attribute_name = "coord2d";
  59.   attribute_coord2d = glGetAttribLocation(program, attribute_name);
  60.   if (attribute_coord2d == -1) {
  61.     fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
  62.     return 0;
  63.   }
  64.  
  65.   return 1;
  66. }
  67.  
  68. void onDisplay()
  69. {
  70.   glClearColor(1.0, 1.0, 1.0, 1.0);
  71.   glClear(GL_COLOR_BUFFER_BIT);
  72.  
  73.   glUseProgram(program);
  74.   glEnableVertexAttribArray(attribute_coord2d);
  75.   GLfloat triangle_vertices[] = {
  76.      0.0,  0.8,
  77.     -0.8, -0.8,
  78.      0.8, -0.8,
  79.   };
  80.   /* Describe our vertices array to OpenGL (it can't guess its format automatically) */
  81.   glVertexAttribPointer(
  82.     attribute_coord2d, // attribute
  83.     2,                 // number of elements per vertex, here (x,y)
  84.     GL_FLOAT,          // the type of each element
  85.     GL_FALSE,          // take our values as-is
  86.     0,                 // no extra data between each position
  87.     triangle_vertices  // pointer to the C array
  88.   );
  89.  
  90.   /* Push each element in buffer_vertices to the vertex shader */
  91.   glDrawArrays(GL_TRIANGLES, 0, 3);
  92.  
  93.   glDisableVertexAttribArray(attribute_coord2d);
  94.  
  95. }
  96.  
  97. void free_resources()
  98. {
  99.   glDeleteProgram(program);
  100. }
  101.  
  102.  
  103. int main(int argc, char* argv[]) {
  104.   sf::Window App(sf::VideoMode(800, 600), "OpenGL");
  105.  
  106.   GLenum glew_status = glewInit();
  107.   if (glew_status != GLEW_OK) {
  108.     fprintf(stderr, "Error: %s\n", glewGetErrorString(glew_status));
  109.     return 1;
  110.   }
  111.  
  112.   init_resources();
  113.  
  114.  
  115.   while (1)
  116.   {
  117.     onDisplay();
  118.     App.display();
  119.   }
  120.  
  121.   free_resources();
  122.   return 0;
  123. }
Advertisement
RAW Paste Data Copied
Advertisement