added drawing of a cube using the custom game object class
This commit is contained in:
parent
5c42364e43
commit
ae99c30c6a
|
@ -11,7 +11,9 @@
|
||||||
#include "textures/Texture2D.h"
|
#include "textures/Texture2D.h"
|
||||||
#include "util/stb_image.h"
|
#include "util/stb_image.h"
|
||||||
#include "util/camera/camera.h"
|
#include "util/camera/camera.h"
|
||||||
|
#include "object/game_object.h"
|
||||||
|
|
||||||
|
// TODO: Replace current rendering with he GameObject
|
||||||
// Continue: https://learnopengl.com/Lighting/Colors
|
// Continue: https://learnopengl.com/Lighting/Colors
|
||||||
// Chapter: A lighting scene
|
// Chapter: A lighting scene
|
||||||
//
|
//
|
||||||
|
@ -167,6 +169,7 @@ int main()
|
||||||
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
|
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
|
||||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
|
||||||
};
|
};
|
||||||
|
std::vector<Nebulix::VertexAttribute> vertexAttribs = { Nebulix::VertexAttribute(), Nebulix::VertexAttribute(GL_FLOAT, GL_FALSE, 2) };
|
||||||
|
|
||||||
GLuint faces[]{
|
GLuint faces[]{
|
||||||
0, 1, 3,
|
0, 1, 3,
|
||||||
|
@ -203,9 +206,9 @@ int main()
|
||||||
// position attribute
|
// position attribute
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
// uv-coordinates (or more general the third vertex attribute which can be anything not just uv-coordinates)
|
// uv-coordinates (or more general the second vertex attribute which can be anything not just uv-coordinates)
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(2);
|
glEnableVertexAttribArray(1);
|
||||||
// I could unbind the VAO, but this is usually not needed because when creating a new VAO we need to bind that to change it anyway, so there shouldn't be a problem
|
// I could unbind the VAO, but this is usually not needed because when creating a new VAO we need to bind that to change it anyway, so there shouldn't be a problem
|
||||||
|
|
||||||
std::string vertexPath = "shaders/default/default.vert";
|
std::string vertexPath = "shaders/default/default.vert";
|
||||||
|
@ -227,7 +230,8 @@ int main()
|
||||||
Texture2D containerImage("images/container.jpg");
|
Texture2D containerImage("images/container.jpg");
|
||||||
Texture2D faceImage("images/awesomeface.png", 0, GL_RGBA);
|
Texture2D faceImage("images/awesomeface.png", 0, GL_RGBA);
|
||||||
|
|
||||||
|
std::vector<float> verts(std::begin(vertices), std::end(vertices));
|
||||||
|
Nebulix::GameObject cube = Nebulix::GameObject(verts, vertexAttribs);
|
||||||
// main loop
|
// main loop
|
||||||
while(!glfwWindowShouldClose(window))
|
while(!glfwWindowShouldClose(window))
|
||||||
{
|
{
|
||||||
|
@ -254,7 +258,7 @@ int main()
|
||||||
|
|
||||||
glBindVertexArray(vertexArrayObject);
|
glBindVertexArray(vertexArrayObject);
|
||||||
|
|
||||||
for (int i = 0; i < 10; i++) // somehow cubePositions->length() does not return the correct number?
|
for (int i = 1; i < 10; i++)
|
||||||
{
|
{
|
||||||
mat4 model = mat4(1.0f);
|
mat4 model = mat4(1.0f);
|
||||||
model = glm::translate(model, cubePositions[i]);
|
model = glm::translate(model, cubePositions[i]);
|
||||||
|
@ -267,6 +271,8 @@ int main()
|
||||||
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // for EBOs
|
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // for EBOs
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 36); // for VAOs
|
glDrawArrays(GL_TRIANGLES, 0, 36); // for VAOs
|
||||||
}
|
}
|
||||||
|
shader->SetMatrix("modelMatrix", cube.GetModelMatrix());
|
||||||
|
cube.Draw();
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
|
@ -3,8 +3,6 @@
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
#include "../shaders/Shader.h"
|
|
||||||
|
|
||||||
namespace Nebulix
|
namespace Nebulix
|
||||||
{
|
{
|
||||||
struct VertexAttribute
|
struct VertexAttribute
|
||||||
|
@ -43,13 +41,13 @@ namespace Nebulix
|
||||||
public:
|
public:
|
||||||
glm::vec3 Position;
|
glm::vec3 Position;
|
||||||
|
|
||||||
GameObject(std::vector<float>& vertices, std::vector<VertexAttribute> vertexAttributes, Shader shader)
|
GameObject(std::vector<float>& vertices, std::vector<VertexAttribute> vertexAttributes)
|
||||||
: vertices{ vertices }, vertexAttributes{ vertexAttributes }, shader{shader}, Position{glm::vec3(0.0f)}
|
: vertices{ vertices }, vertexAttributes{ vertexAttributes }, Position{glm::vec3(0.0f)}
|
||||||
{
|
{
|
||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
GameObject(std::vector<float> &vertices, std::vector<VertexAttribute> vertexAttributes, Shader shader, glm::vec3& position)
|
GameObject(std::vector<float> &vertices, std::vector<VertexAttribute> vertexAttributes, glm::vec3& position)
|
||||||
: vertices{vertices}, vertexAttributes{ vertexAttributes }, shader{ shader }, Position{position}
|
: vertices{vertices}, vertexAttributes{ vertexAttributes }, Position{position}
|
||||||
{
|
{
|
||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
|
@ -60,11 +58,10 @@ namespace Nebulix
|
||||||
{
|
{
|
||||||
glBindVertexArray(vertexArray);
|
glBindVertexArray(vertexArray);
|
||||||
|
|
||||||
// TODO: Set shader matrices. Wait for materials
|
// TODO: Use shader; Set shader matrices. Wait for materials
|
||||||
shader.Use();
|
|
||||||
|
|
||||||
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // for EBOs
|
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // for EBOs
|
||||||
glDrawArrays(GL_TRIANGLES, 0, vertices.size()); // for VAOs
|
glDrawArrays(GL_TRIANGLES, 0, nrOfVertices); // for VAOs
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,11 +74,12 @@ namespace Nebulix
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<float> vertices;
|
std::vector<float> vertices;
|
||||||
Shader shader;
|
|
||||||
|
|
||||||
GLuint vertexBuffer, vertexArray;
|
GLuint vertexBuffer, vertexArray;
|
||||||
std::vector<VertexAttribute> vertexAttributes;
|
std::vector<VertexAttribute> vertexAttributes;
|
||||||
|
|
||||||
|
int nrOfVertices;
|
||||||
|
|
||||||
void Init()
|
void Init()
|
||||||
{
|
{
|
||||||
glGenVertexArrays(1, &vertexArray);
|
glGenVertexArrays(1, &vertexArray);
|
||||||
|
@ -89,7 +87,7 @@ namespace Nebulix
|
||||||
glBindVertexArray(vertexArray);
|
glBindVertexArray(vertexArray);
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
|
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices.data(), GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices[0]), vertices.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
int stride = 0;
|
int stride = 0;
|
||||||
for (size_t i = 0; i < vertexAttributes.size(); i++)
|
for (size_t i = 0; i < vertexAttributes.size(); i++)
|
||||||
|
@ -98,12 +96,22 @@ namespace Nebulix
|
||||||
}
|
}
|
||||||
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
|
int nrNonVertices = 0;
|
||||||
for (size_t i = 0; i < vertexAttributes.size(); i++)
|
for (size_t i = 0; i < vertexAttributes.size(); i++)
|
||||||
{
|
{
|
||||||
VertexAttribute attr = vertexAttributes[i];
|
VertexAttribute attr = vertexAttributes[i];
|
||||||
glVertexAttribPointer(i, attr.NrOfElements, attr.AttributeType, attr.Normalised, stride, (void*)offset);
|
glVertexAttribPointer(i, attr.NrOfElements, attr.AttributeType, attr.Normalised, stride, (void*)offset);
|
||||||
glEnableVertexAttribArray(i);
|
glEnableVertexAttribArray(i);
|
||||||
offset += attr.Stride;
|
offset += attr.Stride;
|
||||||
|
|
||||||
|
if (i != 0)
|
||||||
|
nrNonVertices += attr.NrOfElements;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < vertices.size(); i += 3)
|
||||||
|
{
|
||||||
|
nrOfVertices++;
|
||||||
|
i += nrNonVertices;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
//layout (location = 1) in vec3 aColour;
|
layout (location = 1) in vec2 aTexCoord;
|
||||||
layout (location = 2) in vec2 aTexCoord;
|
|
||||||
|
|
||||||
out vec3 ourColour;
|
out vec3 ourColour;
|
||||||
out vec2 texCoord;
|
out vec2 texCoord;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user