OpenGl draw on top of tile map C++ - c++

I am new to OpenGL. I have the following code that I am using from a tutorial I followed, what it does is render a tile map. It does this successfully but my problem now is that I want to add a moveable object onto the window however it is not appearing.
#include "stdafx.h"
#include <string>
#include <windows.h>
#include <iostream>
#include <conio.h>
#include <sstream>
#include <math.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include "GL/freeglut.h"
#pragma comment(lib, "OpenGL32.lib")
// window size and update rate (60 fps)
int width = 700;
int height = 700;
int interval = 1000 / 60;
// ball
float ball_pos_x = width / 2;
float ball_pos_y = height / 2;
float ball_dir_x = -1.0f;
float ball_dir_y = 0.0f;
int ball_width = 20;
int ball_height = 20;
int ball_speed = 5;
GLuint texture; //the array for our texture
GLuint texture2; //the array for our second texture
void keyboard() {
if (GetAsyncKeyState(VK_LEFT)) ball_pos_x -= ball_speed;
if (GetAsyncKeyState(VK_RIGHT)) ball_pos_x += ball_speed;
if (GetAsyncKeyState(VK_UP)) ball_pos_y += ball_speed;
if (GetAsyncKeyState(VK_DOWN)) ball_pos_y -= ball_speed;
}
int cMap[30][30] = { //our map
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
void drawRect(float x, float y, float width, float height) {
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x + width, y);
glVertex2f(x + width, y + height);
glVertex2f(x, y + height);
glEnd();
}
GLuint LoadTexture( const char * filename, int width, int height )
{
GLuint texture;
unsigned char * data;
FILE * file;
//The following code will read in our RAW file
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_MODULATE ); //set texture environment parameters
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR );
//Here we are setting the parameter to repeat the texture
//instead of clamping the texture
//to the edge of our shape.
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_REPEAT );
//Generate the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, data);
free( data ); //free the texture
return texture; //return whether it was successfull
}
void FreeTexture( GLuint texture )
{
glDeleteTextures( 1, &texture );
}
void drawTiles (void) { //our function to draw the tiles
for (int i = 0; i < 10; i++) //loop through the height of the map
{
for (int j = 0; j < 10; j++) //loop through the width of the map
{
if (cMap[i][j] == 0) //if the map at this position contains a 0
{
glBindTexture( GL_TEXTURE_2D, texture ); //bind our grass texture to our shape
}
else //otherwise
{
glBindTexture( GL_TEXTURE_2D, texture2 ); //bind our dirt texture to our shape
}
glPushMatrix(); //push the matrix so that our translations only affect this tile
glTranslatef(j, -i, 0); //translate the tile to where it should belong
glBegin (GL_QUADS); //begin drawing our quads
glTexCoord2d(0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0); //with our vertices we have to assign a texcoord
glTexCoord2d(1.0, 0.0);
glVertex3f(1.0, 0.0, 0.0); //so that our texture has some points to draw to
glTexCoord2d(1.0, 1.0);
glVertex3f(1.0, 1.0, 0.0);
glTexCoord2d(0.0, 1.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glPopMatrix(); //pop the matrix
} //end first loop
} //end second loop
}
void draw() {
// clear (has to be done at the beginning)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // ToDo: draw our scene
// draw ball
drawRect(ball_pos_x - ball_width / 2, ball_pos_y - ball_height / 2, ball_width, ball_height);
glEnable( GL_TEXTURE_2D );
glTranslatef(-5, 4, -20); //translate back a bit to view the map correctly
drawTiles(); //draw our tiles
// swap buffers (has to be done at the end)
glutSwapBuffers();
}
void UpdatePlayer(){
// hit by right racket?
if (ball_pos_x < ball_pos_x + ball_width &&
ball_pos_x > enemy_pos_x &&
ball_pos_y < enemy_pos_y + enemy_height &&
ball_pos_y > enemy_pos_y) {
ball_pos_x = ball_pos_x;
ball_pos_y = ball_pos_y;
}
}
void update(int value) { // Call update() again in 'interval' milliseconds
// input handling
keyboard();
UpdatePlayer();
glutTimerFunc(interval, update, 0);
// Redisplay frame
glutPostRedisplay();
}
void enable2D(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, width, 0.0f, height, 0.0f, 1.0f);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}
int _tmain(int argc, char** argv)
{
// initialize opengl (via glut)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height); glutCreateWindow("noobtuts.com Pong");
// Register callback functions
glutDisplayFunc(draw);
glutIdleFunc (draw);
glutReshapeFunc (reshape);
glutTimerFunc(interval, update, 0);
// setup scene to 2d mode and set draw color to white
enable2D(width, height);
glColor3f(1.0f, 1.0f, 1.0f);
//Load our texture
texture = LoadTexture("texture.raw", 256, 256);
texture2 = LoadTexture("texture2.raw", 256, 256);
glutMainLoop ();
//Free our texture
FreeTexture(texture);
FreeTexture(texture2);
return 0;
}
If I was to comment out in the main...
glutIdleFunc (draw);
glutReshapeFunc (reshape);
The tile map will disappear and my moveable square is visible and works...I just can't implement the two together successfully. Again i'm completely new to this so apologise if I am doing something really stupid Any ideas where I am going wrong?

