Last active
January 9, 2018 20:34
-
-
Save sebas86/9a4136202cb129ef543020e522bb0be3 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 <GL/glew.h> | |
#include <GLFW/glfw3.h> | |
#include <iostream> | |
//-----------------------------SHADERS-------------------------------------------------------------------- | |
static unsigned int CompileShader(unsigned int type, const std::string& source) | |
{ | |
unsigned int id = glCreateShader(type); | |
const char* src = source.c_str(); | |
glShaderSource(id, 1, &src, nullptr); | |
glCompileShader(id); | |
int result; | |
glGetShaderiv(id, GL_COMPILE_STATUS, &result); | |
if (result == GL_FALSE) | |
{ | |
int length; | |
glGetShaderiv(id,GL_INFO_LOG_LENGTH,&length); | |
char* message=(char*)alloca(length *sizeof(char)); | |
glGetShaderInfoLog(id,length,&length,message); | |
std::cout<<"Failed to compile shader"<<std::endl; | |
std::cout<<message<<std::endl; | |
glDeleteShader(id); | |
return(0); | |
} | |
std::cout << id<< std::endl; | |
return id; | |
} | |
static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader) | |
{ | |
unsigned int program = glCreateProgram(); | |
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader); | |
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader); | |
glAttachShader(program, vs); | |
glAttachShader(program, fs); | |
glLinkProgram(program); | |
glValidateProgram(program); | |
glDeleteShader(vs); | |
glDeleteShader(fs); | |
std::cout << program<< std::endl; | |
return program; | |
} | |
//-------------------------------------MAIN--------------------------------------------------- | |
int main(void) | |
{ | |
//-------------------------INICJALIZACJA-I-TWORZENIE-OKNA------------------------ | |
GLFWwindow* window; | |
/* Initialize the library */ | |
if (!glfwInit()) | |
{ | |
std::cout<<"ERROR!"<<std::endl; | |
return -1; | |
} | |
/* Create a windowed mode window and its OpenGL context */ | |
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); | |
if (!window) | |
{ | |
std::cout<<"Error!"<<std::endl; | |
glfwTerminate(); | |
return -1; | |
} | |
/* Make the window's context current */ | |
glfwMakeContextCurrent(window); | |
if (glewInit() != GLEW_OK) | |
{ | |
std::cout << "Error!"<< std::endl; | |
return -1; | |
}; | |
//------------------------------------TWORZENIE-WYPELNIANIE-BUFORA---------- | |
float positions[] = { | |
-0.5f,-0.5f, | |
0.0f, 0.5f, | |
0.5f,-0.5f}; | |
GLuint buffer; // zmienna przechowująca id buforu | |
glGenBuffers(1, &buffer); //Tworzenie buforu, podanie jego nazwy do zmiennej buffer | |
glBindBuffer(GL_ARRAY_BUFFER, buffer); // Połączenie buforu z GL_ARRAY_BUFFER | |
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW); //spisanie danych do buforu | |
glEnableVertexAttribArray(0); | |
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0); | |
//------------------------------------SHADERY-------------------------------- | |
std::string vertexShader= | |
"attribute vec3 position;\n" | |
"void main ()\n" | |
"{\n" | |
" gl_Position = vec4(position,1.0);\n" | |
"}"; | |
std::string fragmentShader= | |
"void main()\n" | |
"{\n" | |
" gl_FragColor=vec4(1.0,0.0,0.0,1.0);\n" | |
"}\n"; | |
std::cout<<fragmentShader<<std::endl; | |
unsigned int shader=CreateShader(vertexShader,fragmentShader); | |
glUseProgram(shader); | |
while (!glfwWindowShouldClose(window)) | |
{ | |
glClearColor(1, 1, 1, 1); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
glfwSwapBuffers(window); | |
glfwPollEvents(); | |
} | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment