Add Textures
This commit is contained in:
parent
78dc21fee8
commit
94a63390ef
19 changed files with 8232 additions and 29 deletions
14
clean.bat
Normal file
14
clean.bat
Normal file
|
@ -0,0 +1,14 @@
|
|||
|
||||
rm -rf bin
|
||||
|
||||
rm -rf CMakeFiles
|
||||
|
||||
rm -f CMakeCache.txt
|
||||
|
||||
rm -f cmake_install.cmake
|
||||
|
||||
rm -f Makefile
|
||||
rm -f *.vcxproj*
|
||||
rm -f *.sln
|
||||
rm -f *.db
|
||||
rm -rf .vs
|
BIN
res/images/2x2_grid.png
Normal file
BIN
res/images/2x2_grid.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.5 KiB |
BIN
res/images/nintendo_64_256.png
Normal file
BIN
res/images/nintendo_64_256.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 54 KiB |
BIN
res/images/pearl-0003.png
Normal file
BIN
res/images/pearl-0003.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.4 MiB |
|
@ -1,6 +1,7 @@
|
|||
uniform float a;
|
||||
#version 150
|
||||
|
||||
uniform float y;
|
||||
|
||||
void main(void){
|
||||
if(a >= 0)gl_FragColor = vec4(a,0,0,1);
|
||||
else gl_FragColor = vec4(0,-a,0,1);
|
||||
gl_FragColor= vec4(1, 1 - abs(y)/50,1 - abs(y)/50,1);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
|
||||
#version 150
|
||||
|
||||
void main(void){
|
||||
gl_FrontColor = gl_Color;
|
||||
|
|
14
src/audio.h
14
src/audio.h
|
@ -10,11 +10,25 @@
|
|||
#include <sndfile.h>
|
||||
#include <vector>
|
||||
|
||||
/*
|
||||
Class created by MrDev023
|
||||
*/
|
||||
class Audio{
|
||||
|
||||
public:
|
||||
/*
|
||||
Initialise le Device pour lire des pistes audio
|
||||
*/
|
||||
static void initOpenAL();
|
||||
|
||||
/*
|
||||
Permet de detruire le device
|
||||
*/
|
||||
static void destroyOpenAL();
|
||||
|
||||
/*
|
||||
Fonction permettant de lire un son puis de retourner l identifiant de la source
|
||||
*/
|
||||
static ALuint loadSound(std::string);
|
||||
static ALuint createSource(ALuint*);
|
||||
static ALuint createSource(const std::string);
|
||||
|
|
11
src/image.cpp
Normal file
11
src/image.cpp
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include "renderer.h"
|
||||
|
||||
Image::Image(unsigned char* data,int width,int height){
|
||||
this->data = data;
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
}
|
||||
|
||||
Image::~Image(){
|
||||
delete this->data;
|
||||
}
|
6224
src/lodepng.cpp
Normal file
6224
src/lodepng.cpp
Normal file
File diff suppressed because it is too large
Load diff
1760
src/lodepng.h
Normal file
1760
src/lodepng.h
Normal file
File diff suppressed because it is too large
Load diff
53
src/main.cpp
53
src/main.cpp
|
@ -2,39 +2,44 @@
|
|||
#include "audio.h"
|
||||
#include "renderer.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
void event(SDL_Event e){
|
||||
|
||||
switch( e.type ){
|
||||
case SDL_KEYDOWN:
|
||||
switch( e.key.keysym.sym ){
|
||||
|
||||
}
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
switch( e.key.keysym.sym ){
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv){
|
||||
Window::initWindow(800,600,"Test");
|
||||
Audio::initOpenAL();
|
||||
GLuint s = Shader::createShader("test.vert","test.frag");
|
||||
float i = 0;
|
||||
Shader::bindShader(&s);
|
||||
while(!Window::closeRequested){//main loop
|
||||
Shader::setUniform(&s,"a",cos(i));
|
||||
Window::pollEvent(&event);
|
||||
|
||||
Window::initWindow(800,800,"Title");
|
||||
//Texture* t = Texture::createTexture("res/images/2x2_grid.png");
|
||||
Texture* t = Texture::createTexture("res/images/pearl-0003.png");
|
||||
while(!Window::closeRequested){
|
||||
Window::pollEvent(event);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glLoadIdentity();
|
||||
Texture::bindTexture(t->id);
|
||||
glBegin(GL_QUADS);
|
||||
glVertex2f(-1,-1);
|
||||
glVertex2f(-1,1);
|
||||
glVertex2f(1,1);
|
||||
glVertex2f(1,-1);
|
||||
glColor3f(1,1,1);
|
||||
glTexCoord2f(0,0);
|
||||
glVertex2i(-1,-1);
|
||||
glTexCoord2f(0,t->ratioY);
|
||||
glVertex2i(-1,1);
|
||||
glTexCoord2f(t->ratioX,t->ratioY);
|
||||
glVertex2i(1,1);
|
||||
glTexCoord2f(t->ratioX,0);
|
||||
glVertex2i(1,-1);
|
||||
glEnd();
|
||||
|
||||
Window::displayUpdate();
|
||||
i+=1/5000.0f;
|
||||
}
|
||||
Shader::destroyShader(&s);
|
||||
Audio::destroyOpenAL();
|
||||
delete t;
|
||||
Window::destroyWindow();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
#include <cstddef>
|
||||
#include <algorithm>
|
||||
|
||||
#define byte char
|
||||
|
||||
extern const float PI;
|
||||
float lerp(float,float,float);
|
||||
float cLerp(float,float,float);
|
||||
|
|
2
src/physics.cpp
Normal file
2
src/physics.cpp
Normal file
|
@ -0,0 +1,2 @@
|
|||
#include "physics.h"
|
||||
|
6
src/physics.h
Normal file
6
src/physics.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#ifndef PHYSICS_H
|
||||
#define PHYSICS_H
|
||||
|
||||
|
||||
|
||||
#endif
|
|
@ -13,6 +13,8 @@
|
|||
#include <sys/stat.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include "lodepng.h"
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <GL/glew.h>
|
||||
#include <GL/gl.h>
|
||||
|
@ -20,6 +22,38 @@
|
|||
|
||||
bool fileExists(const std::string&);
|
||||
|
||||
class Shader;
|
||||
class Texture;
|
||||
class Image;
|
||||
|
||||
class Image{
|
||||
public:
|
||||
unsigned char* data;
|
||||
int width;
|
||||
int height;
|
||||
|
||||
Image(unsigned char*,int,int);
|
||||
~Image();
|
||||
};
|
||||
|
||||
class Texture{
|
||||
public:
|
||||
static Texture* createTexture(const char*);
|
||||
static void destroyTexture(GLuint*);
|
||||
static void bindTexture(GLuint*);
|
||||
static void unbindTexture();
|
||||
|
||||
double ratioX;
|
||||
double ratioY;
|
||||
GLuint* id;
|
||||
|
||||
Texture(GLuint,double,double);
|
||||
~Texture();
|
||||
|
||||
private:
|
||||
static Image* loadTexture(const char*);
|
||||
};
|
||||
|
||||
class Shader{
|
||||
public:
|
||||
static std::string LoadSource(const char*);
|
||||
|
|
|
@ -128,6 +128,8 @@ GLuint Shader::createShader(const char* vertName,const char* fragName){
|
|||
glValidateProgram(program);
|
||||
glDeleteShader(vertShader);
|
||||
glDeleteShader(fragShader);
|
||||
printf("Vertex Shader '%s' loaded !\n",vertName);
|
||||
printf("Fragment Shader '%s' loaded !\n",fragName);
|
||||
return program;
|
||||
}
|
||||
|
||||
|
|
90
src/texture.cpp
Normal file
90
src/texture.cpp
Normal file
|
@ -0,0 +1,90 @@
|
|||
#include "renderer.h"
|
||||
|
||||
Texture::Texture(GLuint id,double u3,double v3){
|
||||
this->id = new GLuint(id);
|
||||
this->ratioX = u3;
|
||||
this->ratioY = v3;
|
||||
}
|
||||
|
||||
Texture::~Texture(){
|
||||
Texture::destroyTexture(this->id);
|
||||
delete this->id;
|
||||
}
|
||||
|
||||
Texture* Texture::createTexture(const char* file){
|
||||
Image* img = Texture::loadTexture(file);
|
||||
if(img == NULL)return NULL;
|
||||
GLuint t;
|
||||
glGenTextures(1, &t);
|
||||
glBindTexture(GL_TEXTURE_2D, t);
|
||||
|
||||
// Texture size must be power of two for the primitive OpenGL version this is written for. Find next power of two.
|
||||
size_t u2 = 1; while(u2 < img->width) u2 *= 2;
|
||||
size_t v2 = 1; while(v2 < img->height) v2 *= 2;
|
||||
// Ratio for power of two version compared to actual version, to render the non power of two image with proper size.
|
||||
double u3 = (double)img->width / u2;
|
||||
double v3 = (double)img->height / v2;
|
||||
|
||||
// Make power of two version of the image.
|
||||
std::vector<unsigned char> image2(u2 * v2 * 4 * (img->width/48));
|
||||
for(size_t y = 0; y < img->height; y++)
|
||||
for(size_t x = 0; x < img->width; x++)
|
||||
for(size_t c = 0; c < 4; c++)
|
||||
{
|
||||
image2[4 * u2 * y + 4 * x + c] = img->data[4 * img->width * y + 4 * x + c];
|
||||
}
|
||||
|
||||
// Enable the texture for OpenGL.
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //GL_NEAREST = no smoothing
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, u2, v2, 0, GL_RGBA, GL_UNSIGNED_BYTE, &image2[0]);
|
||||
|
||||
std::cout << "GL Texture created (" << file << ":" << img->width << "x" << img->height << ") #" << t << " !" << std::endl;
|
||||
image2.clear();
|
||||
delete img;
|
||||
|
||||
return new Texture(t,u3,v3);
|
||||
}
|
||||
|
||||
void Texture::bindTexture(GLuint* texture){
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnable(GL_BLEND);
|
||||
glDisable(GL_ALPHA_TEST);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glBindTexture(GL_TEXTURE_2D,*texture);
|
||||
}
|
||||
|
||||
void Texture::unbindTexture(){
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
glBindTexture(GL_TEXTURE_2D,0);
|
||||
}
|
||||
|
||||
void Texture::destroyTexture(GLuint* texture){
|
||||
glDeleteTextures(1,texture);
|
||||
}
|
||||
|
||||
Image* Texture::loadTexture(const char* file){
|
||||
std::vector<unsigned char> image; //the raw pixels
|
||||
unsigned width, height;
|
||||
|
||||
//decode
|
||||
unsigned error = lodepng::decode(image, width, height, file);
|
||||
|
||||
//if there's an error, display it
|
||||
if(error){
|
||||
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//the pixels are now in the vector "image", 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...
|
||||
|
||||
std::cout << "Image loaded (" << file << ") ! : " << width << "x" << height << " with " << image.size() << " byte of data." << std::endl;
|
||||
|
||||
unsigned char* p = (unsigned char*)malloc(sizeof(unsigned char) * image.size());
|
||||
for(int i = 0;i < image.size();i++)p[i] = image[i];
|
||||
image.clear();
|
||||
return new Image(p,width,height);
|
||||
}
|
|
@ -5,6 +5,10 @@ SDL_Event Window::event;
|
|||
SDL_GLContext Window::context;
|
||||
bool Window::closeRequested = false;
|
||||
|
||||
unsigned long deltaTime = 0;
|
||||
unsigned long updateTime = 0;
|
||||
|
||||
std::string Window::title;
|
||||
|
||||
void Window::initWindow(int width,int height,std::string title){
|
||||
/* Initialisation simple */
|
||||
|
@ -17,7 +21,10 @@ void Window::initWindow(int width,int height,std::string title){
|
|||
SDL_WINDOWPOS_UNDEFINED,
|
||||
width,
|
||||
height,
|
||||
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
|
||||
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
|
||||
|
||||
Window::title = title.c_str();
|
||||
|
||||
if(!Window::pWindow){
|
||||
fprintf(stderr,"Erreur de création de la fenêtre: %s\n",SDL_GetError());
|
||||
exit(-1);
|
||||
|
@ -45,6 +52,13 @@ void Window::initWindow(int width,int height,std::string title){
|
|||
|
||||
}
|
||||
|
||||
void Window::setTitleSuffixe(const char* t){
|
||||
std::string* a = new std::string(t);
|
||||
*a = title + *a;
|
||||
SDL_SetWindowTitle(Window::pWindow, a->c_str());
|
||||
delete a;
|
||||
}
|
||||
|
||||
void Window::destroyWindow(){
|
||||
SDL_GL_DeleteContext ( context ) ;
|
||||
SDL_DestroyWindow(pWindow);
|
||||
|
@ -65,3 +79,14 @@ void Window::pollEvent(void(*f)(SDL_Event)){
|
|||
f(Window::event);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long currentMillisTime(){
|
||||
return std::chrono::system_clock::now().time_since_epoch() /
|
||||
std::chrono::milliseconds(1);
|
||||
}
|
||||
|
||||
unsigned long currentNanoTime(){
|
||||
return std::chrono::system_clock::now().time_since_epoch() /
|
||||
std::chrono::nanoseconds(1);
|
||||
return 0;
|
||||
}
|
||||
|
|
13
src/window.h
13
src/window.h
|
@ -9,6 +9,17 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
#include <chrono>
|
||||
|
||||
#define NTIME currentNanoTime()
|
||||
#define UTIME currentMillisTime()
|
||||
|
||||
extern unsigned long deltaTime;
|
||||
extern unsigned long updateTime;
|
||||
|
||||
unsigned long currentMillisTime();
|
||||
unsigned long currentNanoTime();
|
||||
|
||||
class Window{
|
||||
|
||||
|
@ -17,7 +28,9 @@ class Window{
|
|||
static void destroyWindow();
|
||||
static void pollEvent(void(*f)(SDL_Event));
|
||||
static void displayUpdate();
|
||||
static void setTitleSuffixe(const char*);
|
||||
|
||||
static std::string title;
|
||||
static SDL_Window* pWindow;
|
||||
static SDL_Event event;
|
||||
static bool closeRequested;
|
||||
|
|
Reference in a new issue