mirror of https://github.com/bjornbytes/lovr.git
lovr.graphics.cube test;
This commit is contained in:
parent
d4f20c8379
commit
5f884710e6
|
@ -1,6 +1,5 @@
|
|||
#include "buffer.h"
|
||||
#include "graphics.h"
|
||||
#include "../util.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void lovrBufferDestroy(Buffer* buffer) {
|
||||
|
@ -82,9 +81,7 @@ void lovrBufferSetVertexMap(Buffer* buffer, unsigned int* map, int count) {
|
|||
}
|
||||
|
||||
vec_clear(&buffer->map);
|
||||
vec_reserve(&buffer->map, count);
|
||||
vec_pusharr(&buffer->map, map, count);
|
||||
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), buffer->map.data, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "../glfw.h"
|
||||
#include "../vendor/vec/vec.h"
|
||||
#include "../util.h"
|
||||
|
||||
#ifndef LOVR_BUFFER_TYPES
|
||||
#define LOVR_BUFFER_TYPES
|
||||
|
@ -16,8 +16,6 @@ typedef enum {
|
|||
BUFFER_STREAM = GL_STREAM_DRAW
|
||||
} BufferUsage;
|
||||
|
||||
typedef vec_t(unsigned int) vec_uint_t;
|
||||
|
||||
typedef struct {
|
||||
int size;
|
||||
GLfloat* data;
|
||||
|
|
|
@ -22,8 +22,10 @@ void lovrGraphicsInit() {
|
|||
state.defaultShader = lovrGraphicsNewShader(lovrDefaultVertexShader, lovrDefaultFragmentShader);
|
||||
state.lastColor = LOVR_COLOR(255, 255, 255, 255);
|
||||
glGenBuffers(1, &state.shapeBuffer);
|
||||
glGenBuffers(1, &state.shapeIndexBuffer);
|
||||
glGenVertexArrays(1, &state.shapeArray);
|
||||
vec_init(&state.shapeData);
|
||||
vec_init(&state.shapeIndices);
|
||||
lovrGraphicsReset();
|
||||
}
|
||||
|
||||
|
@ -234,27 +236,70 @@ void lovrGraphicsGetDimensions(int* width, int* height) {
|
|||
glfwGetFramebufferSize(window, width, height);
|
||||
}
|
||||
|
||||
void lovrGraphicsSetShapeData(float* data, int count) {
|
||||
void lovrGraphicsSetShapeData(float* vertexData, int vertexLength, unsigned int* indexData, int indexLength) {
|
||||
vec_clear(&state.shapeData);
|
||||
vec_pusharr(&state.shapeData, data, count);
|
||||
vec_pusharr(&state.shapeData, vertexData, vertexLength);
|
||||
glBindVertexArray(state.shapeArray);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, state.shapeBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, count * sizeof(float), data, GL_STREAM_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertexLength * sizeof(float), vertexData, GL_STREAM_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
||||
|
||||
vec_clear(&state.shapeIndices);
|
||||
if (indexData) {
|
||||
vec_pusharr(&state.shapeIndices, indexData, indexLength);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state.shapeIndexBuffer);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, state.shapeIndices.length * sizeof(unsigned int), state.shapeIndices.data, GL_STREAM_DRAW);
|
||||
}
|
||||
}
|
||||
|
||||
void lovrGraphicsDrawShape(DrawMode mode) {
|
||||
int usingIbo = state.shapeIndices.length > 0;
|
||||
lovrGraphicsPrepare();
|
||||
glBindVertexArray(state.shapeArray);
|
||||
glEnableVertexAttribArray(0);
|
||||
glDrawArrays(mode, 0, state.shapeData.length / 3);
|
||||
|
||||
if (usingIbo) {
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state.shapeIndexBuffer);
|
||||
glDrawElements(mode, state.shapeIndices.length, GL_UNSIGNED_INT, NULL);
|
||||
} else {
|
||||
glDrawArrays(mode, 0, state.shapeData.length / 3);
|
||||
}
|
||||
}
|
||||
|
||||
void lovrGraphicsLine(float* points, int count) {
|
||||
lovrGraphicsSetShapeData(points, count);
|
||||
lovrGraphicsSetShapeData(points, count, NULL, 0);
|
||||
lovrGraphicsDrawShape(DRAW_MODE_LINE);
|
||||
}
|
||||
|
||||
void lovrGraphicsCube(float x, float y, float z, float size) {
|
||||
float points[] = {
|
||||
// Bottom
|
||||
-.5, .5, -.5,
|
||||
.5, .5, -.5,
|
||||
.5, -.5, -.5,
|
||||
-.5, -.5, -.5,
|
||||
|
||||
// Top
|
||||
-.5, .5, .5,
|
||||
.5, .5, .5,
|
||||
.5, -.5, .5,
|
||||
-.5, -.5, .5
|
||||
};
|
||||
|
||||
unsigned int indices[] = {
|
||||
0, 1, 1, 2, 2, 3, 3, 0, // Bottom
|
||||
4, 5, 5, 6, 6, 7, 7, 4, // Top
|
||||
0, 4, 1, 5, 2, 6, 3, 7 // Connections
|
||||
};
|
||||
|
||||
lovrGraphicsPush();
|
||||
lovrGraphicsScale(size, size, size);
|
||||
lovrGraphicsTranslate(x, y, z);
|
||||
lovrGraphicsSetShapeData(points, 24, indices, 24);
|
||||
lovrGraphicsDrawShape(DRAW_MODE_LINES);
|
||||
lovrGraphicsPop();
|
||||
}
|
||||
|
||||
Buffer* lovrGraphicsNewBuffer(int size, BufferDrawMode drawMode, BufferUsage usage) {
|
||||
Buffer* buffer = malloc(sizeof(Buffer));
|
||||
|
||||
|
|
|
@ -22,7 +22,8 @@ typedef struct {
|
|||
|
||||
typedef enum {
|
||||
DRAW_MODE_FILL = GL_TRIANGLE_FAN,
|
||||
DRAW_MODE_LINE = GL_LINE_STRIP
|
||||
DRAW_MODE_LINE = GL_LINE_STRIP,
|
||||
DRAW_MODE_LINES = GL_LINES
|
||||
} DrawMode;
|
||||
|
||||
typedef struct {
|
||||
|
@ -39,7 +40,9 @@ typedef struct {
|
|||
ScissorRectangle scissor;
|
||||
GLuint shapeArray;
|
||||
GLuint shapeBuffer;
|
||||
GLuint shapeIndexBuffer;
|
||||
vec_float_t shapeData;
|
||||
vec_uint_t shapeIndices;
|
||||
} GraphicsState;
|
||||
#endif
|
||||
|
||||
|
@ -70,9 +73,10 @@ void lovrGraphicsRotate(float w, float x, float y, float z);
|
|||
void lovrGraphicsScale(float x, float y, float z);
|
||||
void lovrGraphicsTransform(mat4 transform);
|
||||
void lovrGraphicsGetDimensions(int* width, int* height);
|
||||
void lovrGraphicsSetShapeData(float* data, int count);
|
||||
void lovrGraphicsSetShapeData(float* data, int count, unsigned int* indices, int indexCount);
|
||||
void lovrGraphicsDrawShape();
|
||||
void lovrGraphicsLine(float* points, int count);
|
||||
void lovrGraphicsCube(float x, float y, float z, float size);
|
||||
Buffer* lovrGraphicsNewBuffer(int size, BufferDrawMode drawMode, BufferUsage usage);
|
||||
Model* lovrGraphicsNewModel(const char* path);
|
||||
Shader* lovrGraphicsNewShader(const char* vertexSource, const char* fragmentSource);
|
||||
|
|
|
@ -26,6 +26,7 @@ const luaL_Reg lovrGraphics[] = {
|
|||
{ "rotate", l_lovrGraphicsRotate },
|
||||
{ "scale", l_lovrGraphicsScale },
|
||||
{ "line", l_lovrGraphicsLine },
|
||||
{ "cube", l_lovrGraphicsCube },
|
||||
{ "getWidth", l_lovrGraphicsGetWidth },
|
||||
{ "getHeight", l_lovrGraphicsGetHeight },
|
||||
{ "getDimensions", l_lovrGraphicsGetDimensions },
|
||||
|
@ -274,12 +275,21 @@ int l_lovrGraphicsLine(lua_State* L) {
|
|||
}
|
||||
}
|
||||
|
||||
lovrGraphicsSetShapeData(points.data, count);
|
||||
lovrGraphicsSetShapeData(points.data, count, NULL, 0);
|
||||
lovrGraphicsDrawShape(DRAW_MODE_LINE);
|
||||
vec_deinit(&points);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_lovrGraphicsCube(lua_State* L) {
|
||||
float x = luaL_optnumber(L, 1, 0.f);
|
||||
float y = luaL_optnumber(L, 2, 0.f);
|
||||
float z = luaL_optnumber(L, 3, 0.f);
|
||||
float s = luaL_optnumber(L, 4, 1.f);
|
||||
lovrGraphicsCube(x, y, z, s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_lovrGraphicsGetWidth(lua_State* L) {
|
||||
int width;
|
||||
lovrGraphicsGetDimensions(&width, NULL);
|
||||
|
|
|
@ -29,6 +29,7 @@ int l_lovrGraphicsTranslate(lua_State* L);
|
|||
int l_lovrGraphicsRotate(lua_State* L);
|
||||
int l_lovrGraphicsScale(lua_State* L);
|
||||
int l_lovrGraphicsLine(lua_State* L);
|
||||
int l_lovrGraphicsCube(lua_State* L);
|
||||
int l_lovrGraphicsGetWidth(lua_State* L);
|
||||
int l_lovrGraphicsGetHeight(lua_State* L);
|
||||
int l_lovrGraphicsGetDimensions(lua_State* L);
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
#include "vendor/vec/vec.h"
|
||||
|
||||
#ifndef UTIL_TYPES
|
||||
#define UTIL_TYPES
|
||||
typedef vec_t(unsigned int) vec_uint_t;
|
||||
#endif
|
||||
|
||||
void error(const char* format, ...);
|
||||
int fileExists(char* filename);
|
||||
|
|
Loading…
Reference in New Issue