]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/glwidget.cpp
d4a65ec59568986c4555ad9cbe767bbbba9692ef
[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, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
57         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
58 //      glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
59         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
60
61         double w = (double)rasterWidth  / (double)textureWidth;
62         double h = (double)rasterHeight / (double)textureHeight;
63         unsigned u = outputWidth;
64         unsigned v = outputHeight;
65
66         glBegin(GL_TRIANGLE_STRIP);
67         glTexCoord2f(0, 0); glVertex3i(0, v, 0);
68         glTexCoord2f(w, 0); glVertex3i(u, v, 0);
69         glTexCoord2f(0, h); glVertex3i(0, 0, 0);
70         glTexCoord2f(w, h); glVertex3i(u, 0, 0);
71         glEnd();
72 }
73
74 void GLWidget::resizeGL(int width, int height)
75 {
76         if (width > textureWidth || height > textureHeight)
77         {
78 //              textureWidth  = max(width,  textureWidth);
79 //              textureHeight = max(height, textureHeight);
80 // Seems that power of 2 sizes are still mandatory...
81                 textureWidth  = 1024;//(width > textureWidth ? width : textureWidth);
82                 textureHeight = 512;//(height > textureHeight ? height : textureHeight);
83 //              textureWidth  = (width > textureWidth ? width : textureWidth);
84 //              textureHeight = (height > textureHeight ? height : textureHeight);
85 #if 0
86 printf("Resizing: new texture width/height = %i x %i\n", textureWidth, textureHeight);
87 printf("Resizing: new raster width/height = %i x %i\n", rasterWidth, rasterHeight);
88 #endif
89
90                 if (buffer)
91                 {
92                         delete[] buffer;
93                         glDeleteTextures(1, &texture);
94                 }
95
96                 buffer = new uint32_t[textureWidth * textureHeight];
97                 glGenTextures(1, &texture);
98                 glBindTexture(GL_TEXTURE_2D, texture);
99                 glPixelStorei(GL_UNPACK_ROW_LENGTH, textureWidth);
100                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
101                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
102 //              glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
103                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
104         }
105 }
106
107 #if 0
108 class RubyGLWidget: public QGLWidget
109 {
110   public:
111     GLuint texture;
112     unsigned textureWidth, textureHeight;
113
114     uint32_t * buffer;
115     unsigned rasterWidth, rasterHeight;
116
117     bool synchronize;
118     unsigned filter;
119
120     void updateSynchronization() {
121       #ifdef __APPLE__
122       makeCurrent();
123       CGLContextObj context = CGLGetCurrentContext();
124       GLint value = synchronize;  //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
125       CGLSetParameter(context, kCGLCPSwapInterval, &value);
126       #endif
127     }
128 } * widget;
129 #endif