Are you sure you want to draw the tile map with a perspective projection? It seems to me that you should be using an orthographic projection for both the tile map and the ball.
Change your reshape function to simply call enable2D(w, h). Then change your drawTiles function to draw using screen coordinates instead. That should work.
Some additional notes:
Don't forget to disable texturing when drawing your ball (you don't seem to be setting any texture for it, so it will just render with the last texture set).
You could also disable depth testing with glDisable(GL_DEPTH_TEST) since you don't really need it (and use glVertex2f for drawing the tile map instead of glVertex3f)

Related

OpenGL - Load mutliple texures in one function

I've already constructed a 15x15 grid of cubes with glutSolidCube(). Then i have a menu handler in which when I click "Start Game", loads the texture I used to all of the cubes, calling a custom glutSolidCube and having glTexCoord2d before each declaration of vertices, cause we can't have textures on the latter I think. For uploading the texture from an image, I'm using a STB_IMAGE_IMPLEMENTATION implementation having also a header file included. The function loadTextureFromFile(const char *filename) does the loading part.
How can I upload more textures (I want 2 more, in the same loadTextureFromFile() function preferably) and how to handle each texture with the glTexCoord2d()?
Here's my entire code:
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h> // openGL header
#include <GL/glu.h> // glut header
#include <GL/glut.h> // glut header
#define STB_IMAGE_IMPLEMENTATION
/////////////////////////////////////Textures==============================================/////////////////////////////////////
#include "stb_image.h"
GLuint texture; //the array for our texture
void loadTextureFromFile(const char *filename)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
//glShadeModel(GL_FLAT);
//glEnable(GL_DEPTH_TEST);
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// set the texture wrapping/filtering options (on the currently bound texture object)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load and generate the texture
int width, height, nrChannels;
unsigned char *data = stbi_load("paper.bmp", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
//glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
}
void FreeTexture(GLuint texture)
{
glDeleteTextures(1, &texture);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void
drawBox(GLfloat size, GLenum type)
{
static GLfloat n[6][3] =
{
{-1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, -1.0, 0.0},
{0.0, 0.0, 1.0},
{0.0, 0.0, -1.0}
};
static GLint faces[6][4] =
{
{0, 1, 2, 3},
{3, 2, 6, 7},
{7, 6, 5, 4},
{4, 5, 1, 0},
{5, 6, 2, 1},
{7, 4, 0, 3}
};
GLfloat v[8][3];
GLint i;
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2;
for (i = 5; i >= 0; i--) {
glBegin(type);
glNormal3fv(&n[i][0]);
glTexCoord2d(0.0,0.0);
glVertex3fv(&v[faces[i][0]][0]);
glTexCoord2d(0.0,1.0);
glVertex3fv(&v[faces[i][1]][0]);
glTexCoord2d(1.0,1.0);
glVertex3fv(&v[faces[i][2]][0]);
glTexCoord2d(1.0,0.0);
glVertex3fv(&v[faces[i][3]][0]);
glEnd();
}
}
void APIENTRY
myglutSolidCube(GLdouble size)
{
drawBox(size, GL_QUADS);
}
//int red_color[]={255,0,0};
//int blue_colot[]={0,0,255};
//////////////////////////////=========MENU============/////////////
enum MENU_TYPE //menu options-values
{
MENU_START,
MENU_EXIT,
};
//create the menu - Prototype
void my_createmenu(void);
// Menu handling function declaration - Prototype
void menu(int);
void init()
{ //for 3d lighting
glEnable(GL_DEPTH_TEST); //depth test
glEnable(GL_LIGHTING); //enable light from a single source
glEnable(GL_LIGHT0); //enable white light , diffuse and specular components
glEnable(GL_COLOR_MATERIAL); //track the current color
}
void display()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Black and opaque
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//define the projection matrix just once and use the modelview matrix all other times
glMatrixMode(GL_PROJECTION); //Applies subsequent matrix operations to the projection matrix stack
glLoadIdentity();//Reset
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport); //The params parameter returns four values: the x and y window coordinates of the viewport, followed by its width and height
double aspect = (double)viewport[2] / (double)viewport[3]; // y/width would be 1.0
gluPerspective(60,aspect, 1, 100); //using perspective projection
//gluOrtho2D(0.0,600.0,-60.0,600.0);
glMatrixMode(GL_MODELVIEW); //for trasformations - Applies subsequent matrix operations to the texture matrix stack
glLoadIdentity();
// move back a bit for viewer , cause of gluPerspective
glTranslatef( 0, 0, -35 );
float e=0,f=0;
//construct the grid with reference the central cube
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
glPushMatrix();
glTranslatef(0.0f+e,0.0f+f,0.0f); //right and below
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
glColor3ub(245, 245, 220); //Beige
glutSolidCube(2.25);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f-e,0.0f+f,0.0f); //left and below
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
glColor3ub(245, 245, 220); //Beige
glutSolidCube(2.25);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f+e,0.0f-f,0.0f); //right and up
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
glColor3ub(245, 245, 220); //Beige
glutSolidCube(2.25);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f-e,0.0f-f,0.0f); //left and up
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
glColor3ub(245, 245, 220); //Beige
glutSolidCube(2.25);
glPopMatrix();
f += -2.63;
}
f=0;
e+=2.63;
}
glutSwapBuffers(); //implicit glFlush
}
//for the second part of program
void display_game()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Black and opaque
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//define the projection matrix just once and use the modelview matrix all other times
glMatrixMode(GL_PROJECTION); //Applies subsequent matrix operations to the projection matrix stack
glLoadIdentity();//Reset
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport); //The params parameter returns four values: the x and y window coordinates of the viewport, followed by its width and height
double aspect = (double)viewport[2] / (double)viewport[3]; // y/width would be 1.0
gluPerspective(60,aspect, 1, 100); //using perspective projection
//glOrtho(0.0f, 600.0f, 600.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW); //for trasformations - Applies subsequent matrix operations to the texture matrix stack
glLoadIdentity();
// move back a bit for viewer , cause of gluPerspective
glTranslatef( 0, 0, -35 );
float e=0,f=0;
glEnable(GL_TEXTURE_2D);
//construct the grid with reference the central cube
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
glPushMatrix();
glTranslatef(0.0f+e,0.0f+f,0.0f); //right and below
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
//glColor3ub(245, 245, 220); //Beige
//glColor3ub( rand()%255,rand()%255, rand()%255 );
myglutSolidCube(2.25);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f-e,0.0f+f,0.0f); //left and below
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
//glColor3ub( rand()%255,rand()%255, rand()%255 );
myglutSolidCube(2.25);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f+e,0.0f-f,0.0f); //right and up
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
myglutSolidCube(2.25);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f-e,0.0f-f,0.0f); //left and up
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
myglutSolidCube(2.25);
glPopMatrix();
f += -2.63;
}
f=0;
e+=2.63;
}
//SEED to some constant value for 2nd part of the program. If it is not used , cubes would change color in runtime
//srand(0x98765432);
glutSwapBuffers(); //implicit glFlush
glDisable(GL_TEXTURE_2D);
}
void reshape(GLsizei width, GLsizei height) {
// GLsizei for non-negative integer // Compute aspect ratio of the new window
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height; // Set the viewport to cover the new window
glViewport(0, 0, width, height); // Set the aspect ratio of the clipping volume glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset // Enable perspective projection with fovy, aspect, zNear and zFar
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}
void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
void mouseEscape( int button, int state, int x, int y )
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN &&button==MENU_EXIT)
{
int windowID = glutCreateWindow("CUBES");
glutDestroyWindow(windowID);
exit(0);
}
glutPostRedisplay();
}
//for loading the texture
const char* filename = "salt_on_spoon.bmp";
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(600,600);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE| GLUT_MULTISAMPLE);
glEnable(GL_MULTISAMPLE); //anti-alliasing
glutCreateWindow("CUBES");
//create and handle the menu
my_createmenu();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
init();
//glEnable(GL_TEXTURE_2D); //for texture
//glutMouseFunc(mouseEscape);
//glutKeyboardFunc(keyEscape);
//Load our texture
//loadTextureFromFile(filename);
glutMainLoop();
//Free our texture
//FreeTexture(texture);
return 0;
}
//create the menu-entries
void my_createmenu(void) {
// Create a menu
glutCreateMenu(menu);
// Add menu items
glutAddMenuEntry("Start Game", MENU_START);
glutAddMenuEntry("Exit", MENU_EXIT);
// Associate a mouse button with menu
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
// Menu handling function-what to do in each value
void menu(int item)
{
switch (item)
{
case MENU_START: {
//glEnable(GL_TEXTURE_2D); //for texture
//Load our texture
loadTextureFromFile(filename);
glutDisplayFunc(display_game);
}
break;
case MENU_EXIT:
{
int windowID = glutCreateWindow("CUBES"); //exit game
glutDestroyWindow(windowID);
exit(0);
}
break;
default:
{ /* Nothing */ }
break;
}
glutPostRedisplay();
return;
}
I'm doing the texture loading part in the menu function. How will I be able to handle three textures? The ultimate goal is to make a rand call also for the three textures to be rendered on the cubes.
I also have two pictures: 1st: when the program begins:
2nd: after clicking "Star Game" where you can see the texture rendered in all of the cubes:
The goal is for more 2 types of textures and all of them render in random cubes.
You can create more than 1 texture object.
glBindTexture binds a named texture to a texturing target, that is a global state. glTexImage2D specify a two-dimensional texture image for the texture, which is currently bound to the specified target. glTexParameter set parameter to the texture object.
I recommend to write a function which loads a texture form a file to a given texture object (name id):
void loadTextureFromFile(const char *filename, unsigned int texture)
{
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load and generate the texture
int width, height, nrChannels;
unsigned char *data = stbi_load(filename, &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, data);
//glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
}
const char* filename1 = "salt_on_spoon.bmp";
const char* filename2 = ...;
const char* filename3 = ...;
unsigned int tob[3];
int main(int argc, char **argv)
{
// [...]
glGenTextures(3, &tob[0]);
loadTextureFromFile(filename1, tob[0]);
loadTextureFromFile(filename2, tob[1]);
loadTextureFromFile(filename3, tob[2]);
// [...]
}
When two-dimensional texturing is enabled, then the image of the texture object, which is currently bound to the target GL_TEXTURE_2D is wrapped on the mesh.
You've to bind the proper texture object, before you draw the geometry. e.g:
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
glBindTexture(GL_TEXTURE_2D, tob[0]);
glPushMatrix();
glTranslatef(0.0f+e,0.0f+f,0.0f); //right and below
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
myglutSolidCube(2.25);
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, tob[1]);
glPushMatrix();
glTranslatef(0.0f-e,0.0f+f,0.0f); //left and below
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
myglutSolidCube(2.25);
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, tob[2]);
glPushMatrix();
glTranslatef(0.0f+e,0.0f-f,0.0f); //right and up
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
myglutSolidCube(2.25);
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, tob[0]);
glPushMatrix();
glTranslatef(0.0f-e,0.0f-f,0.0f); //left and up
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
myglutSolidCube(2.25);
glPopMatrix();
f += -2.63;
}
f=0;
e+=2.63;
}
Note, the distribution of the textures is just an example. You've to ensure that unsigned int tob[3]; is declared before, in global namespace.

