-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial9.cpp
More file actions
151 lines (114 loc) · 4.25 KB
/
tutorial9.cpp
File metadata and controls
151 lines (114 loc) · 4.25 KB
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
//
// tutorial9.cpp
// sample
//
// Created by 伊藤 皓程 on 2015/07/22.
// Copyright (c) 2015年 伊藤皓程. All rights reserved.
//
#define GLFW_DLL
#define GLEW_STATIC
#if defined (_MSC_VER)
#include <GL/g]ew.h>
#endif
#include <GL/glfw.h>
#include <cstdlib>
#include <fstream>
#include <vector>
#if defined (_MSC_VER)
#pragma comment(lib, "GLFWDLL.lib")
#pragma comment(lib, "opengl32.lib")
#ifdef _DEBUG
#pragma comment(lib, "glew32sd.lib")
#else
#pragma comment(lib, "glew32s.lib")
#endif
#endif
//テクスチャ識別子にファイルから読み込んだデータを与える
bool setupTexture(const GLuint id, const char* file, int width, int height){
//ファイルをバイナリモードで開く
std::ifstream fstr(file, std::ios::binary);
if(!fstr) return false;
//ファイルサイズを取得
const size_t file_size = static_cast<size_t>(fstr.seekg(0, fstr.end).tellg());
//読み込み位置をファイルの先頭に戻す
fstr.seekg(0, fstr.beg);
//動的配列を使ってファイルを読み込む場所を確保する
std::vector<char> texture_buffer(file_size);
//確保した場所へファイルを読み込む
fstr.read(&texture_buffer[0], file_size);
//テクスチャ識別子に対して指示を与える
glBindTexture(GL_TEXTURE_2D, id);
//1ピクセルに3色と透明度の情報を持つ, ピクセルの画像データをopenglに転送する
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &texture_buffer[0]);
//画像が拡大された時の振る舞いを指定する
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//テクスチャの丸め込み方法を指定する
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
return true;
}
int main(){
if(!glfwInit()){
return EXIT_FAILURE;
}
if(!glfwOpenWindow(0, 0, 0, 0, 0, 0, 0, 0, GLFW_WINDOW)){
glfwTerminate();
return EXIT_FAILURE;
}
//テクスチャ識別子を作る
GLuint texture_id;
glGenTextures(1, &texture_id);
//画像を読み込む
//失敗したら識別子を削除して終了する
if(!setupTexture(texture_id, "sample.raw", 256, 256)){
glDeleteTextures(1, &texture_id);
glfwTerminate();
return EXIT_FAILURE;
}
glfwSwapInterval(1);
while(glfwGetWindowParam(GLFW_OPENED)){
//描画バッファを塗りつぶす色成分を指定する
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//描画バッファを塗りつぶす
glClear(GL_COLOR_BUFFER_BIT);
//描画する点の座標を配列で用意する
static const GLfloat vtx[] = {
-0.5f, -0.5f,
0.5f, -0.5f,
0.5f, 0.5f,
-0.5f, 0.5f
};
//頂点ごとのUV座標を指定する
static const GLfloat texture_uv[] = {
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f
};
static const GLfloat texture_uv1[] = {
-2.5f, 0.0f,
2.5f, 0.0f,
2.5f, 5.0f,
-2.5f, 5.0f
};
//描画に使う頂点配列を指定する
glVertexPointer(2, GL_FLOAT, 0, vtx);
glTexCoordPointer(2, GL_FLOAT, 0, texture_uv1);
glEnable(GL_TEXTURE_2D);
//頂点配列で描画するモードに切り替える
glEnableClientState(GL_VERTEX_ARRAY);
//描写時にテクスチャ座標配列も使うと指示する
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_QUADS, 0, 4);
//描画モードを元に戻す
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
glfwSwapBuffers();
}
//テクスチャ識別子を削除する
glDeleteTextures(1, &texture_id);
glfwTerminate();
return EXIT_SUCCESS;
}