1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include <SFML\Graphics.hpp>
#include <GL\glew.h>
#include <iostream>
using std::cout;
using std::endl;
GLuint compile_shader()
{
static const GLchar * vertex_shader_source[] =
{
"#version 400 core \n"
" \n"
"layout(location = 0) in vec3 position; \n"
"layout(location = 1) in vec3 input_color; \n"
" \n"
"out vec3 output_color; \n"
" \n"
"void main(void) \n"
"{ \n"
" output_color = input_color; \n"
" gl_Position = vec4(position.x, position.y, position.z, 1.0f); \n"
"} \n"
};
static const GLchar * fragment_shader_source[] =
{
"#version 400 core \n"
" \n"
"in vec3 output_color; \n"
" \n"
"out vec3 color; \n"
" \n"
"void main(void) \n"
"{ \n"
" \n"
" color = output_color; \n"
"} \n"
};
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, vertex_shader_source, nullptr);
glCompileShader(vertex_shader);
GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, fragment_shader_source, nullptr);
glCompileShader(fragment_shader);
GLint success1;
GLchar info1[512];
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success1);
if(!success1)
{
glGetShaderInfoLog(vertex_shader, 512, nullptr, info1);
for(int i = 0; i < 2; i++)
cout << info1;
}
GLint success2;
GLchar info2[512];
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success2);
if(!success2)
{
glGetShaderInfoLog(fragment_shader, 512, nullptr, info2);
for(int i = 0; i < 2; i++)
cout << info2;
}
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertex_shader);
glAttachShader(shaderProgram, fragment_shader);
glLinkProgram(shaderProgram);
GLint success3;
GLchar info3[512];
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success3);
if(!success3)
{
glGetProgramInfoLog(shaderProgram, 512, nullptr, info3);
for(int i = 0; i < 2; i++)
cout << info3;
}
glDetachShader(shaderProgram, vertex_shader);
glDetachShader(shaderProgram, fragment_shader);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
return shaderProgram;
}
int main()
{
sf::RenderWindow Window(sf::VideoMode(800, 600), "OpenGL EBO");
if(GLEW_OK != glewInit())
cout << "Error! Could not initialize Glew" << endl;
const GLfloat vertices[] =
{
0.5f, 0.5f, 0.0f, // TR
0.5f, -0.5f, 0.0f, // BR
-0.5f, -0.5f, 0.0f, // BL
-0.5f, 0.5f, 0.0f // TL
};
GLuint indices[] =
{
0, 1, 3,
1, 2, 3
};
GLfloat greenColor[] =
{
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
GLuint shaderProgram = compile_shader();
GLuint VAO1;
glGenVertexArrays(1, &VAO1);
glBindVertexArray(VAO1);
GLuint VBO1;
glGenBuffers(1, &VBO1);
glBindBuffer(GL_ARRAY_BUFFER, VBO1);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
GLuint VBO2;
glGenBuffers(1, &VBO2);
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
glBufferData(GL_ARRAY_BUFFER, sizeof(greenColor), greenColor, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(1);
GLuint EBO1;
glGenBuffers(1, &EBO1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO1);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindVertexArray(0);
while(Window.isOpen())
{
Window.clear();
sf::Event Event;
while(Window.pollEvent(Event))
{
switch(Event.type)
{
case sf::Event::Closed:
Window.close();
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
Window.close();
break;
}
}
glUseProgram(shaderProgram);
glBindVertexArray(VAO1);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
Window.display();
}
glDeleteVertexArrays(1, &VAO1);
glDeleteBuffers(1, &VBO1);
glDeleteBuffers(1, &VBO2);
glDeleteBuffers(1, &EBO1);
system("Pause");
return 0;
}
|