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()) { while (!this->done && !this->scenes.empty()) {
this->pWindow->updateDimensions(); this->pWindow->updateDimensions();
this->done = this->pInput->pollEvent(); this->done = this->pInput->pollEvent();
this->pInput->pollKeys();
if (this->scenes.top()->destroy) { if (this->scenes.top()->destroy) {
delete this->scenes.top(); delete this->scenes.top();

View File

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

View File

@ -15,6 +15,14 @@
class Game; class Game;
class Window; 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 { class Graphics {
Game* pGame; Game* pGame;
SDL_GLContext context = nullptr; SDL_GLContext context = nullptr;
@ -38,7 +46,8 @@ class Graphics {
static void drawArrays(GLuint& vertexBufObj, static void drawArrays(GLuint& vertexBufObj,
int mode, int mode,
int count, int stripCount,
VertexStrip* strips,
int attrBindingIdx, int attrBindingIdx,
int attrCount, int attrCount,
int attrOffset, int attrOffset,

View File

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

View File

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

View File

@ -5,6 +5,8 @@
#include <GLES3/gl3.h> #include <GLES3/gl3.h>
#include <GL/glew.h> #include <GL/glew.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_scancode.h>
#include <game/game.h> #include <game/game.h>
#include <graphics/graphics.h> #include <graphics/graphics.h>
#include <scene/gears/gears.h> #include <scene/gears/gears.h>
@ -266,8 +268,8 @@ void GearsScene::drawGear(Gear* gear,
Graphics::setUniformValue((GLint)materialColorLoc, color); Graphics::setUniformValue((GLint)materialColorLoc, color);
// Draw the triangle strips that comprise the gear // Draw the triangle strips that comprise the gear
Graphics::drawArrays(gear->vertexBufObj, GL_TRIANGLE_STRIP, gear->nVertices, Graphics::drawArrays(gear->vertexBufObj, GL_TRIANGLE_STRIP, gear->nStrips,
0, 2, 0, sizeof(GLfloat) * 3); gear->strips, 0, 2, 0, sizeof(GLfloat) * 3);
} }
void GearsScene::reshape() { 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() { void GearsScene::draw() {
const static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0}; 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}; const static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0};
@ -344,5 +362,6 @@ void GearsScene::draw() {
blue); blue);
reshape(); reshape();
keypress();
idle(); idle();
} }

View File

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