Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <glew.h>
  3. #include <GLFW/glfw3.h>
  4. #include <string>
  5. #include <stdlib.h>
  6. #include <fstream>
  7.  
  8. std::string LoadFileToString( const char* filepath ) {
  9.     std::string fileData = "";
  10.     std::ifstream stream( filepath, std::ios::in );
  11.  
  12.     if ( stream.is_open() ) {
  13.         std::string line = "";
  14.  
  15.         while ( getline( stream, line ) ) {
  16.             fileData += "\n" + line;
  17.         }
  18.  
  19.         stream.close();
  20.  
  21.     }
  22.  
  23.     return fileData;
  24. }
  25.  
  26. GLuint LoadShaders ( const char* vertShaderPath, const char* fragShaderPath ) {
  27.     GLuint vertShader = glCreateShader( GL_VERTEX_SHADER );
  28.     GLuint fragShader = glCreateShader( GL_FRAGMENT_SHADER );
  29.  
  30.     std::string vertShaderSource = LoadFileToString( vertShaderPath );
  31.     std::string fragShaderSource = LoadFileToString( fragShaderPath );
  32.     const char* rawVertShaderSource = vertShaderSource.c_str();
  33.     const char* rawFragShaderSource = fragShaderSource.c_str();
  34.  
  35.     glShaderSource( vertShader, 1, &rawVertShaderSource, NULL );
  36.     glShaderSource( fragShader, 1, &rawFragShaderSource, NULL) ;
  37.     glCompileShader( vertShader );
  38.     glCompileShader( fragShader );
  39.  
  40.     GLuint program = glCreateProgram();
  41.  
  42.     glAttachShader( program, vertShader );
  43.     glAttachShader( program, fragShader );
  44.     glLinkProgram(program);
  45.  
  46.     return program;
  47. }
  48.  
  49. int main(void){
  50.  
  51.     if( !glfwInit() )
  52.     {
  53.         fprintf( stderr, "Failed to initialize GLFW\n" );
  54.         return -1;
  55.     }
  56.  
  57.     GLFWwindow* window;
  58.  
  59.     window = glfwCreateWindow( glfwGetVideoMode(glfwGetPrimaryMonitor()) -> width, glfwGetVideoMode(glfwGetPrimaryMonitor()) -> height, "Test", NULL, NULL);
  60.  
  61.     if( !window ){
  62.         fprintf( stderr, "Failed to open window.\n" );
  63.         glfwTerminate();
  64.         return -1;
  65.     }
  66.  
  67.     glfwMakeContextCurrent(window);
  68.  
  69.     if (glewInit()) {
  70.         fprintf( stderr, "Failed to initialize GLEW\n" );
  71.         glfwTerminate();
  72.         return -1;
  73.     }
  74.  
  75.     GLuint vaoID;
  76.     glGenVertexArrays( 1, &vaoID );
  77.     glBindVertexArray( vaoID );
  78.  
  79.     static const GLfloat verts[] = {
  80.  
  81.         -1.0f, -1.0f, 0.0f,
  82.          1.0f, -1.0f, 0.0f,
  83.          0.0f,  1.0f, 0.0f
  84.  
  85.     };
  86.  
  87.     GLuint program = LoadShaders("shader.vertshader", "shader.fragshader");
  88.  
  89.     GLuint vboID;
  90.     glGenBuffers( 1, &vboID );
  91.     glBindBuffer( GL_ARRAY_BUFFER, vboID );
  92.     glBufferData( GL_ARRAY_BUFFER, sizeof( verts ), verts, GL_STATIC_DRAW );
  93.  
  94.  
  95.     while (!glfwWindowShouldClose(window)) {
  96.         glEnableVertexAttribArray( 0 );
  97.         glBindBuffer( GL_ARRAY_BUFFER, vboID );
  98.         glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 );
  99.         glUseProgram(program);
  100.         glDrawArrays( GL_TRIANGLES, 0, 3 );
  101.         glDisableVertexAttribArray( 0 );
  102.  
  103.         glfwSwapBuffers(window);
  104.         glfwPollEvents();
  105.     }
  106.     glfwTerminate();
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement