-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial14.cpp
More file actions
122 lines (93 loc) · 3.63 KB
/
tutorial14.cpp
File metadata and controls
122 lines (93 loc) · 3.63 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
//
// tutorial14.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)){
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;
}
//描画用に100頂点を生成する
GLfloat vtx[100 * 2];
for(int i = 0; i < 100; ++i){
vtx[i * 2] = (rand() % 1000) / 500.0f - 1.0f;
vtx[i * 2 + 1] = (rand() % 1000) / 500.0f - 1.0f;
}
glVertexPointer(2, GL_FLOAT, 0, vtx);
while(glfwGetWindowParam(GLFW_OPENED)){
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(32.0f);
glEnable(GL_TEXTURE_2D);
//ポイントスプライトを有効にする
glEnable(GL_POINT_SPRITE);
//ポイントスプライト向けにテクスチャ座標を計算する機能をONにする
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
//ポイントスプライトのどの位置をテクスチャ座標の(0, 0)とするか指定する
glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_UPPER_LEFT);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_POINTS, 0, 100);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_POINT_SPRITE);
glDisable(GL_TEXTURE_2D);
glfwSwapBuffers();
}
glDeleteTextures(1, &texture_id);
glfwTerminate();
return EXIT_SUCCESS;
}