C++ C++;OpenGL,GLFW绘制一个简单的立方体
所以,我试图在openGL和GLFW中绘制一个简单的立方体 在下面的代码中,我可以绘制立方体,但它只是显示为一个简单的矩形。这里发生了什么 我尝试过“glTransformf(0,0,-10);”,但是如果我做的任何事情小于-2,那么立方体就会出现。在-2处,将显示正面侧。在默认位置0处,我可以看到立方体的背面 另外,当我尝试旋转它时,显示的只是一个从窗口顶部移动到底部的矩形。看起来很奇怪 有人能帮我找出为什么程序会这样吗C++ C++;OpenGL,GLFW绘制一个简单的立方体,c++,opengl,glfw,C++,Opengl,Glfw,所以,我试图在openGL和GLFW中绘制一个简单的立方体 在下面的代码中,我可以绘制立方体,但它只是显示为一个简单的矩形。这里发生了什么 我尝试过“glTransformf(0,0,-10);”,但是如果我做的任何事情小于-2,那么立方体就会出现。在-2处,将显示正面侧。在默认位置0处,我可以看到立方体的背面 另外,当我尝试旋转它时,显示的只是一个从窗口顶部移动到底部的矩形。看起来很奇怪 有人能帮我找出为什么程序会这样吗 #if defined(_WIN32) || defined(_WIN6
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
const char* gameTitle = "TEST";
GLFWwindow* window;
GLfloat vertices[] =
{
-1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1,
1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1,
-1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1,
-1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1
};
GLfloat colors[] =
{
0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0,
1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0,
0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0,
0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1
};
static void controls(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if(action == GLFW_PRESS)
if(key == GLFW_KEY_ESCAPE)
glfwSetWindowShouldClose(window, GL_TRUE);
}
bool initWindow(const int resX, const int resY)
{
if(!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return false;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
// Open a window and create its OpenGL context
window = glfwCreateWindow(resX, resY, gameTitle, NULL, NULL);
if(window == NULL)
{
fprintf(stderr, "Failed to open GLFW window.\n");
glfwTerminate();
return false;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, controls);
// Get info of GPU and supported OpenGL version
printf("Renderer: %s\n", glGetString(GL_RENDERER));
printf("OpenGL version supported %s\n", glGetString(GL_VERSION));
glEnable(GL_DEPTH_TEST); // Depth Testing
glDepthFunc(GL_LEQUAL);
glDisable(GL_CULL_FACE);
glCullFace(GL_BACK);
return true;
}
static void drawCube()
{
static float alpha = 0;
glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
glTranslatef(0,0,-2);
glMatrixMode(GL_MODELVIEW_MATRIX);
//attempt to rotate cube
//glRotatef(alpha, 1, 0, 0);
/* We have a color array and a vertex array */
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);
/* Send data : 24 vertices */
glDrawArrays(GL_QUADS, 0, 24);
/* Cleanup states */
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
alpha += 0.1;
}
static void display()
{
glClearColor(0.0, 0.8, 0.3, 1.0);
while(!glfwWindowShouldClose(window))
{
// Draw stuff
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawCube();
// Update Screen
//glFlush();
glfwSwapBuffers(window);
// Check for any input, or window movement
glfwPollEvents();
// Scale to window size
GLint windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
}
}
int main(int argc, char** argv)
{
if(initWindow(1024, 620))
{
display();
}
printf("Goodbye!\n");
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
#如果已定义(_WIN32)|已定义(_WIN64)
#包括
#恩迪夫
#包括
#包括
#包括
#包括
#定义GLEW_静态
#包括
#包括
#包括
const char*gameTitle=“测试”;
GLFWwindow*窗口;
GLfloat顶点[]=
{
-1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1,
1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1,
-1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1,
-1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1
};
GLfloat颜色[]=
{
0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0,
1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0,
0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0,
0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1
};
静态无效控件(GLFWwindow*窗口、int键、int扫描码、int操作、int mods)
{
如果(动作==GLFW_按)
if(key==GLFW\u key\u ESCAPE)
glfwSetWindowShouldClose(窗口,GL_TRUE);
}
bool initWindow(const int resX,const int resY)
{
如果(!glfwInit())
{
fprintf(stderr,“未能初始化GLFW\n”);
返回false;
}
glfwWindowHint(GLFW_样本,4);//4x抗锯齿
//打开窗口并创建其OpenGL上下文
window=glfwCreateWindow(resX,resY,gameTitle,NULL,NULL);
如果(窗口==NULL)
{
fprintf(stderr,“无法打开GLFW窗口。\n”);
glfwTerminate();
返回false;
}
glfwMakeContextCurrent(窗口);
glfwSetKeyCallback(窗口、控件);
//获取GPU和支持的OpenGL版本的信息
printf(“渲染器:%s\n”,glGetString(GL_渲染器));
printf(“支持%s\n的OpenGL版本”,glGetString(GL_版本));
glEnable(GL_深度测试);//深度测试
glDepthFunc(GL_LEQUAL);
glDisable(GLU消隐面);
正面(背面);
返回true;
}
静态void drawCube()
{
静态浮点α=0;
glMatrixMode(GL_投影矩阵);
glLoadIdentity();
glTranslatef(0,0,-2);
glMatrixMode(GL_模型视图_矩阵);
//尝试旋转多维数据集
//glRotatef(α,1,0,0);
/*我们有一个颜色数组和一个顶点数组*/
glEnableClientState(GL_顶点_数组);
glEnableClientState(GL_颜色_阵列);
glVertexPointer(3,GLU浮点,0,顶点);
glColorPointer(3,GL_浮点,0,颜色);
/*发送数据:24个顶点*/
gldrawArray(GL_四边形,0,24);
/*清理状态*/
glDisableClientState(GL_颜色_数组);
glDisableClientState(GL_顶点_数组);
α+=0.1;
}
静态无效显示()
{
glClearColor(0.0,0.8,0.3,1.0);
而(!glfwWindowShouldClose(窗口))
{
//画画
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
drawCube();
//更新屏幕
//glFlush();
glfwSwapBuffers(窗口);
//检查是否有任何输入或窗口移动
glfwPollEvents();
//按窗口大小缩放
闪烁窗宽、窗高;
GLFWGetWindowsSize(窗口、窗口宽度和窗口高度);
glViewport(0,0,windowWidth,windowHeight);
}
}
int main(int argc,字符**argv)
{
如果(初始化窗口(1024620))
{
显示();
}
printf(“再见!\n”);
GLFW窗口(窗口);
glfwTerminate();
返回0;
}
drawCube()
、单一责任原则和所有这些中设置矩阵stdio.h
->cstdio
)。用那些来代替#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cstdio>
void controls(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if(action == GLFW_PRESS)
if(key == GLFW_KEY_ESCAPE)
glfwSetWindowShouldClose(window, GL_TRUE);
}
GLFWwindow* initWindow(const int resX, const int resY)
{
if(!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return NULL;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
// Open a window and create its OpenGL context
GLFWwindow* window = glfwCreateWindow(resX, resY, "TEST", NULL, NULL);
if(window == NULL)
{
fprintf(stderr, "Failed to open GLFW window.\n");
glfwTerminate();
return NULL;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, controls);
// Get info of GPU and supported OpenGL version
printf("Renderer: %s\n", glGetString(GL_RENDERER));
printf("OpenGL version supported %s\n", glGetString(GL_VERSION));
glEnable(GL_DEPTH_TEST); // Depth Testing
glDepthFunc(GL_LEQUAL);
glDisable(GL_CULL_FACE);
glCullFace(GL_BACK);
return window;
}
void drawCube()
{
GLfloat vertices[] =
{
-1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1,
1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1,
-1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1,
-1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1
};
GLfloat colors[] =
{
0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0,
1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0,
0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0,
0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1
};
static float alpha = 0;
//attempt to rotate cube
glRotatef(alpha, 0, 1, 0);
/* We have a color array and a vertex array */
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);
/* Send data : 24 vertices */
glDrawArrays(GL_QUADS, 0, 24);
/* Cleanup states */
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
alpha += 1;
}
void display( GLFWwindow* window )
{
while(!glfwWindowShouldClose(window))
{
// Scale to window size
GLint windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
// Draw stuff
glClearColor(0.0, 0.8, 0.3, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
gluPerspective( 60, (double)windowWidth / (double)windowHeight, 0.1, 100 );
glMatrixMode(GL_MODELVIEW_MATRIX);
glTranslatef(0,0,-5);
drawCube();
// Update Screen
glfwSwapBuffers(window);
// Check for any input, or window movement
glfwPollEvents();
}
}
int main(int argc, char** argv)
{
GLFWwindow* window = initWindow(1024, 620);
if( NULL != window )
{
display( window );
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
#包括
#包括
#包括
无效控件(GLFWwindow*窗口、int键、int扫描码、int操作、int mods)
{
如果(动作==GLFW_按)
if(key==GLFW\u key\u ESCAPE)
glfwSetWindowShouldClose(窗口,GL_TRUE);
}
GLFWwindow*initWindow(常量整数resX,常量整数resY)
{
如果(!glfwInit())
{
fprintf(stderr,“未能初始化GLFW\n”);
返回NULL;
}
glfwWindowHint(GLFW_样本,4);//4x抗锯齿
//打开窗口并创建其OpenGL上下文
GLFWwindow*window=glfwCreateWindow(resX,resY,“测试”,NULL,NULL);
如果(窗口==NULL)
{
fprintf(stderr,“无法打开GLFW窗口。\n”);
glfwTerminate();
返回NULL;
}
glfwMakeContextCurrent(窗口);
glfwSetKeyCallback(窗口、控件);
//获取GPU和支持的OpenGL版本的信息
printf(“渲染器:%s\n”,glGetString(GL_渲染器));
printf(“支持%s\n的OpenGL版本”,glGetString(GL_版本));
glEnable(GL_深度测试);//深度测试
glDepthFunc(GL_LEQUAL);
glDisable(GLU消隐面);
正面(背面);
返回窗口;
}
void drawCube()
{
GLfloat顶点[]=
{
-1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1,
1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1,
-1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1,
-1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1
};
GLfloat颜色[]=
{
0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0,
1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0,
0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0,
0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1
};
静态浮动
glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
gluPerspective(45, windowWidth / windowHeight, 0.1f, 100.0f);
// Draw calls.