/
glstuff.c
233 lines (205 loc) · 8.03 KB
/
glstuff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//global includes
#include <GL/glew.h>
#include <GL/gl.h>
#include "SDL.h"
//local includes
#include "globaldefs.h"
#include "sdlstuff.h" //todo something this
#include "shaderstuff.h"
#include "glstuff.h"
//local vars
GLuint texid2, texid1;
GLuint fbid2, fbid1;
GLuint fsvbo, fsivbo, fsvao;
int which = FALSE;
int count = 0;
float aspect = 0;
//float fsverts[] = {-1.0, -1.0, -0.5, 0.0, 0.0,
// 1.0, -1.0, -0.5, 1.0, 0.0,
// 1.0, 1.0, -0.5, 1.0, 1.0,
// -1.0, 1.0, -0.5, 0.0, 1.0};
float fsverts[] = {-1.0, -1.0, 0.0, 0.0,
1.0, -1.0, 1.0, 0.0,
1.0, 1.0, 1.0, 1.0,
-1.0, 1.0, 0.0, 1.0};
GLuint fsindices[] = {0,1,2, 2,3,0};
//functions
extern void glDrawScreen();
int initFB(void){
if(playwidth ==0) playwidth = 1;
if(playheight ==0) playheight = 1;
aspect = (float)screenWidth/(float)screenHeight;
glGenTextures(1, &texid1);
glBindTexture(GL_TEXTURE_2D, texid1);
//may need to change gl_int to byte or something fo speed
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, playwidth, playheight, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
glGenFramebuffers(1, &fbid1);
glBindFramebuffer(GL_FRAMEBUFFER, fbid1);
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, texid1, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER, 0);//unbind
if(debugmode) printf("DEBUG -- Bound framebuffer1 to: %i and texture to : %i \n", fbid1, texid1);
glGenTextures(1, &texid2);
glBindTexture(GL_TEXTURE_2D, texid2);
//may need to change gl_int to byte or something fo speed
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, playwidth, playheight, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
glGenFramebuffers(1, &fbid2);
glBindFramebuffer(GL_FRAMEBUFFER, fbid2);
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, texid2, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER, 0);//unbind
if(debugmode) printf("DEBUG -- Bound framebuffer2 to: %i and texture to : %i \n", fbid2, texid2);
//todo error detection
return TRUE;
}
int initVBO(void){
glGenVertexArrays(1, &fsvao);
glGenBuffers(1, &fsvbo);
glGenBuffers(1, &fsivbo);
glBindVertexArray(fsvao);
glBindBuffer(GL_ARRAY_BUFFER, fsvbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(fsverts),fsverts, GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 4*sizeof(GLfloat), NULL);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 4*sizeof(GLfloat), ((char*)NULL)+2*sizeof(GLfloat) );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, fsivbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(fsindices),fsindices, GL_STATIC_DRAW);
return TRUE;
}
int glInit(void){
if(debugmode) printf("DEBUG -- Initialising OpenGL \n");
GLenum glewError = glewInit();
if( glewError != GLEW_OK ){
printf( "Error initializing GLEW! %s\n", glewGetErrorString( glewError ) );
return FALSE;
}
glDisable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glEnable(GL_TEXTURE_2D);
//todo move this stuff
glViewport(0, 0, playwidth, playheight);
glMatrixMode(GL_PROJECTION);// Select The Projection Matrix
glLoadIdentity();// Reset The Projection Matrix
glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, 0.1f, 100.0f); //maybe change
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
glColor3f(1.0f, 1.0f, 1.0f); //wont need this much longer
//todo error checkelecking
if(!initShader()) return FALSE;
if(!initFB()) return FALSE;
// glUseProgram(programobject);
initVBO();
return TRUE;
}
void drawfsquad(void){
/* glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);glVertex3f(-1.0f,-1.0f, -0.5f);
glTexCoord2f(1.0f, 0.0f);glVertex3f( 1.0f,-1.0f, -0.5f);
glTexCoord2f(1.0f, 1.0f);glVertex3f( 1.0f, 1.0f, -0.5f);
glTexCoord2f(0.0f, 1.0f);glVertex3f(-1.0f, 1.0f, -0.5f);
glEnd();
*/
/*
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);glVertex2f(-1.0f,-1.0f);
glTexCoord2f(1.0f, 0.0f);glVertex2f( 1.0f,-1.0f);
glTexCoord2f(1.0f, 1.0f);glVertex2f( 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);glVertex2f(-1.0f, 1.0f);
glEnd();
*/
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
void drawsmallquad(const float sizex, const float sizey){
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);glVertex3f(-sizex,-sizey, -0.5f);
glTexCoord2f(1.0f, 0.0f);glVertex3f( sizex,-sizey, -0.5f);
glTexCoord2f(1.0f, 1.0f);glVertex3f( sizex, sizey, -0.5f);
glTexCoord2f(0.0f, 1.0f);glVertex3f(-sizex, sizey, -0.5f);
glEnd();
}
void drawposquad(const float sizex, const float sizey, const float xpos, const float ypos){ //todo maybe calculate these when changes occour instead of every frame?
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);glVertex3f(xpos-sizex,ypos-sizey, -0.5f);
glTexCoord2f(1.0f, 0.0f);glVertex3f(xpos+sizex,ypos-sizey, -0.5f);
glTexCoord2f(1.0f, 1.0f);glVertex3f(xpos+sizex,ypos+sizey, -0.5f);
glTexCoord2f(0.0f, 1.0f);glVertex3f(xpos-sizex,ypos+sizey, -0.5f);
glEnd();
}
int loadTexture(const char * filename){
//todo make texture struct
void * data;
int width, height;
GLuint texture;
sdlImportImage(filename, &height, &width, &data);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER,fbid2);
glBindTexture(GL_TEXTURE_2D, texture);
glUseProgram(0);
drawsmallquad((float)width/(float)playwidth, (float)height/(float)playheight); //i forgot the casting rules for int division
glDeleteTextures( 1, &texture);
glDrawScreen(FALSE);
glUseProgram(programobject);
return TRUE;
}
void startsmall(void){
glBindFramebuffer(GL_FRAMEBUFFER, fbid2);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f); //wont need this much longer
drawsmallquad(0.5f, 0.5f);
}
int glRender(void){
// glUseProgram(programobject);
if(which){ // this is a fraction of a fraction of a percent faster... theoretically
glBindFramebuffer(GL_FRAMEBUFFER,fbid2);
glBindTexture(GL_TEXTURE_2D, texid1);
// glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, texid2, 0);
} else {
glBindFramebuffer(GL_FRAMEBUFFER,fbid1);
glBindTexture(GL_TEXTURE_2D, texid2);
// glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, texid1, 0);
}
drawfsquad();
if(count > skip){
glDrawScreen(FALSE);
count = 0;
}
which = !which;
count++;
//todo errorcheckin
return TRUE;
}
void glDrawScreen(int redraw){
glViewport(0, 0, screenWidth, screenHeight);
glMatrixMode(GL_PROJECTION);// Select The Projection Matrix
glLoadIdentity();// Reset The Projection Matrix
glOrtho(-aspect, aspect, -1.0f, 1.0f, 0.1f, 100.0f); //maybe change
// glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
// glLoadIdentity(); // Reset The Modelview Matrix
if(which)glBindTexture(GL_TEXTURE_2D, texid1);
else glBindTexture(GL_TEXTURE_2D, texid2);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glUseProgram(0);
if(redraw) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawposquad(zoom, zoom, viewposx, viewposy);
// glBlitFramebuffer(0,0,1000,1000,0,0,1000,1000, GL_COLOR_BUFFER_BIT,GL_LINEAR);
//todo make this cleaner
SDL_GL_SwapBuffers();
glViewport(0, 0, playwidth, playheight);
glMatrixMode(GL_PROJECTION);// Select The Projection Matrix
glLoadIdentity();// Reset The Projection Matrix
glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, 0.1f, 100.0f); //maybe change
// glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
// glLoadIdentity(); // Reset The Modelview Matrix
glUseProgram(programobject);
}