C++ 尽管坐标发生变化,顶点始终位于原点
我正在尝试在新的Linux安装上使用OpenGL开发,无论我将顶点坐标更改为什么,我最终都会在窗口的中心找到一个点(微小的白点): 以下是main.cpp:C++ 尽管坐标发生变化,顶点始终位于原点,c++,linux,opengl,graphics,glut,C++,Linux,Opengl,Graphics,Glut,我正在尝试在新的Linux安装上使用OpenGL开发,无论我将顶点坐标更改为什么,我最终都会在窗口的中心找到一个点(微小的白点): 以下是main.cpp: #include <stdio.h> #include <GL/glew.h> #include <GL/glut.h> #include "math_3d.h" GLuint VBO; static void CreateVertexBuffer() { Vector3f Vertices
#include <stdio.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include "math_3d.h"
GLuint VBO;
static void CreateVertexBuffer()
{
Vector3f Vertices[1];
我将GL_FLOAT
传递到glvertexattributepointer
中,因此0.5f应为屏幕X和Y方向的1/2
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
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);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(200, 200);
glutCreateWindow("Move you little Ass!");
InitializeGlutCallbacks();
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();
}
这是math_3d.h中的Vector3f
结构:
struct Vector3f
{
float x;
float y;
float z;
Vector3f()
{
}
Vector3f(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
};
这是完整的密码吗?没有着色器?如果是这种情况,这个答案很可能适用于您的问题:。若要更改向量坐标,需要调用glTranslatef或glRotatef函数。这些功能将允许您围绕帧移动对象。顶点[0]=向量3f(0.5f,0.5f,0.0f);将只创建一个带有RGB颜色编码的矢量,与像素移动无关。需要围绕帧移动顶点的X和Y坐标。如果要在框架上绘制形状,可能还需要了解glPushMatrix()和glMatrixMode(GL_MODELVIEW)函数。@Reto Koradi Yea就是这样,谢谢@Juniar认为绘图函数家族已被弃用,人们现在应该做的是教授着色器,而不是固定的函数管道
struct Vector3f
{
float x;
float y;
float z;
Vector3f()
{
}
Vector3f(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
};