Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: texture mapping issues

  1. #1
    Intern Contributor
    Join Date
    Oct 2008
    Posts
    63

    texture mapping issues

    I have gotten my texture program working. Just one problem, when i run the program, it just displays the frame, with an empty window inside. Pretty sure the code is ok as i got it from another opengl expert, and it works on their computer. So why would it do this on mine? Any advice please, thanks

    Code :
    #include "stdafx.h"
    #include "windows.h"
    #include "gl/gl.h"
    #include "glut.h"
     
    #include <vector>
    #include <string>
    #include <fstream>
     
    bool LoadBitmap(const std::string &amp;filename, int texture) 
    {
    	std::ifstream file(filename.c_str(),std::ios::binary);
     
    	if(!file)
    	{
    		return false;
    	}
     
    	BITMAPFILEHEADER fileheader; 
    	BITMAPINFOHEADER infoheader;
     
    	file.read(reinterpret_cast<char *>(&amp;fileheader), sizeof(fileheader));
    	file.read(reinterpret_cast<char *>(&amp;infoheader), sizeof(infoheader));
     
    	std::vector<byte> texturedata;
    	unsigned amount = infoheader.biWidth * infoheader.biHeight;
    	texturedata.reserve(amount * 4);
     
    	for(unsigned i = 0; i < amount; i++)
    	{            
    		RGBTRIPLE rgb = {255,255,255};
     
    		file.read(reinterpret_cast<char *>(&amp;rgb), sizeof(rgb));
    		texturedata.push_back(rgb.rgbtRed);
    		texturedata.push_back(rgb.rgbtGreen);
    		texturedata.push_back(rgb.rgbtBlue);
    		texturedata.push_back(255);
    	}
     
    	glBindTexture(GL_TEXTURE_2D, texture);
     
     
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
     
    	void *raw = &amp;texturedata.front();
    	glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, raw);
    	gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, raw);
     
    	return true;
    }
     
    GLuint texture = 0;
     
    void draw()
    {
    	glClear(GL_COLOR_BUFFER_BIT);
     
    	glEnable(GL_TEXTURE_2D);
    	glBindTexture(GL_TEXTURE_2D,texture);
     
     
    	glutSolidTeapot(0.5);
    }
     
    int win;
     
    void keyboard(unsigned char key, int x, int y)
    {
    	if(key == 'q')
    	{
    		glutDestroyWindow(win);
    		exit(0);
    	}
    }
     
    int main(int argc, char **argv)
    {
    	glutInit(&amp;argc, argv);
    	glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
    	glutInitWindowSize(800,400);
    	glutInitWindowPosition(100,100);
     
    	win = glutCreateWindow("Hello");
     
    	glutDisplayFunc(draw);
    	glutKeyboardFunc(keyboard);
     
    	glClearColor(0,0,0,0);
     
    	glGenTextures(1, &amp;texture);
     
    	bool b = LoadBitmap("texture1.bmp",texture); 
     
    	if(!b)
    	{
    		MessageBox(NULL,L"failed to load bitmap",L"Hello",MB_OK);
    		return 1;
    	}
     
    	glutMainLoop();
    }

  2. #2
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: texture mapping issues

    In your code I don't see you register a reshape function for glutReshapeFunc.
    You need a function that is similar as this
    Code :
    void reshape(GLsizei width, GLsizei height)	
    {
      glViewport(0,0,width,height);					
      glMatrixMode(GL_PROJECTION);					
      glLoadIdentity();							gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
     
      glMatrixMode(GL_MODELVIEW);			
      glLoadIdentity();					
    }

    And translate back you teapot a little bit.
    Code :
    glLoadIdentity();
    glTranslatef(0.0,0.0,-z);
    glutSolidTeapot(0.5);


  3. #3
    Intern Contributor
    Join Date
    Oct 2008
    Posts
    63

    Re: texture mapping issues

    i added that stuff in but for some reason i am getting the same error which is that only a frame is displayed with no window in the middle. Its hard to explain for this, but if you know java gui, its like a JFrame with no ContentPane. I presume the code is right, so is there somthing else which could cause this problem?

  4. #4
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: texture mapping issues

    Just to be sure do you add a call for glutReshapeFunc(reshape) inside your main. Try to and a function void init(void) with glClearColor(1.0,0.0,0.0,1.0); inside to see if you will have a red background. call it just before glutMainLoop(). And by the way in the init function add glEnable(GL_DEPTH_TEST); and in your window creation add GLUT_DEPTH to have a depth buffer active.

    Edit: And modify the call glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

  5. #5
    Intern Contributor
    Join Date
    Oct 2008
    Posts
    63

    Re: texture mapping issues

    Strange, i have added all that in and still exactly the same. I have tested all my other programs which are just basic drawing etc, and they all work fine. This is how the code is looking
    Code :
    #include "stdafx.h"
    #include "windows.h"
    #include "gl/gl.h"
    #include "glut.h"
     
    #include <vector>
    #include <string>
    #include <fstream>
     
    bool LoadBitmap(const std::string &amp;filename, int texture) 
    {
    	std::ifstream file(filename.c_str(),std::ios::binary);
     
    	if(!file)
    	{
    		return false;
    	}
     
    	BITMAPFILEHEADER fileheader; 
    	BITMAPINFOHEADER infoheader;
     
    	file.read(reinterpret_cast<char *>(&amp;fileheader), sizeof(fileheader));
    	file.read(reinterpret_cast<char *>(&amp;infoheader), sizeof(infoheader));
     
    	std::vector<byte> texturedata;
    	unsigned amount = infoheader.biWidth * infoheader.biHeight;
    	texturedata.reserve(amount * 4);
     
    	for(unsigned i = 0; i < amount; i++)
    	{            
    		RGBTRIPLE rgb = {255,255,255};
     
    		file.read(reinterpret_cast<char *>(&amp;rgb), sizeof(rgb));
    		texturedata.push_back(rgb.rgbtRed);
    		texturedata.push_back(rgb.rgbtGreen);
    		texturedata.push_back(rgb.rgbtBlue);
    		texturedata.push_back(255);
    	}
     
    	glBindTexture(GL_TEXTURE_2D, texture);
     
     
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
     
    	void *raw = &amp;texturedata.front();
    	glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, raw);
    	gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, raw);
     
    	return true;
    }
     
    GLuint texture = 0;
     
    void draw()
    {
    	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
     
    	glEnable(GL_TEXTURE_2D);
    	glBindTexture(GL_TEXTURE_2D,texture);
     
     
    	glLoadIdentity();
    	glTranslatef(0.0,0.0,-0.5);
    	glutSolidTeapot(0.5);
     
    }
     
    void init(void)
    {
    glClearColor(1.0,0.0,0.0,1.0);
    glEnable(GL_DEPTH_TEST);
    }
     
    void reshape(GLsizei width, GLsizei height)	
    {
      glViewport(0,0,width,height);					
      glMatrixMode(GL_PROJECTION);					
      glLoadIdentity();							gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
     
      glMatrixMode(GL_MODELVIEW);			
      glLoadIdentity();					
    }
     
     
    int win;
     
    void keyboard(unsigned char key, int x, int y)
    {
    	if(key == 'q')
    	{
    		glutDestroyWindow(win);
    		exit(0);
    	}
    }
     
    int main(int argc, char **argv)
    {
    	glutInit(&amp;argc, argv);
    	glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH);
    	glutInitWindowSize(800,400);
    	glutInitWindowPosition(100,100);
     
    	win = glutCreateWindow("Hello");
     
    	glutDisplayFunc(draw);
    	glutKeyboardFunc(keyboard);
        glutReshapeFunc(reshape);
     
    	glClearColor(0,0,0,0);
     
    	glGenTextures(1, &amp;texture);
     
    	bool b = LoadBitmap("texture1.bmp",texture); 
     
    	if(!b)
    	{
    		MessageBox(NULL,L"failed to load bitmap",L"Hello",MB_OK);
    		return 1;
    	}
            init();
    	glutMainLoop();
     
    }

    And i am just using a standard bmp image with the size 64*64. Does it work for you? Maybe there is somthing wrong with my software although i have only recently downloaded it. Very strange.

  6. #6
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: texture mapping issues

    I will test it without texture because I am on Linux.

  7. #7
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: texture mapping issues

    Ok, got it working without texture. I will try to add a texture over it with another library.

    Edit: Got it working with a texture. Try first without a mipmap to see.

  8. #8
    Intern Contributor
    Join Date
    Oct 2008
    Posts
    63

    Re: texture mapping issues

    I have commented out everything which is to do with textures, and still the same problem. Very wierd. I am putting a window in my code arnt i? I am going to create a new project and see if it works and if not, i will try reinstalling it.
    cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •