PDA

View Full Version : Texture



joslen_bomon1985
12-26-2016, 07:52 AM
Hello!
Can i get simplest source code for put texture from "bmp" file ?

i use win 8-64. Visual studio 2015.

Harukoxd
12-26-2016, 08:08 AM
GLuint m_textureID = 0;

int x, y, bytesPerPixel;
unsigned char* data = stbi_load((path + m_fileName).c_str(), &x, &y, &bytesPerPixel, 4);
if (data[0] == '\0')
{
std::cerr << "Unable to load texture: " << m_fileName << std::endl;
return false;
}

// Generate a texture
glGenTextures(1, &m_textureID);

// Bind the texture
glBindTexture(GL_TEXTURE_2D, m_textureID);

// Place the data
glTexImage2D(m_textureTarget, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

// PARAMETERS, For info about them look at https://open.gl/textures

// Unbind the texture
glBindTexture(m_textureTarget, 0);

// free the data
stbi_image_free(data);


I am using stb_image library (https://github.com/nothings/stb/). Just copy paste the header and source.

joslen_bomon1985
01-08-2017, 11:57 PM
I am trying to use this way. But it is not working.
Can you send simplest project ?

3DPrgmer
01-09-2017, 12:23 AM
I prefer Qt. You can use that library for OpenGL, matrices and images. Pretty sure QImage support bmp.

Qt is very powerful and you can use it for everything. Even the QString is much better than std:: string.

And Qt is compatible with VS.

And Qt has a lot of example codes that come with it: http://doc.qt.io/qt-5/qtopengl-cube-example.html

john_connor
01-09-2017, 01:42 AM
I am trying to use this way. But it is not working.
Can you send simplest project ?

https://en.wikipedia.org/wiki/BMP_file_format#External_links

there you can find a simple headder file with the class "CBitmap"
http://www.kalytta.com/bitmap.h

to load a .bmp file:

CBitmap mybmp;
bool success = mybmp.Load("abc.bmp");
if (success)
{
unsigned int width = mybmp.GetWidth();
unsigned int height= mybmp.GetHeight();
void* imagedata = mybmp.GetBits();

/* setup your texture */
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imagedata);
}


of course you have to generate a texture first + set some reasonable texture parameters
https://www.khronos.org/opengl/wiki/Texture#Sampling_parameters

if you want to be able to read some other common formatl like .jpg or .png or .dds, use SOIL instead
https://www.khronos.org/opengl/wiki/Image_Libraries#SOIL

mhagain
01-09-2017, 01:56 AM
But it is not working.
What's not working about it? What happens when you try it? What doesn't happen?

There are about a billion things that could go wrong here, so unless you actually give information other than "not working", it's very difficult to give you meaningful help.

joslen_bomon1985
01-09-2017, 03:36 AM
#include "stdafx.h"
#include "bmp.h"
#include <gl/freeglut.h>

void display(void);

int window_width = 500;
int window_height = 500;

void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitWindowPosition(window_width, window_height);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("");
glClearColor(255.0, 255.0, 255.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

CBitmap mybmp;
bool success = mybmp.Load("abc.bmp");

if (success)
{
unsigned int width = mybmp.GetWidth();
unsigned int height = mybmp.GetHeight();
void* imagedata = mybmp.GetBits();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imagedata);
}

glutSwapBuffers();
}


i can read my BMP but howto put my picture in window.

3DPrgmer
01-09-2017, 08:01 AM
do you wanna add the texture to an object, or do you wanna use it as background image??

Example using glew and my own tga importer: https://git.rwth-aachen.de/carstenf/OpenGL/tree/master/MshViewer
Example using Qt: https://git.rwth-aachen.de/carstenf/OpenGL/tree/master/QtMeshViewer

joslen_bomon1985
01-09-2017, 10:19 PM
I want to put my image on the window

mhagain
01-10-2017, 02:30 AM
What you really need is some basic OpenGL tutorials, starting with drawing a triangle and working through making it spin, lighting it, putting a texture on it.

I'm sorry but nobody is going to be doing you any favours if they just give you code for this. You'll have working code that you don't understand and you'll learn nothing. You're more likley to get useful help if you just search the web for OpenGL tutorials, work through them, and ask specific questions about any difficulties you have with them.