]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/glwidget.cpp
Initial changeset to experimental branch
[virtualjaguar] / src / gui / glwidget.cpp
1 // OpenGL implementation in Qt
2 // Parts of this are blantantly ripped off from BSNES (thanks Byuu!)
3 //
4 // by James L. Hammons
5 // (C) 2010 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  01/14/2010  Created this file
12 //
13
14 #include "glwidget.h"
15
16 #include "settings.h"
17
18 GLWidget::GLWidget(QWidget * parent/*= 0*/): QGLWidget(parent), texture(0),
19         textureWidth(0), textureHeight(0), buffer(0), rasterWidth(64), rasterHeight(64)
20 //      textureWidth(0), textureHeight(0), buffer(0), rasterWidth(256), rasterHeight(256)
21 {
22 }
23
24 GLWidget::~GLWidget()
25 {
26 }
27
28 void GLWidget::initializeGL()
29 {
30         format().setDoubleBuffer(true);
31         resizeGL(rasterWidth, rasterHeight);
32
33         glDisable(GL_ALPHA_TEST);
34         glDisable(GL_BLEND);
35         glDisable(GL_DEPTH_TEST);
36         glDisable(GL_POLYGON_SMOOTH);
37         glDisable(GL_STENCIL_TEST);
38         glEnable(GL_DITHER);
39         glEnable(GL_TEXTURE_2D);
40         glClearColor(0.0, 0.0, 0.0, 0.0);
41 }
42
43 void GLWidget::paintGL()
44 {
45         unsigned outputWidth  = width();
46         unsigned outputHeight = height();
47
48         glMatrixMode(GL_PROJECTION);
49         glLoadIdentity();
50         glOrtho(0, outputWidth, 0, outputHeight, -1.0, 1.0);
51         glViewport(0, 0, outputWidth, outputHeight);
52
53         glMatrixMode(GL_MODELVIEW);
54         glLoadIdentity();
55
56         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (smoothGLOutput ? GL_LINEAR : GL_NEAREST));
57         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (smoothGLOutput ? GL_LINEAR : GL_NEAREST));
58 //      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST /*GL_LINEAR*/);
59 //      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST /*GL_LINEAR*/);
60 //      glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
61         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
62
63         double w = (double)rasterWidth  / (double)textureWidth;
64         double h = (double)rasterHeight / (double)textureHeight;
65         unsigned u = outputWidth;
66         unsigned v = outputHeight;
67
68         glBegin(GL_TRIANGLE_STRIP);
69         glTexCoord2f(0, 0); glVertex3i(0, v, 0);
70         glTexCoord2f(w, 0); glVertex3i(u, v, 0);
71         glTexCoord2f(0, h); glVertex3i(0, 0, 0);
72         glTexCoord2f(w, h); glVertex3i(u, 0, 0);
73         glEnd();
74 }
75
76 void GLWidget::resizeGL(int width, int height)
77 {
78         if (width > textureWidth || height > textureHeight)
79         {
80 //              textureWidth  = max(width,  textureWidth);
81 //              textureHeight = max(height, textureHeight);
82 // Seems that power of 2 sizes are still mandatory...
83                 textureWidth  = 1024;//(width > textureWidth ? width : textureWidth);
84                 textureHeight = 512;//(height > textureHeight ? height : textureHeight);
85 //              textureWidth  = (width > textureWidth ? width : textureWidth);
86 //              textureHeight = (height > textureHeight ? height : textureHeight);
87 #if 0
88 printf("Resizing: new texture width/height = %i x %i\n", textureWidth, textureHeight);
89 printf("Resizing: new raster width/height = %i x %i\n", rasterWidth, rasterHeight);
90 #endif
91
92                 if (buffer)
93                 {
94                         delete[] buffer;
95                         glDeleteTextures(1, &texture);
96                 }
97
98                 buffer = new uint32_t[textureWidth * textureHeight];
99                 glGenTextures(1, &texture);
100                 glBindTexture(GL_TEXTURE_2D, texture);
101                 glPixelStorei(GL_UNPACK_ROW_LENGTH, textureWidth);
102                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
103                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
104 //              glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
105                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
106         }
107 }
108
109 #if 0
110 class RubyGLWidget: public QGLWidget
111 {
112   public:
113     GLuint texture;
114     unsigned textureWidth, textureHeight;
115
116     uint32_t * buffer;
117     unsigned rasterWidth, rasterHeight;
118
119     bool synchronize;
120     unsigned filter;
121
122     void updateSynchronization() {
123       #ifdef __APPLE__
124       makeCurrent();
125       CGLContextObj context = CGLGetCurrentContext();
126       GLint value = synchronize;  //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
127       CGLSetParameter(context, kCGLCPSwapInterval, &value);
128       #endif
129     }
130 } * widget;
131 #endif