PDA

View Full Version : blank window for particle simulation



saikumar
11-20-2015, 07:15 AM
hey guys i am getting a blank window could you please help me out please.
this is a basic particle simulation program for perspective
the particles render when i dont use perspective
but when i include perspective it shows just the background color;



#include <iostream>
#include <GL\glew.h>
#include "display.h"
#include <glm\glm.hpp>
#include "shader.h"
#include <stdlib.h>

#define no_of_points 12
using namespace glm;
using namespace std;

GLuint bufferID;

vec3 random()
{
float min = -1.0f, max = 1.0f;
float r[3];
for (int i = 0; i < 3; i++)
{
r[i] = (float)rand() / (float)RAND_MAX;
}
vec3 temp;
temp.x = r[0];
temp.y = r[1];
temp.z = r[2];
return temp;
}

class vertices
{
private:
vec3 pos;
public:
void setpos(vec3 pos)
{
this->pos = pos;
}
vec3 getpos()
{
return pos;
}
};

class velocity
{
private:
vec3 vel;
public:
void setvelocity(vec3 vel)
{
this->vel = vel;
}
vec3 getvelocity()
{
return vel;
}
};
int main(int argc, char** argv)
{

vertices point[no_of_points];
for (int i = 0; i < no_of_points;i++)
{
vec3 temp1 = random();
vec3 temp;
temp.x = temp1.x;
temp.y = temp1.y;
temp.z = temp1.z;
point[i].setpos(temp);
}

velocity vel[no_of_points];
for (int i = 0; i < no_of_points; i++)
{
vel[i].setvelocity(vec3(0.009f, 0.05f, 0.0f));
}
display window(800, 600, "point");
glEnable(GL_BLEND);
shader sload("./basics");

glEnable(GL_POINT_SMOOTH);
glGenBuffers(1, &bufferID);
glBindBuffer(GL_ARRAY_BUFFER, bufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(point), &point, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

vec3 ptemporary;
vec3 vtemporary;

while (!window.windowclose())
{
window.clear(0.35f, 0.35f, 0.35f, 1.0f);
sload.bind();
sload.shaderupdate();
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(point), &point);
glPointSize(30.0f);
glDrawArrays(GL_POINTS, 0, no_of_points);
window.update();
for (int i = 0; i < no_of_points; i++)
{
ptemporary = point[i].getpos();
vtemporary = vel[i].getvelocity();
if ((ptemporary.x > 1) | (ptemporary.x < -1))
{
vtemporary.x = -vtemporary.x;
}
ptemporary.x = ptemporary.x + vtemporary.x;

if ((ptemporary.y > 1) | (ptemporary.y < -1))
{
vtemporary.y = -vtemporary.y;
}
ptemporary.y = ptemporary.y + vtemporary.y;

point[i].setpos(ptemporary);
vel[i].setvelocity(vtemporary);
}
}

window.~display();
return 0;
}




#include "shader.h"
#include <iostream>
#include <fstream>
#include <glm\gtc\matrix_transform.hpp>

using namespace std;
using namespace glm;

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

void shader::shaderupdate()
{
mat4 ProjectionMatrix = perspective(50.0f, float(800) / float(600), 0.1f, 10.0f);

mat4 FullTransformMatrix = ProjectionMatrix;
GLint ftm_location = glGetUniformLocation(m_program, "ftm");
glUniformMatrix4fv(ftm_location, 1, GL_FALSE, &FullTransformMatrix[0][0]);
}


and shders are good i changed them for perspective mode