PDA

View Full Version : hey guys i am animating a triangle so that only colors rotate at the edge and inside



saikumar
11-13-2015, 10:38 PM
hey guys i am animating a triangle so that colors rotates inside side the triangle by interpolation. but i am getting a blank window, please help me out, thanks in advance.

main code


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

GLfloat vertices[] = { -0.5f, -0.5f, 0.0f,

0.5f, -0.5f, 0.0f,

+0.0f, +0.5f, 0.0f,
};

GLfloat color[] = { +1.0f, +0.0f, +0.0f,
+0.0f, +1.0f, +0.0f,
+0.0f, +0.0f, +1.0f,
};


Shader shaders("./basics");
mesh mes(vertices, (sizeof(vertices))/3,color);

GLfloat deltacolor=0.05f;

while (!disp.windowclose())
{

disp.clear(0.0f, 0.15f, 0.3f, 1.0f);
mes.updatemesh(vertices, color);
shaders.Bind();
mes.draw();
disp.update();

////vertex 1 coloar change
if (color[0] <= 1 && color[0] > 0)
{
color[1] += deltacolor;
color[0] -= deltacolor;
}
else if (color[1] <= 1 && color[1] > 0)
{
color[2] += deltacolor;
color[1] -= deltacolor;
}
else if (color[2] <= 1 && color[2] > 0)
{
color[0] += deltacolor;
color[2] -= deltacolor;
}

////vertex 2 coloar change
if (color[3] <= 1 && color[3] > 0)
{
color[4] += deltacolor;
color[3] -= deltacolor;
}
else if (color[4] <= 1 && color[4] > 0)
{
color[5] += deltacolor;
color[4] -= deltacolor;
}
else if (color[5] <= 1 && color[5] > 0)
{
color[3] += deltacolor;
color[5] -= deltacolor;
}

//vertex 3 coloar change
if (color[6] <= 1 && color[6] > 0)
{
color[7] += deltacolor;
color[6] -= deltacolor;
}
else if (color[7] <= 1 && color[7] > 0)
{
color[8] += deltacolor;
color[7] -= deltacolor;
}
else if (color[8] <= 1 && color[8] > 0)
{
color[6] += deltacolor;
color[8] -= deltacolor;
}
SDL_Delay(1);
}

return 0;
}



mesh code


mesh::mesh(const GLfloat* vertices, unsigned int num_vertices,GLfloat* color)
{
m_drawCount = num_vertices;

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

glGenBuffers(num_buffers, vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[position]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices)+sizeof(color), NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(sizeof(vertices)));

glBindVertexArray(0);
}

mesh::~mesh()
{
glDeleteBuffers(num_buffers, vertexArrayBuffers);
glDeleteVertexArrays(1, &vertexArrayObject);
}

void mesh::draw()
{
glBindVertexArray(vertexArrayObject);
glDrawArrays(GL_TRIANGLES, 0, m_drawCount);
glBindVertexArray(0);
}

void mesh::updatemesh(const GLfloat* vertices, const GLfloat* color)
{
glBindVertexArray(vertexArrayObject);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(color), color);
}



shader code


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

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]);

glBindAttribLocation(m_program, 0, "position");
glBindAttribLocation(m_program, 1, "vertexColor");

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


std::string Shader::LoadShader(const std::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 std::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);

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

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

if (shader == 0)
std::cerr << "Error compiling shader type " << type << std::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;
}


thanks in advance