Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <glew.h>
- #include <GLFW/glfw3.h>
- #include <string>
- #include <stdlib.h>
- #include <fstream>
- std::string LoadFileToString( const char* filepath ) {
- std::string fileData = "";
- std::ifstream stream( filepath, std::ios::in );
- if ( stream.is_open() ) {
- std::string line = "";
- while ( getline( stream, line ) ) {
- fileData += "\n" + line;
- }
- stream.close();
- }
- return fileData;
- }
- GLuint LoadShaders ( const char* vertShaderPath, const char* fragShaderPath ) {
- GLuint vertShader = glCreateShader( GL_VERTEX_SHADER );
- GLuint fragShader = glCreateShader( GL_FRAGMENT_SHADER );
- std::string vertShaderSource = LoadFileToString( vertShaderPath );
- std::string fragShaderSource = LoadFileToString( fragShaderPath );
- const char* rawVertShaderSource = vertShaderSource.c_str();
- const char* rawFragShaderSource = fragShaderSource.c_str();
- glShaderSource( vertShader, 1, &rawVertShaderSource, NULL );
- glShaderSource( fragShader, 1, &rawFragShaderSource, NULL) ;
- glCompileShader( vertShader );
- glCompileShader( fragShader );
- GLuint program = glCreateProgram();
- glAttachShader( program, vertShader );
- glAttachShader( program, fragShader );
- glLinkProgram(program);
- return program;
- }
- int main(void){
- if( !glfwInit() )
- {
- fprintf( stderr, "Failed to initialize GLFW\n" );
- return -1;
- }
- GLFWwindow* window;
- window = glfwCreateWindow( glfwGetVideoMode(glfwGetPrimaryMonitor()) -> width, glfwGetVideoMode(glfwGetPrimaryMonitor()) -> height, "Test", NULL, NULL);
- if( !window ){
- fprintf( stderr, "Failed to open window.\n" );
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- if (glewInit()) {
- fprintf( stderr, "Failed to initialize GLEW\n" );
- glfwTerminate();
- return -1;
- }
- GLuint vaoID;
- glGenVertexArrays( 1, &vaoID );
- glBindVertexArray( vaoID );
- static const GLfloat verts[] = {
- -1.0f, -1.0f, 0.0f,
- 1.0f, -1.0f, 0.0f,
- 0.0f, 1.0f, 0.0f
- };
- GLuint program = LoadShaders("shader.vertshader", "shader.fragshader");
- GLuint vboID;
- glGenBuffers( 1, &vboID );
- glBindBuffer( GL_ARRAY_BUFFER, vboID );
- glBufferData( GL_ARRAY_BUFFER, sizeof( verts ), verts, GL_STATIC_DRAW );
- while (!glfwWindowShouldClose(window)) {
- glEnableVertexAttribArray( 0 );
- glBindBuffer( GL_ARRAY_BUFFER, vboID );
- glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0 );
- glUseProgram(program);
- glDrawArrays( GL_TRIANGLES, 0, 3 );
- glDisableVertexAttribArray( 0 );
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement