Очень много времени трачу на этот код, пытаюсь найти ошибку, но не получается. Здесь есть функция, которая считывает модель из файла. Она вроде всё правильно считывает. Также у модели есть два вида отображения, с текстурой и без. Посмотрите пожалуйста, правильно ли я вывожу объект на сцену, правильно ли заданы параметры перспективы и т.д.
#include "model.h"
#include <stdio.h>
#include <string.h>
#include "vars.h"
#include "conf.h"
#include "gl.h"
extern struct conf cf;
Model::Model ( ) {
this->frames = nullptr;
this->texture = nullptr;
float aspect = ( float ) cf.screen_width / ( float ) cf.screen_height;
this->proj = glm::perspective ( glm::radians ( 45.f ), aspect, 0.1f, 100.f );
this->view = glm::translate ( glm::mat4 ( 1.0f ), glm::vec3 ( 0.f, 0.f, 0.f ) );
this->model = glm::mat4 ( 1.0f );
this->rotate = glm::mat4 ( 1.0f );
this->current_frame = 0;
this->program_wit = cf.programs [ SHADER_MODEL_WITH_TEXTURE ];
this->program_wiot = cf.programs [ SHADER_MODEL_WITHOUT_TEXTURE ];
this->wit.sampler_location = gl::GetUniformLocation ( this->program_wit, "s_texture" );
this->wit.transform_location = gl::GetUniformLocation ( this->program_wit, "transform" );
this->wit.perspective_location = gl::GetUniformLocation ( this->program_wit, "perspective" );
this->wit.rotate_location = gl::GetUniformLocation ( this->program_wit, "rotate" );
this->wiot.transform_location = gl::GetUniformLocation ( this->program_wiot, "transform" );
this->wiot.perspective_location = gl::GetUniformLocation ( this->program_wiot, "perspective" );
this->wiot.rotate_location = gl::GetUniformLocation ( this->program_wiot, "rotate" );
}
void Model::transform ( const glm::vec3 &translate ) {
this->view = glm::translate ( this->model, translate );
}
void Model::set_color ( glm::vec4 color ) {
this->color = color;
}
void draw_with_texture ( Model *m ) {
gl::UseProgram ( m->program_wit );
gl::ActiveTexture ( gl::TEXTURE0 );
gl::BindTexture ( gl::TEXTURE_2D, m->textureid );
gl::Uniform1i ( m->wit.sampler_location, 0 );
gl::UniformMatrix4fv ( m->wit.transform_location, 1, gl::FALSE_, &m->view[0][0] );
gl::UniformMatrix4fv ( m->wit.perspective_location, 1, gl::FALSE_, &m->proj[0][0] );
gl::UniformMatrix4fv ( m->wit.rotate_location, 1, gl::FALSE_, &m->rotate[0][0] );
gl::EnableVertexAttribArray ( 0 );
gl::EnableVertexAttribArray ( 1 );
gl::EnableVertexAttribArray ( 2 );
gl::VertexAttribPointer ( 0, 3, gl::FLOAT, gl::FALSE_, 0, m->frames[m->current_frame].v );
gl::VertexAttribPointer ( 1, 3, gl::FLOAT, gl::FALSE_, 0, m->frames[m->current_frame].vn );
gl::VertexAttribPointer ( 2, 2, gl::FLOAT, gl::FALSE_, 0, m->frames[m->current_frame].vt );
gl::DrawArrays ( gl::TRIANGLES, 0, m->frames[m->current_frame].length_v );
gl::DisableVertexAttribArray ( 0 );
gl::DisableVertexAttribArray ( 1 );
gl::DisableVertexAttribArray ( 2 );
}
void draw_without_texture ( Model *m ) {
gl::UseProgram ( m->program_wiot );
gl::UniformMatrix4fv ( m->wiot.transform_location, 1, gl::FALSE_, &m->view[0][0] );
gl::UniformMatrix4fv ( m->wiot.perspective_location, 1, gl::FALSE_, &m->proj[0][0] );
gl::UniformMatrix4fv ( m->wiot.rotate_location, 1, gl::FALSE_, &m->rotate[0][0] );
gl::EnableVertexAttribArray ( 0 );
gl::EnableVertexAttribArray ( 1 );
gl::EnableVertexAttribArray ( 2 );
gl::VertexAttribPointer ( 0, 3, gl::FLOAT, gl::FALSE_, 0, m->frames[m->current_frame].v );
gl::VertexAttribPointer ( 1, 3, gl::FLOAT, gl::FALSE_, 0, m->frames[m->current_frame].vn );
gl::VertexAttribPointer ( 2, 4, gl::FLOAT, gl::FALSE_, 0, &m->color[0] );
gl::DrawArrays ( gl::TRIANGLES, 0, m->frames[m->current_frame].length_v );
gl::DisableVertexAttribArray ( 0 );
gl::DisableVertexAttribArray ( 1 );
gl::DisableVertexAttribArray ( 2 );
}
void Model::switch_to ( ) {
if ( this->texture ) this->map = draw_with_texture;
else this->map = draw_without_texture;
}
void Model::draw ( ) {
this->map ( this );
}
void Model::read_file ( const char *file ) {
int count;
char path[255] = { 0 };
snprintf ( path, 254, "%s/models/%s", DEFAULT_ASSETS, file );
FILE *fp = fopen ( path, "r" );
fread ( &count, sizeof ( int ), 1, fp );
this->frames = new struct frame [ count ];
for ( int i = 0; i < count; i++ ) {
fread ( &this->frames[i].length_v, sizeof ( unsigned int ), 1, fp );
fread ( &this->frames[i].length_vt, sizeof ( unsigned int ), 1, fp );
fread ( &this->frames[i].length_vn, sizeof ( unsigned int ), 1, fp );
this->frames[i].v = new float [ this->frames[i].length_v ];
this->frames[i].vt = new float [ this->frames[i].length_vt ];
this->frames[i].vn = new float [ this->frames[i].length_vn ];
fread ( &this->frames[i].v[0], sizeof ( float ), this->frames[i].length_v, fp );
fread ( &this->frames[i].vn[0], sizeof ( float ), this->frames[i].length_vn, fp );
fread ( &this->frames[i].vt[0], sizeof ( float ), this->frames[i].length_vt, fp );
}
fclose ( fp );
}
model.h
#include "../glm/glm.hpp"
#include "../glm/ext.hpp"
#include "sprite.h"
#include "object.h"
struct frame {
unsigned int length_v;
unsigned int length_vt;
unsigned int length_vn;
float *v;
float *vn;
float *vt;
};
struct with_t {
int sampler_location;
int transform_location;
int perspective_location;
int rotate_location;
};
struct without_t {
int transform_location;
int perspective_location;
int rotate_location;
};
class Model : public Object {
private:
glm::mat4 proj;
glm::mat4 view;
glm::mat4 model;
glm::mat4 rotate;
glm::mat4 result;
glm::vec4 color;
struct frame *frames;
unsigned int current_frame;
unsigned int program_wit;
unsigned int program_wiot;
struct with_t wit;
struct without_t wiot;
unsigned int textureid;
friend void draw_with_texture ( Model * );
friend void draw_without_texture ( Model * );
void (*map) ( Model * );
public:
Model ( );
void read_file ( const char *file ) override;
void draw ( ) override;
void set_color ( glm::vec4 color );
void switch_to ( void );
void transform ( const glm::vec3 &translate );
Sprite *texture;
};
А вот вершинный шейдер.
#version 400
layout ( location = 0 ) in vec3 position;
layout ( location = 1 ) in vec3 normaly;
layout ( location = 2 ) in vec4 color;
uniform mat4 transform;
uniform mat4 perspective;
uniform mat4 rotate;
out vec4 v_color;
void main ( )
{
gl_Position = perspective * transform * vec4 ( position, 1.0 );
v_color = color;
}
Вот фрагментный
#version 400
precision mediump float;
in vec4 v_color;
layout ( location = 0 ) out vec4 out_color;
void main ( )
{
out_color = v_color;
}
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Задача: отсортировать массив пар "ключ-значение" по ключу сортировкой подсчётаПроблема: валится на некоторых тестах, таких, как 45894 5439345 32 979 194639...
Открываю VS2019 v163
Как запустить Start Experimental Instance of Visual Studio 2019 с помощью DTE? Достучаться до VS 2019 получается так: