PDA

View Full Version : Why my code doesn't work?



JohnnyKPL
04-25-2015, 11:05 AM
I'm a beginner of OpenGL programming, I've finished this project some day ago, this would be something like a "simple (but really simple) game engine" but for some reasons it doesn't work!

I've got 4 cpp files and a header file for each of them:

shader.h


#pragma once

#include "global.h"

#ifndef SHADER_H
#define SHADER_H

class shader
{
private:
enum
{
VERTEX_SHADER,
FRAGMENT_SHADER,
NUM_SHADERS
};

GLuint program, shaders[NUM_SHADERS];

protected:
public:
shader();
virtual ~shader();

void activate();
void deactivate();
};

#endif


shader.cpp


#include "shader.h"

static GLuint create(const GLchar* source, GLuint type);
static bool checkError(GLuint shader);

static const GLchar* vertexSource
{
"#version 430 core\n"
"in vec3 position;"
"in vec3 color;"
"out vec3 Color;"
"void main()"
"{"
"Color = color;"
"gl_Position = vec4(position, 1.0);"
"}"
};

static const GLchar* fragmentSource
{
"#version 430 core\n"
"in vec3 Color;"
"void main()"
"{"
"gl_FragColor = vec4(Color, 1.0);"
"}"
};

shader::shader()
{
program = glCreateProgram();
shaders[VERTEX_SHADER] = create(vertexSource, GL_VERTEX_SHADER);
shaders[FRAGMENT_SHADER] = create(fragmentSource, GL_FRAGMENT_SHADER);

for (unsigned int i = 0; i < NUM_SHADERS; i++)
std::cout << "shader: " << checkError(shaders[i]) << "\n";

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

glLinkProgram(program);
glValidateProgram(program);
std::cout << "program: " << program << "\n";
}

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

glDeleteProgram(program);
}

static GLuint create(const GLchar* source, GLuint type)
{
GLuint shader = glCreateShader(type);

glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);

return shader;
}

static bool checkError(GLuint shader)
{
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);

if (status == GL_TRUE)
return true;

return false;
}

void shader::activate()
{
glUseProgram(program);

glBindAttribLocation(program, 0, "position");
glBindAttribLocation(program, 1, "color");
}
void shader::deactivate()
{
glUseProgram(0);
}


The two shader that I've created compiles correctly (return 1), also the program compiles fine.
So I think the problem is the meshes.

mesh.h


#pragma once

#include "global.h"

#ifndef MESH_H
#define MESH_H

class mesh
{
private:
unsigned int drawsCount;

enum
{
POSITION,
COLOR,
VB_NUMBER
};

GLuint vao, vbos[VB_NUMBER];

protected:
public:
mesh(GLfloat*, unsigned int);
virtual ~mesh();

void draw();
};

#endif


mesh.cpp


#include "mesh.h"

mesh::mesh(GLfloat* vertices, unsigned int number)
{
drawsCount = number;

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

glGenBuffers(1, vbos);

/* Bind vertex buffer object -> POSITION */

glBindBuffer(GL_ARRAY_BUFFER, vbos[POSITION]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(POSITION);
glVertexAttribPointer(POSITION, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);

glEnableVertexAttribArray(COLOR);
glVertexAttribPointer(COLOR, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));

glBindVertexArray(0);
}

mesh::~mesh()
{
glDeleteBuffers(1, &vao);
glDeleteVertexArrays(1, vbos);
}

void mesh::draw()
{
glBindVertexArray(vao);

glDrawArrays(GL_TRIANGLES, 0, drawsCount);

glBindVertexArray(0);
}


Here's the main class of my program (excuding the GLFW window life cycle that call the method: render() and process() and check if they return true).

game.h


#pragma once

#include "global.h"
#include "shader.h"
#include "mesh.h"

#ifndef GAME_H
#define GAME_H

class game
{
private:
unsigned char keys[256];

shader* _shader;
mesh* _mesh;

protected:
public:
game();
virtual ~game();

bool render();
bool process();

void readKeyboard(unsigned char key, bool press);
};

#endif


and finally... game.cpp


#include "game.h"

game::game()
{
for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++)
keys[i] = 0;

GLfloat vertices[] =
{
0.0f, 1.0f, 0.0f, /**/ 1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, /**/ 1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f, /**/ 1.0f, 0.0f, 0.0f
};

_shader = new shader();
_mesh = new mesh(vertices, sizeof(vertices) / sizeof(GLfloat));

_shader->activate();
}

game::~game()
{

}

bool game::render()
{
glClear(GL_COLOR_BUFFER_BIT);

_mesh->draw();

return true;
}

bool game::process()
{
if (keys[36])
return false;

return true;
}

void game::readKeyboard(unsigned char key, bool press)
{
keys[key] = press;
}


Sorry for the "thread confusion" that i've maked but since that there's no "spoiler function" i had to put all visible! :(

P.S: I'm italian :P

JohnnyKPL
04-25-2015, 02:08 PM
Someone help me please :( :sorrow:

Dark Photon
04-25-2015, 06:28 PM
...but for some reasons it doesn't work!
What does it do?
What is it "supposed" to do?
What have you done to narrow down the problem besides just compiling it?

Please read this for tips on getting help on these forums:
* Forum Posting Guidelines (https://www.opengl.org/discussion_boards/showthread.php/176139-Forum-Posting-Guidelines?p=1262303)

JohnnyKPL
04-26-2015, 08:41 AM
I've just resolve, the mistake is in:

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

Because I've tried to get the size of a pointer