Set up transformations in vertex shader

This commit is contained in:
Chris Sarbora 2024-07-28 04:18:54 -05:00
parent d61deae68d
commit 16df2d764a
No known key found for this signature in database
8 changed files with 57 additions and 19 deletions

View File

@ -125,11 +125,11 @@ Once CMake finishes, the built files will be put in `builds/mac/Descent3/Debug`
* If you would like to manage the code dependencies yourself:
* APT users
```sh
sudo apt install -y --no-install-recommends libsdl2-dev zlib1g-dev libgtest-dev
sudo apt install -y --no-install-recommends libsdl2-dev zlib1g-dev libgtest-dev libglm-dev
```
* DNF users
```sh
sudo dnf install -y SDL2-devel zlib-devel gtest
sudo dnf install -y SDL2-devel zlib-devel gtest glm-devel
```
3. **Clone the Descent3 source code.**

View File

@ -131,6 +131,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
add_compile_options("-Wno-multichar;-Wall")
endif()
find_package(glm REQUIRED)
find_package(SDL2 REQUIRED)
# Some versions of the SDL2 find_package set SDL2_INCLUDE_DIR and some set a plural SDL2_INCLUDE_DIRS. Check both.
message("SDL2 Include Dir is ${SDL2_INCLUDE_DIR} ${SDL2_INCLUDE_DIRS}")

View File

@ -33,6 +33,7 @@ set(CPPS
add_library(renderer STATIC ${HEADERS} ${CPPS})
target_include_directories(renderer PRIVATE ${GENERATED_HEADERS})
target_link_libraries(renderer PRIVATE
glm::glm
SDL2::SDL2
bitmap
ddio

View File

@ -77,7 +77,29 @@ struct Renderer {
shader_.Use();
}
/**
* Sets the vertex transformation matrices. Takes all three of the MVP matrices at once, in order to avoid
* multiple SetUniform operations. Pass std::nullopt for any matrices that should not be altered.
*/
void setTransform(std::optional<glm::mat4x4> const &model, std::optional<glm::mat4x4> const &view,
std::optional<glm::mat4x4> const &projection) {
if (model) {
model_ = *model;
}
if (view) {
view_ = *view;
}
if (projection) {
projection_ = *projection;
}
shader_.setUniformMat4f("u_transform", projection_ * view_ * model_);
}
private:
glm::mat4x4 model_;
glm::mat4x4 view_;
glm::mat4x4 projection_;
ShaderProgram<PosColorUV2Vertex> shader_;
};
std::optional<Renderer> gRenderer;
@ -1933,18 +1955,11 @@ void rend_TransformSetToPassthru(void) {
int height = gpu_state.screen_height;
// TODO: Generalize
// Projection
dglMatrixMode(GL_PROJECTION);
dglLoadIdentity();
dglOrtho((GLfloat)0.0f, (GLfloat)(width), (GLfloat)(height), (GLfloat)0.0f, 0.0f, 1.0f);
gRenderer->setTransform(glm::mat4x4{1}, glm::mat4x4{1}, glm::ortho<float>(0, width, height, 0, 0, 1));
// Viewport
dglViewport(0, 0, width, height);
dglScissor(0, 0, width, height);
// ModelView
dglMatrixMode(GL_MODELVIEW);
dglLoadIdentity();
}
void rend_TransformSetViewport(int lx, int ty, int width, int height) {
@ -1952,11 +1967,9 @@ void rend_TransformSetViewport(int lx, int ty, int width, int height) {
}
void rend_TransformSetProjection(float trans[4][4]) {
dglMatrixMode(GL_PROJECTION);
dglLoadMatrixf(&trans[0][0]);
gRenderer->setTransform(std::nullopt, std::nullopt, glm::make_mat4x4(&trans[0][0]));
}
void rend_TransformSetModelView(float trans[4][4]) {
dglMatrixMode(GL_MODELVIEW);
dglLoadMatrixf(&trans[0][0]);
gRenderer->setTransform(glm::make_mat4x4(&trans[0][0]), glm::mat4x4{1}, std::nullopt);
}

View File

@ -22,6 +22,9 @@
#include <functional>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <glm/mat4x4.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "dyna_gl.h"
#include "holder.h"
@ -167,7 +170,25 @@ struct ShaderProgram {
dglUseProgram(0);
}
void setUniformMat4f(std::string const& name, glm::mat4x4 const& matrix) {
dglUniformMatrix4fv(getUniformId(name), 1, GL_FALSE, glm::value_ptr(matrix));
}
private:
GLint getUniformId(std::string const& name) {
auto it = uniform_cache_.find(name);
if (it != uniform_cache_.end()) {
return it->second;
}
it = uniform_cache_.emplace(name, dglGetUniformLocation(id_, name.c_str())).first;
if (it->second != -1) {
return it->second;
}
throw std::runtime_error("uniform " + name + " nonexistent or inactive");
}
static void DeleteProgram(GLuint id) {
dglDeleteProgram(id);
}
@ -176,4 +197,5 @@ private:
Shader<GL_VERTEX_SHADER> vertex_;
Shader<GL_FRAGMENT_SHADER> fragment_;
VertexBuffer<V> vbo_;
std::unordered_map<std::string, GLint> uniform_cache_;
};

View File

@ -191,12 +191,9 @@ DYNAEXTERN(glGetShaderInfoLog);
DYNAEXTERN(glGetShaderiv);
DYNAEXTERN(glGetString);
DYNAEXTERN(glGetStringi);
DYNAEXTERN(glGetUniformLocation);
DYNAEXTERN(glHint);
DYNAEXTERN(glLinkProgram);
DYNAEXTERN(glLoadIdentity);
DYNAEXTERN(glLoadMatrixf);
DYNAEXTERN(glMatrixMode);
DYNAEXTERN(glOrtho);
DYNAEXTERN(glPixelStorei);
DYNAEXTERN(glPolygonOffset);
DYNAEXTERN(glReadPixels);
@ -209,6 +206,7 @@ DYNAEXTERN(glTexEnvf);
DYNAEXTERN(glTexImage2D);
DYNAEXTERN(glTexParameteri);
DYNAEXTERN(glTexSubImage2D);
DYNAEXTERN(glUniformMatrix4fv);
DYNAEXTERN(glUseProgram);
DYNAEXTERN(glVertexAttribPointer);
DYNAEXTERN(glVertexPointer);

View File

@ -23,7 +23,9 @@ in vec4 in_color;
in vec2 in_uv0;
in vec2 in_uv1;
uniform mat4 u_transform;
void main()
{
gl_Position = vec4(0);
gl_Position = u_transform * vec4(in_pos, 1);
}

View File

@ -1,6 +1,7 @@
{
"builtin-baseline": "198d68dbcc6c907cb3d0b9b1d93c3df6ecf93c62",
"dependencies": [
"glm",
"gtest",
"zlib",
{