bate's blog

調べたこと実装したことなどを取りとめもなく書きます。

GLFW3.0.1とGLEW1.10.0でVBO

後はシェーダーを使えば最少サンプルになる。

// glew32s.libを使う.
#define GLEW_STATIC

#include <stdio.h>
#include <stdlib.h>

#include <GL/glew.h>
#include <GLFW/glfw3.h>

const int g_WindowWidth = 640;
const int g_WindowHeight = 480;
GLuint g_VBO = 0;
GLuint g_Indices = 0;
GLFWwindow* g_Window = NULL;

float g_RotZ = 0.0f;

int main()
{
	if(!glfwInit()) {
		exit(EXIT_FAILURE);
	}
	g_Window = glfwCreateWindow(g_WindowWidth, g_WindowHeight, "Test", NULL, NULL);
	if(!g_Window) {
		glfwTerminate();
		exit(EXIT_FAILURE);
	}
	glfwMakeContextCurrent(g_Window);

	GLenum err = glewInit();
	if(err != GLEW_OK) {
		glfwTerminate();
		exit(EXIT_FAILURE);
	}

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	float aspectRatio = g_WindowHeight/float(g_WindowWidth);
	glFrustum(0.5f, -0.5f, -0.5f*aspectRatio, 0.5f*aspectRatio, 1.0f, 50.f);
	glMatrixMode(GL_MODELVIEW);

	GLfloat vertexBuffer [] = { 0, 2, 0, -2, -2, 0, 2, -2, 0 };
	GLubyte indexBuffer[] = { 0, 1, 2 };

	glGenBuffers(1, &g_VBO);
	glGenBuffers(1, &g_Indices);

	glBindBuffer(GL_ARRAY_BUFFER, g_VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBuffer), vertexBuffer, GL_STATIC_DRAW);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_Indices);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexBuffer), indexBuffer, GL_STATIC_DRAW);

	while(!glfwWindowShouldClose(g_Window)) {

		if(glfwGetKey(g_Window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
			glfwSetWindowShouldClose(g_Window, GL_TRUE);
		}

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		glTranslatef(0,0,-10);
		glRotatef(g_RotZ, 0, 0, 1);
		glColor3f(1, 0, 0);

		g_RotZ += 0.5f;

		glBindBuffer(GL_ARRAY_BUFFER, g_VBO);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_Indices);

		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, 0);
		
		glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, 0);

		glfwSwapBuffers(g_Window);

		glfwPollEvents();
	}

	glDeleteBuffers(1, &g_VBO);
	glDeleteBuffers(1, &g_Indices);

	glfwDestroyWindow(g_Window);
	glfwTerminate();
	return 0;
}

広告を非表示にする