Skip to content

Instantly share code, notes, and snippets.

@Atem2069
Created January 20, 2021 20:45
Show Gist options
  • Save Atem2069/7b088a75e07edd3918be041de0c33a77 to your computer and use it in GitHub Desktop.
Save Atem2069/7b088a75e07edd3918be041de0c33a77 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<glad/glad.h>
#include<glfw/glfw3.h>
struct vec3
{
float x, y, z;
};
int main()
{
if (!glfwInit())
return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
GLFWwindow* m_window = glfwCreateWindow(500, 500, "Hello world!", nullptr, nullptr);
glfwMakeContextCurrent(m_window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
return -1;
vec3 triverts[4] =
{
{0.5f,0.5f,0.0f},
{0.5f,-0.5f,0.0f},
{-0.5f,-0.5f,0.0f},
{-0.5f,0.5f,0.0f}
};
unsigned int indices[6] =
{
0,1,3,1,2,3
};
GLuint m_vao = 0;
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
GLuint m_vbo = 0;
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(vec3), (void*)&triverts[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
GLuint m_ebo = 0;
glGenBuffers(1, &m_ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), (void*)&indices[0], GL_STATIC_DRAW);
while (!glfwWindowShouldClose(m_window))
{
glfwPollEvents();
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(m_vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
glfwSwapBuffers(m_window);
}
return 0;
}
@lowmoya
Copy link

lowmoya commented Jan 20, 2021

it completely slipped my mind that you could use a struct to organize the array, that is a very nice change that you made, and it makes perfect sense too sense structs are just aligned right next to each other starting at zero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment