OpenGL and C++

So I recently started doing things with OpenGL (after completing many 2D games in SDL) and it is relatively straightforward. However I have ran into a problem, I cannot figure out how to load a texture. I have tried DevIL, SOIL, libPNG, libJPG, but either none of them work for me or I don't know how to properly implement them. I am not against writing an image loader for uncompressed bmps, but if is the route you think I should go, I will most likely need heavy assistance. Thanks!
closed account (o1vk4iN6)
You load textures into opengl through one of the supported pixel types.
Try do copy this and insert it into your code
http://nehe.gamedev.net/tutorial/lesson_06_texturing_update/47002/
Works just fine for my game.

Also dont forget to glEnable(GL_TEXTURE_2D);
Have you put out your texCoords?
The first comment didnt really help at all, and for the second comment, I tried installing the SOIL headers then putting the code into mine, (I also put the glEnable(GL_TEXTURE_2D) but I was getting a bunch of undefined errors. I am on a different computer now however so I wont be able to give you the specific ones.
Sorry but it is pretty hard to help you without more info
I am going to try to be as specific as can be.

So I know this code probably isn't very efficient, it was taken from my code that I used while I was going through the swiftless tutorials. It could probably be shortened and whatnot, but that is not what I am here about, I would like to know how to get a texture on the 4.0f x 4.0f slate by using either SOIL or DevIL or a bitmap loader that I would make myself. I have tried to set them up but I can never do it completely right, so if anyone could walk me through the steps for setting it up in code::blocks it would be great.
P.S. I left out the code that I was too lazy to remove from the project that handled key events (i'm not using any).

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
//main.cpp
#include <GL/glut.h>
#include <GL/gl.h>
#include "keyOps.h"
#include "windowOps.h"
#include "rendering.h"


void init (void) {
    glEnable (GL_DEPTH_TEST);
    glEnable (GL_LIGHTING);
    glEnable (GL_LIGHT0);
    glEnable(GL_COLOR_MATERIAL);
}

int main( int argc, char **argv ) {

    glutInit( &argc, argv );

    glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );

    glutInitWindowSize( 500, 500 );
    glutInitWindowPosition( 100, 100 );

    glutCreateWindow( "OpenGL" );

    init();

    glutDisplayFunc( display );
    glutIdleFunc( display );
    glutReshapeFunc(reshape);

    glutKeyboardFunc(keyPressed);
    glutKeyboardUpFunc(keyUp);

    glutSpecialFunc( keySpecial );
    glutSpecialUpFunc( keySpecialUp );

    glutReshapeFunc( reshape );

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glutMainLoop();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//rendering.cpp
#include <GL/glut.h>
#include "windowOps.h"
#include "keyOps.h"
#include "rendering.h"

void renderPrimitive( void ) {


    glBegin(GL_QUADS);
        glVertex3f( -2.0, 2.0, 0.0 );
        glVertex3f( -2.0, -2.0, 0.0 );
        glVertex3f( 2.0, -2.0, 0.0 );
        glVertex3f( 2.0, 2.0, 0.0 );
    glEnd();

}


1
2
3
4
5
6
//rendering.h
extern bool movingUp;
extern float yLocation;
extern float yRotationAngle;

extern void renderPrimitive( void );


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
//windowOps.cpp
#include <GL/glut.h>
#include "windowOps.h"
#include "keyOps.h"
#include "rendering.h"

void reshape( int width, int height ) {

    glViewport(0, 0, (GLsizei)width, (GLsizei)height );

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0 );

    glMatrixMode(GL_MODELVIEW);
}

void display( void ) {

    keyOperations();

    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef( 0.0f, 0.0f, -5.0f );

    gluLookAt ( 3.0, 2.0, 2.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

    renderPrimitive();

    glutSwapBuffers();
}


1
2
3
4
//windowOps.h
extern void reshape( int width, int height );
extern void display( void );
extern void light();

Last edited on
I have no experience at all with Glut. i'm sorry but i can't help you very much.

But basicly if youre not using glut its pretty much like this.

1
2
3
4
5
6
InitFunc()
{
 glEnable(GL_TEXTURE_2D);
 

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
RenderFunc()
{
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D,TileData.TexID);

	glBegin(GL_QUADS);

		glTexCoord2f(0.0f,0.0f); glVertex2f(box.x, box.y);
		glTexCoord2f(1.0f,0.0f); glVertex2f(box.x + box.w, box.y);
		glTexCoord2f(1.0f,1.0f); glVertex2f(box.x + box.w, box.y + box.h);
		glTexCoord2f(0.0f,1.0f); glVertex2f(box.x, box.y + box.h);

	glEnd();

	glDisable(GL_TEXTURE_2D);
}
closed account (o1vk4iN6)
http://www.opengl.org/wiki/Fixed_Function_Pipeline

It also doesn't exist in opengl es 2.0 if you ever plan to use that.
To stoffe1100: That makes a lot of sense, but it also looks a little too easy, also where does TileData.TexID come from? Other than that that code really helped me, its just that one variable that I am confused with
1) Initialize your textures into some sort of variable or array. With my game engine I use SOIL, and have been quite happy with that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
GLuint textureList[maxTextures];
void client::loadTexts()

{
    textureList[TEXTURE_CROSSHAIR] = SOIL_load_OGL_texture(

                                         "resource/art/UI/crosshair.tga",

                                         SOIL_LOAD_AUTO,

                                         SOIL_CREATE_NEW_ID,

                                         SOIL_FLAG_INVERT_Y | SOIL_FLAG_MULTIPLY_ALPHA

                                     );
                  if(!textureList[TEXTURE_CROSSHAIR]){cout<<"TEXTURE LOAD FAILIURE: "<<SOIL_last_result()<<endl;}
.....etc
}

Then make sure when you go to render your quad or whatever, make sure you do:
1
2
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,textureList[TEXTURE_CROSSHAIR]);


This is simplified, but that is the basic loading and usage of textures.
Edit: Look up the soil loading flags for more information on loading textures.
Last edited on
Topic archived. No new replies allowed.