Colour mapping on meshes working

This commit is contained in:
Ben
2018-09-17 13:43:26 +01:00
parent 2ca62a2201
commit 52203417e1
7 changed files with 79 additions and 36 deletions

View File

@@ -14,10 +14,17 @@ int main(int argc, char** argv) {
GLfloat vertices[] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f,-0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // 1.0f, 0.0f, // bottom right
-0.5f,-0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // 0.0f, 0.0f, // bottom left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // 1.0f, 1.0f, // top right
-0.5f,-0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f // 0.0f, 1.0f // top left
// 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
//-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
// 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
};
unsigned int indices[] = {

View File

@@ -4,28 +4,18 @@ Mesh::Mesh(GLfloat *vertices, unsigned int *indices, unsigned int numVerticies)
m_drawCount = numVerticies;
glGenVertexArrays(1, &m_VAO);
glGenBuffers(1, &m_VBO);
glGenBuffers(1, &m_EBO);
glBindVertexArray(m_VAO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glGenBuffers(NUM_BUFFERS, m_VBO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, numVerticies * sizeof(vertices[0]), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
std::cout << "Mesh loaded successfully" << std::endl;
}

View File

@@ -13,13 +13,11 @@ public:
private:
enum {
POSITION_VB,
NUM_BUFFERS,
TEXCOORD_VB
NUM_BUFFERS
};
unsigned int m_VAO;
unsigned int m_VBO;
unsigned int m_EBO;
unsigned int m_VBO[NUM_BUFFERS];
unsigned int m_drawCount;
};

View File

@@ -13,6 +13,9 @@ Shader::Shader(std::string path) {
std::cout << "Shader successfully attatched" << std::endl;
}
glBindAttribLocation(m_program, 0, "aPos");
glBindAttribLocation(m_program, 1, "aColor");
glLinkProgram(m_program);
int success;

View File

@@ -0,0 +1,50 @@
#include <iostream>
#include <GL/glew.h>
#include "display.h"
#include "mesh.h"
#include "shader.h"
#include "texture.h"
#undef main
int main(int argc, char** argv) {
Display display(600, 600, "Crumpet Engine");
glClearColor(0.1f, 0.45f, 0.9f, 1.0f);
GLfloat vertices[] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // 1.0f, 0.0f, // bottom right
-0.5f,-0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // 0.0f, 0.0f, // bottom left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // 1.0f, 1.0f, // top right
-0.5f,-0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f // 0.0f, 1.0f // top left
// 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
//-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
// 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
Mesh mesh(vertices, indices, sizeof(vertices) / sizeof(vertices[0]));
Shader shader("C:/Users/Ben/Desktop/crumpet-engine/resources/shaders/simple2d");
Texture chanceCube("C:/Users/Ben/Desktop/crumpet-engine/resources/textures/chance-cube.jpg");
while(!display.isClosed()) {
glClear(GL_COLOR_BUFFER_BIT);
shader.Bind();
chanceCube.Bind(0);
mesh.Draw();
display.Update();
}
return 0;
}

View File

@@ -1,11 +1,8 @@
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D ourTexture;
void main() {
FragColor = texture(ourTexture, TexCoord);
void main () {
FragColor = vec4(ourColor, 1.0f);
}

View File

@@ -1,13 +1,11 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
void main() {
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = aTexCoord;
}
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
}