texturing bricks

Started by
2 comments, last by AndyEsser 12 years, 7 months ago
I amj working on a breakout game using opengl and C. I have used google to find information on texture mapping but I am still confused.I want to apply a texture to each brick in my game.Here is the code I have so far.
[font="Consolas"][font="Consolas"]

GLuint LoadTextureRAW([/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]const[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]char[/font][/font][/font][font="Consolas"][font="Consolas"] * filename, [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] wrap)

{

GLuint texture;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] width,height;

BYTE * data;

FILE * file;

file=fopen(filename,[/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]"rb"[/font][/font][/font][font="Consolas"][font="Consolas"]);

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](file==NULL) [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]return[/font][/font][/font][font="Consolas"][font="Consolas"] 0;

width = 256;

height = 256;

[/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]// data = malloc(width * height * 3);

[/font][/font][/font][font="Consolas"][font="Consolas"]fread(data,width*height*3,1,file);

fclose(file);

glGenTextures(1,&texture);

glBindTexture(GL_TEXTURE_2D,texture);

}



[/font][/font]
Advertisement
With that function, you'll load a texture and save it's id in some GLuint variable - this will be your texture. But in your case, you can only load ".RAW" textures. If you want something else like bmp, jpg, gif etc. you'll have to do it differently.

Anyway, after you load your texture, you simply bind that texture before drawing your quad, like this:

[font="Verdana"]
GLuint myTexture = LoadTextureRad("test.raw", TRUE);[/font]

[font="verdana"]glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, myTexture);
glBegin( GL_QUADS );
glTexCoord2d(0, 0, 0, 0); glVertex2d(0, 0, 0, 0);
[/font][font="verdana"]glTexCoord2d(1, 0, 0, 0); glVertex2d(1, 0, 0, 0);
[/font][font="verdana"]glTexCoord2d(1, 0, 1, 0); glVertex2d(1, 0, 1, 0);
[/font][font="verdana"]glTexCoord2d(0, 0, 1, 0); glVertex2d(0, 0, 1, 0);
glEnd();[/font]
well for some reason this line of code does not work
[font="Consolas"][font="Consolas"]GLuint myTexture = LoadTextureRad([/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]"test.raw"[/font][/font][/font][font="Consolas"][font="Consolas"],[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]true[/font][/font][/font][font="Consolas"][font="Consolas"]);

see sbove code for reference


[/font][/font]
Why would it work? You're never actually returning the GLuint from the function.

You need to add:

return texture;

Just before the the closing curly brace. You also need to add that malloc() line back in - with an appropriate free() after you're done with the data.

Also - you will have to call the glTexParameteri() family of functions before the texture is considered by OpenGL to be complete. And also pass the actual data to OpenGL using glTexImage2D. Where did you get this code from? This shows a distinct lack of understanding and/or research.

And please, whatever you do - don't call that function every frame. Call it once at startup and then store the variable and use it each frame.

This topic is closed to new replies.

Advertisement