Image preview working
This commit is contained in:
@@ -1,116 +1,116 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
GLuint loadBMP_custom(const char * imagepath);
|
||||
|
||||
int main(void)
|
||||
void EnableTransparency()
|
||||
{
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
}
|
||||
|
||||
GLuint LoadTexture(char *filename,int *textw,int *texth) {
|
||||
SDL_Surface *surface;
|
||||
GLuint textureid;
|
||||
int mode;
|
||||
surface = IMG_Load(filename);
|
||||
// Or if you don't use SDL_image you can use SDL_LoadBMP here instead:
|
||||
// surface = SDL_LoadBMP(filename);
|
||||
|
||||
// could not load filename
|
||||
if (!surface) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// work out what format to tell glTexImage2D to use...
|
||||
if (surface->format->BytesPerPixel == 3) { // RGB 24bit
|
||||
mode = GL_RGB;
|
||||
|
||||
} else if (surface->format->BytesPerPixel == 4) { // RGBA 32bit
|
||||
mode = GL_RGBA;
|
||||
} else {
|
||||
SDL_FreeSurface(surface);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*textw=surface->w;
|
||||
*texth=surface->h;
|
||||
// create one texture name
|
||||
glGenTextures(1, &textureid);
|
||||
// tell opengl to use the generated texture name
|
||||
glBindTexture(GL_TEXTURE_2D, textureid);
|
||||
// this reads from the sdl surface and puts it into an opengl texture
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels);
|
||||
// these affect how this texture is drawn later on...glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
// clean up
|
||||
SDL_FreeSurface(surface);
|
||||
return textureid;
|
||||
|
||||
}
|
||||
|
||||
void DrawTexture(int x, int y, GLuint textureid,int textw,int texth) {
|
||||
//printf("x: %d\ty: %d\tw: %d\th: %d\n", x, y, textw, texth);
|
||||
//int textw,texth;
|
||||
// tell opengl to use the generated texture name
|
||||
glBindTexture(GL_TEXTURE_2D, textureid);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
// make a rectangle
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2d(0.0,0.0); glVertex2f(-1, -1);
|
||||
glTexCoord2d(1.0,0.0); glVertex2f(1, -1);
|
||||
glTexCoord2d(1.0,1.0); glVertex2f(1, 1);
|
||||
glTexCoord2d(0.0,1.0); glVertex2f(-1, 1);
|
||||
glEnd();
|
||||
glDisable(GL_TEXTURE_2D );
|
||||
}
|
||||
|
||||
int main(int argc, char* args[]) {
|
||||
GLFWwindow* window;
|
||||
GLuint myglu;
|
||||
int textw, texth;
|
||||
|
||||
|
||||
/* Initialize the library */
|
||||
/* GLFW */
|
||||
if (!glfwInit())
|
||||
return -1;
|
||||
|
||||
/* SDL */
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||
printf("SDL Error");
|
||||
/* Create a windowed mode window and its OpenGL context */
|
||||
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
|
||||
if (!window)
|
||||
{
|
||||
if (!window) {
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Make the window's context current */
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// INIT
|
||||
|
||||
GLuint image = loadBMP_custom("../img/tests/stones.bmp");
|
||||
|
||||
|
||||
|
||||
|
||||
myglu=LoadTexture("../img/tests/stones.bmp",&textw,&texth);
|
||||
|
||||
// INIT END
|
||||
|
||||
/* Loop until the user closes the window */
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
/* Render here */
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// RENDER
|
||||
|
||||
|
||||
|
||||
DrawTexture(50,50,myglu,textw,texth);
|
||||
|
||||
|
||||
// RENDER END
|
||||
|
||||
/* Swap front and back buffers */
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
/* Poll for and process events */
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
GLuint loadBMP_custom(const char * imagepath) {
|
||||
// Data read from the header of the BMP file
|
||||
unsigned char header[54]; // Each BMP file begins by a 54-bytes header
|
||||
unsigned int dataPos; // Position in the file where the actual data begins
|
||||
unsigned int width, height;
|
||||
unsigned int imageSize; // = width*height*3
|
||||
// Actual RGB data
|
||||
unsigned char * data;
|
||||
|
||||
// Open the file
|
||||
FILE * file = fopen(imagepath,"rb");
|
||||
if (!file){printf("Image could not be opened\n"); return 0;}
|
||||
|
||||
if ( fread(header, 1, 54, file)!=54 ){ // If not 54 bytes read : problem
|
||||
printf("Not a correct BMP file\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( header[0]!='B' || header[1]!='M' ){
|
||||
printf("Not a correct BMP file\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read ints from the byte array
|
||||
dataPos = *(int*)&(header[0x0A]);
|
||||
imageSize = *(int*)&(header[0x22]);
|
||||
width = *(int*)&(header[0x12]);
|
||||
height = *(int*)&(header[0x16]);
|
||||
|
||||
// Some BMP files are misformatted, guess missing information
|
||||
if (imageSize==0) imageSize=width*height*3; // 3 : one byte for each Red, Green and Blue component
|
||||
if (dataPos==0) dataPos=54; // The BMP header is done that way
|
||||
|
||||
// Create a buffer
|
||||
data = new unsigned char [imageSize];
|
||||
|
||||
// Read the actual data from the file into the buffer
|
||||
fread(data,1,imageSize,file);
|
||||
|
||||
//Everything is in memory now, the file can be closed
|
||||
fclose(file);
|
||||
|
||||
// Create one OpenGL texture
|
||||
GLuint textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
|
||||
// "Bind" the newly created texture : all future texture functions will modify this texture
|
||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||
|
||||
// Give the image to OpenGL
|
||||
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user