source: yuvplayer/win/yuvplayer/OpenGLView.cpp @ 90

Revision 90, 6.2 KB checked in by aqua, 14 months ago (diff)

convert vs2005 project to vs2008 project and add license term(BSD)

Line 
1/*
2 * Copyright (c) 2010, Tae-young Jung
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 *    must display the following acknowledgement:
14 *    This product includes software developed by the <organization>.
15 * 4. Neither the name of the <organization> nor the
16 *    names of its contributors may be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31// OpenGLView.cpp : implementation file
32//
33
34#include "stdafx.h"
35#include "yuvplayer.h"
36#include "OpenGLView.h"
37
38#include <gl/gl.h>
39
40// COpenGLView
41
42IMPLEMENT_DYNCREATE(COpenGLView, CView)
43
44COpenGLView::COpenGLView()
45{
46        loaded = FALSE;
47
48        t_width = 0;
49        t_height = 0;
50
51        ratio = 1.0;
52}
53
54COpenGLView::~COpenGLView()
55{
56}
57
58BEGIN_MESSAGE_MAP(COpenGLView, CView)
59        ON_WM_CREATE()
60        ON_WM_SIZE()
61        ON_WM_DESTROY()
62        ON_WM_ERASEBKGND()
63END_MESSAGE_MAP()
64
65
66// COpenGLView drawing
67
68void COpenGLView::OnDraw(CDC* pDC)
69{
70        CDocument* pDoc = GetDocument();
71        HDC dc = ::GetDC(m_hWnd);
72
73        // TODO: add draw code here     
74        glClear(GL_COLOR_BUFFER_BIT);           // clear screen and depth buffer
75
76        if( loaded ){
77                glBindTexture( GL_TEXTURE_2D, texture);
78                glBegin(GL_QUADS);
79                        glTexCoord2f( 0.f, 0.f );
80                        glVertex3i( 0, 0, 0);
81
82                        glTexCoord2f( 0.f, 1.f );
83                        glVertex3i( 0, t_height, 0);
84
85                        glTexCoord2f( 1.f, 1.f );
86                        glVertex3i( t_width, t_height, 0);
87
88                        glTexCoord2f( 1.f, 0.f );
89                        glVertex3i( t_width, 0, 0);
90                glEnd();
91        }
92        SwapBuffers( dc );
93
94        ::ReleaseDC( m_hWnd, dc );
95
96}
97
98
99// COpenGLView diagnostics
100
101#ifdef _DEBUG
102void COpenGLView::AssertValid() const
103{
104        CView::AssertValid();
105}
106
107#ifndef _WIN32_WCE
108void COpenGLView::Dump(CDumpContext& dc) const
109{
110        CView::Dump(dc);
111}
112#endif
113#endif //_DEBUG
114
115
116// COpenGLView message handlers
117BOOL COpenGLView::PreCreateWindow(CREATESTRUCT& cs)
118
119{
120
121       // TODO: Modify the Window class or styles here by modifying
122       cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CS_OWNDC; 
123
124       return CView::PreCreateWindow(cs);
125
126} 
127int COpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct)
128{
129        if (CView::OnCreate(lpCreateStruct) == -1)
130                return -1;
131
132        // TODO:  Add your specialized creation code here
133        int nPixelFormat;                                       // our pixel format index
134
135        static PIXELFORMATDESCRIPTOR pfd = {
136                sizeof(PIXELFORMATDESCRIPTOR),  // size of structure
137                1,                                                              // default version
138                PFD_DRAW_TO_WINDOW |                    // window drawing support
139                PFD_SUPPORT_OPENGL |                    // OpenGL support
140                PFD_DOUBLEBUFFER,                               // double buffering support
141                PFD_TYPE_RGBA,                                  // RGBA color mode
142                32,                                                             // 32 bit color mode
143                0, 0, 0, 0, 0, 0,                               // ignore color bits, non-palettized mode
144                0,                                                              // no alpha buffer
145                0,                                                              // ignore shift bit
146                0,                                                              // no accumulation buffer
147                0, 0, 0, 0,                                             // ignore accumulation bits
148                0,                                                              // no z-buffer size
149                0,                                                              // no stencil buffer
150                0,                                                              // no auxiliary buffer
151                PFD_MAIN_PLANE,                                 // main drawing plane
152                0,                                                              // reserved
153                0, 0, 0 };                                              // layer masks ignored
154
155        HDC hdc = ::GetDC(m_hWnd);
156
157        nPixelFormat = ChoosePixelFormat(hdc, &pfd);
158        BOOL success = SetPixelFormat(hdc, nPixelFormat, &pfd);
159        m_hRC = wglCreateContext(hdc);
160
161        wglMakeCurrent(hdc, m_hRC);
162
163        glDisable(GL_DEPTH_TEST);
164        glEnable(GL_TEXTURE_2D);
165
166        glGenTextures( 1, &texture );
167        glBindTexture(GL_TEXTURE_2D, texture );
168
169        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
170        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
171
172        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
173        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
174
175        glClearColor(0,0,0,0);
176
177        return 0;
178}
179
180void COpenGLView::OnSize(UINT nType, int cx, int cy)
181{
182        CView::OnSize(nType, cx, cy);
183
184        // TODO: Add your message handler code here
185        glViewport( 0, 0, cx, cy );
186       
187    glMatrixMode(GL_PROJECTION);        // set projection matrix current matrix
188        glLoadIdentity();
189        glOrtho( 0, cx, cy, 0, 0, 100);
190
191        glMatrixMode(GL_MODELVIEW);
192        glLoadIdentity();
193
194        glTranslatef( 0, 0, -10.0f );
195        glScalef( ratio, ratio, 1.f );
196
197}
198
199void COpenGLView::OnDestroy()
200{
201        CView::OnDestroy();
202
203        // TODO: Add your message handler code here
204        wglDeleteContext(m_hRC);
205}
206
207void COpenGLView::SetParam(int width, int height, float ratio)
208{
209        for( t_width = 2  ; t_width  < width  ; t_width  *= 2 );
210        for( t_height = 2 ; t_height < height ; t_height *= 2 );
211
212        this->ratio = ratio;
213}
214
215void COpenGLView::LoadTexture(unsigned char* rgba)
216{
217
218        glBindTexture(GL_TEXTURE_2D, texture );
219        if( loaded )
220                glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, t_width, t_height, GL_RGBA, GL_UNSIGNED_BYTE, rgba );
221        else{
222                glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, t_width, t_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba );
223                loaded = TRUE;
224        }
225        Invalidate(NULL);
226}
227
228BOOL COpenGLView::OnEraseBkgnd(CDC* pDC)
229{
230        // TODO: Add your message handler code here and/or call default
231
232        return TRUE;
233        //return CView::OnEraseBkgnd(pDC);
234}
Note: See TracBrowser for help on using the repository browser.