我想在屏幕上绘制两个对象。
在Etay Meiri 的这个非常简单的教程中,他在屏幕上创建了一个点。我尝试将其扩展为两个点(哇),但我根本无法理解如何绘制第二个对象。
这是我尝试过的:
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "math_3d.h"
GLuint VBO;
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_POINTS, 0, 1);
glDisableVertexAttribArray(0);
glutSwapBuffers();
}
static void InitializeGlutCallbacks()
{
glutDisplayFunc(RenderSceneCB);
}
static void CreateVertexBuffer() {
Vector3f Object[1];
Object[0] = Vector3f(0.0f, 0.0f, 0.0f);
// Added another object
Vector3f Object2[1];
Object2[0] = Vector3f(1.0f, 0.0f, 0.0f);
glGenBuffers(2, &VBO); // Incrementet the number of buffers to 2
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Object), Object, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(Object2), Object, GL_STATIC_DRAW); // Added another buffer
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(1024, 768);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutorial 02");
InitializeGlutCallbacks();
// Must be done after glut is initialized!
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
CreateVertexBuffer();
glutMainLoop();
return 0;
}
垫子文件:
#ifndef MATH_3D_H
#define MATH_3D_H
struct Vector3f
{
float x;
float y;
float z;
Vector3f()
{
}
Vector3f(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
};
#endif /* MATH_3D_H */