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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
#include <iostream>
#include <SFML\Graphics.hpp>
#include <GL\glew.h>
#include "SOIL.h"
GLuint compiler_shader()
{
GLuint vertex_shader;
GLuint fragment_shader;
GLuint program;
static const GLchar * vertex_shader_source[] =
{
"#version 400 core \n"
" \n"
"layout (location = 0) in vec3 position; \n"
"layout (location = 1) in vec3 color; \n"
"layout (location = 2) in vec2 texCoord; \n"
" \n"
"out vec3 ourColor; \n"
"out vec2 TexCoord; \n"
" \n"
"void main() \n"
"{ \n"
" gl_Position = vec4(position, 1.0f); \n"
" ourColor = color; \n"
" TexCoord = texCoord; \n"
" \n"
"} \n"
};
static const GLchar * fragment_shader_source[] =
{
"#version 400 core \n"
" \n"
"in vec3 outColor; \n"
"in vec2 TexCoord; \n"
" \n"
"out vec4 color; \n"
" \n"
"uniform sampler2D ourTexture1; \n"
" \n"
" \n"
"void main() \n"
"{ \n"
" color = texture(ourTexture1, TexCoord); \n"
"} \n"
" \n"
};
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, vertex_shader_source, nullptr);
glCompileShader(vertex_shader);
GLint success1;
GLchar info1[520];
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success1);
if(!success1)
{
glGetShaderInfoLog(vertex_shader, 512, nullptr, info1);
for(int i = 0; i < 502; i++)
std::cout << info1 << std::endl;
}
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, fragment_shader_source, nullptr);
glCompileShader(fragment_shader);
GLint success2;
GLchar info2[520];
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success2);
if(!success2)
{
glGetShaderInfoLog(fragment_shader, 512, nullptr, info2);
for(int i = 0; i < 502; i++)
std::cout << info2 << std::endl;
}
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
GLint success3;
GLchar info3[512];
glGetProgramiv(program, GL_LINK_STATUS, &success3);
if(!success3)
{
glGetProgramInfoLog(program, 512, nullptr, info3);
for(int i = 0; i < 512; i++)
std::cout << "Program: " << info3 << std::endl;
}
glDetachShader(program, vertex_shader);
glDetachShader(program, fragment_shader);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
return program;
}
int main()
{
const unsigned WindowX = 600;
const unsigned WindowY = 600;
sf::RenderWindow Window(sf::VideoMode(WindowX, WindowY), "OpenGL Textures", sf::Style::Default, sf::ContextSettings(32));
if(GLEW_OK != glewInit())
std::cout << "Error! Could not load Glew" << std::endl;
GLuint render_program = compiler_shader();
GLfloat vertices[] = {
// Position // Color
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top Right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Bottom Right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f // Top Left
};
GLfloat indices[] = {
0, 1, 3,
1, 2, 3
};
GLuint VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
GLuint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
GLuint EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Position attributes
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*) 0);
glEnableVertexAttribArray(0);
// Color attributes
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid *) (3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
// Tex Coordinates attributes
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid *) (6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
//--------------
// Texture
//--------------
GLuint Texture1;
glGenTextures(1, &Texture1);
glBindTexture(GL_TEXTURE_2D, Texture1);
// Set Texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set Texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int tex_width;
int tex_height;
unsigned char * image = SOIL_load_image("container.jpg", &tex_width, &tex_height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);
sf::Clock clock;
bool stop = false;
while(Window.isOpen())
{
Window.clear();
sf::Time dt = clock.restart();
sf::Event Event;
while(Window.pollEvent(Event))
{
switch(Event.type)
{
case sf::Event::Closed:
Window.close();
stop = true;
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
Window.close();
stop = true;
}
break;
}
}
if(stop)
break;
glUseProgram(render_program);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
Window.display();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
return 0;
}
|