+ 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); } VAO::~VAO() { glDeleteVertexArrays(1, &m_name); }
void VAO::bind() { glBindVertexArray(m_name); } void VAO::bind() { glBindVertexArray(m_name); }
void VAO::unbind() { glBindVertexArray(0); }
void VAO::fill(GLuint index, GLint size, GLenum type, void VAO::fill(GLuint index, GLint size, GLenum type,
GLboolean normalized, GLsizei stride, GLboolean normalized, GLsizei stride,
const GLvoid *pointer) const GLvoid *pointer)
{ {
(void) type;
glEnableVertexAttribArray(index); glEnableVertexAttribArray(index);
glVertexAttribPointer(index, size, GL_FLOAT, normalized, stride, pointer); glVertexAttribPointer(index, size, type, normalized, stride, pointer);
} }
Shader::Shader() : m_program(0) { } Shader::Shader() : m_program(0) { }

View file

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

View file

@ -1,11 +1,27 @@
R"raw_string( R"raw_string(
#version 120 #version 120
varying vec3 vertex; varying vec3 vertex;
varying vec3 normal;
uniform vec3 color; uniform vec3 color;
varying vec3 lightvec;
void main() void main()
{ {
//gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); //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(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" )raw_string"

View file

@ -1,12 +1,26 @@
R"raw_string( R"raw_string(
#version 120 #version 120
attribute vec3 in_vertex;
attribute vec3 in_normal;
varying vec3 vertex; varying vec3 vertex;
varying vec3 normal;
varying vec3 lightvec;
uniform mat4 model; uniform mat4 model;
uniform vec3 lightpos;
void main() void main()
{ {
//vec3 p = position + scale*gl_Vertex.xyz; //vec3 p = position + scale*gl_Vertex.xyz;
vec3 p = (model*gl_Vertex).xyz; vec3 p = (model*gl_Vertex).xyz;
gl_Position = vec4(p, 1.0); lightvec = normalize(lightpos - p);
vertex = p.xyz; vertex = p.xyz;
normal = in_normal.xyz;
gl_Position = vec4(p, 1.0);
} }
)raw_string" )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); m_data_max_age.resize(m_numParticles);
std::string vss_particles = std::string vss_particles =
"#version 120\n" #include "particle.vert"
"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"
; ;
std::string fss_particles = std::string fss_particles =
"#version 120\n" #include "particle.frag"
"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"
; ;
m_shader.init(); m_shader.init();

View file

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

View file

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