Created
October 24, 2022 03:56
-
-
Save LucasM127/f28dfe22ee687d827dbd6bd911e4340f to your computer and use it in GitHub Desktop.
UV Instancing
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 <SFML/Window.hpp> | |
#include "shader.hpp" | |
#include <iostream> | |
//list of triangles points | |
const float tris[] = | |
{ | |
-0.75,0.0, | |
0.0,0.0, | |
0.0,0.75, | |
-0.75,-0.25, | |
0.0,-0.5, | |
-0.75,-0.75, | |
0.25,-0.75, | |
0.5,0.5, | |
0.75,-0.75 | |
}; | |
float uvs[] = | |
{ | |
0.0, 0.0, | |
0.5, 0.0, | |
1.0, 1.0 | |
}; | |
int main() | |
{ | |
sf::ContextSettings contextSettings; | |
contextSettings.majorVersion = 3; | |
contextSettings.minorVersion = 3; | |
contextSettings.stencilBits = 8; | |
sf::Window window(sf::VideoMode(640,640),"Instancing Test", sf::Style::Default, contextSettings); | |
window.setActive(true); | |
if(!gladLoadGL()) | |
{ | |
std::cout<<"Failed to initialize GLAD"<<std::endl; | |
return -1; | |
} | |
GLuint vao; | |
glGenVertexArrays(1, &vao); | |
GLuint vertexBuffer, uvBuffer; | |
glGenBuffers(1, &vertexBuffer); | |
glGenBuffers(1, &uvBuffer); | |
glBindVertexArray(vao); | |
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(uvs), uvs, GL_STATIC_DRAW); | |
glEnableVertexAttribArray(0); | |
glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,2*sizeof(float),nullptr); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(tris), tris, GL_STATIC_DRAW); | |
glEnableVertexAttribArray(1); | |
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,2*sizeof(float),nullptr); | |
glVertexAttribDivisor(0,0); | |
glVertexAttribDivisor(1,1); | |
glBindVertexArray(0); | |
GLuint program = compileProgram("filledBezVert.glsl","filledBezFrag.glsl"); | |
glClearColor(0.0f,0.1f,0.1f,1.0f); | |
bool isRunning = true; | |
while (isRunning) | |
{ | |
glClear(GL_COLOR_BUFFER_BIT); | |
//it works... something to do with the shader then !!! | |
glUseProgram(program); | |
glBindVertexArray(vao); | |
// glDrawArrays(GL_TRIANGLES,0,3);//what? | |
glDrawArraysInstanced(GL_TRIANGLES,0,3,9);//3 instances | |
glBindVertexArray(0); | |
glUseProgram(0); | |
window.display(); | |
sf::Event event; | |
window.waitEvent(event); | |
if(event.type == sf::Event::Closed) | |
isRunning = false; | |
} | |
window.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment