I have a problem. I'm using Xcode and OpenGL for a graphing project.
I find the following problem: The images are not shown on the screen. Only the picture where I want to place the texture is drawn. My code is as follows:
Header: Image.hpp
#ifndef IMAGEN_HPP_INCLUDED
#define IMAGEN_HPP_INCLUDED
// Especificacion de Image que nos ayudara a cargar y mostrar las imagenes
class Image
{
private:
unsigned char* Data;
int ancho;
int alto;
public:
// Constructor con parametros de Ancho y Alto que son de la imagen real y
// la direccion de la imagen
Image(int iw, int ih, const char* path)
{
this->ancho = iw;
this->alto = ih;
CargarImagen(path);
}
// Funcion para cargar una imagen en memoria
int CargarImagen(const char* path)
{
FILE *imagen = fopen(path, "r");
int InfoTam = this->ancho * this->alto * 3;
this->Data = (unsigned char*) malloc( InfoTam );
if( imagen == NULL ){
cout<<"ERROR"<<endl;
return 0; }
else{
cout<<"Imagen cargada"<<endl;
}
fread(this->Data, InfoTam, 1, imagen);
fclose(imagen);
return 1;
}
// Dibujar la imagen que esta en memoria
void Dibujar(int x, int y, int w, int h)
{
glColor3f(1.0f, 1.0f, 1.0f);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, this->ancho, this->alto, 0, GL_RGB, GL_UNSIGNED_BYTE, Data);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex2f(x, y);
glTexCoord2f(1.0, 1.0); glVertex2f(x + w, y);
glTexCoord2f(1.0, 0.0); glVertex2f(x + w, y + h);
glTexCoord2f(0.0, 0.0); glVertex2f(x, y + h);
glEnd();
}
};
#endif
And this is the block the main file:
#include <iostream>
#include <GLUT/GLUT.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h> //New
#include <time.h>
#include <cstdio>
using namespace std;
#include "image.hpp"
#define pi 3.1415962
int ancho=700, alto=700;
float cx, cy=70; //Posición inicial. Donde empieza a caer la basura
int xpos, ypos, caida=1; //Posicion del mouse
int puntaje=0;
//Declaracion de imagenes
Image* BotePapel = new
Image(256,256,"/Users/luissegovia/Documents/Recycle.raw");
Image* Fondo = new
Image(2048,2048,"/Users/luissegovia/Documents/Background.raw");
//Funcion de display
void CodeFun(){
glClear(GL_COLOR_BUFFER_BIT); //Limpia Buffer
glPointSize(100); //Tamaño del punto
//----------
glEnable(GL_TEXTURE_2D);
BotePapel->Dibujar(3, 1, 10, 10);
glDisable(GL_TEXTURE_2D);
//Fondo->Dibujar(0, 0, 700, 700);
//
glFlush();
}
int main(int argc, char *argv[]) {
//Parametros de inicio de OpenGL
glutInit(&argc,argv); //Inicializar libreria Glut
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); //Establece modo de
visualizacion incial
glutInitWindowSize(ancho,alto); //Tamanio de ventana
glutInitWindowPosition(0,0); //Posicion de la ventana
glutCreateWindow("Prototipo Juego 2D"); //Titulo de ventana
glClearColor(1,0.5,0.5,1); //Color de fondo
gluOrtho2D(0,70,0,70);//Inicializacion del plano (1er cuadrante)
glutDisplayFunc(CodeFun);
glutMainLoop();
return 0;
}
Only the white box is drawn. What will it be? Thank you very much for your help in advance. Greetings!