Sign in to follow this  

OpenGL doesn't render my triangle

Recommended Posts

Somehow this code is not working. I don't know why and I also don't get any OpenGL errors.
 
 

#define GLEW_STATIC
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <GL\glew.h>
#include <glm.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
 
#define WIDTH 800
#define HEIGHT 600
#define TITLE "Dynamic"
 
GLFWwindow* window;
int vaoID;
 
 
float vertices[] = {-0.5f, 0.5f, 0, -0.5f, -0.5f, 0, 0.5f, -0.5f, 0, 0,};
GLushort indices[] = { 0, 1, 2};
 
void update() {
GLuint vertexBuffer;
GLuint indexBuffer;
GLuint vao;
glGenBuffers(1, &vertexBuffer);
glGenBuffers(1, &indexBuffer);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(0);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1, 0, 0, 1);
glfwSwapBuffers(window);
glDrawElements(GL_TRIANGLES, sizeof(indices), GL_UNSIGNED_SHORT, nullptr);
std::cout << glGetError() << std::endl;
}
}
 
int main() {
if (!glfwInit())
std::cout << "Couldn't initialize GLFW!" << std::endl;
 
window = glfwCreateWindow(WIDTH, HEIGHT, TITLE, NULL, NULL);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
 
if (GLEW_OK != glewInit())
std::cout << "GLEW is not working!" << std::endl;
 
std::cout << "Your GL version: " << glGetString(GL_VERSION) << std::endl;
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
update();
}

Share this post


Link to post
Share on other sites

You're drawing the triangle after you have swapped the back and front buffers. You need to first clear, then draw, and then swap the buffers.

Thank you, now it is working!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this