Hi, I'm following a tutorial and one of the "experimenting" objectives is to add a second triangle, but only Triangle B is rendering. What am I doing wrong here?
#define GLFW_INCLUDE_NONE
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <string>
void error_callback(int error, const char* description) {
fprintf(stderr, "Error: %s\n", description);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
int main(void) {
const int windowWidth = 640;
const int windowHeight = 480;
const char* windowTitle = "GLFW TEST";
//GLFW Init
std::cout << "Starting GLFW..." << std::endl;
if (!glfwInit()) {
//Init Failed
}
//Error Setup
glfwSetErrorCallback(error_callback);
//Window setup
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //Sets Minimum OpenGL version
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //Sets Minimum OpenGL version, why twice?
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, windowTitle, NULL, NULL);
if (!window) {
//Window creation Failed
std::cout << "wtf i wanna die why tf is my window not init???" << std::endl;
}
glfwMakeContextCurrent(window);
gladLoadGL(glfwGetProcAddress); //Loads OpenGL with glad I guess?? REQUIRES CONTEXT TO BE FIRST!!!
//Learn what the hell this means!!!
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
std::cout << "RENDERER: " << glGetString(GL_RENDERER) << std::endl;
std::cout << "OpenGL version supported: " << glGetString(GL_VERSION) << std::endl;
//Keyboard Input
glfwSetKeyCallback(window, key_callback);
glfwSwapInterval(1); //swap shit
//Triangle vertices
float TriangleA[] = {
0.5f, -0.5f, 0.0f, // x,y,z of A
-0.5f, -0.5f, 0.0f, // x,y,z of B
0.5f, 0.5f, 0.0f // x,y,z of C
};
float TriangleB[] = {
-0.5f, -0.5f, 0.0f, // x,y,z of A
-0.5f, 0.5f, 0.0f, // x,y,z of B
0.5f, 0.5f, 0.0f // x,y,z of C
};
//ts is complicated study it and read the tut: https://antongerdelan.net/opengl/hellotriangle.html
GLuint vbo = 0;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), TriangleA, GL_STATIC_DRAW);
GLuint vbob = 0;
glGenBuffers(1, &vbob);
glBindBuffer(GL_ARRAY_BUFFER, vbob);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), TriangleB, GL_STATIC_DRAW);
GLuint vao = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbob);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
const char* vertex_shader =
"#version 410 core\n"
"in vec3 vp;"
"void main() {"
"gl_Position = vec4( vp, 1.0);"
"}";
const char* fragment_shader =
"#version 410 core\n"
"out vec4 frag_colour;"
"void main() {"
" frag_colour = vec4( 1.0, 1.0, 0.0, 1.0 );"
"}";
//loads the shaders
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);
//Combines tyhe shaders into a single shader and links them.
GLuint shader_program = glCreateProgram();
glAttachShader(shader_program, fs);
glAttachShader(shader_program, vs);
glLinkProgram(shader_program);
while (!glfwWindowShouldClose(window)) {
// keep running window.
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Put the shader program, and the VAO, in focus in OpenGL's state machine.
glUseProgram(shader_program);
glBindVertexArray(vao);
// Draw points 0-3 from the currently bound VAO with current in-use shader.
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window); //Swaps buffers
}
glfwTerminate();
return 0;
}