Created
January 22, 2013 07:21
-
-
Save anonymous/4592760 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SFML/Graphics.hpp> | |
#include <GL/glew.h> | |
#include <SFML/OpenGL.hpp> | |
#include <iostream> | |
#include "symGL.h" | |
int main() | |
{ | |
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML OpenGL"); | |
// Print OpenGL driver info | |
sf::ContextSettings settings = window.getSettings(); | |
std::cout << "Max version: " << settings.majorVersion << std::endl; | |
std::cout << "Min version: " << settings.minorVersion << std::endl; | |
std::cout << "Stencil bits: " << settings.stencilBits << std::endl; | |
std::cout << "Depth bits: " << settings.depthBits << std::endl; | |
std::cout << "Anti aliasing: " << settings.antialiasingLevel << std::endl; | |
sf::Clock clock; | |
GLenum glewStatus = glewInit(); | |
if(glewStatus != GLEW_OK) | |
{ | |
std::cout << "Error: " << glewGetErrorString(glewStatus) << std::endl; | |
return 1; | |
} | |
const float dotVertice[] = { | |
-1.0f, -1.0f, 0.0f, | |
0.0f, 1.0f, 0.0f, | |
1.0f, -1.0f, 0.0f, | |
}; | |
GLuint vbo; | |
glGenBuffers(1, &vbo); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(dotVertice), dotVertice, GL_STATIC_DRAW); | |
GLuint shaderProgram = glCreateProgram(); | |
GLuint shaderVertex = glCreateShader(GL_VERTEX_SHADER); | |
symGL::loadShader("vertexshader.shader", shaderProgram, shaderVertex); | |
glUseProgram(shaderProgram); | |
while (window.isOpen()) | |
{ | |
double tick = clock.restart().asSeconds(); | |
// Process events | |
sf::Event Event; | |
while (window.pollEvent(Event)) | |
{ | |
// Close window : exit | |
if (Event.type == sf::Event::Closed) | |
window.close(); | |
// Escape key : exit | |
if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape)) | |
window.close(); | |
} | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glEnableVertexAttribArray(0); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
glDisableVertexAttribArray(0); | |
window.display(); | |
} | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "symGL.h" | |
#include <fstream> | |
#include <iostream> | |
void symGL::loadShader(const std::string& filePath, GLuint& shaderProgram, GLuint& shader) | |
{ | |
std::ifstream reader(filePath.c_str()); | |
if(reader.is_open()) | |
{ | |
std::string sourceCode = ""; | |
std::string line = ""; | |
while(std::getline(reader, line)) | |
{ | |
sourceCode += "\n" + line; | |
line = ""; | |
} | |
reader.close(); | |
std::cout << std::endl; | |
std::cout << ">>>>>>>>>>> SOURCE CODE <<<<<<<<<<<" << std::endl; | |
std::cout << sourceCode << std::endl; | |
std::cout << std::endl; | |
std::cout << ">>>>>>>>>>> >>>> <<<< <<<<<<<<<<<" << std::endl; | |
const char* sourceCodePointer = sourceCode.c_str(); | |
// Error checking variables | |
int infoLogLength = 0; | |
GLint isError = GL_FALSE; | |
// Give shader it's source code and compile it | |
glShaderSource(shader, 1, &sourceCodePointer, NULL); | |
glCompileShader(shader); | |
// Print compilation errors, if any | |
glGetShaderiv(shader, GL_COMPILE_STATUS, &isError); | |
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength); | |
std::vector<char> compErrorMsg(infoLogLength); | |
glGetShaderInfoLog(shader, infoLogLength, NULL, &compErrorMsg[0]); | |
if(isError != GL_TRUE) {std::cout << "Compilation errors: " << &compErrorMsg[0] << std::endl;} | |
// Attach shader to program object, and link | |
glAttachShader(shaderProgram, shader); | |
glLinkProgram(shaderProgram); | |
isError = GL_FALSE; // Reset status | |
infoLogLength = 0; | |
// Print linking errors, if any | |
glGetProgramiv(shader, GL_LINK_STATUS, &isError); | |
glGetProgramiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength); | |
std::vector<char> linkErrorMsg(infoLogLength); | |
glGetProgramInfoLog(shader, infoLogLength, NULL, &linkErrorMsg[0]); | |
if(isError != GL_TRUE) {std::cout << "Linker errors: " << &linkErrorMsg[0] << std::endl;} | |
// Validate shader program | |
glValidateProgram(shaderProgram); | |
isError = GL_FALSE; | |
infoLogLength = 0; | |
// Print validation errors, if any | |
glGetProgramiv(shader, GL_VALIDATE_STATUS, &isError); | |
glGetProgramiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength); | |
std::vector<char> validErrorMsg(infoLogLength); | |
glGetProgramInfoLog(shader, infoLogLength, NULL, &validErrorMsg[0]); | |
if(isError != GL_TRUE) { std::cout << "Validation errors: " << &validErrorMsg[0] << std::endl;} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SYMGL_H_INCLUDED | |
#define SYMGL_H_INCLUDED | |
#include <GL/glew.h> | |
#include <SFML/OpenGL.hpp> | |
#include <string> | |
#include <vector> | |
class symGL | |
{ | |
public: | |
static void loadShader(const std::string& filePath, GLuint& shaderProgram, GLuint& shader); | |
}; | |
#endif // SYMGL_H_INCLUDED |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment