2012-11-24 3 views
2

OpenGL에서 입자 분수를 만들고 있습니다. 입자가 제대로 작동하고 있습니다. 나는 그것으로부터 튀어 오르는 것처럼 보이는 비행기를 추가하기로 결정했다. 내가 얻으려고하는 것은이 enter image description hereOpenGL 평면이 보이지 않음

같은 것입니다 불행하게도 내가 무엇을 얻고 것은 일반 전혀 등장하지 않는 것이

enter image description here

입니다. 나는 코디네이터들과 어리석은 행동을 시도했는데 아무 것도하지 않는 것 같습니다. This은 질감으로 사용하는 이미지입니다. 256 X 256 24 비트 bmp입니다.

내가

void Load_Plane(){ 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); 
    glColor4f(0.0f, 0.2f, 0.2f, 0.5f); 
    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D, txPlane); 
    glBegin(GL_QUADS); 
     glNormal3f(-10.0f, 0.0f, -10.0f); 
     glTexCoord2f(0.0f, 0.0f); 
     glVertex3f(-10.0f, 0.0f, 10.0f); 
     glTexCoord2f(1.0f, 0.0f); 
     glVertex3f(10.0f, 0.0f, 10.0f); 
     glTexCoord2f(1.0f, 1.0f); 
     glVertex3f(10.0f, 0.0f, -10.0f); 
     glTexCoord2f(0.0f, 1.0f); 
     glVertex3f(-10.0f, 0.0f, -10.0f); 
    glEnd(); 
} 

전체 코드가

// particle_fountain.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include<stdlib.h> 
#include <stdio.h> 
#include<Windows.h> 
#include <time.h> 
#include <GL\glut.h> 
#include<GL\GLU.h> 

#define MAX_PARTICLES 200 //max number of particles 
#define MAX_BOUNCE_COUNT 5 //number of times a particle should bounce 
#define MAX_PARTICLE_AGE 95 

//Colours 
float R = 0.8f; 
float G = 0.2f; 
float B = 0.0f; 
float cR = 0.001f; 
float cG = 0.002f; 
float cB = 0.003f; 
float Size = 0.02f; //size for points 
GLuint txParticle; 
GLuint txPlane; 


struct PARTICLE { 
    float X,Y,Z; // Current position 
    float sX,sY,sZ; // Current Speed/Movement 
    float tX,tY,tZ; // Target Speed/Movement 
    float R,B,G; // Particle Colour 
    bool Active; // Is particle Active 
    int Age; // Age of the particle 
    int MaxAge; // Maximum Age before particle dies 
    int BounceCount; 
} Particles[MAX_PARTICLES]; 

void Init_Particles(); 
void Activate_Particles(); 
void Adjust_Particles(); 
void Render_Particles(); 
bool LoadBitmapTexture(char * FileName, GLuint &texid); 
void timer(int extra); 
void Load_Plane(); 
void DrawGLscene(); 
void Reshape(GLsizei w, GLsizei h); 

int main(int argc, char** argv){ 
    glutInit(&argc,argv); 
    glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE); 
    glutInitWindowSize(640, 480);  
    glutCreateWindow("Particle fountain"); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glTranslatef(0.0, -0.9, -3.0); 


    Init_Particles(); 
    glutDisplayFunc(DrawGLscene); 
    glutTimerFunc(0, timer, 0); 
    glutMainLoop(); 

} 

void timer(int extra) 
{ 
    glutPostRedisplay(); 
    glutTimerFunc(20, timer, 0); 
} 

void Load_Plane(){ 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); 
    glColor4f(0.0f, 0.2f, 0.2f, 0.5f); 
    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D, txPlane); 
    glBegin(GL_QUADS); 
     glNormal3f(-10.0f, 0.0f, -10.0f); 
     glTexCoord2f(0.0f, 0.0f); 
     glVertex3f(-10.0f, 0.0f, 10.0f); 
     glTexCoord2f(1.0f, 0.0f); 
     glVertex3f(10.0f, 0.0f, 10.0f); 
     glTexCoord2f(1.0f, 1.0f); 
     glVertex3f(10.0f, 0.0f, -10.0f); 
     glTexCoord2f(0.0f, 1.0f); 
     glVertex3f(-10.0f, 0.0f, -10.0f); 
    glEnd(); 
} 
void DrawGLscene(){ 
    Load_Plane(); 
    glPushMatrix(); 
    glScalef(1.0f, -1.0f, 1.0f); 
    Render_Particles(); 
    glPopMatrix(); 
    Render_Particles(); 

} 


