Advertisement

OpenGL doesn't render my triangle

Started by December 24, 2016 02:16 PM
1 comment, last by MrX17 8 years ago

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();
}

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.

Advertisement

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!

This topic is closed to new replies.

Advertisement