コード例 #1
0
ファイル: GLView.cpp プロジェクト: red112/Naver3DGrp
void CGLView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: 여기에 그리기 코드를 추가합니다.
	BeginGL();

	Setup_Viewport();

	ClearBuffer();

	Setup_Projection();

	Setup_Camera();

	glLightfv(GL_LIGHT0, GL_POSITION, m_vp.LightPosition0);

	DrawGL();


	glFinish();

	Capture();

	SwapBuffers(m_dp.hdc);

	EndGL();

}
コード例 #2
0
ファイル: GLView.cpp プロジェクト: red112/Naver3DGrp
void CGLView::Init()
{
	BeginGL();

	glDepthFunc(GL_LESS);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_CULL_FACE);

	glLightfv(GL_LIGHT0, GL_AMBIENT, m_vp.LightAmbient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, m_vp.LightDiffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, m_vp.LightSpecular);
	glLightfv(GL_LIGHT0, GL_POSITION, m_vp.LightPosition0);
	if(m_vp.LightOn)
	{
		glEnable(GL_LIGHT0);
	}


	InitTexture();

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_INDEX_ARRAY);

	EndGL();
}
コード例 #3
0
ファイル: GLView.cpp プロジェクト: red112/Naver3DGrp
void CGLView::OnDestroy()
{
	CView::OnDestroy();

	BeginGL();
	ReleaseTexture();
	EndGL();


	// TODO: 여기에 메시지 처리기 코드를 추가합니다
	::ReleaseDC(m_hWnd,m_dp.hdc);
	wglDeleteContext(m_dp.hglrc);

}
コード例 #4
0
void ImageDrawingArea::InitializeOpenGL()
{
    BeginGL();

    // check PBO is supported
    const char *extensions = (const char*)glGetString(GL_EXTENSIONS);
    if ( strstr( extensions, "GL_ARB_pixel_buffer_object") != NULL)
    {

#ifdef _WIN32
        // get pointers to GL functions
        glGenBuffersARB = (PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffersARB");
        glBindBufferARB = (PFNGLBINDBUFFERARBPROC)wglGetProcAddress("glBindBufferARB");
        glBufferDataARB = (PFNGLBUFFERDATAARBPROC)wglGetProcAddress("glBufferDataARB");
        glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)wglGetProcAddress("glDeleteBuffersARB");

        if ( glGenBuffersARB == 0 || glBindBufferARB == 0 || glBufferDataARB == 0 || glDeleteBuffersARB == 0 )
        {
            // failed to get function pointer
        }
        else
        {
            m_PBOSupported = true;
            glGenBuffersARB( 1, &m_PBO);
        }
#else
        //
        // We can't figure out why extensions don't work on linux, so disabling.
        //
#endif
    }

    InitializeImageTexture();

    glShadeModel( GL_FLAT );

    // Initialize matrices
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluOrtho2D( 0, 1, 0, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    EndGL();
}
コード例 #5
0
bool ImageDrawingArea::on_configure_event( GdkEventConfigure* /*event*/ )
{
    BeginGL();

    int screenWidth = 0;
    int screenHeight = 0;
    get_window()->get_size( screenWidth, screenHeight );

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    if ( m_stretchToFit)
    {
        double heightStretch = (double) m_imageHeight * screenWidth / ( m_imageWidth * screenHeight);
        if ( heightStretch > 1.0)
        {
            m_scaleX = 1.0 / heightStretch;
            m_scaleY = 1.0;
        } else {
            m_scaleX = 1.0;
            m_scaleY = heightStretch;
        }
    }
    else
    {
        m_scaleX = (double)m_imageWidth / screenWidth;
        m_scaleY = (double)m_imageHeight / screenHeight;
    }

    LimitShift();

    double sx, sy;
    GetImageShift( sx, sy);
    m_signal_offset_changed( sx, sy);

    glViewport(
        0,
        0,
        screenWidth,
        screenHeight);

    EndGL();

    return true;
}
コード例 #6
0
bool ImageDrawingArea::on_expose_event( GdkEventExpose* /*event*/ )
{
    if ( m_pixBuf == NULL )
    {
        return true;
    }

    Glib::Mutex::Lock lock(m_pixBufMutex);

    const int width = m_pixBuf->get_width();
    const int height = m_pixBuf->get_height();
    double validTextureWidth = 1.0;
    double validTextureHeight = 1.0;
    bool useTiledTextures = false;
    GLenum errorno;

    BeginGL();

    SetImageSize( width, height );

    int screenWidth = 0;
    int screenHeight = 0;
    get_window()->get_size( screenWidth, screenHeight );

    // This makes sure that sampling for rendering doesn't occur on the border of pixels.
    const double halfPixelAdjustW = 0.25 / (double)screenWidth;
    const double halfPixelAdjustH = 0.25 / (double)screenHeight;

    glEnable( GL_TEXTURE_2D );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    // center the image
    glTranslated(
        -(m_scaleX - 1.0)/2.0 + halfPixelAdjustW,
        -(m_scaleY - 1.0)/2.0 + halfPixelAdjustH, 0.0);

    // apply mouse-drag shift
    glTranslated(m_shiftX, m_shiftY, 0.0);

    // scale the image
    glScaled(m_scaleX, m_scaleY, 0.0);

    // draw the image
    glBindTexture(GL_TEXTURE_2D, m_imageTextures[0]);

#ifdef _WIN32
    if (m_PBOSupported)
    {
        glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, m_PBO);
        errorno = glGetError();

        if (errorno != GL_NO_ERROR)
        {
            m_PBOSupported = false;
        }
        else
        {
            glBufferDataARB(
                GL_PIXEL_UNPACK_BUFFER_ARB,
                width * height * 4,
                m_pixBuf->get_pixels(),
                GL_STREAM_DRAW_ARB);
            errorno = glGetError();

            if (errorno != GL_NO_ERROR)
            {
                m_PBOSupported = false;
            }
        }
    }
#endif

    glTexImage2D(
        GL_TEXTURE_2D,
        0,
        GL_RGBA,
        width,
        height,
        0,
        GL_BGRA_EXT,
        GL_UNSIGNED_BYTE,
        m_PBOSupported ? NULL : m_pixBuf->get_pixels() );
    errorno = glGetError();

    if (errorno != GL_NO_ERROR)
    {
        // Attempt to fall back and use a power-of-two sized texture.
        // This is for older cards that don't support more arbitrary
        // texture sizes.

#ifdef _WIN32
        if (m_PBOSupported)
        {
            // unbind PBO to use normal texture transfer
            glBindBufferARB( GL_PIXEL_UNPACK_BUFFER_ARB, 0);
        }
#endif

        const int textureWidth = GetMinimumPowerOfTwo(width);
        const int textureHeight = GetMinimumPowerOfTwo(height);
        validTextureWidth = (double)width / textureWidth;
        validTextureHeight = (double)height / textureHeight;

        glTexImage2D(
            GL_TEXTURE_2D,
            0,
            GL_RGBA,
            textureWidth,
            textureHeight,
            0,
            GL_BGRA_EXT,
            GL_UNSIGNED_BYTE,
            NULL );
        errorno = glGetError();
        if (errorno != GL_NO_ERROR)
        {
            // The graphics doesn't seem to support this texture size.
            // Images must be split and then tiled.
            useTiledTextures = true;
        }
        else
        {
            glTexSubImage2D(
                GL_TEXTURE_2D,
                0,
                0,
                0,
                width,
                height,
                GL_BGRA_EXT,
                GL_UNSIGNED_BYTE,
                m_pixBuf->get_pixels() );
            errorno = glGetError();
            if ( errorno != GL_NO_ERROR)
            {
                // Error
            }
        }
    }

    if (useTiledTextures)
    {
        //
        // The image is split into multiple textures.
        //
        const int bytesPerPixel = 4;
        const int tileSize = 1024;
        int horizResidual = width % tileSize;
        int vertResidual = height % tileSize;
        int numHorizTextures = width / tileSize + (horizResidual > 0);
        int numVertTextures = height / tileSize + (vertResidual > 0);

        unsigned char *tileBuffer = new unsigned char[tileSize * tileSize * bytesPerPixel];

        for (int tileY = 0; tileY < numVertTextures ; tileY++)
        {
            for (int tileX = 0; tileX < numHorizTextures; tileX++)
            {
                int subTexHeight = tileSize;
                if ( tileY == numVertTextures - 1 && vertResidual > 0)
                {
                    subTexHeight = vertResidual;
                }

                int subTexWidth = tileSize;
                if (tileX == numHorizTextures - 1 && horizResidual > 0)
                {
                    subTexWidth = horizResidual;
                }

                // copy image buffer to the tile
                for (int line = 0; line < subTexHeight; line++)
                {
                    memcpy(
                        tileBuffer + line * tileSize * bytesPerPixel,
                        m_pixBuf->get_pixels() + ((line + tileSize * tileY) * width + tileSize * tileX) * bytesPerPixel,
                        subTexWidth * bytesPerPixel);
                }

                const unsigned int texId = tileY * numHorizTextures + tileX;
                if (texId >= sk_maxNumTextures)
                {
                    continue;
                }

                glBindTexture( GL_TEXTURE_2D, m_imageTextures[ texId] );
                glTexImage2D(
                    GL_TEXTURE_2D,
                    0,
                    GL_RGBA,
                    tileSize,
                    tileSize,
                    0,
                    GL_BGRA_EXT,
                    GL_UNSIGNED_BYTE,
                    tileBuffer );

                const double validTileWidth = (double)subTexWidth/tileSize;
                const double validTileHeight = (double)subTexHeight/tileSize;

                const double x_begin = (double)tileSize / width * tileX;
                const double x_end = (double)tileSize / width * ( tileX + validTileWidth);
                const double y_begin = 1.0 - (double)tileSize / height * ( tileY + validTileHeight);
                const double y_end = 1.0 - (double)tileSize / height * tileY;

                glBegin( GL_QUADS );

                glTexCoord2d(0.0, validTileHeight);
                glVertex2d(x_begin, y_begin);

                glTexCoord2d(validTileWidth, validTileHeight);
                glVertex2d(x_end, y_begin);

                glTexCoord2d(validTileWidth, 0.0);
                glVertex2d(x_end, y_end);

                glTexCoord2d(0.0, 0.0);
                glVertex2d(x_begin, y_end);

                glEnd();
            }
        }

        delete [] tileBuffer;
    }
    else
    {
        // Just one texture
        glBegin( GL_QUADS );

        glTexCoord2d(0.0, validTextureHeight);
        glVertex2d(0.0, 0.0);

        glTexCoord2d(validTextureWidth, validTextureHeight);
        glVertex2d(1.0, 0.0);

        glTexCoord2d(validTextureWidth, 0.0);
        glVertex2d(1.0, 1.0);

        glTexCoord2d(0.0, 0.0);
        glVertex2d(0.0, 1.0);

        glEnd();
    }

    if ( m_showCrosshair )
    {
        const double aspectRatio = (double)width/height;
        glTranslated(0.5, 0.5, 0.0);
        glScaled(1.0, aspectRatio, 0.0);
        ShowCrosshair();
    }

    if (get_gl_window()->is_double_buffered())
    {
        get_gl_window()->swap_buffers();
    }
    else
    {
        glFlush();
    }

    EndGL();

    lock.release();

    m_displayedFrameRate.NewFrame();

    return true;
}