void Init_Particles(){ 
    LoadBitmapTexture("./Particle.bmp", txParticle); //load the particle texture 
    LoadBitmapTexture("./Plain.bmp",txPlane); //load the plain texture 

    int p; 
    srand((int)time(NULL)); 
    for(p=0; p<MAX_PARTICLES; p++){ 
     Particles[p].Active = FALSE; 
     Particles[p].tX = 0.0f; 
     Particles[p].tY = -0.1f; 
     Particles[p].tZ = 0.0f; 
    } 
} 

void Activate_Particles(){ 
    int p; 
    for(p=0; p<MAX_PARTICLES; p++){ 
     if(!Particles[p].Active){ 
      // Start the particle at 0,0,0 origin 
      Particles[p].X = 0.0f; 
      Particles[p].Y = 0.0f; 
      Particles[p].Z = 0.0f; 
      // The following lines set a random speed value 
      Particles[p].sX = (((float)((rand() % 100) + 1))/
       1000.0f) - 0.05f; 
      Particles[p].sY = (((float)((rand() % 100) + 50))/
       500.0f); 
      Particles[p].sZ = (((float)((rand() % 100) + 1))/
       1000.0f) - 0.05f; 
      // We also activate the particle 
      Particles[p].Active = true; 
      // Set it's Age to zero 
      Particles[p].Age = 0; 
      // We also assign a max age to the particles 
      Particles[p].MaxAge = MAX_PARTICLE_AGE; 
      // We Also reset the bouncecount to zero 
      Particles[p].BounceCount = 0; 

      //Adding the colours 
      Particles[p].R = R; 
      Particles[p].G = G; 
      Particles[p].B = B; 
      R+=cR; 
      G+=cG; 
      B+=cB; 
      if(R>1.0f){R=1.0f; cR=-cR;} 
      if(R<0.0f){R=0.0f; cR=-cR;} 
      if(G>1.0f){G=1.0f; cG=-cG;} 
      if(G<0.0f){G=0.0f; cG=-cG;} 
      if(B>1.0f){B=1.0f; cB=-cB;} 
      if(B<0.0f){B=0.0f; cB=-cB;} 
      return; 
     } 
} 
} 

void Adjust_Particles(){ 
    int p; 
    for(p=0; p<MAX_PARTICLES; p++){ 
     // We move the speed towards the target speed by 1/20 (5%) 
     Particles[p].sX+= (Particles[p].tX - Particles[p].sX)/20.0f; 
     Particles[p].sY+= (Particles[p].tY - Particles[p].sY)/20.0f; 
     Particles[p].sZ+= (Particles[p].tZ - Particles[p].sZ)/20.0f; 
     // Then we adjust the position of 
     // the particle by the new speed 
     Particles[p].X+= Particles[p].sX; 
     Particles[p].Y+= Particles[p].sY; 
     Particles[p].Z+= Particles[p].sZ; 
     // Now for the bounce code. 
     if(Particles[p].Y < 0.0f){ 
      Particles[p].Y = 0.0f; 
      Particles[p].sY = -Particles[p].sY; 
      Particles[p].BounceCount++; 
      if(Particles[p].BounceCount > MAX_BOUNCE_COUNT){ 
       Particles[p].Active = FALSE; 
      } 
     } 
     // And finally the age check 
     Particles[p].Age++; 
     if(Particles[p].Age > Particles[p].MaxAge){ 
      Particles[p].Active = FALSE; 
     } 
    } 

} 

void Render_Particles(){ 
    Activate_Particles(); 
    Adjust_Particles(); 

    glClear(GL_COLOR_BUFFER_BIT); 

    int p; 
    // Enable textures and bind our particle texture 
    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D, txParticle); 
    // Disable Depth testing. 
    glDisable(GL_DEPTH_TEST); 
    // Enable blending 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_COLOR,GL_ONE); 
    for(p=0; p<MAX_PARTICLES; p++){ 
     if(Particles[p].Active){ 
      glColor4f(Particles[p].R, 
       Particles[p].G, 
       Particles[p].B, 1.0f); 
      glPushMatrix(); 
      glTranslatef(Particles[p].X, 
       Particles[p].Y, 
       Particles[p].Z); 
      glBegin(GL_QUADS); 
      glNormal3f(0.0f, 0.0f, 1.0f); 
      glTexCoord2f(0.0f, 0.0f); 
      glVertex3f(-Size, -Size, 0.0f); 
      glTexCoord2f(1.0f, 0.0f); 
      glVertex3f(Size, -Size, 0.0f); 
      glTexCoord2f(1.0f, 1.0f); 
      glVertex3f(Size, Size, 0.0f); 
      glTexCoord2f(0.0f, 1.0f); 
      glVertex3f(-Size, Size, 0.0f); 
      glEnd(); 
      glPopMatrix(); 


     } 
    } 
    glEnable(GL_DEPTH_TEST); 
    glutSwapBuffers(); 
} 

