-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.h
110 lines (90 loc) · 2.83 KB
/
main.h
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
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <iostream>
#include "definitions.h"
#include "mygl.h"
GLuint tex;
void (*DrawFunc)(void);
//*****************************************************************************
void display(void)
{
DrawFunc();
// Copia o framebuffer para a textura.
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, IMAGE_WIDTH, IMAGE_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, FBptr);
glEnable(GL_TEXTURE_2D);
// Desenha o quadrilátero com a textura mapeada
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glViewport(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
glBegin(GL_TRIANGLES);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
//*****************************************************************************
void exitprog(void)
{
// Libera a memória referente ao framebuffer.
if (!FBptr)
delete [] FBptr;
std::clog << "Exiting...\n";
}
//*****************************************************************************
void InitOpenGL(int *argc, char **argv)
{
glutInit(argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(IMAGE_WIDTH, IMAGE_HEIGHT);
glutInitWindowPosition(100,100);
glutCreateWindow("My OpenGL");
// Ajusta a projeção ortográfica.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//*****************************************************************************
void InitCallBacks(void)
{
atexit( exitprog );
glutDisplayFunc(display);
}
//*****************************************************************************
void InitDataStructures(void)
{
// Aloca o framebuffer e inicializa suas posições com 0.
FBptr = new unsigned char[IMAGE_WIDTH * IMAGE_HEIGHT * 5];
for (unsigned int i = 0; i < IMAGE_WIDTH * IMAGE_HEIGHT ; i++)
{
FBptr[i*4] = 0;
FBptr[i*4+1] = 0;
FBptr[i*4+2] = 0;
FBptr[i*4+3] = 255;
}
// Cria uma textura 2D, RGBA (8 bits por componente).
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
}