Help with openGL texture - please help

Hey, so I've just hit the wall with textures in openGL (C++.) I've tried hundreds of different example source codes, tried to load hundreds of different third party libraries, everything - and I just can't do it. Nothing works for me. I always get some error that makes little to no sense to me, and I've not the slightest idea on how to do this manually.

If someone - anyone - could give me or give me the download link to a file with all of the libraries and a simple, single source code file that I can just copy and paste into a new project and straight-up run it and have it work, I would be ever-grateful. I honestly can not figure this out on my own.

I have a Windows 7 OS and am using Microsoft Visual 2010 C++ for the compiler.

Thank you.
You can check out NeHeGL's tutorials, the BMP/TGA Texture Loading Functions have been updated "Recently" so you will not need glaux anymore.
http://nehe.gamedev.net/tutorial/lessons_06__10/17010/
Especially the Lesson 6, there's a SOIL-update (SOIL is the Simple OpenGL Image Loader). It can help you getting more experiences on OpenGL.
I don't know of any complete code, but I can give you some pointers.

Make sure you call glEnable(GL_TEXTURE_2D) before working with textures. Then insert the following code:

1
2
3
4
5
6
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);


When you load the texture, do this:

1
2
3
4
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); //not sure why this is needed
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, /*width*/, /*height*/, GL_RGB, GL_UNSIGNED_BYTE, /*image data*/);


Using the glu function is often considered bad form, but it works well for me. Make sure width and height are powers of 2. This assumes that image data is a 24-bit bitmap stored with red first.

When it comes time to render, rebind the texture and make sure you supply texture coordinates with the vertices, just like you would use color values.
Last edited on
Okay, so I think I'm close to the solution. Here's what I have so far, but I'm only getting a white square. Does anyone know what the problem is?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include    <windows.h>                          
#include    <stdio.h>                         
#include    <gl\gl.h>                         
#include    <gl\glu.h>      
#include    <GL\glut.h>                   
#include "SOIL.h"
bool* keyStates = new bool[256];
GLuint texture[1];  
int LoadGLTextures()     
{
    texture[0] = SOIL_load_OGL_texture("line.bmp", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
    if(texture[0] == 0) return false;
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    return true;                                       
}
void resize(int height, int width) {
    const float ar = (float) width / (float) height;
    glViewport(0, 10, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 90.0);
    //gluLookAt(0, 2, 0, -1, 1, -3, 0, 1, 0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}
void keyOperations (void) {   
	if (!keyStates['a']) {} 
}   
static void Draw(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();                           
    glTranslatef(0.0f,0.0f,-5.0f);  
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
	glEnd();
	glutSwapBuffers();
}
void keyPressed (unsigned char key, int x, int y) {   
	keyStates[key] = false; 
}    
void keyUp (unsigned char key, int x, int y) {   
	keyStates[key] = true;
}   
int main(int argc, char **argv)
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInit(&argc, argv);
    glutInitWindowSize(600, 600);
    glutCreateWindow("ugh fml");
    glutReshapeFunc(resize);
    glutDisplayFunc(Draw);
	glutKeyboardFunc(keyPressed);
	glutKeyboardUpFunc(keyUp);
	/////////////////////////////////////
	glEnable(GL_TEXTURE_2D);                     
    glShadeModel(GL_SMOOTH);               
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);            
    glClearDepth(1.0f);                   
    glEnable(GL_DEPTH_TEST);                   
    glDepthFunc(GL_LEQUAL);                   
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 
    glutMainLoop();
}
You don't seem to be calling LoadGLTextures.
Wow, what a rookie mistake! OH MY GOD, IT WORKS.

Don't take this in the wrong direction, Telion, but I would hug you if I could right now.
Topic archived. No new replies allowed.