Trying to draw a colored coordinate system ...

simple_math_single_header
Martin Felis 2018-02-27 23:15:07 +01:00
parent d5fe4d366f
commit 207094b0c8
7 changed files with 135 additions and 20 deletions

View File

@ -1,9 +1,18 @@
#version 330 core #version 150 core
layout(location = 0) out vec3 outColor;
uniform vec4 uColor; uniform vec4 uColor;
in vec3 fragmentColor;
out vec3 outColor;
void main() { void main() {
outColor = uColor.rgb; outColor = vec3(
uColor.r * fragmentColor.r,
uColor.g * fragmentColor.g,
uColor.b * fragmentColor.b
);
outColor = max(uColor.rgb, fragmentColor);
//outColor = fragmentColor + uColor.rgb - uColor.rgb;
} }

View File

@ -1,10 +1,14 @@
#version 150 core #version 150 core
#extension GL_ARB_explicit_uniform_location : require #extension GL_ARB_explicit_attrib_location : require
in vec3 inVertex; layout(location = 0) in vec3 inCoord;
layout(location = 1) in vec3 inColor;
uniform mat4 uModelViewProj; uniform mat4 uModelViewProj;
out vec3 fragmentColor;
void main() { void main() {
gl_Position = uModelViewProj * vec4(inVertex, 1); gl_Position = uModelViewProj * vec4(inCoord, 1);
fragmentColor = inColor;
} }

View File

