gKit2 light
Functions | Variables
shader_kit.cpp File Reference

shader_kit light, bac a sable fragment shader, cf shader_toy More...

#include <cstdio>
#include <cstring>
#include <chrono>
#include "glcore.h"
#include "window.h"
#include "files.h"
#include "program.h"
#include "uniforms.h"
#include "texture.h"
#include "mesh.h"
#include "wavefront.h"
#include "vec.h"
#include "mat.h"
#include "orbiter.h"
#include "text.h"
#include "widgets.h"

Go to the source code of this file.

Functions

void reload_program ()
 
const char * option_find (std::vector< const char * > &options, const char *ext)
 
int init (std::vector< const char * > &options)
 
int quit ()
 
int draw (void)
 
int main (int argc, char **argv)
 

Variables

const char * program_filename
 
GLuint program
 
std::string program_log
 
int program_area
 
bool program_failed
 
const char * mesh_filename
 
Mesh mesh
 
Point mesh_pmin
 
Point mesh_pmax
 
int vertex_count
 
GLuint vao
 
bool wireframe = false
 
std::vector< const char * > texture_filenames
 
std::vector< GLuint > textures
 
Orbiter camera
 
Widgets widgets
 
size_t last_load = 0
 

Detailed Description

shader_kit light, bac a sable fragment shader, cf shader_toy

Definition in file shader_kit.cpp.

Function Documentation

◆ init()

int init ( std::vector< const char * > &  options)

compile les shaders et construit le programme + les buffers + le vertex array. renvoie -1 en cas d'erreur.

Definition at line 92 of file shader_kit.cpp.

93 {
94  widgets= create_widgets();
95  camera= Orbiter();
96 
97  program= 0;
98  const char *option;
99  option= option_find(options, ".glsl");
100  if(option != nullptr)
101  {
102  program_filename= option;
103  reload_program();
104  }
105 
106  vao= 0;
107  mesh_pmin= Point(normalize(Vector(-1, -1, 0)) * 2.5f);
108  mesh_pmax= Point(normalize(Vector( 1, 1, 0)) * 2.5f);
109 
110  option= option_find(options, ".obj");
111  if(option != nullptr)
112  {
113  mesh= read_mesh(option);
114  if(mesh.vertex_count() > 0)
115  {
116  mesh_filename= option;
117 
118  vao= mesh.create_buffers(mesh.has_texcoord(), mesh.has_normal(), mesh.has_color(), mesh.has_material_index());
119  vertex_count= mesh.vertex_count();
120 
121  mesh.bounds(mesh_pmin, mesh_pmax);
122  camera.lookat(mesh_pmin, mesh_pmax);
123  }
124 
125  // ou generer une erreur ?
126  }
127 
128  if(vao == 0)
129  {
130  glGenVertexArrays(1, &vao);
131  vertex_count= 3;
132  }
133 
134  // charge les textures, si necessaire
135  for(int i= 0; i < int(options.size()); i++)
136  {
137  if(options[i] == nullptr)
138  continue;
139 
140  GLuint texture= read_texture(0, options[i]);
141  if(texture > 0)
142  {
143  texture_filenames.push_back(options[i]);
144  textures.push_back(texture);
145  }
146  }
147 
148  // nettoyage
149  glUseProgram(0);
150  glBindTexture(GL_TEXTURE_2D, 0);
151  glBindVertexArray(0);
152  glBindBuffer(GL_ARRAY_BUFFER, 0);
153  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
154 
155  // etat openGL par defaut
156  glClearColor(0.2f, 0.2f, 0.2f, 1.f);
157  glClearDepth(1.f);
158 
159  glCullFace(GL_BACK);
160  glFrontFace(GL_CCW);
161  //~ glEnable(GL_CULL_FACE); // n'affiche que les faces correctement orientees...
162  glDisable(GL_CULL_FACE); // les faces mal orientees sont affichees avec des hachures oranges...
163 
164  glDepthFunc(GL_LESS);
165  glEnable(GL_DEPTH_TEST);
166 
167  return 0;
168 }
GLuint create_buffers(const bool use_texcoord, const bool use_normal, const bool use_color, const bool use_material_index)
construit les buffers et le vertex array object necessaires pour dessiner l'objet avec openGL....
Definition: mesh.cpp:579
void bounds(Point &pmin, Point &pmax) const
renvoie min et max les coordonnees des extremites des positions des sommets de l'objet (boite engloba...
Definition: mesh.cpp:501
int vertex_count() const
renvoie le nombre de sommets.
Definition: mesh.h:291
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
Definition: orbiter.h:17
void lookat(const Point &center, const float size)
observe le point center a une distance size.
Definition: orbiter.cpp:7
Widgets create_widgets()
cree une interface graphique. a detruire avec release_widgets( ).
Definition: widgets.cpp:12
Vector normalize(const Vector &v)
renvoie un vecteur unitaire / longueur == 1.
Definition: vec.cpp:123
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition: wavefront.cpp:14
GLuint read_texture(const int unit, const char *filename, const GLenum texel_type)
Definition: texture.cpp:154
representation d'un point 3d.
Definition: vec.h:21
representation d'un vecteur 3d.
Definition: vec.h:59