+ added light shit

This commit is contained in:
end 2016-09-29 04:24:14 +02:00
parent a86d4e5d92
commit d7970ed797
9 changed files with 88 additions and 56 deletions

View file

@ -9,15 +9,14 @@ namespace endofthejedi {
VAO::~VAO() { glDeleteVertexArrays(1, &m_name); }
void VAO::bind() { glBindVertexArray(m_name); }
void VAO::unbind() { glBindVertexArray(0); }
void VAO::fill(GLuint index, GLint size, GLenum type,
GLboolean normalized, GLsizei stride,
const GLvoid *pointer)
{
(void) type;
glEnableVertexAttribArray(index);
glVertexAttribPointer(index, size, GL_FLOAT, normalized, stride, pointer);
glVertexAttribPointer(index, size, type, normalized, stride, pointer);
}
Shader::Shader() : m_program(0) { }

View file

@ -36,6 +36,7 @@ class VAO {
VAO();
~VAO();
void bind();
void unbind();
void fill(GLuint index, GLint size, GLenum type, GLboolean normalized,
GLsizei stride = 0, const GLvoid *pointer = NULL);
};

View file

@ -1,11 +1,27 @@
R"raw_string(
#version 120
varying vec3 vertex;
varying vec3 normal;
uniform vec3 color;
varying vec3 lightvec;
void main()
{
//gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
//gl_FragColor = vec4(0.5+0.5*vertex.xyz, 1.0);
gl_FragColor = vec4(color, 1.0);
vec3 Eye = normalize(-vertex);
vec3 Reflected = normalize(reflect( -lightvec, normal));
//todo materials (& light color)
vec4 IAmbient = vec4(0.0f, 0.0f, 0.0f, 0.0f);
vec4 IDiffuse = vec4(1.0f, 1.0f, 1.0f, 0.0f) * max(dot(normal, lightvec), 0.0);
//todo shininess
vec4 ISpecular = vec4(1.0f, 0.0f, 0.0f, 0.0f) * pow(max(dot(Reflected, Eye), 0.0), 3);
gl_FragColor = vec4((IAmbient + IDiffuse) + ISpecular);
}
)raw_string"

View file

@ -1,12 +1,26 @@
R"raw_string(
#version 120
attribute vec3 in_vertex;
attribute vec3 in_normal;
varying vec3 vertex;
varying vec3 normal;
varying vec3 lightvec;
uniform mat4 model;
uniform vec3 lightpos;
void main()
{
//vec3 p = position + scale*gl_Vertex.xyz;
vec3 p = (model*gl_Vertex).xyz;
gl_Position = vec4(p, 1.0);
lightvec = normalize(lightpos - p);
vertex = p.xyz;
normal = in_normal.xyz;
gl_Position = vec4(p, 1.0);
}
)raw_string"

View file

@ -0,0 +1,15 @@
R"raw_string(
#version 120
varying vec2 vertex;
uniform float maxAge;
uniform float time;
void main()
{
//gl_FragColor = vec4(0.5+0.5*vertex.x, 0.5+0.5*vertex.y, 0.0, 1.0);
if (length(vertex) > 1.0) {
discard;
}
float decay = time / maxAge;
gl_FragColor = vec4(1.0/max(1.0, decay), 1.0/max(1.0, 6.0*decay), 0.0, 1.0);
}
)raw_string"

View file

@ -0,0 +1,17 @@
R"raw_string(
#version 120
attribute vec2 in_vertex;
attribute vec2 in_position;
attribute vec2 in_velocity;
varying vec2 vertex;
uniform float time;
uniform float size;
void main()
{
vec2 p = size*in_vertex;
p += time * in_velocity;
p += in_position;
gl_Position = vec4(p, 0.0, 1.0);
vertex = in_vertex;
}
)raw_string"

View file