@ -161,13 +161,6 @@ int main(void)
glfwPollEvents(); glfwPollEvents();
ImGui_ImplGlfwGL3_NewFrame(); ImGui_ImplGlfwGL3_NewFrame();
// imguiBeginFrame (gGuiInputState->mouseX,
// gGuiInputState->mouseY,
// gGuiInputState->mouseButton,
// gGuiInputState->mouseScroll,
// width,
// height);
if (module_manager.CheckModulesChanged()) { if (module_manager.CheckModulesChanged()) {
gLog("Detected module update at frame %d. Unloading all modules.", frame_counter); gLog("Detected module update at frame %d. Unloading all modules.", frame_counter);
module_manager.UnloadModules(); module_manager.UnloadModules();

View File

@ -41,6 +41,15 @@ static const GLfloat g_textured_quad_vertex_buffer_data[] = {
1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
}; };
static const GLfloat g_coordinate_system_vertex_buffer_data[] = {
0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
// //
// Module // Module
// //
@ -186,6 +195,13 @@ void Renderer::Initialize(int width, int height) {
glBindBuffer(GL_ARRAY_BUFFER, mPlane.mVertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, mPlane.mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
// Coordinate System
glGenVertexArrays(1, &mCoordinateSystem.mVertexArrayId);
glBindVertexArray(mCoordinateSystem.mVertexArrayId);
glGenBuffers(1, &mCoordinateSystem.mVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, mCoordinateSystem.mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_coordinate_system_vertex_buffer_data), g_coordinate_system_vertex_buffer_data, GL_STATIC_DRAW);
// Simple Shader // Simple Shader
mDefaultProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl"); mDefaultProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl");
bool load_result = mDefaultProgram.Load(); bool load_result = mDefaultProgram.Load();
@ -230,6 +246,8 @@ void Renderer::Shutdown() {
glDeleteBuffers(1, &mMesh.mVertexBuffer); glDeleteBuffers(1, &mMesh.mVertexBuffer);
glDeleteVertexArrays(1, &mPlane.mVertexArrayId); glDeleteVertexArrays(1, &mPlane.mVertexArrayId);
glDeleteBuffers(1, &mPlane.mVertexBuffer); glDeleteBuffers(1, &mPlane.mVertexBuffer);
glDeleteVertexArrays(1, &mCoordinateSystem.mVertexArrayId);
glDeleteBuffers(1, &mCoordinateSystem.mVertexBuffer);
} }
@ -275,7 +293,31 @@ void Renderer::RenderGl() {
(void*)0 // offset (void*)0 // offset
); );
glDrawArrays(GL_TRIANGLES, 0, 3); // starting from vertex 0; 3 vertices total // glDrawArrays(GL_TRIANGLES, 0, 3); // starting from vertex 0; 3 vertices total
glDisableVertexAttribArray(0);
// Coordinate system
glBindVertexArray(mCoordinateSystem.mVertexArrayId);
glEnableVertexAttribArray(0);
glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 1.0f, 1.0f).data());
glBindBuffer(GL_ARRAY_BUFFER, mCoordinateSystem.mVertexBuffer);
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
(sizeof(float[6])),
(void*)0
);
glVertexAttribPointer(
1,
3,
GL_FLOAT,
GL_FALSE,
(sizeof(float[6])),
(void*) (sizeof(float[3]))
);
glDrawArrays(GL_LINES, 0, 6);
if (mSettings->DrawDepth) { if (mSettings->DrawDepth) {
mRenderTarget.RenderToLinearizedDepth(true); mRenderTarget.RenderToLinearizedDepth(true);

View File

@ -102,11 +102,6 @@ struct Light {
} }
}; };
struct Mesh {
GLuint mVertexArrayId = -1;
GLuint mVertexBuffer = -1;
};
struct RendererSettings; struct RendererSettings;
struct Renderer { struct Renderer {
@ -119,6 +114,7 @@ struct Renderer {
Camera mCamera; Camera mCamera;
Mesh mMesh; Mesh mMesh;
Mesh mPlane; Mesh mPlane;
Mesh mCoordinateSystem;
Texture mDefaultTexture; Texture mDefaultTexture;

View File

@ -323,3 +323,34 @@ bool Texture::Load(const char* filename, int num_components) {
// unsigned char* rgb = stbi_load(filename, &mWidth, &mHeight, num_components); // unsigned char* rgb = stbi_load(filename, &mWidth, &mHeight, num_components);
assert(false); assert(false);
} }
void VertexArray::Initialize(const int& size, GLenum usage) {
mSize = size;
mUsed = 0;
glGenVertexArrays (1, &mVertexArrayId);
glBindVertexArray(mVertexArrayId);
glGenBuffers(1, &mVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData) * size, NULL, usage);
}
GLuint VertexArray::AllocateMesh(const int& size) {
GLuint mesh_data_size = size * sizeof(VertexData);
if (mUsed + mesh_data_size > mSize) {
gLog("Cannot allocate mesh in VertexArray: not enough vertices available");
assert(false);
return -1;
}
GLuint offset = mUsed;
mUsed += mesh_data_size;
return offset;
}
void VertexArrayMesh::Initialize(VertexArray &array, const int& size) {
mOffset = array.AllocateMesh(size);
assert(mOffset != -1);
}

View File

@ -186,4 +186,44 @@ struct Texture {
bool Load(const char* path, int num_components = 3); bool Load(const char* path, int num_components = 3);
}; };
struct Mesh {
GLuint mVertexArrayId = -1;
GLuint mVertexBuffer = -1;
};
/**
* Multiple VertexArrayMeshes can be stored in a single VertexArray.
* Storage order is:
* (VVVV)(NNNN)(UV)(CCCC)
*/
struct VertexArray {
GLuint mVertexBuffer = -1;
GLuint mVertexArrayId = -1;
GLuint mSize = -1;
GLuint mUsed = -1;
struct VertexData {
float mCoords[4];
float mNormals[4];
float mTexCoords[2];
GLubyte mColor[4];
};
void Initialize(const int& size, GLenum usage);
GLuint AllocateMesh(const int& size);
};
struct VertexArrayMesh {
GLuint mOffset;
void Initialize(VertexArray &array, const int& size);
void SetData(
const std::vector<Vector4f> &coords,
const std::vector<Vector4f> &normals,
const std::vector<Vector2f> &uvs,
const std::vector<Vector4f> &colors
);
};
#endif #endif