Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SFML/Window.hpp"
- //#include <SFML/OpenGL.hpp>
- #include "GL/glew.h"
- #include <iostream>
- //create a handle for the glslprogram (vert+frag)
- GLuint glslprogram;
- //this will hold our vertex coords that we pass on to the glsl program
- GLuint attribute_coord2d;
- int initresources()
- {
- //this will hold the status of the compiler check
- // set it to false initially
- GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
- //get a handle for our vertex shader
- GLuint myvertexshader = glCreateShader(GL_VERTEX_SHADER);
- //get a handle for our fragment shader
- GLuint myfragmentshader = glCreateShader(GL_FRAGMENT_SHADER);
- //define the source of the vertex shader
- const char* vs_source =
- "#version 150"
- "attribute vec2 coord2d;"
- "void main(void) {"
- "gl_Position = vec4(coord2d, 0.0, 1.0);"
- "}";
- //define the source of the fragment shader
- const char* fs_source =
- "#version 150 \n"
- "void main(void) { "
- " gl_FragColor[0] = 0.0; " //r
- " gl_FragColor[1] = 0.0; " //g
- " gl_FragColor[2] = 1.0; " //b
- "}";
- //set the sourcecode of myvertexshader to vs_source
- glShaderSource(myvertexshader, 1, &vs_source, NULL);
- //set the sourcecode of myfragmentshader to fs_source
- glShaderSource(myfragmentshader, 1, &fs_source, NULL);
- //compile the vertex shader
- glCompileShader(myvertexshader);
- //compile the fragment shader
- glCompileShader(myfragmentshader);
- //check if compile was OK, the result will be written to compile_ok
- glGetShaderiv(myvertexshader, GL_COMPILE_STATUS, &compile_ok);
- if (compile_ok == 0)
- std::cout << "Error compiling vertex shader";
- //check if compile of fragment shader was ok
- glGetShaderiv(myfragmentshader, GL_COMPILE_STATUS, &compile_ok);
- if (compile_ok == 0)
- std::cout << "Error compiling fragment shader";
- //now that we have compiled the shaders, lets make a program
- glslprogram = glCreateProgram();
- //attach the vertex shader to our program
- glAttachShader(glslprogram, myvertexshader);
- //attach the fragment shader to our progrma
- glAttachShader(glslprogram, myfragmentshader);
- //let's link the program
- glLinkProgram(glslprogram);
- //check if the linking was successful
- glGetProgramiv(glslprogram, GL_LINK_STATUS, &link_ok);
- if (!link_ok)
- std::cout << "Error linking our glsl program";
- //we called our variable in the glslprogram "coord2d"
- //lets make sure that opengl knows where to get it
- //ie lets bind attribute_coord2d (our code) to coord2d (shader code)
- const char* attribute_name = "coord2d";
- attribute_coord2d = glGetAttribLocation(glslprogram, attribute_name);
- if (attribute_coord2d == -1)
- std::cout << "Could not bind attribute";
- return 1;
- }
- void onDisplay()
- {
- //now that we have made our shaders and combinded them into a program
- //and now that the program knows where to get its input
- //lets make a display function that feeds our vertexes to the program
- glClearColor(1.0, 1.0, 1.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- //tell GL to use our program
- glUseProgram(glslprogram);
- glEnableVertexAttribArray(attribute_coord2d);
- //lets define our vertexes
- GLfloat triangle_vertices[] = {
- 0.0, 0.8,
- -0.8, -0.8,
- 0.8, -0.8
- };
- //describe the array to opengl
- glVertexAttribPointer(
- attribute_coord2d, //our attribute
- 2, //number of elements per vertex (x, y)
- GL_FLOAT, // these are floats
- GL_FALSE, // take our value as-is
- 0, // no extra data between each position
- triangle_vertices); // the name of our array
- //
- glDrawArrays(GL_TRIANGLES, 0, 3);
- glDisableVertexAttribArray(attribute_coord2d);
- }
- int main(int argc, char* argv[])
- {
- sf::Window App(sf::VideoMode(800, 600, 32),
- "OpenGL 3 window",
- sf::Style::Close,
- sf::ContextSettings(24, 8, 0, 3, 3));
- sf::ContextSettings settings = App.getSettings();
- std::cout << "depth bits:" << settings.depthBits << std::endl;
- std::cout << "stencil bits:" << settings.stencilBits << std::endl;
- std::cout << "antialiasing level:" << settings.antialiasingLevel << std::endl;
- std::cout << "version:" << settings.majorVersion << "." << settings.minorVersion << std::endl;
- App.display();
- glewInit();
- if ( !initresources() )
- return 1;
- while (1)
- {
- onDisplay(); //do the opengl rendering
- App.display(); //draw it on the screen...
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement