PDA

View Full Version : error at gldrawelements, for drawing cube



saikumar
11-16-2015, 05:36 AM
hello guys i am new to opengl, my program is closing when it reaches gldrawelements,
it compiles nicely but it gets close at gldrawelements.
please help me out, here is my program

thanks in advance

main function:


#include <iostream>
#include <SDL2\SDL.h>
#include <GL\glew.h>
#include <glm\glm.hpp>
#include "display.h"
#include "mesh.h"
#include "shader.h"

using namespace std;
using namespace glm;

int main(int argc, char** argv)
{
display disp(800, 600, "learning3");

Vertex vertices[] = {
Vertex(vec3(-1.0f, -1.0f, 1.0f)),
Vertex(vec3(1.0f, -1.0f, 1.0f)),
Vertex(vec3(1.0f, 1.0f, 1.0f)),
Vertex(vec3(-1.0f, 1.0f, 1.0f)),

Vertex(vec3(-1.0f, -1.0f, -1.0f)),
Vertex(vec3(1.0f, -1.0f, -1.0f)),
Vertex(vec3(1.0f, 1.0f, -1.0f)),
Vertex(vec3(-1.0f, 1.0f, -1.0f)),
};
GLuint indices[] = {
0, 1, 2,
2, 3, 0,
// top
3, 2, 6,
6, 7, 3,
// back
7, 6, 5,
5, 4, 7,
// bottom
4, 5, 1,
1, 0, 4,
// left
4, 0, 3,
3, 7, 4,
// right
1, 5, 6,
6, 2, 1,
};

mesh mes(vertices, sizeof(vertices) / sizeof(vertices[0]), indices, sizeof(indices) / sizeof(indices[0]));
shader shaderload("./basics");

while (!disp.windowclose())
{
disp.clear(0.0f, 0.2f, 0.6f, 1.0f);
shaderload.bind();
mes.Draw();
disp.update();
}
return (0);
}


shader loading:



#include "shader.h"
#include <iostream>
#include <fstream>

using namespace std;

shader::shader(const std::string& fileName)
{
m_program = glCreateProgram();

m_shaders[0] = createshader(loadshader(fileName + ".vert"), GL_VERTEX_SHADER);
m_shaders[1] = createshader(loadshader(fileName + ".frag"), GL_FRAGMENT_SHADER);

for (unsigned int i = 0; i < NUM_SHADERS; i++)
glAttachShader(m_program, m_shaders[i]);

glLinkProgram(m_program);
CheckShaderError(m_program, GL_LINK_STATUS, true, "Error linking shader program");

glValidateProgram(m_program);
CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "Invalid shader program");
}


shader::~shader()
{
for (unsigned int i = 0; i < NUM_SHADERS; i++)
{
glDetachShader(m_program, m_shaders[i]);
glDeleteShader(m_shaders[i]);
}

glDeleteProgram(m_program);
}

void shader::bind()
{
glUseProgram(m_program);
}

string shader::loadshader(const string& fileName)
{
std::ifstream file;
file.open((fileName).c_str());

std::string output;
std::string line;

if (file.is_open())
{
while (file.good())
{
getline(file, line);
output.append(line + "\n");
}
}
else
{
std::cerr << "Unable to load shader: " << fileName << std::endl;
}

return output;
}

void shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const string& errorMessage)
{
GLint success = 0;
GLchar error[1024] = { 0 };

if (isProgram)
glGetProgramiv(shader, flag, &success);
else
glGetShaderiv(shader, flag, &success);

if (success == GL_FALSE)
{
if (isProgram)
glGetProgramInfoLog(shader, sizeof(error), NULL, error);
else
glGetShaderInfoLog(shader, sizeof(error), NULL, error);

cerr << errorMessage << ": '" << error << "'" << endl;
}
}

GLuint shader::createshader(const string& text, unsigned int type)
{
GLuint shader = glCreateShader(type);

if (shader == 0)
cerr << "Error compiling shader type " << type << endl;

const GLchar* p[1];
p[0] = text.c_str();
GLint lengths[1];
lengths[0] = text.length();

glShaderSource(shader, 1, p, lengths);
glCompileShader(shader);

CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!");

return shader;
}




loading data to opengl mesh.cpp


#include "mesh.h"
#include <iostream>

mesh::mesh(Vertex* vertices, unsigned int numVertices, GLuint* indices, unsigned int numindices)
{
m_drawCount = numindices;
GLuint m_count = sizeof(vertices);


glGenVertexArrays(1,&m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);

glGenBuffers(1, &m_vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers);
glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(vertices[0]), vertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindVertexArray(0);

glGenBuffers(1, &m_indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}

void mesh::Draw()
{
glBindVertexArray(m_vertexArrayObject);
glDrawElements(GL_TRIANGLES, m_drawCount, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}


mesh::~mesh()
{
glDeleteBuffers(1,&m_vertexArrayBuffers);
glDeleteVertexArrays(1, &m_vertexArrayObject);
}


display function


#include "display.h"
#include <SDL2\SDL.h>
#include <GL\glew.h>
#include <iostream>

display::display(int width, int height, const string& title)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glwindow = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
glcontext = SDL_GL_CreateContext(glwindow);

GLenum res = glewInit();
if (res != GLEW_OK)
{
cerr << "Glew failed to initialize!" << endl;
}

close = false;
}

void display::clear(float r, float g, float b, float a)
{
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

}

void display::update()
{
SDL_GL_SwapWindow(glwindow);
SDL_Event e;
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
close = true;
}
}

bool display::windowclose()
{
return close;
}

display::~display()
{
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(glwindow);
SDL_Quit();
}

saikumar
11-16-2015, 05:51 AM
shaders



#version 430

in layout(location = 0) vec3 position;

void main()
{
gl_Position=vec4(position,1.0f);
}




#version 430
out vec4 color;
void main()
{
color=vec4(0.1f, 0.5f, 0.4f, 1.0f);
}