Jump to content
  • Advertisement
Sign in to follow this  
MrX17

OpenGL doesn't render my triangle

This topic is 665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!