add input handling, move vertexstrip struct to graphics, fix broken rendering of the gears

This commit is contained in:
Furkan Mudanyali 2023-05-31 02:56:13 +03:00
parent 93a32c735a
commit d465e0bc34
7 changed files with 46 additions and 12 deletions

View File

@ -18,6 +18,7 @@ void Game::loop() {
while (!this->done && !this->scenes.empty()) {
this->pWindow->updateDimensions();
this->done = this->pInput->pollEvent();
this->pInput->pollKeys();
if (this->scenes.top()->destroy) {
delete this->scenes.top();

View File

@ -102,7 +102,8 @@ void Graphics::enable(int cap) {
void Graphics::drawArrays(GLuint& vertexBufObj,
int mode,
int count,
int stripCount,
VertexStrip* strips,
int attrBindingIdx,
int attrCount,
int attrOffset,
@ -121,7 +122,9 @@ void Graphics::drawArrays(GLuint& vertexBufObj,
glBindVertexBuffer(attrBindingIdx, vertexBufObj, 0, attrCount * attrSize);
/* Draw the triangle strips that comprise the gear */
glDrawArrays(mode, 0, count);
for (int n = 0; n < stripCount; ++n) {
glDrawArrays(mode, strips[n].first, strips[n].count);
}
/* Disable the attributes */
for (int i = attrCount - 1; i >= 0; --i) {

View File

@ -15,6 +15,14 @@
class Game;
class Window;
// Struct describing the vertices in triangle strip
using VertexStrip = struct VertexStrip {
// First vertex in the strip
GLint first;
// Number of consecutive verices in the strip after the first
GLint count;
};
class Graphics {
Game* pGame;
SDL_GLContext context = nullptr;
@ -38,7 +46,8 @@ class Graphics {
static void drawArrays(GLuint& vertexBufObj,
int mode,
int count,
int stripCount,
VertexStrip* strips,
int attrBindingIdx,
int attrCount,
int attrOffset,

View File

@ -3,6 +3,7 @@
* All rights reserved
*/
#include <SDL2/SDL_keyboard.h>
#include <game/game.h>
#include <input/input.h>
@ -22,3 +23,7 @@ bool Input::pollEvent() {
event.window.windowID ==
SDL_GetWindowID(this->game->pWindow->window);
}
void Input::pollKeys() {
this->keys = SDL_GetKeyboardState(nullptr);
}

View File

@ -18,7 +18,9 @@ class Input {
public:
Input(Game*);
~Input() = default;
const Uint8* keys;
bool pollEvent();
void pollKeys();
};
#endif

View File

@ -5,6 +5,8 @@
#include <GLES3/gl3.h>
#include <GL/glew.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_scancode.h>
#include <game/game.h>
#include <graphics/graphics.h>
#include <scene/gears/gears.h>
@ -266,8 +268,8 @@ void GearsScene::drawGear(Gear* gear,
Graphics::setUniformValue((GLint)materialColorLoc, color);
// Draw the triangle strips that comprise the gear
Graphics::drawArrays(gear->vertexBufObj, GL_TRIANGLE_STRIP, gear->nVertices,
0, 2, 0, sizeof(GLfloat) * 3);
Graphics::drawArrays(gear->vertexBufObj, GL_TRIANGLE_STRIP, gear->nStrips,
gear->strips, 0, 2, 0, sizeof(GLfloat) * 3);
}
void GearsScene::reshape() {
@ -317,6 +319,22 @@ void GearsScene::idle() {
}
}
void GearsScene::keypress() {
static const Uint8* keys = pGame->pInput->keys;
if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) {
viewRotation[1] += 5.0F;
}
if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) {
viewRotation[1] -= 5.0F;
}
if (keys[SDL_SCANCODE_UP] || keys[SDL_SCANCODE_W]) {
viewRotation[0] += 5.0F;
}
if (keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S]) {
viewRotation[0] -= 5.0F;
}
}
void GearsScene::draw() {
const static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0};
const static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0};
@ -344,5 +362,6 @@ void GearsScene::draw() {
blue);
reshape();
keypress();
idle();
}

View File

@ -11,13 +11,7 @@
#define VERTICES_PER_TOOTH 34
#define GEAR_VERTEX_STRIDE 6
// Class describing the vertices in triangle strip
using VertexStrip = struct {
// First vertex in the strip
GLint first;
// Number of consecutive verices in the strip after the first
GLint count;
};
struct VertexStrip;
// Each vertex consists of GEAR_VERTEX_STRIDE GLfloat attributes
using GearVertex = GLfloat[GEAR_VERTEX_STRIDE];
@ -75,6 +69,7 @@ class GearsScene : public Scene {
void idle();
void reshape();
void keypress();
// The direction of the directional light for the scene
const GLfloat lightSourcePos[4] = {5.0, 5.0, 10.0, 1.0};