私はゲーム開発を始めていますが、コツをつかむには、GLFW を使用するのが良いと思いました。基本的なチュートリアルに従い、いくつかの基本的な機能を備えたシンプルなゲームを作成しました。
私の問題はチュートリアルにあります.OpenGLウィンドウが設定されている場合、固定頂点を使用しますglBufferData
:
typedef struct {
GLfloat positionCoordinates[3];
GLfloat textureCoordiantes[2];
} VertexData;
VertexData vertices[] = {
{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
{{50.0f, 0.0f, 0.0f}, {1.0f, 0.0f}},
{{50.0f, 50.0f, 0.0f}, {1.0f, 1.0f}},
{{0.0f, 50.0f, 0.0f}, {0.0f, 1.0f}}
};
void GameWindow::setupGL() {
glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
glViewport(0, 0, _width, _height);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, _width, 0, _height);
glMatrixMode(GL_MODELVIEW);
glGenBuffers(1, &_vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(VertexData), (GLvoid *) offsetof(VertexData, positionCoordinates));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(VertexData), (GLvoid *) offsetof(VertexData, textureCoordiantes));
}
私が理解していることから、これは正しい方法であり、理にかなっていますが、読み込まれるすべてのテクスチャはこのサイズに準拠しています。したがって、画像の解像度が異なっていても、その頂点の仕様に引き伸ばされます。
GLuint GameWindow::loadAndBufferImage(const char *filename) {
GLFWimage imageData;
glfwReadImage(filename, &imageData, NULL);
GLuint textureBufferID;
glGenTextures(1, &textureBufferID);
glBindTexture(GL_TEXTURE_2D, textureBufferID);
std::cout << "Width:" << imageData.Width << " Height:" << imageData.Height << std::endl;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageData.Width, imageData.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData.Data);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glfwFreeImage(&imageData);
return textureBufferID;
}
これは OpenGL のかなり基本的な側面でなければならないことはわかっていますが、これらの画像を歪ませずに読み込むにはどうすればよいでしょうか?
編集: スプライトのレンダリング関数は次のとおりです(によって返される bufferID を使用します
loadAndBufferImage()
)
void Sprite::render() {
glBindTexture(GL_TEXTURE_2D, _textureBufferID);
glLoadIdentity();
glTranslatef(_position.x, _position.y, 0);
glRotatef(_rotation, 0, 0, 1.0f);
glDrawArrays(GL_QUADS, 0, 4);
}
編集2:
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0,0,0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(width,0,0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(width,height,0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0,height,0);
glEnd();