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 "util/stb_image.h"
|
||||
#include "util/camera/camera.h"
|
||||
#include "object/game_object.h"
|
||||
|
||||
// TODO: Replace current rendering with he GameObject
|
||||
// Continue: https://learnopengl.com/Lighting/Colors
|
||||
// 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, 1.0f
|
||||
};
|
||||
std::vector<Nebulix::VertexAttribute> vertexAttribs = { Nebulix::VertexAttribute(), Nebulix::VertexAttribute(GL_FLOAT, GL_FALSE, 2) };
|
||||
|
||||
GLuint faces[]{
|
||||
0, 1, 3,
|
||||
|
@ -203,9 +206,9 @@ int main()
|
|||
// position attribute
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
// uv-coordinates (or more general the third vertex attribute which can be anything not just uv-coordinates)
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
// uv-coordinates (or more general the second vertex attribute which can be anything not just uv-coordinates)
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
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
|
||||
|
||||
std::string vertexPath = "shaders/default/default.vert";
|
||||
|
@ -227,7 +230,8 @@ int main()
|
|||
Texture2D containerImage("images/container.jpg");
|
||||
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
|
||||
while(!glfwWindowShouldClose(window))
|
||||
{
|
||||
|
@ -254,7 +258,7 @@ int main()
|
|||
|
||||
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);
|
||||
model = glm::translate(model, cubePositions[i]);
|
||||
|
@ -267,6 +271,8 @@ int main()
|
|||
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // for EBOs
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36); // for VAOs
|
||||
}
|
||||
shader->SetMatrix("modelMatrix", cube.GetModelMatrix());
|
||||
cube.Draw();
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
|
|
|
@ -3,8 +3,6 @@
|
|||
#include <exception>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "../shaders/Shader.h"
|
||||
|
||||
namespace Nebulix
|
||||
{
|
||||
struct VertexAttribute
|
||||
|
@ -43,13 +41,13 @@ namespace Nebulix
|
|||
public:
|
||||
glm::vec3 Position;
|
||||
|
||||
GameObject(std::vector<float>& vertices, std::vector<VertexAttribute> vertexAttributes, Shader shader)
|
||||
: vertices{ vertices }, vertexAttributes{ vertexAttributes }, shader{shader}, Position{glm::vec3(0.0f)}
|
||||
GameObject(std::vector<float>& vertices, std::vector<VertexAttribute> vertexAttributes)
|
||||
: vertices{ vertices }, vertexAttributes{ vertexAttributes }, Position{glm::vec3(0.0f)}
|
||||
{
|
||||
Init();
|
||||
}
|
||||
GameObject(std::vector<float> &vertices, std::vector<VertexAttribute> vertexAttributes, Shader shader, glm::vec3& position)
|
||||
: vertices{vertices}, vertexAttributes{ vertexAttributes }, shader{ shader }, Position{position}
|
||||
GameObject(std::vector<float> &vertices, std::vector<VertexAttribute> vertexAttributes, glm::vec3& position)
|
||||
: vertices{vertices}, vertexAttributes{ vertexAttributes }, Position{position}
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
@ -60,11 +58,10 @@ namespace Nebulix
|
|||
{
|
||||
glBindVertexArray(vertexArray);
|
||||
|
||||
// TODO: Set shader matrices. Wait for materials
|
||||
shader.Use();
|
||||
// TODO: Use shader; Set shader matrices. Wait for materials
|
||||
|
||||
//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:
|
||||
std::vector<float> vertices;
|
||||
Shader shader;
|
||||
|
||||
GLuint vertexBuffer, vertexArray;
|
||||
std::vector<VertexAttribute> vertexAttributes;
|
||||
|
||||
int nrOfVertices;
|
||||
|
||||
void Init()
|
||||
{
|
||||
glGenVertexArrays(1, &vertexArray);
|
||||
|
@ -89,7 +87,7 @@ namespace Nebulix
|
|||
glBindVertexArray(vertexArray);
|
||||
|
||||
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;
|
||||
for (size_t i = 0; i < vertexAttributes.size(); i++)
|
||||
|
@ -98,12 +96,22 @@ namespace Nebulix
|
|||
}
|
||||
|
||||
int offset = 0;
|
||||
int nrNonVertices = 0;
|
||||
for (size_t i = 0; i < vertexAttributes.size(); i++)
|
||||
{
|
||||
VertexAttribute attr = vertexAttributes[i];
|
||||
glVertexAttribPointer(i, attr.NrOfElements, attr.AttributeType, attr.Normalised, stride, (void*)offset);
|
||||
glEnableVertexAttribArray(i);
|
||||
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
|
||||
layout (location = 0) in vec3 aPos;
|
||||
//layout (location = 1) in vec3 aColour;
|
||||
layout (location = 2) in vec2 aTexCoord;
|
||||
layout (location = 1) in vec2 aTexCoord;
|
||||
|
||||
out vec3 ourColour;
|
||||
out vec2 texCoord;
|
||||
|
|
Loading…
Reference in New Issue
Block a user