Skip to content

Instantly share code, notes, and snippets.

@alexmercerind
Created May 23, 2020 10:08
Show Gist options
  • Save alexmercerind/a1d9115d9944fa1e82f8c2abcd1c9b5d to your computer and use it in GitHub Desktop.
Save alexmercerind/a1d9115d9944fa1e82f8c2abcd1c9b5d to your computer and use it in GitHub Desktop.
Textures in SFML GLEW C++ OpenGL
#include <GL/glew.h>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <shader_compiler.h>
int main() {
using namespace sf;
RenderWindow window(VideoMode(800, 800), "Alex Mercer", Style::Default);
window.setFramerateLimit(60);
glewInit();
compile_shader shader("vertexshader.vert", "fragmentshader.frag");
GLfloat vertices[12] = {
-0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
};
GLfloat texture[12] = {
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f,
};
GLuint indices[6] = {
0, 1, 2,
2, 3, 0,
};
GLuint VBOVertices, VBOIndices, VBOTextures, VAO;
glGenBuffers(1, &VBOVertices);
glBindBuffer(GL_ARRAY_BUFFER, VBOVertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)(sizeof(GLfloat) * 0));
glEnableVertexAttribArray(0);
glGenBuffers(1, &VBOTextures);
glBindBuffer(GL_ARRAY_BUFFER, VBOTextures);
glBufferData(GL_ARRAY_BUFFER, sizeof(texture), texture, GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (GLvoid*)(sizeof(GLfloat) * 0));
glEnableVertexAttribArray(1);
GLuint Texture;
glGenTextures(1, &Texture);
glBindTexture(GL_TEXTURE_2D, Texture);
sf::Image image;
image.loadFromFile("texture.jpg");
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getSize().x, image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
glGenBuffers(1, &VBOIndices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBOIndices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindVertexArray(0);
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
window.close();
};
};
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
shader.use_shader();
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
window.display();
};
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment