Fix rendering.

This commit is contained in:
Relintai 2023-12-19 18:54:54 +01:00
parent 41a20717b2
commit 663c551d14
5 changed files with 66 additions and 45 deletions

View File

@ -47,6 +47,7 @@ ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/wind
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/shader.cpp -o sfw/application/shader.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/material.cpp -o sfw/application/material.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/mesh.cpp -o sfw/application/mesh.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/camera.cpp -o sfw/application/camera.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c game_scene.cpp -o game_scene.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c main.cpp -o main.o
@ -59,6 +60,7 @@ ccache g++ -Wall -lm -ldl -lpthread -lX11 -D_REENTRANT -g sfw/aabb.o sfw/basis.
sfw/pool_vector.o sfw/pool_allocator.o sfw/mutex.o sfw/stime.o \
sfw/application/application.o sfw/application/scene.o sfw/application/window.o \
sfw/application/shader.o sfw/application/material.o sfw/application/mesh.o \
sfw/application/camera.o \
game_scene.o main.o \
-o game

View File

@ -5,6 +5,8 @@
#include "3rd_glad.h"
#include "memory.h"
//#include "camera.h"
//#include "sprite.h"
@ -101,7 +103,7 @@ void GameScene::render() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//camera->bind();
camera->bind();
//tile_map->render();
@ -118,13 +120,6 @@ GameScene::GameScene() {
up = false;
down = false;
camera = new OrthographicCamera();
camera->width = 16;
camera->height = 16;
camera->position.x = 8;
camera->position.y = 8;
//camera->position.z = -2;
int w;
int h;
SDL_GetWindowSize(Application::get_singleton()->window, &w, &h);
@ -170,8 +165,16 @@ GameScene::GameScene() {
tile_map->build_mesh();
*/
camera = new OrthographicCamera();
camera->width = 2;
camera->height = 2;
camera->position.x = 0;
camera->position.y = 0;
//camera->position.z = -2;
mesh = memnew(Mesh(2));
material = memnew(ColoredMaterial());
material->color = glm::vec4(1, 1, 0, 1);
mesh->clear();
@ -218,6 +221,7 @@ GameScene::~GameScene() {
delete sprite;
*/
memdelete(camera);
memdelete(mesh);
memdelete(material);
}

View File

@ -4,7 +4,7 @@
#include "scene.h"
/*
#include "camera.h"
#include "mesh_instance.h"
#include "sprite.h"
#include "texture.h"
@ -14,6 +14,7 @@
#include "colored_material.h"
#include "mesh.h"
#include "camera.h"
class GameScene : public Scene {
public:
@ -29,13 +30,13 @@ public:
bool up;
bool down;
Camera *camera;
Texture *texture;
TextureMaterial *material;
TileMap *tile_map;
Sprite *sprite;
*/
Camera *camera;
Mesh *mesh;
ColoredMaterial *material;
};

View File

@ -6,7 +6,7 @@
#include "../../libs/glm/vec4.hpp"
#include "../../libs/glm/gtc/type_ptr.hpp"
//#include "camera.h"
#include "camera.h"
class ColoredMaterial : public Material {
public:
@ -15,48 +15,43 @@ public:
}
void bind_uniforms() {
//glUniformMatrix4fv(projection_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->projection_matrix));
//glUniformMatrix4fv(model_view_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->model_view_matrix));
glUniformMatrix4fv(projection_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->projection_matrix));
glUniformMatrix4fv(model_view_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->model_view_matrix));
//glUniform4f(tri_color_uniform_location, color.r, color.g, color.b, color.a);
glUniform4f(tri_color_uniform_location, color.r, color.g, color.b, color.a);
}
void setup_uniforms() {
// projection_matrix_location = get_uniform("u_proj_matrix");
// model_view_matrix_location = get_uniform("u_model_view_matrix");
projection_matrix_location = get_uniform("u_proj_matrix");
model_view_matrix_location = get_uniform("u_model_view_matrix");
//tri_color_uniform_location = get_uniform("fragment_color");
tri_color_uniform_location = get_uniform("fragment_color");
}
// "uniform mat4 u_proj_matrix;\n"
// "uniform mat4 u_model_view_matrix;\n"
// "\n"
//"attribute vec4 a_position;\n"
// "\n"
// "void main() {\n"
// " gl_Position = u_proj_matrix * u_model_view_matrix * a_position;\n"
// "}"
const GLchar **get_vertex_shader_source() {
static const GLchar *vertex_shader_source[] = {
"uniform mat4 u_proj_matrix;\n"
"uniform mat4 u_model_view_matrix;\n"
"\n"
"attribute vec4 a_position;\n"
"\n"
"void main() {\n"
" gl_Position = a_position;\n"
" gl_Position = u_proj_matrix * u_model_view_matrix * a_position;\n"
"}"
};
return vertex_shader_source;
}
//"precision mediump float;\n"
//"\n"
//"uniform vec4 fragment_color;\n"
//"\n"
const GLchar **get_fragment_shader_source() {
static const GLchar *fragment_shader_source[] = {
"precision mediump float;\n"
"\n"
"uniform vec4 fragment_color;\n"
"\n"
"void main() {\n"
" gl_FragColor = vec4(1, 0, 0, 1);\n"
" gl_FragColor = fragment_color;\n"
"}"
};

View File

@ -168,21 +168,38 @@ void AppWindow::window_hints(unsigned flags) {
#ifdef __APPLE__
/* We need to explicitly ask for a 3.2 context on OS X */
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // osx
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // osx, 2:#version150,3:330
//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // osx
//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // osx, 2:#version150,3:330
#else
// Compute shaders need 4.5 otherwise. But...
// According to the GLFW docs, the context version hint acts as a minimum version.
// i.e, the context you actually get may be a higher or highest version (which is usually the case)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
#endif
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //osx
#endif
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //osx+ems
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //osx+ems
glfwWindowHint(GLFW_STENCIL_BITS, 8); //osx
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
//according to the documentation, it must be GLFW_OPENGL_ANY_PROFILE.
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
/*
#if defined(_WIN64) || defined(_WIN32)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
#endif
*/
//glfwWindowHint( GLFW_RED_BITS, 8 );
//glfwWindowHint( GLFW_GREEN_BITS, 8 );
@ -369,13 +386,15 @@ bool AppWindow::create_from_handle(void *handle, float scale, unsigned int flags
//glEnable(GL_TEXTURE_2D);
// 0:disable vsync, 1:enable vsync, <0:adaptive (allow vsync when framerate is higher than syncrate and disable vsync when framerate drops below syncrate)
flags |= _vsync ? WINDOW_VSYNC : WINDOW_VSYNC_DISABLED;
flags |= _vsync_adaptive ? WINDOW_VSYNC_ADAPTIVE : 0;
//flags |= _vsync ? WINDOW_VSYNC : WINDOW_VSYNC_DISABLED;
//flags |= _vsync_adaptive ? WINDOW_VSYNC_ADAPTIVE : 0;
int has_adaptive_vsync = glfwExtensionSupported("WGL_EXT_swap_control_tear") || glfwExtensionSupported("GLX_EXT_swap_control_tear") || glfwExtensionSupported("EXT_swap_control_tear");
int wants_adaptive_vsync = (flags & WINDOW_VSYNC_ADAPTIVE);
int interval = has_adaptive_vsync && wants_adaptive_vsync ? -1 : (flags & WINDOW_VSYNC_DISABLED ? 0 : 1);
glfwSwapInterval(interval);
flags |= WINDOW_VSYNC_DISABLED;
//int has_adaptive_vsync = glfwExtensionSupported("WGL_EXT_swap_control_tear") || glfwExtensionSupported("GLX_EXT_swap_control_tear") || //glfwExtensionSupported("EXT_swap_control_tear");
//int wants_adaptive_vsync = (flags & WINDOW_VSYNC_ADAPTIVE);
//int interval = has_adaptive_vsync && wants_adaptive_vsync ? -1 : (flags & WINDOW_VSYNC_DISABLED ? 0 : 1);
//glfwSwapInterval(interval);
const GLFWvidmode *mode = glfwGetVideoMode(monitor ? monitor : glfwGetPrimaryMonitor());
@ -385,7 +404,7 @@ bool AppWindow::create_from_handle(void *handle, float scale, unsigned int flags
//PRINTF("GPU driver: %s\n", glGetString(GL_VERSION));
#ifndef __EMSCRIPTEN__
//PRINTF("GPU OpenGL: %d.%d\n", GLAD_VERSION_MAJOR(gl_version), GLAD_VERSION_MINOR(gl_version));
LOG_MSG("GPU OpenGL: " + String::num(GLAD_VERSION_MAJOR(gl_version)) + " " + String::num(GLAD_VERSION_MINOR(gl_version)));
if (FLAGS_TRANSPARENT) { // @transparent
glfwSetWindowAttrib(_window, GLFW_DECORATED, GLFW_FALSE); // @todo: is decorated an attrib or a hint?