-2

我在 Visual Studio 2010 win32 控制台应用程序中制作了一个太阳能系统项目。
我现在想在控制台中写一些关于行星的信息。

在该main方法中,我有鼠标功能,在绘制每个行星之前,我已经使用了该glLoadName功能。目前我在任何地方点击它都说我没有选择任何内容。

所有代码:

#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 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();
}



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 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;

    }
}
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);
    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);
}
4

1 回答 1

0

如果这是您拥有的所有代码,那么您没有正确选择。您有glRenderMode(GL_RENDER),但要使用选择模式,您需要调用glRenderMode(GL_SELECT)并渲染要在选择模式中选择的对象。看看这个OpenGL selection 的描述

顺便说一句,这确实是旧的做事方式;它有效,但您可能会研究更好、更现代的方法。

于 2013-04-22T09:05:51.363 回答