문제

SDL에서 SDL + OpenGL로 오픈 소스 입자 엔진 테스트를 포팅하려고합니다. 나는 그것을 컴파일하고 실행할 수 있었지만 화면은 내가 무엇을하든 검은 색을 유지합니다. main.cpp :

#include "glengine.h"



int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow
)
{
//Create a glengine instance
ultragl::glengine *gle = new ultragl::glengine();

if(gle->init())
    gle->run();

else
    std::cout << "glengine initializiation failed!" << std::endl;

//If we can't initialize, or the lesson has quit we delete the instance
delete gle;

return 0;
};

glengine.h :

//we need to include window first because GLee needs to be included before GL.h
#include "window.h"
#include <math.h>           // Math Library Header File
#include <vector>
#include <stdio.h>

using namespace std;

namespace ultragl
{
    class glengine
    {
        protected:
                window m_Window; ///< The window for this lesson
                unsigned int m_Keys[SDLK_LAST]; ///< Stores keys that are pressed
                float piover180;

                virtual void draw();
                virtual void resize(int x, int y);
                virtual bool processEvents();
                void controls();

        private:
            /*
             * We need a structure to store our vertices in, otherwise we
             * just had a huge bunch of floats in the end
             */
            struct Vertex
            {
                float x, y, z;

                Vertex(){}

                Vertex(float x, float y, float z)
                {
                    this->x = x;
                    this->y = y;
                    this->z = z;
                }
            };

            struct particle
            {
                public :
                double angle;
                double speed;
                Vertex v;
                int r;
                int g;
                int b;
                int a;

                particle(double angle, double speed, Vertex v, int r, int g, int b, int a)
                {
                    this->angle = angle;
                    this->speed = speed;
                    this->v = v;
                    this->r = r;
                    this->g = g;
                    this->b = b;
                    this->a = a;
                }

                particle()
                {

                }

            };

            particle p[500];
            float particlesize;


        public:
            glengine();
            ~glengine();

            virtual void run();
            virtual bool init();
            void glengine::test2(int num);
            void glengine::update();
    };
};

Window.h :

#include <string>
#include <iostream>

#include "GLee/GLee.h"

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <GL/glu.h>

using namespace std;

namespace ultragl
{
    class window
    {
        private:
            int w_height;
            int w_width;
            int w_bpp;
            bool w_fullscreen;
            string w_title;

        public:
            window();
            ~window();

            bool createWindow(int width, int height, int bpp, bool fullscreen, const string& title);
            void setSize(int width, int height);
            int getHeight();
            int getWidth();
    };
};

glengine.cpp (보는 주요 것) :

#include "glengine.h"

namespace ultragl{

    glengine::glengine()
    {
        piover180 = 0.0174532925f;
    }

    glengine::~glengine()
    {

    }