How to fill the deph buffer in OpenGL 1 with a depth texture?

I separate moveable 3d object and non moveable 3d. I can generate 2 textures for non moveables : background color texture and background depth texture . I have no problem. The values are ok for both. I check the value with a gray gradient texture for the depth.
And I would like to write / fill the color buffer and the depth buffer in opengl with those texture but the depth doesn't work (the color buffer is fine on the screen).
My goal is to never recompute the non movable 3d objects when the camera doesn't move.
(rem : The depth buffer bits size is always in 16 bits)
This is my code for generate the depth texture :
U16 m_backgroundDepthBuffer[4096 * 4096]; // it's a member of my class
glReadPixels(0,0, width(), height(), GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, (void*)m_backgroundDepthBuffer);
// Allocate GPU-memory for the depth-texture.
glDeleteTextures(1, &m_backgroundDepthTextureId);
glGenTextures(1, &m_backgroundDepthTextureId);
glBindTexture(GL_TEXTURE_2D, m_backgroundDepthTextureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16,
width(), height(), 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, m_backgroundDepthBuffer);
This is my code for filling the color buffer :
void Application::Render()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
// render background
Render::RenderImage(m_backgroundTextureId, 0,0, width(), height());
// for debug
// Render::RenderImage(m_backgroundDepthGrayTextureId, 0,0, width(), height());
// This is how I fill the depth buffer : it's work but slow
glDrawPixels(width(), height(), GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, (GLvoid*) m_backgroundDepthBuffer); //
// doesn't work
// Render::RenderDepth(m_backgroundDepthTextureId, 0,0, width(), height());
}
This is my Render::RenderImage() : it works
void Render::RenderImage(U32 tex, int x, int y, int w, int h, float anchorX, float anchorY)
{
glClear(GL_DEPTH_BUFFER_BIT);
GLboolean depth = glIsEnabled(GL_DEPTH_TEST);
if (depth)
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, s_width, s_height, 0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3ub(255, 255, 255);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
x -= (int)(anchorX * w);
y -= (int)(anchorY * h);
gVertices[0] = VECTOR3F(x, y, 0);
gVertices[1] = VECTOR3F(x + w - 1, y, 0);
gVertices[2] = VECTOR3F(x + w - 1, y + h - 1, 0);
gVertices[3] = VECTOR3F(x, y + h - 1, 0);
gTexCoords[0] = VECTOR2F(0, 1);
gTexCoords[1] = VECTOR2F(1, 1);
gTexCoords[2] = VECTOR2F(1, 0);
gTexCoords[3] = VECTOR2F(0, 0);
gIndexes[0] = 2;
gIndexes[1] = 1;
gIndexes[2] = 0;
gIndexes[3] = 0;
gIndexes[4] = 3;
gIndexes[5] = 2;
glVertexPointer(3, GL_FLOAT, 0, gVertices);
glTexCoordPointer(2, GL_FLOAT, 0, gTexCoords);
glDrawElements(GL_TRIANGLES, 3 * 2, GL_UNSIGNED_SHORT, gIndexes);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
if (depth)
glEnable(GL_DEPTH_TEST);
}
This is my Render::RenderDepth() : it doesn't work / I have a texture with depth = 0.0f instead to use the depth of each texel.
I want only to change the depth buffer and do nothing to the color buffer
Render::RenderDepth() is similar to Render::RenderImage() : I render 2 triangles
void Render::RenderDepth(U32 tex, int x, int y, int w, int h, float anchorX, float anchorY)
{
glClear(GL_DEPTH_BUFFER_BIT);
/*glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_DEPTH_TO_TEXTURE_EXT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_ALWAYS);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_DEPTH_TEXTURE_MODE, GL_ALPHA);
*/
GLboolean depth = glIsEnabled(GL_DEPTH_TEST);
//if (depth)
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, s_width, s_height, 0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3ub(255, 255, 255);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
x -= (int)(anchorX * w);
y -= (int)(anchorY * h);
gVertices[0] = VECTOR3F(x, y, 0.0);
gVertices[1] = VECTOR3F(x + w - 1, y, 0.0);
gVertices[2] = VECTOR3F(x + w - 1, y + h - 1, 0.0);
gVertices[3] = VECTOR3F(x, y + h - 1, 0.0);
gTexCoords[0] = VECTOR2F(0, 1);
gTexCoords[1] = VECTOR2F(1, 1);
gTexCoords[2] = VECTOR2F(1, 0);
gTexCoords[3] = VECTOR2F(0, 0);
gIndexes[0] = 2;
gIndexes[1] = 1;
gIndexes[2] = 0;
gIndexes[3] = 0;
gIndexes[4] = 3;
gIndexes[5] = 2;
glVertexPointer(3, GL_FLOAT, 0, gVertices);
glTexCoordPointer(2, GL_FLOAT, 0, gTexCoords);
glDrawElements(GL_TRIANGLES, 3 * 2, GL_UNSIGNED_SHORT, gIndexes);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}

