-
Solar system Help
Hello everybody,
I want to ask help for a project I make.
I have made a complete solar system with key action for zoom in zoom out, moving along x,y,z axes on plus or minus and going up/down/right/left.
I am now in a bit of a dilemma.
I want to add a text window ( i have made my project in visual studio 2010 in win32 console application) where to put a text like a description of each planet to apear when i hit the left mouse button.
E.g. When i click with the left mouse button the venus planet I want a description to apear in my text window.
However I don't know how to put make a text window and how to correctly define the picking methods... for now everywhere i hit the mouse button it appears in to the console that i hit the sun.
Please help me!
Thanks in advance.
-
Did you use a library for graphics and mouse input? If so, it would be helpful to know which.
-
Here i post my code so that you could me in a better position to help me:
Code:
#include "SolarSystem.h"
#include "windows.h"
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
#include <gl/glut.h>
#include <GL/glu.h>
#include <gl/GL.h>
#include <math.h>
// Valori lumina
GLfloat whiteLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat sourceLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };
//verificare pt incarcare texturi doar o data
bool texturesCreated = false;
//texturi
GLuint sunTexture;
GLuint mercuryTexture;
GLuint venusTexture;
GLuint earthTexture;
GLuint moonTexture;
GLuint marsTexture;
GLuint jupiterTexture;
GLuint saturnTexture;
GLuint uranusTexture;
GLuint neptuneTexture;
GLuint plutoTexture;
GLuint starsTexture;
//pozitia si rotatia soarelui
GLfloat fSunX = 0.0f;
GLfloat fSunY = 0.0f;
GLfloat fSunZ = -300.0f;
GLfloat fCamX = 0.0f;
GLfloat fCamY = 0.0f;
GLfloat fCamZ = 0.0f;
GLfloat fSunRotX = 0.0f;
GLfloat fSunRotY = 0.0f;
GLfloat fSunRotZ = 0.0f;
//definitii functii
void keyDown(unsigned char, int, int);
unsigned char *LoadBmp(char *fn, int *wi, int *hi);
void GenerateTextures(char *, int);
void InitialiseTextures(void);
void gl_init(int w, int h);
void gl_select(int x, int y);
void mouseClick();
void mousedw(int x, int y, int but);
void list_hits(GLint hits, GLuint *names);
//definitii pt click
#define Sun 1
#define Mercur 2
#define Venus 3
#define Terra 4
#define Moon 5
#define Marte 6
#define Jupiter 7
#define Saturn 8
#define Uranus 9
#define Pluto 10
#define Neptun 11
#define MAXSELECT 100
#define BUFSIZE 512
#define SW 450
#define SH 450
GLuint selectBuf[BUFSIZE];
GLuint buf_selectie[MAXSELECT];
GLint windH;
GLint viewport [4];
// Scena
void RenderScene(void)
{
//picking initializare stiva
glInitNames();
GLUquadricObj* pObj;
// Unghiul de revolutie al planetelor
static float fMoonRot = 0.0f;
static float fEarthRot = 0.0f;
static float fMercuryRot = 0.0f;
static float fVenusRot = 0.0f;
static float fMarsRot = 0.0f;
static float fJupiterRot = 0.0f;
static float fSaturnRot = 0.0f;
static float fUranusRot = 0.0f;
static float fNeptuneRot = 0.0f;
static float fPlutoRot = 0.0f;
//
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Salvarea matricii cu stari
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// translatarea scenei in functie de soare
glTranslatef(fSunX, fSunY, fSunZ);
// Soare
glDisable(GL_LIGHTING);
pObj = gluNewQuadric(); //creare obiecte cuadrice pt pObj
gluQuadricTexture(pObj,GL_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, sunTexture); //setare textura soare
//Rotatia soarelui in jurul axei sale
glRotatef(fSunRotX, 1.0f, 0.0f, 0.0f); //rotatie soare in jurul axei x
glRotatef(fSunRotY, 0.0f, 1.0f, 0.0f); //rotatie soare in jurul axei y
glRotatef(fSunRotZ, 0.0f, 0.0f, 1.0f); //rotatie soare in jurul axei z
glLoadName(Sun);
gluSphere(pObj, 33.0f, 30, 17); //draw sphere for the sun
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(pObj); //eliberare obiect pObj
glEnable(GL_LIGHTING);
// mutarea luminii la pozitia soareui
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
#pragma region skybox
// memorare matrice currenta
glPushMatrix();
glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D);
glColor4f(1,1,1,1);
GLfloat val = 450.0f;
// Render the front quad
glBindTexture(GL_TEXTURE_2D, starsTexture);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(val, -val, val);
glTexCoord2f(1, 0); glVertex3f(-val, -val, val);
glTexCoord2f(1, 1); glVertex3f(-val, val, val);
glTexCoord2f(0, 1); glVertex3f(val, val, val);
glEnd();
// Render the left quad
glBindTexture(GL_TEXTURE_2D, starsTexture);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(val, -val, -val);
glTexCoord2f(1, 0); glVertex3f(val, -val, val);
glTexCoord2f(1, 1); glVertex3f(val, val, val);
glTexCoord2f(0, 1); glVertex3f(val, val, -val);
glEnd();
// Render the back quad
glBindTexture(GL_TEXTURE_2D, starsTexture);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-val, -val, -val);
glTexCoord2f(1, 0); glVertex3f(val, -val, -val);
glTexCoord2f(1, 1); glVertex3f(val, val, -val);
glTexCoord2f(0, 1); glVertex3f(-val, val, -val);
glEnd();
// Render the right quad
glBindTexture(GL_TEXTURE_2D, starsTexture);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-val, val, -val);
glTexCoord2f(1, 0); glVertex3f(-val, val, val);
glTexCoord2f(1, 1); glVertex3f(-val, -val, val);
glTexCoord2f(0, 1); glVertex3f(-val, -val, -val);
glEnd();
// Render the top quad
glBindTexture(GL_TEXTURE_2D, starsTexture);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(val, val, -val);
glTexCoord2f(1, 0); glVertex3f(val, val, val);
glTexCoord2f(1, 1); glVertex3f(-val, val, val);
glTexCoord2f(0, 1); glVertex3f(-val, val, -val);
glEnd();
// Render the bottom quad
glBindTexture(GL_TEXTURE_2D, starsTexture);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-val, -val, -val);
glTexCoord2f(1, 0); glVertex3f(-val, -val, val);
glTexCoord2f(1, 1); glVertex3f(val, -val, val);
glTexCoord2f(0, 1); glVertex3f(val, -val, -val);
glEnd();
// Resetare matrice
glPopAttrib();
glPopMatrix();
#pragma endregion
#pragma region mercury
//Mercur
glPushMatrix(); //salvare matrice
pObj = gluNewQuadric();
gluQuadricTexture(pObj,GL_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, mercuryTexture); //setare textura mercur
//coord de rotatie pt mercur
glRotatef(fMercuryRot, 0.0f, 2.0f, 0.3f);
//desenare Mercur
glTranslatef(45.0f,-15.0f,0.0f); //mutare mercur la distanta de soare
glRotatef(270.0f, 1.0f, 0.0f, 0.0f); //rotire
glRotatef(fMercuryRot * 3, 0.0f, 0.0f, 1.0f); //rotatie in jurul axei sale
glLoadName(Mercur);
gluSphere(pObj, 8.0f, 30, 17); //desenare sfera
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(pObj);
//viteza de rotatie
fMercuryRot += 20.0f;
//resetare rotatie
if(fMercuryRot >= 360.0f)
fMercuryRot = 0.0f;
glPopMatrix(); //resetare matrice
#pragma endregion
#pragma region venus
//Venus
glPushMatrix(); //salvare matrice
pObj = gluNewQuadric();
gluQuadricTexture(pObj,GL_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, venusTexture); //setare textura venus
//coord de rotire pt venus
glRotatef(fVenusRot, 0.0f, 1.0f, 0.0f);
//Desenare Venus
glTranslatef(65.0f,0.0f,0.0f); //mutare venus la distanta de soare
glRotatef(270.0f, 1.0f, 0.0f, 0.0f); //rotire
glRotatef(fVenusRot * 3, 0.0f, 0.0f, 1.0f); //rotatie in jurul axei sale
glLoadName(Venus);
gluSphere(pObj, 14.0f, 30, 17); //desenare sfera pt venus
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(pObj);
//viteza rotire
fVenusRot += 10.0f;
//resetare rotire
if(fVenusRot >= 360.0f)
fVenusRot = 0.0f;
glPopMatrix();
#pragma endregion
#pragma region marte
//Marte
glPushMatrix();
pObj = gluNewQuadric();
gluQuadricTexture(pObj,GL_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, marsTexture); //setare textura marte
//coord de rotire pt marte
glRotatef(fMarsRot, 0.0f, 1.0f, 0.3f);
//desenare marte
glTranslatef(130.0f,0.0f,0.0f); //pozitionare marte la distanta de soare
glRotatef(270.0f, 1.0f, 0.0f, 0.0f); //rotatie
glRotatef(fMarsRot * 3, 0.0f, 0.0f, 1.0f); //rotatie in jurul axei sale
glLoadName(Marte);
gluSphere(pObj, 7.0f, 30, 17); //desenare sfera
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(pObj);
//viteza rotatie
fMarsRot += 4.0f;
//resetare rotatie
if(fMarsRot >= 360.0f)
fMarsRot = 0.0f;
glPopMatrix();
#pragma endregion
#pragma region jupiter
//Jupiter
glPushMatrix();
pObj = gluNewQuadric();
gluQuadricTexture(pObj,GL_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, jupiterTexture); //setare textura jupiter
//coord de rotire pt jupiter
glRotatef(fJupiterRot, 0.0f, 1.0f, -0.3f);
//Desenare Jupiter
glTranslatef(200.0f,0.0f,0.0f); //pozitionare jupiter la distanta de soare
glRotatef(270.0f, 1.0f, 0.0f, 0.0f); //rotatie
glRotatef(fJupiterRot * 3, 0.0f, 0.0f, 1.0f); //rotatie in jurul axei sale
glLoadName(Jupiter);
gluSphere(pObj, 22.0f, 30, 17); //desenare sfera
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(pObj);
//setare viteza de rotatie
fJupiterRot += 2.0f;
//resetare rotatie
if(fJupiterRot >= 360.0f)
fJupiterRot = 0.0f;
glPopMatrix();
#pragma endregion
#pragma region saturn
//Saturn
glPushMatrix();
pObj = gluNewQuadric();
gluQuadricTexture(pObj,GL_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, saturnTexture); //setare textura Saturn
//Coord de rotire pt Saturn
glRotatef(fSaturnRot, 0.0f, 0.8f, -0.2f);
//Desenare Saturn
glTranslatef(245.0f,0.0f,0.0f); //setare pozitie Saturn la distanta de soare
glRotatef(270.0f, 1.0f, 0.0f, 0.0f); //Rotatie
glRotatef(fSaturnRot * 3, 0.0f, 0.0f, 1.0f); //rotatie in jurul axei sale
glLoadName(Saturn);
gluSphere(pObj, 20.0f, 30, 17); //desenare sfera
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(pObj);
//viteza rotatie
fSaturnRot += 1.5f;
//resetare rotatie
if(fSaturnRot >= 360.0f)
fSaturnRot = 0.0f;
glPopMatrix();
#pragma endregion
#pragma region uranus
//Uranus
glPushMatrix();
pObj = gluNewQuadric();
gluQuadricTexture(pObj,GL_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, uranusTexture); //setare textura pt Uranus
//coord de rotire pt Uranus
glRotatef(fUranusRot, 0.0f, 0.8f, 0.2f);
//Desenare Uranus
glTranslatef(285.0f,0.0f,0.0f); //pozitionare Uranus la distanta de soare
glRotatef(270.0f, 1.0f, 0.0f, 0.0f); //rotatie
glRotatef(fUranusRot * 3, 0.0f, 0.0f, 1.0f); //rotatie in jurul axei sale
glLoadName(Uranus);
gluSphere(pObj, 10.0f, 30, 17); //desenare sfera
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(pObj);
//viteza de rotatie
fUranusRot += 1.0f;
//resetare rotatie
if(fUranusRot >= 360.0f)
fUranusRot = 0.0f;
glPopMatrix();
#pragma endregion
#pragma region neptun
//Neptun
glPushMatrix();
pObj = gluNewQuadric();
gluQuadricTexture(pObj,GL_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, neptuneTexture); //setare textura pt neptun
//coord de rotatie pt neptun
glRotatef(fNeptuneRot, 0.0f, 0.8f, 0.2f);
//Desenare Neptune
glTranslatef(315.0f,0.0f,0.0f); //setare pozitie la distanta de soare
glRotatef(270.0f, 1.0f, 0.0f, 0.0f); //rotire
glRotatef(fNeptuneRot * 3, 0.0f, 0.0f, 1.0f); //rotatie in jurul axei sale
glLoadName(Neptun);
gluSphere(pObj, 12.0f, 30, 17); //desenare sfera pt neptun
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(pObj);
//viteza de rotatie
fNeptuneRot += 0.6f;
//resetare rotatie
if(fNeptuneRot >= 360.0f)
fNeptuneRot = 0.0f;
glPopMatrix();
#pragma endregion
#pragma region pluto
//Pluto
glPushMatrix();
pObj = gluNewQuadric();
gluQuadricTexture(pObj,GL_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, plutoTexture); //setare textura pt pluto
//coord de rotatie pt Pluto
glRotatef(fPlutoRot, 0.0f, 0.8f, 0.2f);
//Desenare Pluto
glTranslatef(350.0f,0.0f,0.0f); //setare la distanta de soare
glRotatef(270.0f, 1.0f, 0.0f, 0.0f); //rotatie
glRotatef(fPlutoRot * 3, 0.0f, 0.0f, 1.0f); //rotatie in jurul axei sale
glLoadName(Pluto);
gluSphere(pObj, 12.0f, 30, 17); //desenare sfera
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(pObj);
//viteza de rotatie
fPlutoRot += 0.2f;
//
if(fPlutoRot >= 360.0f)
fPlutoRot = 0.0f;
glPopMatrix();
#pragma endregion
#pragma region Terra
//Pamant
glPushMatrix();
pObj = gluNewQuadric();
gluQuadricTexture(pObj,GL_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, earthTexture);//setare textura
// coord de rotire pt pamant
glRotatef(fEarthRot, 0.0f, 1.0f, 0.0f); //rotatie in jurul soarelui
// Desenare Terra
glTranslatef(100.0f,0.0f,0.0f); //setare distanta de soare
glRotatef(270.0f, 1.0f, 0.0f, 0.0f); //rotire
glRotatef(fEarthRot * 3, 0.0f, 0.0f, 1.0f); //rotire in jurul axei sale
glLoadName(Terra);
gluSphere(pObj, 16.0f, 30, 17); //desenare sfera
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(pObj);
#pragma region earthMoon
// Desenare Luna
pObj = gluNewQuadric();
gluQuadricTexture(pObj,GL_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, moonTexture); //setare textura Luna
glRotatef(fMoonRot,0.0f, 0.0f, 1.0f); //rotire luna in jurul pamantului
glTranslatef(25.0f, 0.0f, 0.0f); //distanta fata de pamant
glRotatef(270.0f, 1.0f, 0.0f, 0.0f); //rotire
glRotatef(fMoonRot * 3, 0.0f, 0.0f, 1.0f); //rotire in jurul axei sale
//viteza de rotatie
fMoonRot+= 15.0f;
//
if(fMoonRot >= 360.0f)
fMoonRot = 0.0f;
glLoadName(Moon);
gluSphere(pObj, 4.0f, 30, 17); //desenare sfera pt luna
glDisable(GL_TEXTURE_2D);
gluDeleteQuadric(pObj);
glPopMatrix();
#pragma endregion
//viteza de rotire a pamantului
fEarthRot += 5.0f;
//
if(fEarthRot >= 360.0f)
fEarthRot = 0.0f;
glPopMatrix(); //
#pragma endregion
// Show image
glutSwapBuffers();
}
void SetupRC()
{
//valoare si coord lumina
glEnable(GL_DEPTH_TEST); //
glFrontFace(GL_CCW); // invers acelor de ceasornic (pt sfere)
glEnable(GL_CULL_FACE); //
// on lumina
glEnable(GL_LIGHTING);
// setare lumina
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
glLightfv(GL_LIGHT0,GL_AMBIENT_AND_DIFFUSE,sourceLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);
//
glEnable(GL_COLOR_MATERIAL);
//
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// setare background negru
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
}
void TimerFunc(int value)
{
glutPostRedisplay();
glutTimerFunc(100, TimerFunc, 1);
}
//posibilitate de minimizare si maximizare de catre user
void ChangeSize(int w, int h)
{
GLfloat fAspect;
//
if(h == 0)
h = 1;
// setare vedere in functie de marimea ferestrei
glViewport(0, 0, w, h);
// aspect in functie de marimea ferestrei
fAspect = (GLfloat)w/(GLfloat)h;
// setare coord pt ferestra
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// modul de vizionare
gluPerspective(45.0f, fAspect, 1.0, 1600.0);
//
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void gl_init(int w, int h)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0, 0.0001, 1000.0);
glMatrixMode(GL_MODELVIEW);
}
void list_hits(GLint hits, GLuint *names)
{
printf("%d hits:\n", hits);
switch(hits)
{
case Mercur:
cout<<"Ati selectat Planeta Mercur"<<endl;
break;
case Sun:
cout<<"Ati selectat Soarele"<<endl;
break;
case Saturn:
cout<<"Ati selectat Planeta Saturn"<<endl;
break;
case Neptun:
cout<<"Ati selectat Planeta Neptun"<<endl;
break;
case Uranus:
cout<<"Ati selectat Planeta Uranus"<<endl;
break;
case Jupiter:
cout<<"Ati selectat Planeta Jupiter"<<endl;
break;
case Marte:
cout<<"Ati selectat Planeta Marte"<<endl;
break;
case Venus:
cout<<"Ati selectat Planeta Venus"<<endl;
break;
case Pluto:
cout<<"Ati selectat Planeta Pluto"<<endl;
break;
case Terra:
cout<<"Ati selectat Planeta Pamant"<<endl;
break;
case Moon:
cout<<"Ati selectat Luna"<<endl;
break;
default:
cout<<"Nu ati selectat nimic"<<endl;
break;
}
}
void gl_select(int x, int y)
{
GLuint buff[64] = {0};
GLint hits, view[4];
/*
This choose the buffer where store the values for the selection data
*/
glSelectBuffer(64, buff);
/*
This retrieve info about the viewport
*/
glGetIntegerv(GL_VIEWPORT, view);
/*
Switching in selecton mode
*/
glRenderMode(GL_SELECT);
/*
Clearing the name's stack
This stack contains all the info about the objects
*/
glInitNames();
/*
Now fill the stack with one element (or glLoadName will generate an error)
*/
glLoadName(0);
/*
Now modify the vieving volume, restricting selection area around the cursor
*/
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
/*
restrict the draw to an area around the cursor
*/
gluPickMatrix(x, y, 0.2, 0.2, view);
gluPerspective(60, 0.5, 0.0001, 1000.0);
/*
Draw the objects onto the screen
*/
glMatrixMode(GL_MODELVIEW);
/*
draw only the names in the stack, and fill the array
*/
glutSwapBuffers();
RenderScene();
/*
Do you remeber? We do pushMatrix in PROJECTION mode
*/
glMatrixMode(GL_PROJECTION);
glPopMatrix();
/*
get number of objects drawed in that area
and return to render mode
*/
hits = glRenderMode(GL_RENDER);
/*
Print a list of the objects
*/
list_hits(hits, buff);
/*
uncomment this to show the whole buffer
* /
gl_selall(hits, buff);
*/
glMatrixMode(GL_MODELVIEW);
}
void mousedw(int x, int y, int but)
{
printf("Mouse button %d pressed at %d %d\n", but, x, y);
gl_select(x,SH-y); //Important: gl (0,0) ist bottom left but window coords (0,0) are top left so we have to change this!
}
void mouseClick(int button, int state, int x, int y)
{
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
{
mousedw(x, y, button);
}
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
///glutInitWindowSize(1000, 600);
glutInitWindowSize(SW, SH);
glutInitWindowPosition(0, 0);
glutCreateWindow("Solar System"); //numele ferestrei
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
glutKeyboardFunc(keyDown); //pt taste
//INREGISTRARIILE FUNCTIILOR PT MOUSE
glutMouseFunc(mouseClick);
// glutMouseFunc(mouse);
gl_init(SW, SH);
glutTimerFunc(250, TimerFunc, 1);
SetupRC();
//verificare textura
if (texturesCreated == false)
{
InitialiseTextures();
texturesCreated = true; //
}
glutMainLoop();
return 0;
}
//controale
void keyDown(unsigned char key, int x, int y)
{
//Moves the camera to the right
if (key == 'A' || key == 'a')
{
fSunX += 3.0f;
fCamX += -3.0f;
}
//Moves the camera to the left
if (key == 'D' || key == 'd')
{
fSunX += -3.0f;
fCamX += 3.0f;
}
//Moves the camera up
if (key == 'W' || key == 'w')
{
fSunY += -3.0f;
fCamY += 3.0f;
}
//Moves the camera down
if (key == 'S' || key == 's')
{
fSunY += 3.0f;
fCamY += -3.0f;
}
//Zooms the camera out
if (key == 'X' || key == 'x')
{
fSunZ += -3.0f;
fCamZ += 3.0f;
}
//Zooms the camera in
if (key == 'Z' || key == 'z')
{
fSunZ += 3.0f;
fCamZ += -3.0f;
}
//Rotates the camera along the positive X axis
if (key == 'J' || key == 'j')
{
fSunRotY += 3.0f;
}
//Rotates the camera along the negative X axis
if (key == 'L' || key == 'l')
{
fSunRotY += -3.0f;
}
//Rotates the camera along the negative Y axis
if (key == 'I' || key == 'i')
{
fSunRotX += 3.0f;
}
//Rotates the camera along the positive Y axis
if (key == 'K' || key == 'k')
{
fSunRotX += -3.0f;
}
//Rotates the camera along the positive Z axis
if (key == 'M' || key == 'm')
{
fSunRotZ += -3.0f;
}
//Rotates the camera along the positive Z axis
if (key == 'N' || key == 'n')
{
fSunRotZ += 3.0f;
}
}
unsigned char *LoadBmp(char *fn, int *wi, int *hi)
{
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
WORD bits;
FILE *t24;
unsigned char *lpBitmapBits;
long imagesize,nc;
// citire bitmap
t24=fopen((char *)fn,"rb");
if(t24 == NULL){printf("Could not open input file\n"); exit(0);}
fread((char *)&bmfh,sizeof(BITMAPFILEHEADER),1,t24);
fread((char *)&bmih,sizeof(BITMAPINFOHEADER),1,t24);
if(bmih.biClrUsed != 0)nc=bmih.biClrUsed;
else{
bits = bmih.biBitCount;
switch (bits){
case 1: nc=2; break;
case 4: nc=16; break;
case 8: nc=256; break;
default: nc=0; break;
}
}
if(nc > 0){printf("Cannot handle paletted image\n"); exit(0);}
imagesize=bmfh.bfSize-bmfh.bfOffBits;
if((lpBitmapBits=(unsigned char *)malloc(imagesize)) == NULL){ fclose(t24); exit (0); }
fread((char *)lpBitmapBits,imagesize,1,t24);
fclose(t24);
*wi=bmih.biWidth; *hi=bmih.biHeight;
return lpBitmapBits;
}
void InitialiseTextures()
{
//load texturi
GenerateTextures("earth.bmp", 1);
GenerateTextures("sun.bmp", 2);
GenerateTextures("mercur.bmp", 3);
GenerateTextures("venus.bmp", 4);
GenerateTextures("moon.bmp", 5);
GenerateTextures("marte.bmp", 6);
GenerateTextures("jupiter.bmp", 7);
GenerateTextures("saturn.bmp", 8);
GenerateTextures("uranus.bmp", 9);
GenerateTextures("neptun.bmp", 10);
GenerateTextures("pluto.bmp", 11);
GenerateTextures("stars.bmp", 12);
}
//
void GenerateTextures(char *name, int i)
{
unsigned char *pix;
int w,h;
glEnable(GL_TEXTURE_2D);
//asigane pt texturi
pix=LoadBmp(name,&w,&h);
if (i == 1) //gets & sets textura pt pamant
{
glGenTextures(1, &earthTexture);
glBindTexture(GL_TEXTURE_2D, earthTexture);
}
if (i == 2) //gets & sets textura pt soare
{
glGenTextures(1, &sunTexture);
glBindTexture(GL_TEXTURE_2D, sunTexture);
}
if (i == 3) //gets & sets textura pt mercur
{
glGenTextures(1, &mercuryTexture);
glBindTexture(GL_TEXTURE_2D, mercuryTexture);
}
if (i == 4) //gets & sets textura pt venus
{
glGenTextures(1, &venusTexture);
glBindTexture(GL_TEXTURE_2D, venusTexture);
}
if (i == 5) //gets & sets textura pt luna
{
glGenTextures(1, &moonTexture);
glBindTexture(GL_TEXTURE_2D, moonTexture);
}
if (i == 6) //gets & sets textura pt marte
{
glGenTextures(1, &marsTexture);
glBindTexture(GL_TEXTURE_2D, marsTexture);
}
if (i == 7) //gets & sets textura pt jupiter
{
glGenTextures(1, &jupiterTexture);
glBindTexture(GL_TEXTURE_2D, jupiterTexture);
}
if (i == 8) //gets & sets textura pt saturn
{
glGenTextures(1, &saturnTexture);
glBindTexture(GL_TEXTURE_2D, saturnTexture);
}
if (i == 9) //gets & setstextura pt uranus
{
glGenTextures(1, &uranusTexture);
glBindTexture(GL_TEXTURE_2D, uranusTexture);
}
if (i == 10) //gets & sets textura pt neptun
{
glGenTextures(1, &neptuneTexture);
glBindTexture(GL_TEXTURE_2D, neptuneTexture);
}
if (i == 11) //gets & sets textura pt pluto
{
glGenTextures(1, &plutoTexture);
glBindTexture(GL_TEXTURE_2D, plutoTexture);
}
if (i == 12) //gets & sets textura pt stele
{
glGenTextures(1, &starsTexture);
glBindTexture(GL_TEXTURE_2D, starsTexture);
}
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, w, h,
0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pix);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
free(pix); //eliberare pix
glDisable(GL_TEXTURE_2D);
}
-
you definitely did not need to post your entire code - You did not say you had a problem with the code - you said you were looking for a way to display text - posting masses of transformations does not contribute to that query. The only thing your code has added is an answer to post #2 - we now know you are using openGL/Glut.
Glut supports text as i recall, you use glRasterPos() to position the string on the screen, check the docs and a simple search like this
-
my problem is that now anywhere i hit the left mouse button it says that i have selected the sun... no matter what planet i have click on... and i don't know where is my mistake into the code.. the output is into the console could you help me?
-
repost your code removing all the planet rendering stuff etc, just show the relevant parts to your cursor control
-
Code:
#define Sun 1
#define Mercur 2
#define Venus 3
#define Terra 4
#define Moon 5
#define Marte 6
#define Jupiter 7
#define Saturn 8
#define Uranus 9
#define Pluto 10
#define Neptun 11
#define MAXSELECT 100
#define BUFSIZE 512
#define SW 450
#define SH 450
GLuint selectBuf[BUFSIZE];
GLuint buf_selectie[MAXSELECT];
GLint windH;
GLint viewport [4];
void gl_init(int w, int h)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0, 0.0001, 1000.0);
glMatrixMode(GL_MODELVIEW);
}
void list_hits(GLint hits, GLuint *names)
{
printf("%d hits:\n", hits);
switch(hits)
{
case Mercur:
cout<<"Ati selectat Planeta Mercur"<<endl;
break;
case Sun:
cout<<"Ati selectat Soarele"<<endl;
break;
case Saturn:
cout<<"Ati selectat Planeta Saturn"<<endl;
break;
case Neptun:
cout<<"Ati selectat Planeta Neptun"<<endl;
break;
case Uranus:
cout<<"Ati selectat Planeta Uranus"<<endl;
break;
case Jupiter:
cout<<"Ati selectat Planeta Jupiter"<<endl;
break;
case Marte:
cout<<"Ati selectat Planeta Marte"<<endl;
break;
case Venus:
cout<<"Ati selectat Planeta Venus"<<endl;
break;
case Pluto:
cout<<"Ati selectat Planeta Pluto"<<endl;
break;
case Terra:
cout<<"Ati selectat Planeta Pamant"<<endl;
break;
case Moon:
cout<<"Ati selectat Luna"<<endl;
break;
default:
cout<<"Nu ati selectat nimic"<<endl;
break;
}
}
void gl_select(int x, int y)
{
GLuint buff[64] = {0};
GLint hits, view[4];
/*
This choose the buffer where store the values for the selection data
*/
glSelectBuffer(64, buff);
/*
This retrieve info about the viewport
*/
glGetIntegerv(GL_VIEWPORT, view);
/*
Switching in selecton mode
*/
glRenderMode(GL_SELECT);
/*
Clearing the name's stack
This stack contains all the info about the objects
*/
glInitNames();
/*
Now fill the stack with one element (or glLoadName will generate an error)
*/
glLoadName(0);
/*
Now modify the vieving volume, restricting selection area around the cursor
*/
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
/*
restrict the draw to an area around the cursor
*/
gluPickMatrix(x, y, 0.2, 0.2, view);
gluPerspective(60, 0.5, 0.0001, 1000.0);
/*
Draw the objects onto the screen
*/
glMatrixMode(GL_MODELVIEW);
/*
draw only the names in the stack, and fill the array
*/
glutSwapBuffers();
RenderScene();
/*
Do you remeber? We do pushMatrix in PROJECTION mode
*/
glMatrixMode(GL_PROJECTION);
glPopMatrix();
/*
get number of objects drawed in that area
and return to render mode
*/
hits = glRenderMode(GL_RENDER);
/*
Print a list of the objects
*/
list_hits(hits, buff);
/*
uncomment this to show the whole buffer
* /
gl_selall(hits, buff);
*/
glMatrixMode(GL_MODELVIEW);
}
void mousedw(int x, int y, int but)
{
printf("Mouse button %d pressed at %d %d\n", but, x, y);
gl_select(x,SH-y); //Important: gl (0,0) ist bottom left but window coords (0,0) are top left so we have to change this!
}
void mouseClick(int button, int state, int x, int y)
{
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
{
mousedw(x, y, button);
}
}
Here are the function regarding the mouse control and the print out into console window
-
in mouseClick where are the values for x and y supplied from is it GLut events? And also you don't appear to have anything to manage translation of the position relative to the drawn objects anyway? Also in your earlier code your declaration of mouseClick takes no arguments, yet in the function definition it has 4.. so how is it compiling?? given the information i can see
-
on the console it also write the coord of the point where i have click... those are correct, the x and y coord as i have understood are take from the glupickmatrix function... i can say that the x and y coord are correct because into the upper left corner are the coord 0.0 when i click it
-
Okay I haven't looked at what you're drawing too closely, but keep in mind that the (X,Y) coordinates reported by a glut mouse click callback are in screen coordinates, not in world (OpenGL) coordinates. So your OpenGL might call gluPerspective and treat the whole screen as going from (-1,1) on the X and (-1,1) on the Y, or some such, but if your glut window is SW by SH (450x450) then in the click callback, you're going to get X coordinates in [0,450) and Y coordinates in [0,450).
You may need to convert between the two. You can do simple multiplication or, for 3D, unprojection (where the Z coordinate is determined by the Z buffer).
Now, as for text, you can draw a simple bitmap font, which is easiest: NeHe Productions: Bitmap Fonts
Or you can draw an actual truetype font with a library like FreeType: NeHe Productions: FreeType Fonts in OpenGL
It's actually quite a pain getting fonts to work in OpenGL, you may want to search for libraries compatible with glut which add font support for you. Actually, if you're using freeglut instead of the original glut, there is built-in text support with a bitmap font: graphics - How do I draw text with GLUT / OpenGL in C++? - Stack Overflow
And if you're still using the original glut, freeglut should be a drop-in replacement.
-
So i could really use someone who would look at my code... I have tried to use the unproject function... but I have only managed to make the console write that i have nothing selected
here is my function
Code:
void mouse(int button, int state, int x, int y)
{
GLuint buff[64] = {0};
GLint hits,view[4];
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy; /* OpenGL y coordinate position */
GLdouble wx, wy, wz; /* returned world x, y, z coords */
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
/* note viewport[3] is height of window in pixels */
realy = viewport[3] - (GLint) y - 1;
//printf ("Coordinates at cursor are (%4d, %4d)\n", x, realy);
hits = glRenderMode(GL_RENDER);
gluUnProject ((GLdouble) x, (GLdouble) realy, 0.0,
mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
//printf ("World coords at z=0.0 are (%f, %f, %f)\n",
// wx, wy, wz);
gluUnProject ((GLdouble) x, (GLdouble) realy, 1.0,
mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
glSelectBuffer(64, buff);
list_hits(hits, buff);
//printf ("World coords at z=1.0 are (%f, %f, %f)\n",
// wx, wy, wz);
}
break;
default:
break;
}
}
here is the reshape function
Code:
void reshape(int w, int h){
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (60.0, (GLfloat) w/(GLfloat) h, 0.0001, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
for list_hits is just a function with a switch between the planet because i have made for each planet a #define Sun 1 for example and before drawing the sun I have use the glLoadName(Sun) function
I have done this for each planet... coulde someone look at my code and tell me where I have made a mistake.
Please