    void glengine::resize(int x, int y)
    {
        std::cout << "Resizing Window to " << x << "x" << y << std::endl;

        if (y <= 0)
        {
            y = 1;
        }

        glViewport(0,0,x,y);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f,(GLfloat)x/(GLfloat)y,1.0f,100.0f);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }

    bool glengine::processEvents()
    {
        SDL_Event event;

        while (SDL_PollEvent(&event))//get all events
        {
            switch (event.type)
            {
                // Quit event
                case SDL_QUIT:
                {
                    // Return false because we are quitting.
                    return false;
                }

                case SDL_KEYDOWN:
                {
                    SDLKey sym = event.key.keysym.sym;

                    if(sym == SDLK_ESCAPE) //Quit if escape was pressed
                    {
                        return false;
                    }

                    m_Keys[sym] = 1;
                    break;
                }

                case SDL_KEYUP:
                {
                    SDLKey sym = event.key.keysym.sym;
                    m_Keys[sym] = 0;
                    break;
                }

                case SDL_VIDEORESIZE:
                {
                    //the window has been resized so we need to set up our viewport and projection according to the new size
                    resize(event.resize.w, event.resize.h);
                    break;
                }
                // Default case
                default:
                {
                    break;
                }
            }
        }

        return true;
    }


    bool glengine::init()
    {
        srand( time( NULL ) );

        for(int i = 0; i < 500; i++)
            p[i] = particle(0, 0, Vertex(0.0f, 0.0f, 0.0f), 0, 0, 0, 0);


        if (!m_Window.createWindow(640, 480, 32, false, "Paricle Test GL"))
        {
            return false;
        }

        particlesize = 0.01;
        glShadeModel(GL_SMOOTH);                // Enable Smooth Shading
        glClearColor(0.0f, 0.0f, 0.0f, 0.5f);   // Black Background
        glClearDepth(1.0f);                     // Depth Buffer Setup
        glEnable(GL_DEPTH_TEST);                // Enables Depth Testing
        glDepthFunc(GL_LEQUAL);                 // The Type Of Depth Testing To Do
        glEnable(GL_BLEND);
        glBlendFunc(GL_ONE , GL_ONE_MINUS_SRC_ALPHA);

        return true;
    }

    void glengine::test2(int num)
    {
        glPushMatrix();
            glTranslatef(p[num].v.x, p[num].v.y, p[num].v.z);
            glBegin(GL_QUADS);
                glColor4i(p[num].r, p[num].g, p[num].b, p[num].a);                // Green for x axis
                    glVertex3f(-particlesize, -particlesize,  particlesize);
                    glVertex3f( particlesize, -particlesize,  particlesize);
                    glVertex3f( particlesize,  particlesize,  particlesize);
                    glVertex3f(-particlesize,  particlesize,  particlesize);
            glEnd();

        glPopMatrix();
    }

    void glengine::draw()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
        glLoadIdentity();                   // Reset The Current Modelview Matrix

        gluLookAt(0, 5, 20, 0, 0, 0, 0, 0, 0);
        for(int i = 0; i < 500; i++)
            test2(i);
    }

    void glengine::update()
    {
        for(int i = 0; i < 500; i++)
        {
            if(p[i].a <= 0)
                p[i] = particle(5 + rand() % 360, (rand() % 10) * 0.1, Vertex(0.0f, 0.0f, 0.0f), 0, 255, 255, 255);

            else
                p[i].a -= 1;

            p[i].v.x += (sin(p[i].angle * (3.14159265/180)) * p[i].speed);
            p[i].v.y -= (cos(p[i].angle * (3.14159265/180)) * p[i].speed);
        }
    }

    void glengine::run()
    {
        while(processEvents())
        {
            update();
            draw();
            SDL_GL_SwapBuffers();
        }
    }
};

그리고 마지막으로 Window.cpp :

#include "window.h"

namespace ultragl
{
    window::window(): w_width(0), w_height(0), w_bpp(0), w_fullscreen(false)
    {

    }

    window::~window()
    {
        SDL_Quit();
    }

    bool window::createWindow(int width, int height, int bpp, bool fullscreen, const string& title)
    {
        if( SDL_Init( SDL_INIT_VIDEO ) != 0 )
            return false;

        w_height = height;
        w_width = width;
        w_title = title;
        w_fullscreen = fullscreen;
        w_bpp = bpp;

        //Set lowest possiable values.
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 5);
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

        //Set title.
        SDL_WM_SetCaption(title.c_str(), title.c_str());

        // Flags tell SDL about the type of window we are creating.
        int flags = SDL_OPENGL;

        if(fullscreen == true)
            flags |= SDL_FULLSCREEN;

        // Create window
        SDL_Surface * screen = SDL_SetVideoMode( width, height, bpp, flags );

        if(screen == 0)
            return false;

        //SDL doesn't trigger off a ResizeEvent at startup, but as we need this for OpenGL, we do this ourself
        SDL_Event resizeEvent;
        resizeEvent.type = SDL_VIDEORESIZE;
        resizeEvent.resize.w = width;
        resizeEvent.resize.h = height;

        SDL_PushEvent(&resizeEvent);

        return true;
    }

    void window::setSize(int width, int height)
    {
        w_height = height;
        w_width = width;
    }

    int window::getHeight()
    {
        return w_height;
    }

    int window::getWidth()
    {
        return w_width;
    }
};

어쨌든, 나는 정말로 이것을 끝내야하지만, 나는 이미 내가 생각할 수있는 모든 것을 시도했습니다. 나는 Glengine 파일을 한 시점에서 다음과 같은 곳에 여러 가지 방법을 테스트했습니다.

