//
// main.cpp
// Ennoteplus
//
// Created by Zhou hao on 2018/3/3.
// Copyright © 2018年 whatever. All rights reserved.
//
#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
float tri[] = {
0.0f, 0.0f, 0.0f,
0.5f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f
};
GLuint vao, vbo, prog;
GLuint loadProgram(string vpath, string fpath);
int main(int argc, const char * argv[]) {
// insert code here...
sf::ContextSettings settings;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 4;
settings.majorVersion = 3;
settings.minorVersion = 3;
settings.attributeFlags = sf::ContextSettings::Core;
sf::RenderWindow window(sf::VideoMode(800, 800), "SFML Window", sf::Style::Default, settings);
glGenVertexArraysAPPLE(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArrayAPPLE(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(tri), tri, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, nullptr);
glEnableVertexAttribArray(0);
prog = loadProgram("Shader/vertex.glsl", "Shader/fragment.glsl");
glClearColor(0.3f, 0.2f, 0.1f, 1.0f);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArrayAPPLE(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
window.display();
}
return 0;
}
GLuint loadProgram(string vpath, string fpath) {
GLuint v, f;
stringstream ss;
char log[512];
v = glCreateShader(GL_VERTEX_SHADER);
fstream vf(vpath);
if (vf.good()) {
ss << vf.rdbuf();
string str = ss.str();
const GLchar* g = str.c_str();
glShaderSource(v, 1, &g, nullptr);
glCompileShader(v);
glGetShaderInfoLog(v, 512, nullptr, log);
cout << "Vertex Shader: " << log << endl;
} else {
cout << "Bad path " << vpath.c_str() << endl;
return static_cast<GLuint>(-1);
}
ss.str("");
f = glCreateShader(GL_FRAGMENT_SHADER);
fstream ff(fpath);
if (ff.good()) {
ss << ff.rdbuf();
string str = ss.str();
const GLchar* g = str.c_str();
glShaderSource(f, 1, &g, nullptr);
glCompileShader(f);
glGetShaderInfoLog(f, 512, nullptr, log);
cout << "Fragment Shader: " << log << endl;
} else {
cout << "Bad path " << fpath.c_str() << endl;
return static_cast<GLuint>(-1);
}
GLuint prog;
prog = glCreateProgram();
glAttachShader(prog, v);
glAttachShader(prog, f);
glLinkProgram(prog);
glGetProgramInfoLog(prog, 512, nullptr, log);
cout << "Program: " << log;
glDeleteShader(v);
glDeleteShader(f);
return prog;
}