Hi thank you for stopping by the question I’m new to openGl and I’m trying to do a video Game , i want to put a ortho cam over a 3d scene so i can put the score and lives but when i put the ortho cam the 3d scene is ruined. ill attach my code under sorry for the mess but I’m new.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "targa.h"
#include "glm.h"
#include <time.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
bool* teclaApretada = new bool[256];
int cont=0;
// angulo de la camara
float angle=0.0;
// direccion
float lx=0.0f,lz=-1.0f;
// posicion
float x=50.0f,z=-1.0f, y = 1.5f;
int i = 0;
GLubyte posColision = 0;
//balas
int numbala;
GLMmodel* balon;
int pantallaSelec = 1;
int vidaJugador = 5;
float deltaAngle = 0.0f;
GLubyte *data,*data2,*dataMapa,*data3;
struct bala{
float velocidad = 0.5;
float x, z;
float lx, lz;
bool active = false;
};
bool colocoEnemigos = false;
struct enemigo{
int vida;
float x, z;
float lx, lz;
bool vivo = true;
float angulo;
};
struct bala balas[99];
struct enemigo enemigos[10];
void dibujaBala(float lx, float lz, float x, float z);
void reshape(int w, int h) {
if(pantallaSelec == 0){
glViewport(0, 0, w, h); //actualizar la ventana
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 2, 0, 2, -10, 10);
}else if(pantallaSelec == 1){
glViewport(0, 0, w, h);
if (h == 0)
h = 1;
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, ratio, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
}
void init(){
glEnable(GL_TEXTURE_2D);
GLint x,y,d;
//cargo en texture 1 pido
data=LoadTGA("./texturas/tierra.tga",&x,&y,&d);
glBindTexture(GL_TEXTURE_2D,1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,x,y,0, GL_RGB,GL_UNSIGNED_BYTE,data);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
delete data;
// fin cargo piso
//inicio cargo paredes
data2=LoadTGA("./texturas/pared.tga",&x,&y,&d);
glBindTexture(GL_TEXTURE_2D,2);
glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,x,y,0, GL_RGB,GL_UNSIGNED_BYTE,data2);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
delete data2;
//fin cargo paredes
//inicio cargo menuPrincipal
data3=LoadTGA("./texturas/inicio.tga",&x,&y,&d);
glBindTexture(GL_TEXTURE_2D,3);
glPixelStorei(GL_UNPACK_ALIGNMENT, 3);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,x,y,0, GL_RGB,GL_UNSIGNED_BYTE,data3);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
delete data3;
//fin cargo menuPrincipal
balon = glmReadOBJ("./objetos/test.obj");
dataMapa=LoadTGA("./texturas/laberinto.tga",&x,&y,&d);
GLubyte valor;
for( int idy=0 ; idy<y ;idy++ )
{
for( int idx=0 ; idx<y ;idx++ )
{
//int indice = idy*x*3 + idx*3 + 0;
valor = dataMapa[ idy*x*3 + idx*3 + 0 ];
//printf("%d:%d
",indice,valor);
}
//printf("
");
}
//delete dataMapa;
}
//------------------------------------------------
// Balas
//------------------------------------------------
void disparar(){
for (int i = 0; i<100; i++) {
if(balas[i].active == false){
numbala = i;
break;
}
else{
numbala = 999;
}
}
if(numbala != 999){
balas[numbala].active = true;
balas[numbala].lx = lx;
balas[numbala].lz = lz;
balas[numbala].x = x;
balas[numbala].z = z;
printf("entre a disparar");
//dibujaBala(balas[numbala].lx,balas[numbala].lz,balas[numbala].x,balas[numbala].z);
}else{
printf("ya no tienes balas");
}
}
//------------------------------------------------
// Enemigos
//------------------------------------------------
void colocarEnemigos(){
srand (time(NULL));
for(int i = 0; i < 4; i++){
enemigos[i].vida = 5;
enemigos[i].x = rand() % 100 + 1;
enemigos[i].z = (rand() % 100 + 1)*-1;
enemigos[i].angulo = rand() % 90 + 1;
printf("posX: %f, posZ: %f, ang: %f
",enemigos[i].x,enemigos[i].z,enemigos[i].angulo);
}
}
//------------------------------------------------
// moverse mouse y teclado
//------------------------------------------------
void Keyboard(unsigned char key, int r, int y){
if(pantallaSelec == 0){
switch (key) {
case 's':
pantallaSelec = 1;
//glLoadIdentity();
}
}else if(pantallaSelec == 1){
int indice;
GLubyte valors;
switch (key) {
case 'a' :
teclaApretada[key] = true;
break;
case 'd' :
indice = floor((z)*-1)*100*3 + floor(x+lx+3.14)*3 + 0;
valors = dataMapa[ indice];
if(valors != 255){
printf("pared");
}else{
teclaApretada[key] = true;
}
break;
case 'w' :
indice = floor((z+lz)*-1)*100*3 + floor(x+lx)*3 + 0;
valors = dataMapa[ indice];
if(valors != 255){
printf("pared");
}else{
teclaApretada[key] = true;
}
printf("valorx: %f valorFlorx: %f valorz:%f valor: %d indice: %d
",x,floor(x),floor(z*-1),valors,indice);
break;
case 's' :
indice = floor((z-lz)*-1)*100*3 + floor(x-lx)*3 + 0;
valors = dataMapa[ indice];
if(valors != 255){
printf("pared");
}else{
teclaApretada[key] = true;
}
break;
case' ':
teclaApretada[key] = true;
disparar();
break;
}
}
}
void KeyboardUp(unsigned char key, int r, int y){
if(pantallaSelec == 0){
}else if(pantallaSelec == 1){
switch (key) {
case 'a':
teclaApretada[key] = false;
break;
case 'd':
teclaApretada[key] = false;
break;
case 'w':
teclaApretada[key] = false;
break;
case 's':
teclaApretada[key] = false;
break;
case' ':
teclaApretada[key] = false;
break;
}
}
}
void moverse(){
int indice;
GLubyte valors;
float velocidad = 0.1f;
if (teclaApretada['w']) {
indice = floor((z+lz)*-1)*100*3 + floor(x+lx)*3 + 0;
valors = dataMapa[ indice];
if(valors != 255){
printf("pared");
}else{
x += lx * velocidad;
z += lz * velocidad;
}
printf("valorx: %f valorFlorx: %f valorz:%f valor: %d indice: %d
",x,floor(x),floor(z*-1),valors,indice);
}
if (teclaApretada['s']) {
indice = floor((z-lz)*-1)*100*3 + floor(x-lx)*3 + 0;
valors = dataMapa[ indice];
if(valors != 255){
printf("pared");
}else{
x -= lx * velocidad;
z -= lz * velocidad;
}
printf("valorx: %f valorFlorx: %f valorz:%f valor: %d indice: %d
",x,floor(x),floor(z*-1),valors,indice);
}
if (teclaApretada['d']) {
//angle += 0.003f;
velocidad = .01;
indice = floor((z)*-1)*100*3 + floor(x+lx+3.14)*3 + 0;
valors = dataMapa[ indice];
if(valors != 255){
printf("pared");
}else{
x += (lx+3.14) * velocidad;
}
printf("angle:%f
",angle);
}
if (teclaApretada['a']) {
velocidad = .01;
indice = floor((z)*-1)*100*3 + floor(x+lx-3.14)*3 + 0;
valors = dataMapa[ indice];
if(valors != 255){
printf("pared");
}else{
x += (lx-3.14) * velocidad;
}
//lx = sin(angle);
//lz = -cos(angle);
//printf("angle:%f
",angle);
//printf("lx: %f
lz:%f
",lx,lz);
}
}
void mouseMove(int x, int y) {
//update deltaAngle
deltaAngle = (x-721)*(.00436/*157/35950*/);
//update camera's direction
lx = sin(angle + deltaAngle);
lz = -cos(angle + deltaAngle);
}
void dibujaParedes(float xInicial, float zInicial, float xFinal, float zFinal){
glBegin(GL_QUADS);
glTexCoord2f(0,1);
glVertex3f(xInicial, 4, zInicial);//arriba izq
glTexCoord2f(0,0);
glVertex3f(xInicial, 0, zInicial);//abajo izq
glTexCoord2f(1,0);
glVertex3f(xFinal, 0, zFinal);//abajo der
glTexCoord2f(1,1);
glVertex3f(xFinal, 4, zFinal);//arriba der
glEnd();
}
void dibujaPiso(){
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(0, 0.0f, 0);
glTexCoord2f(1,0);
glVertex3f(100.0f, 0.0f, 0);
glTexCoord2f(1,1);
glVertex3f(100.0f, 0.0f, -100.0f);
glTexCoord2f(0,1);
glVertex3f(0.0f, 0.0f, -100.0f);
}
//------------------------------------------------
// dibuja la pantalla de inicio
//------------------------------------------------
void principal(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//glColor3f(1, 0, 0);
glBindTexture(GL_TEXTURE_2D,3);
glBegin(GL_QUADS);
glTexCoord2f(1, 1);
glVertex2f(-2, -2);
glTexCoord2f(0, 0);
glVertex2f(-2, 2);
glTexCoord2f(1, 0);
glVertex2f(2, 2);
glTexCoord2f(0, 1);
glVertex2f(2, -2);
glEnd();
//glutSolidCube(1.0);
}
void ataqueEnemigo(){
for (int i = 0; i < 3; i++) {
if (enemigos[i].vivo == true) {
float resX = enemigos[i].x - x;
float resZ = enemigos[i].z - z;
if(resX < .2 && resZ < .2 && resX > .1 && resZ < .1){
vidaJugador--;
x = x-1;
z = z- 1;
printf("me pego");
}
}
}
if(vidaJugador == 0){
printf("jugador Muerto");
}
}
void balasEnemigo(){
for (int i = 0 ; i < 99; i++) {
if(balas[i].active == true){
for (int j = 0; j < 3; j++) {
if (enemigos[j].vivo == true) {
float resX = enemigos[j].x - balas[i].x;
float resZ = enemigos[j].z - balas[i].z;
if(resX < .2 && resZ < .2 && resX > .1 && resZ < .1){
enemigos[j].vida--;
balas[i].active = false;
printf("le pegue
");
if(enemigos[j].vida == 0){
enemigos[j].vivo = false;
}
}
}
}
}
}
}
//------------------------------------------------
// dibuja el juego
//------------------------------------------------
void game(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt( x, 1.0f, z,
x+lx, 1.0f, z+lz,
0.0f, 1.0f, 0.0f);
moverse();
glBindTexture(GL_TEXTURE_2D,1);
dibujaPiso();
glEnd();
//------------------------------------------------
// pared Perimetral
//------------------------------------------------
glBindTexture(GL_TEXTURE_2D,2);
dibujaParedes(0, 0, 100, 0);
dibujaParedes(0, 0, 0, -100);
dibujaParedes(100, 0, 100, -100);
dibujaParedes(0, -100, 100, -100);
if(colocoEnemigos == false){
colocarEnemigos();
colocoEnemigos = true;
}
for(int i = 0; i< 99; i++){
int indice;
GLubyte valors;
//glLoadIdentity();
if(balas[i].active == true){
//printf("entre a dibujar");
glColor3f(190, 190, 190);
glPushMatrix();
printf("posxbala: %f, pos zbala %f
",balas[i].x,balas[i].z);
glTranslatef(balas[i].x, 0, balas[i].z);
glutSolidSphere(1, 50, 50);
balas[i].x += balas[i].lx * balas[i].velocidad;
balas[i].z += balas[i].lz * balas[i].velocidad;
indice = floor((balas[i].z+balas[i].lz)*-1)*100*3 + floor(balas[i].x+balas[i].lx)*3 + 0;
valors = dataMapa[ indice];
if(valors != 255){
balas[i].active = false;
printf("pared");
}
glPopMatrix();
}
}
for (int i = 0; i < 3 ; i++) {
int indice;
GLubyte valors;
if (enemigos[i].vivo == true) {
glPushMatrix();
glTranslatef(enemigos[i].x, 0, enemigos[i].z);
glmDraw(balon, GLM_TEXTURE | GLM_SMOOTH | GLM_MATERIAL);
//glutSolidCube(1.2);
//enemigos[i].x += sin(enemigos[i].angulo)*.01;
//enemigos[i].z += -cos(enemigos[i].angulo)*.01;
indice = floor((enemigos[i].z+-cos(enemigos[i].angulo)*.1))*100*3 + floor(balas[i].x+sin(enemigos[i].angulo)*.1)*3 + 0;
valors = dataMapa[ indice];
if(valors != 255){
//enemigos[i].vivo = false;
int angu = rand() % 90 + 1;
glRotatef(180, 0, 1, 0);
enemigos[i].angulo = 3.14;
//printf("pared");
}
glPopMatrix();
}
}
//checa las colisiones con los enemigos
balasEnemigo();
ataqueEnemigo();
/* glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(-2, 2, -2, 2, -10, 10);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
*/
}
void score(){
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glOrtho(-2, 2, -2, 2, -10, 10);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1.0, 0,0);
glTexCoord2f(1.0,1.0);
glVertex3f(-1.0, 2.0, 0);
glTexCoord2f(0.0,1.0);
glVertex3f(-2.0, 2.0, 0);
glTexCoord2f(0.0,0.0);
glVertex3f(-2.0, 1.5, 0);
glTexCoord2f(1.0,0.0);
glVertex3f(-1.0, 1.5, 0);
glEnd();
}
void Display(void) {
/*if(pantallaSelec == 0){
glLoadIdentity();
glPushMatrix();
principal();
glPopMatrix();
}else if(pantallaSelec == 1){*/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
game();
glPushMatrix();
glPopMatrix();
glLoadIdentity();
//}
glutSwapBuffers();
}
int main(int argc, char **argv) {
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(1440,800);
glutCreateWindow("shooter");
init();
// register callbacks
glutDisplayFunc(Display);
glutReshapeFunc(reshape);
glutIdleFunc(Display);
// here are the new entries
glutKeyboardFunc(Keyboard);
glutKeyboardUpFunc(KeyboardUp);
glutPassiveMotionFunc(mouseMove);
init();
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}