#include "glengine.h"
#include "SOIL/SOIL.h"
#include "SOIL/stb_image_aug.h"
#include "SOIL/image_helper.h"
#include "SOIL/image_DXT.h"

namespace ultragl{

    glengine::glengine()
    {
        piover180 = 0.0174532925f;
    }

    glengine::~glengine()
    {

    }

    void glengine::resize(int x, int y)
    {
        std::cout << "Resizing Window to " << x << "x" << y << std::endl;

        if (y <= 0)
        {
            y = 1;
        }

        glViewport(0,0,x,y);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f,(GLfloat)x/(GLfloat)y,1.0f,1000.0f);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }

    bool glengine::processEvents()
    {
        SDL_Event event;

        while (SDL_PollEvent(&event))//get all events
        {
            switch (event.type)
            {
                // Quit event
                case SDL_QUIT:
                {
                    // Return false because we are quitting.
                    return false;
                }

                case SDL_KEYDOWN:
                {
                    SDLKey sym = event.key.keysym.sym;

                    if(sym == SDLK_ESCAPE) //Quit if escape was pressed
                    {
                        return false;
                    }

                    m_Keys[sym] = 1;
                    break;
                }

                case SDL_KEYUP:
                {
                    SDLKey sym = event.key.keysym.sym;
                    m_Keys[sym] = 0;
                    break;
                }

                case SDL_VIDEORESIZE:
                {
                    //the window has been resized so we need to set up our viewport and projection according to the new size
                    resize(event.resize.w, event.resize.h);
                    break;
                }
                // Default case
                default:
                {
                    break;
                }
            }
        }

        return true;
    }


    bool glengine::init()
    {
        srand( time( NULL ) );

        for(int i = 0; i < 500; i++)
            p[i] = particle(0, 0, Vertex(0.0f, 0.0f, 0.0f), 0, 0, 0, 0);


        if (!m_Window.createWindow(640, 480, 32, false, "Paricle Test GL"))
        {
            return false;
        }

        particlesize = 10.01;
        glShadeModel(GL_SMOOTH);                // Enable Smooth Shading
        glClearColor(0.0f, 0.0f, 0.0f, 0.5f);   // Black Background
        glClearDepth(1.0f);                     // Depth Buffer Setup
        glEnable(GL_DEPTH_TEST);                // Enables Depth Testing
        glDepthFunc(GL_LEQUAL);                 // The Type Of Depth Testing To Do
        glEnable(GL_BLEND);
        glBlendFunc(GL_ONE , GL_ONE_MINUS_SRC_ALPHA);

        return true;
    }

    void glengine::test2(int num)
    {
        //glPushMatrix();
            //glTranslatef(p[num].v.x, p[num].v.y, p[num].v.z);
                glColor4i(255, 255, 255, 255);

        glBegin(GL_QUADS);
            glNormal3f( 0.0f, 0.0f, 1.0f);
            glVertex3f(-particlesize, -particlesize,  particlesize);
            glVertex3f( particlesize, -particlesize,  particlesize);
            glVertex3f( particlesize,  particlesize,  particlesize);
            glVertex3f(-particlesize,  particlesize,  particlesize);
        glEnd();

        // Back Face
        glBegin(GL_QUADS);
            glNormal3f( 0.0f, 0.0f,-1.0f);
            glVertex3f(-particlesize, -particlesize, -particlesize);
            glVertex3f(-particlesize,  particlesize, -particlesize);
            glVertex3f( particlesize,  particlesize, -particlesize);
            glVertex3f( particlesize, -particlesize, -particlesize);
        glEnd();

        // Top Face
        glBegin(GL_QUADS);
            glNormal3f( 0.0f, 1.0f, 0.0f);
            glVertex3f(-particlesize,  particlesize, -particlesize);
            glVertex3f(-particlesize,  particlesize,  particlesize);
            glVertex3f( particlesize,  particlesize,  particlesize);
            glVertex3f( particlesize,  particlesize, -particlesize);
        glEnd();

        // Bottom Face
        glBegin(GL_QUADS);
            glNormal3f( 0.0f,-1.0f, 0.0f);
            glVertex3f(-particlesize, -particlesize, -particlesize);
            glVertex3f( particlesize, -particlesize, -particlesize);
            glVertex3f( particlesize, -particlesize,  particlesize);
            glVertex3f(-particlesize, -particlesize,  particlesize);
        glEnd();

            // Right face
        glBegin(GL_QUADS);
            glNormal3f( 1.0f, 0.0f, 0.0f);
            glVertex3f( particlesize, -particlesize, -particlesize);
            glVertex3f( particlesize,  particlesize, -particlesize);
            glVertex3f( particlesize,  particlesize,  particlesize);
            glVertex3f( particlesize, -particlesize,  particlesize);
        glEnd();

            // Left Face
        glBegin(GL_QUADS);
            glNormal3f(-1.0f, 0.0f, 0.0f);
            glVertex3f(-particlesize, -particlesize, -particlesize);
            glVertex3f(-particlesize, -particlesize,  particlesize);
            glVertex3f(-particlesize,  particlesize,  particlesize);
            glVertex3f(-particlesize,  particlesize, -particlesize);
        glEnd();


        //glPopMatrix();
    }

    void glengine::draw()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
        glLoadIdentity();                   // Reset The Current Modelview Matrix

        gluLookAt(0, 5, 20, 0, 0, 0, 0, 1, 0);
        for(int i = 0; i < 500; i++)
            test2(i);
    }

    void glengine::update()
    {
        for(int i = 0; i < 500; i++)
        {
            if(p[i].a <= 0)
                p[i] = particle(5 + rand() % 360, (rand() % 10) * 0.1, Vertex(0.0f, 0.0f, -5.0f), 0, 255, 255, 255);

            else
                p[i].a -= 1;

            p[i].v.x += (sin(p[i].angle * (3.14159265/180)) * p[i].speed);
            p[i].v.y -= (cos(p[i].angle * (3.14159265/180)) * p[i].speed);
        }
    }

    void glengine::run()
    {
        while(processEvents())
        {
            update();
            draw();
            SDL_GL_SwapBuffers();
        }
    }
};