bool LoadBitmapTexture(char * FileName, GLuint &texid){ 
    HBITMAP hBMP; // Handle Of The Bitmap 
    BITMAP BMP; // Bitmap Structure 
    glGenTextures(1, &texid); // Create The Texture 
    hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL), 
     FileName, 
     IMAGE_BITMAP, 0, 0, 
     LR_CREATEDIBSECTION | LR_LOADFROMFILE 
     ); 

    if (!hBMP) // Does The Bitmap Exist? 
     return FALSE; // If Not Return False 
    GetObject(hBMP, sizeof(BMP), &BMP); // Get The Object 
    // hBMP: Handle To Graphics Object 
    // sizeof(BMP): Size Of Buffer For Object Information 
    // &BMP: Buffer For Object Information 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4); 
    // Pixel Storage Mode (Word Alignment/4 Bytes) 
    // Typical Texture Generation Using Data From The Bitmap 
    glBindTexture(GL_TEXTURE_2D, texid);// Bind To The Texture ID 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
     GL_LINEAR); // Linear Min Filter 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
     GL_LINEAR); // Linear Mag Filter 
    glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 
     0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits); 
    DeleteObject(hBMP); // Delete The Object 
    return TRUE; // Loading Was Successful 
} 
+0

'glGetError'로 오류 검사를 시도 했습니까? – Tim

+0

이미지에'BMP.bmBitsPixel'이란 무엇입니까? – genpfault

+0

어떻게 확인할 수 있습니까? – cyberbemon

답변

1

그것은 당신이 삭제하지 않을 것을 할 수있는 다음의 함수에서 입자를 렌더링하기 전에 다음, init 함수에서 텍스처를로드를 호출 깊이 버퍼

렌더링 할 때 심도 테스트를 사용하지 않도록 설정했기 때문에 입자에 영향을 미치지 않지만 비행기를 렌더링 할 때 심도 테스트를 사용할 수 있으며 심도 버퍼가 지워지지 않았기 때문에 spaz 및 비행기를 렌더링하지 않습니다. 당신이 깊이 버퍼를 지우면을 렌더링하기 전에

glClear(GL_DEPTH_BUFFER_BIT); 

를 수행합니다.

편집 : 당신은 당신이 비행기를 렌더링 후

glClear(GL_COLOR_BUFFER_BIT); 

를 호출

이것은 - 그것을해야합니다. 당신의 DrawGLScene 기능을 봐 :

Load_Plane(); // you are drawing the plane here 
glPushMatrix(); 
glScalef(1.0f, -1.0f, 1.0f); 
Render_Particles(); // this function calls "glClear(GL_COLOR_BUFFER_BIT);" 
// so anything that you rendered before is now removed. 
glPopMatrix(); 
Render_Particles(); // same goes for here. 

이 솔루션은, 당신의 Render_Particles 함수에서 를 glClear 호출을 제거하고 DrawGLScene의 상단에 추가하는 것입니다 :

(뉴 DrawGLScene 코드)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
Load_Plane(); 
glPushMatrix(); 
glScalef(1.0f, -1.0f, 1.0f); 
Render_Particles(); 
glPopMatrix(); 
Render_Particles(); 

편집 # 2 :

당신은 Render_Particles 기능에 glutSwapBuffers를 호출하고 있습니다. 전화하지 마세요. DrawGLScene의 끝에서 호출 :

+0

Load_Plain() 함수를 호출하기 전에'glClear (GL_DEPTH_BUFFER_BIT);'를 추가했지만, 문제가 해결되지 않았습니다. – cyberbemon

+0

@cyberbemon 내가 가지고 있다고 생각해! 내 대답을 다시 읽고, 나는 그것을 편집하고 새로운 가능한 해결책을 넣어 라 !! – Aaron

+0

불행히도 그게 효과가 없었어요, 나는 심지어 질감을 바꾸었고, 여전히 쓸모가 없었습니다! – cyberbemon