Reputation: 60
Hello everybody i started using opengl with c++ and i figured out that the functions aren't the same as in java lwjgl.So let me get to my point. I want to draw indices i looked a tutorial for drawing ibo's but it doesn't work.I get black screen only.
Window.cpp
#include "garbagecollector.h"
#include "window.h"
Window::Window(int width, int height, const std::string& title)
{
if(!glfwInit())
{
GarbageCollector::collectGarbage();
exit(EXIT_FAILURE);
}
/*glfwWindowHint(GLFW_SAMPLES, 32);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
*/
window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
if(!window){
std::cerr << "Cannot create window!" << std::endl;
GarbageCollector::collectGarbage();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
if(glewInit() != GLEW_OK)
{
std::cerr << "Failed to init GLEW!" << std::endl;
GarbageCollector::collectGarbage();
exit(EXIT_FAILURE);
}
}
void Window::clear(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Window::display(){
glfwSwapBuffers(window);
glfwPollEvents();
}
Window::~Window()
{
std::cout << "window" << std::endl;
glfwTerminate();
}
Mesh.h
#ifndef MESH_H
#define MESH_H
#include "object.h"
#include <GL/glew.h>
#include <vector>
struct Vertex{
float position[3];
};
class Mesh : public Object
{
public:
Mesh();
Mesh(Vertex vertecies[], int indices[]){
glGenBuffers(1, &vbo);
glGenBuffers(2, &ibo);
std::vector<unsigned int> indic;
indic.push_back(0);
indic.push_back(1);
indic.push_back(2);
size = indic.size();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 4, &vertecies, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex) * 3,0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, &indic[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void bind(){
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
}
void unbind(){
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void drawWithBindings(){
bind();
glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, 0);
unbind();
}
virtual ~Mesh();
protected:
private:
GLuint vbo, ibo;
unsigned int size;
};
#endif // MESH_H
Mesh.cpp
#include "mesh.h"
Mesh::Mesh()
{
//ctor
}
Mesh::~Mesh()
{
std::cout << "mesh" << std::endl;
}
renderingEngine.h
#ifndef RENDERINGENGINE_H
#define RENDERINGENGINE_H
#include "game.h"
#include "mesh.h"
#include "shader.h"
class RenderingEngine : public Object
{
public:
RenderingEngine();
void render();
virtual ~RenderingEngine();
protected:
private:
Mesh* mesh;
Shader* shader;
Vertex vertex[3];
};
#endif // RENDERINGENGINE_H
renderingEngine.cpp
#include "renderingengine.h"
RenderingEngine::RenderingEngine()
{
vertex[0].position[0] = -1;
vertex[0].position[1] = 0;
vertex[0].position[2] = 0;
vertex[1].position[0] = 1;
vertex[1].position[1] = -1;
vertex[1].position[2] = 0;
vertex[2].position[0] = 1;
vertex[2].position[1] = 1;
vertex[2].position[2] = 0;
int indices[3] = {0, 1, 2};
shader = new Shader("vertex.vs", "fragment.fs");
mesh = new Mesh(vertex, indices);
}
RenderingEngine::~RenderingEngine()
{
std::cout << "renderingEngine" << std::endl;
}
void RenderingEngine::render(){
Game::getWindow().clear();
shader->bind();
mesh->drawWithBindings();
Game::getWindow().display();
}
Shader.h
#ifndef SHADER_H
#define SHADER_H
#include "object.h"
#include <GL/glew.h>
#include <cstdlib>
#include <fstream>
class Shader : public Object
{
public:
Shader();
Shader(const std::string& vertexShader, const std::string& fragmentShader){
program = glCreateProgram();
if(program == 0){
std::cerr << "Cannot create program" << std::endl;
exit(EXIT_FAILURE);
}
glBindAttribLocation(program, 0, "pos");
addVertexShader(vertexShader);
addFragmentShader(fragmentShader);
compileProgram();
}
void addVertexShader(const std::string& filename){
addShader(loadShader(filename), GL_VERTEX_SHADER);
}
void addFragmentShader(const std::string& filename){
addShader(loadShader(filename), GL_FRAGMENT_SHADER);
}
void compileProgram(){
glLinkProgram(program);
GLint errorprogram = 0;
char array[2048];
int i = 0;
glGetProgramiv(program, GL_LINK_STATUS, &errorprogram);
if(errorprogram == 0){
glGetProgramInfoLog(program, 2048, &i, array);
std::cout << array << std::endl;
}
glValidateProgram(program);
glGetProgramiv(program, GL_VALIDATE_STATUS, &errorprogram);
if(errorprogram == 0){
glGetProgramInfoLog(program, 2048, &i, array);
std::cout << array << std::endl;
}
}
void bind(){
glUseProgram(program);
}
virtual ~Shader();
protected:
private:
void addShader(const std::string& source, GLuint type){
GLint shader = glCreateShader(type);
if(shader == 0){
std::cerr << "Cannot shader program" << std::endl;
exit(EXIT_FAILURE);
}
const GLchar* h[1];
h[0] = source.c_str();
const GLint s[1] = {(GLint)source.size()};
glShaderSource(shader, 1, h, s);
glCompileShader(shader);
GLint errorShader = 0;
char array[2048];
int i = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &errorShader);
if(errorShader == 0){
glGetShaderInfoLog(shader, 2048, &i, array);
std::cout << array << std::endl;
}
glAttachShader(program, shader);
}
std::string loadShader(const std::string& filename) const{
std::string source = "";
std::ifstream shader(filename);
if(shader.is_open()){
std::string line;
while(!shader.eof()){
while(getline(shader, line)){
source += line;
source += '\n';
}
}
}
return source;
}
GLuint program = 0;
};
#endif // SHADER_H
Shader.cpp
#include "shader.h"
Shader::Shader()
{
//ctor
}
Shader::~Shader()
{
std::cout << "shader" << std::endl;
}
Upvotes: 1
Views: 1575
Reputation: 54572
Not sure if I spotted everything, but here are a few issues that jumped out:
Wrong argument to glGenBuffers()
:
glGenBuffers(1, &vbo);
glGenBuffers(2, &ibo);
In the second call, you also want to generate only one buffer name, so it should be:
glGenBuffers(1, &vbo);
glGenBuffers(1, &ibo);
Wrong size for vertex buffer:
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 4, &vertecies, GL_STATIC_DRAW);
In the posted example, you only have 3 vertices. So this should be:
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 3, &vertecies, GL_STATIC_DRAW);
In reality, you would probably want to pass the number of vertices as an argument, or pass a vector
instead of an array.
Wrong value for stride:
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex) * 3,0);
The 5th argument is the stride, which is the offset between vertices in bytes. In this case, that's the size of one vertex:
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex), 0);
Wrong size for index buffer:
size = indic.size();
...
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, &indic[0], GL_STATIC_DRAW);
The size
variable contains the number of indices. The argument to glBufferData()
is the size in bytes:
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size * sizeof(GLuint), &indic[0], GL_STATIC_DRAW);
Upvotes: 4