I can't display a texture on a quad

I am trying to wrap a texture on a quad.
All I see is a white rectangle:
To load the texture I used freeimage.
I need help in order to fix this very simple demo:
#include <GL/glut.h>
#include <GL/gl.h>
#include <FreeImage.h>
#include <stdio.h>
GLfloat coordinates[] =
{
-0.5, 0.5, 1,
-0.5, -0.5, 0,
0.5, -0.5, 0,
0.5, 0.5, 0
};
GLfloat texCoords[] =
{
0, 1,
0, 0,
1, 0,
1, 1
};
BYTE* data;
FIBITMAP* bitmap;
GLuint texture;
void initGlutCallbacks();
void initGL();
void onReshape(int w, int h);
void display();
FIBITMAP* loadTexture(const char* fileName);
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(512, 512);
glutInitWindowPosition(64, 64);
glutCreateWindow("arrays");
initGlutCallbacks();
initGL();
// texture
bitmap = loadTexture("rufol.png");
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
data = FreeImage_GetBits(bitmap);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0,
GL_RGBA8, GL_UNSIGNED_BYTE,
data
);
// enable arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// specifying data for the arrays
glVertexPointer
(
3, GL_FLOAT, 0, coordinates
);
glTexCoordPointer
(
2, GL_FLOAT, 0, texCoords
);
glutMainLoop();
}
void initGlutCallbacks(){
glutReshapeFunc(onReshape);
glutDisplayFunc(display);
}
void initGL(){
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glEnable ( GL_TEXTURE_2D );
}
void onReshape(int w, int h){
}
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTexEnvf(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glDrawArrays(GL_QUADS, 0, 4);
glFlush();
glutSwapBuffers();
}
FIBITMAP* loadTexture(const char* fileName){
FIBITMAP *bitmap = FreeImage_Load(FIF_PNG, "rufol.png");
if(bitmap == 0) printf("error loading the image\n");
FIBITMAP *fbitmap = FreeImage_ConvertTo32Bits(bitmap);
FreeImage_Unload(bitmap);
return fbitmap;
}
As you can see I am not even using perspective. Also lighting is not enabled(I don't know if it is required to display textures). I have tested a very similar code but using colors for each vertex instead of texture coordinates and it worked. So I think it might be something wrong when loading the image.
Have you tried using GL_RGBA instead of GL_RGBA8 as second parameter (format)?

Need some hand-holding to render, display and save an image

I've previously asked how I might be able to display my colour buffer and also save it to disk, and the answer I got was that I should;
Render to a FBO and use glReadPixels() to slurp images out of that instead of the front buffer.
How can I generate a screenshot when glReadPixels is empty?
However, I've read a bit about framebuffers and am still totally confused, so I thought I would ask about how to do this on SO. My code does something like this:
/* Callback handler for window re-paint event */
void display()
{
glClear(GL_COLOR_BUFFER_BIT); //Clear the color buffer
glMatrixMode(GL_MODELVIEW); //To operate on the model-view matrix
// do some rendering
glFlush(); // display
}
when I want to save the image at any point, I run this:
std::unique_ptr<RGBA2D> GrabScreen()
{
//we get the width/height of the screen into this array
GLint screen[4];
//get the width/height of the window
glGetIntegerv(GL_VIEWPORT, screen);
GLint width = screen[2];
GLint height = screen[3];
std::unique_ptr<RGBA2D> pixels(new RGBA2D(height, width * 4));
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels->data());
return std::move(pixels);
}
Note that RGBA2D is a 2D eigen vector object (not important). This all works fine, except that it only saves the image if it is being displayed. I want to be able to run my program on a unix machine without a display. I want to render to an FBO. How do I do this?
Not hand-holding, but I can hopefully point you in the right direction.
You will use glGenFramebuffers and glBindFramebuffer to create and bind a Framebuffer Object (FBO).
Then you have a choice if you want to render to a texture or to a renderbuffer. For your purpose, either one will work. The renderbuffer is easier, IMHO. Use glGenRenderbuffers, glBindRenderbuffer and glRenderbufferStorage to set up your color renderbuffer.
Then you attach your color renderbuffer to the FBO, using glFramebufferRenderbuffer.
If you need a depth buffer, repeat the previous two steps to create and attach another renderbuffer used as the depth buffer for your FBO rendering.
Then do your rendering, and grab the frame with glReadPixels.
All these calls are documented in the man pages at www.opengl.org. If you search for the keywords and some of the function names, you should also be able to find some full code samples.
Recently I had a small discussion on the Wayland devel maillist where I wanted to demonstrate how FBOs don't get updated when the X-Server managing the GPU is not holding the VT. Anyway, for demonstration purposes I hacked a quick and dirty program from various sources I had around, that renders to an FBO in a loop and writes the created picture out to a file. It's not optimized for performance, but does what you're interested in, so I drop the source code here (note that the malloc for the readout buffer misses a paired free, so there's a nongrowing memory leak in there)
// framebuffer continuous dump demonstrator
//
// build:
// c++ -o test_fbo test_fbo.cpp -lm -lGL -lGLU -lglut -lGLEW
#include <GL/glew.h>
#include <GL/glut.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
using namespace std;
namespace render
{
int width, height;
float aspect;
void init();
void reshape(int width, int height);
void display();
int const fbo_width = 512;
int const fbo_height = 512;
GLuint fb, color, depth;
void *dumpbuf;
int dumpbuf_fd;
};
void idle();
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("FBO test");
glutDisplayFunc(render::display);
glutReshapeFunc(render::reshape);
glutIdleFunc(idle);
glewInit();
render::init();
glutMainLoop();
return 0;
}
void idle()
{
glutPostRedisplay();
}
void CHECK_FRAMEBUFFER_STATUS()
{
GLenum status;
status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
switch(status) {
case GL_FRAMEBUFFER_COMPLETE:
break;
case GL_FRAMEBUFFER_UNSUPPORTED:
/* choose different formats */
break;
default:
/* programming error; will fail on all hardware */
throw "Framebuffer Error";
}
}
namespace render
{
float const light_dir[]={1,1,1,0};
float const light_color[]={1,0.95,0.9,1};
void init()
{
glGenFramebuffers(1, &fb);
glGenTextures(1, &color);
glGenRenderbuffers(1, &depth);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glBindTexture(GL_TEXTURE_2D, color);
glTexImage2D( GL_TEXTURE_2D,
0,
GL_RGB8,
fbo_width, fbo_height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
glBindRenderbuffer(GL_RENDERBUFFER, depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, fbo_width, fbo_height);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
GLint red_bits, green_bits, blue_bits, alpha_bits;
glGetIntegerv(GL_RED_BITS, &red_bits);
glGetIntegerv(GL_GREEN_BITS, &green_bits);
glGetIntegerv(GL_BLUE_BITS, &blue_bits);
glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
fprintf(stderr, "FBO format R%dG%dB%dA%d\n",
(int)red_bits,
(int)green_bits,
(int)blue_bits,
(int)alpha_bits );
CHECK_FRAMEBUFFER_STATUS();
dumpbuf_fd = open("/tmp/fbodump.rgb", O_CREAT|O_SYNC|O_RDWR, S_IRUSR|S_IWUSR);
assert(-1 != dumpbuf_fd);
dumpbuf = malloc(fbo_width*fbo_height*3);
assert(dumpbuf);
}
void reshape(int width, int height)
{
render::width=width;
render::height=height;
aspect=float(width)/float(height);
glutPostRedisplay();
}
void prepare()
{
static float a=0, b=0, c=0;
glBindTexture(GL_TEXTURE_2D, 0);
glEnable(GL_TEXTURE_2D);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glViewport(0,0,fbo_width, fbo_height);
glClearColor(1,1,1,0);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 1, 1, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glLightfv(GL_LIGHT0, GL_POSITION, light_dir);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color);
glTranslatef(0,0,-5);
glRotatef(a, 1, 0, 0);
glRotatef(b, 0, 1, 0);
glRotatef(c, 0, 0, 1);
glutSolidTeapot(0.75);
a=fmod(a+0.1, 360.);
b=fmod(b+0.5, 360.);
c=fmod(c+0.25, 360.);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(0,0,fbo_width,fbo_height,GL_RGB,GL_UNSIGNED_BYTE,dumpbuf);
lseek(dumpbuf_fd, SEEK_SET, 0);
write(dumpbuf_fd, dumpbuf, fbo_width*fbo_height*3);
}
void intermediary()
{
}
void final()
{
static float a=0, b=0, c=0;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0, width, height);
glClearColor(1.,1.,1.,0.);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, aspect, 1, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-5);
glRotatef(b, 0, 1, 0);
b=fmod(b+0.5, 360.);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, color);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_LIGHTING);
float cube[][5]=
{
{-1, -1, -1, 0, 0},
{ 1, -1, -1, 1, 0},
{ 1, 1, -1, 1, 1},
{-1, 1, -1, 0, 1},
{-1, -1, 1, -1, 0},
{ 1, -1, 1, 0, 0},
{ 1, 1, 1, 0, 1},
{-1, 1, 1, -1, 1},
};
unsigned int faces[]=
{
0, 1, 2, 3,
1, 5, 6, 2,
5, 4, 7, 6,
4, 0, 3, 7,
3, 2, 6, 7,
4, 5, 1, 0
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 5*sizeof(float), &cube[0][0]);
glTexCoordPointer(2, GL_FLOAT, 5*sizeof(float), &cube[0][3]);
glCullFace(GL_BACK);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, faces);
glCullFace(GL_FRONT);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, faces);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
void display()
{
prepare();
intermediary();
final();
glutSwapBuffers();
}
}