@ -30,37 +30,11 @@ namespace endofthejedi {
m_data_max_age.resize(m_numParticles);
std::string vss_particles =
"#version 120\n"
"attribute vec2 in_vertex;\n"
"attribute vec2 in_position;\n"
"attribute vec2 in_velocity;\n"
"varying vec2 vertex;\n"
"uniform float time;\n"
"uniform float size;\n"
"void main()\n"
"{\n"
" vec2 p = size*in_vertex;\n"
" p += time * in_velocity;\n"
" p += in_position;\n"
" gl_Position = vec4(p, 0.0, 1.0);\n"
" vertex = in_vertex;\n"
"}\n"
#include "particle.vert"
;
std::string fss_particles =
"#version 120\n"
"varying vec2 vertex;\n"
"uniform float maxAge;\n"
"uniform float time;\n"
"void main()\n"
"{\n"
//" gl_FragColor = vec4(0.5+0.5*vertex.x, 0.5+0.5*vertex.y, 0.0, 1.0);\n"
" if (length(vertex) > 1.0) {\n"
" discard;\n"
"}\n"
" float decay = time / maxAge;\n"
" gl_FragColor = vec4(1.0/max(1.0, decay), 1.0/max(1.0, 6.0*decay), 0.0, 1.0);\n"
"}\n"
#include "particle.frag"
;
m_shader.init();

View file

@ -95,14 +95,12 @@ namespace endofthejedi {
m_data_position.data(), // Buffer data pointer
GL_STATIC_DRAW); // Usage - Data never changes;
// TODO
#if 0
// 2 holds normal data
glEnableVertexAttribArray(2);
glGenBuffers(1, &(model->vbo_normal)); // Generate buffer
glBindBuffer(GL_ARRAY_BUFFER, model->vbo_normal); // Bind buffer
//normal data
glEnableVertexAttribArray(1);
glGenBuffers(1, &m_vbo_id_normal); // Generate buffer
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_normal); // Bind buffer
glVertexAttribPointer(2,
glVertexAttribPointer(1,
3, // three floats per normal
GL_FLOAT, // Data is floating point type
GL_FALSE, // No fixed point scaling
@ -112,10 +110,10 @@ namespace endofthejedi {
// Fill bound buffer
glBufferData(
GL_ARRAY_BUFFER, // Buffer target
3*model->numVertices*sizeof(float), // Buffer data size
model->normals, // Buffer data pointer
3*m_numVertices*sizeof(float), // Buffer data size
m_data_normal.data(), // Buffer data pointer
GL_STATIC_DRAW); // Usage - Data never changes;
#endif
m_loaded_to_opengl = true;
return true;
@ -123,7 +121,6 @@ namespace endofthejedi {
bool bind()
{
// TODO: check whether this vbo is bound!
if (!m_loaded_to_opengl) {
std::cout<<"[polygonmodel] warning: try to bind model vbo "
<< "which was not uploaded to OpenGL!" << std::endl;
@ -137,21 +134,17 @@ namespace endofthejedi {
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
// TODO
#if 0
// bind normal vbo
GLuint normalLoc = glGetAttribLocation(program, "attr_normal");
glEnableVertexAttribArray(normalLoc);
glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo_normal);
glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
#endif
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_normal);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
m_binding_active = true;
return true;
}
bool render()
bool render(Shader& shader)
{
if (!m_binding_active || !m_loaded_to_opengl) {
std::cout<<"[polygonmodel] warning: try to render model without bind()" << std::endl;
@ -159,6 +152,9 @@ namespace endofthejedi {
return false;
}
glBindAttribLocation(shader.program(), 0, "in_vertex");
glBindAttribLocation(shader.program(), 1, "in_normal");
glDrawArrays(GL_TRIANGLES, 0, m_numVertices);
return true;
}

View file

@ -110,7 +110,7 @@ namespace endofthejedi {
glm::vec3 c = planet->getColor();
glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
m_planetModel->render();
m_planetModel->render(m_shader);
}
}
@ -126,7 +126,7 @@ namespace endofthejedi {
glm::mat4 model = computeModelMatrix(missile);
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
m_missileModel->render();
m_missileModel->render(m_shader);
}
}
}
@ -142,7 +142,7 @@ namespace endofthejedi {
glm::vec3 c = glm::vec3(0.1, 1.0, 0.2);
glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
m_shipModel->render();
m_shipModel->render(m_shader);
}
}