여전히 작동하지 않았습니다. 나는 정말로 내 재치가 끝났다.

도움이 되었습니까?

해결책 2

좋아, 나는 당신의 많은 제안과 내가 놓은 다른 소스 코드를 사용하여 그것을 고칠 수있었습니다. 문제는 3 개의 다른 줄에서 나온 것으로 나타났습니다.

입자 크기 = 0.01; 더 커야했을 것입니다 : 입자 크기 = 1.01;

glColor4i(255, 255, 255, 255) 내가 잘못 사용했기 때문에 큐브를 투명한 색상과 같은 색으로 돌리고있었습니다. 제대로 사용하는 방법을 알 수 없었기 때문에 사용하고 있습니다. glColor4f(0.0f,1.0f,1.0f,0.5f) 대신, 그것은 작동합니다.

마지막으로 gluLookAt(0, 5, 20, 0, 0, 0, 0, 0, 0) 필요했습니다 gluLookAt(0, 5, 20, 0, 0, 0, 0, 1, 0)

도움과 시간에 감사드립니다.

다른 팁

나는 당신의 코드를 확인하지 않았지만, 이런 종류의 문제를 디버깅 할 때 항상해야 할 일은 명확한 색상을 (1, 0, 1) 정도와 같은 화려한 것으로 설정하는 것입니다.

이렇게하면 문제가 그려진 객체가 완전히 검은 색이거나 전혀 그려지지 않은지 확인하는 데 도움이됩니다.

편집 : 의견에 언급 된 사람처럼 : 명확한 작업이 올바른 색상으로 지워지거나 검은 색을 유지하는 경우 올바른 GL 컨텍스트가 있는지 보여줍니다.

SDL-GL-Setattribute () 호출의 반환 값을 확인하지 않습니다.

그리고 5/5/5/5 20-bpp 색상이 비디오 카드에서 지원됩니까?

오류 상태를 확인하십시오. 사용 glslDevil, glIntercept 또는 gDebugger. 을 체크하다 glGetError 기능. SDL이 실제로 장치 컨텍스트를 획득했는지 여부를 테스트 할 수 있습니까? 창에 GlclearColor 호출의 변화가 반영됩니까? 0.5를 알파 값으로 사용하지 마십시오 glClearColor. 이러한 제안을 시도하고 Simucal이 제안한대로 최소한의 예제로 다시보고하십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top