Tiling Mapping OpenGL C++

I'm trying implement tile mapping using openGL and C++.
I found this
tutorial/explanation which I thought was pretty good.
When I compile and run the code in visual studio 2010 it just opens a window and displays a white square.
Maybe my files aren't loading correctly. I've tried the .raw files the website uses as well as my own .png files.
#include <GL/gl.h>
#include <GL/glut.h>
#include <windows.h>
#include <stdio.h>
GLuint texture; //the array for our texture
GLuint texture2; //the array for our second texture
int cMap[10][10] = { //our map
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
//function to load the RAW file
GLuint LoadTexture( const char * filename, int width, int height )
{
GLuint texture;
unsigned char * data;
FILE * file;
//The following code will read in our RAW file
file = fopen( filename, “rb” );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture )
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_MODULATE ); //set texture environment parameters
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR );
//Here we are setting the parameter to repeat the texture
instead of clamping the texture
//to the edge of our shape.
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_REPEAT );
//Generate the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, data);
free( data ); //free the texture
return texture; //return whether it was successfull
}
void FreeTexture( GLuint texture )
{
glDeleteTextures( 1, &texture );
}
void drawTiles (void) { //our function to draw the tiles
for (int i = 0; i < 10; i++) //loop through the height of the map
{
for (int j = 0; j < 10; j++) //loop through the width of the map
{
if (cMap[i][j] == 0) //if the map at this position contains a 0
{
glBindTexture( GL_TEXTURE_2D, texture ); //bind our grass texture to our shape
}
else //otherwise
{
glBindTexture( GL_TEXTURE_2D, texture2 ); //bind our dirt texture to our shape
}
glPushMatrix(); //push the matrix so that our translations only affect
this tile
glTranslatef(j, -i, 0); //translate the tile to where it should belong
glBegin (GL_QUADS); //begin drawing our quads
glTexCoord2d(0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0); //with our vertices we have to assign a texcoord
glTexCoord2d(1.0, 0.0);
glVertex3f(1.0, 0.0, 0.0); //so that our texture has some points to draw to
glTexCoord2d(1.0, 1.0);
glVertex3f(1.0, 1.0, 0.0);
glTexCoord2d(0.0, 1.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glPopMatrix(); //pop the matrix
} //end first loop
} //end second loop
}
void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glEnable( GL_TEXTURE_2D );
glTranslatef(-5, 4, -20); //translate back a bit to view the map correctly
drawTiles(); //draw our tiles
glutSwapBuffers();
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (“A basic OpenGL Window“);
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
//Load our texture
texture = LoadTexture(“texture.raw”, 256, 256);
texture2 = LoadTexture(“texture2.raw”, 256, 256);
glutMainLoop ();
//Free our texture
FreeTexture(texture);
FreeTexture(texture2);
return 0;
}
Make sure your working directory is what you're assuming it to be.
Or use absolute file names.
Otherwise fopen() will fail because it can't find the file.