所以,我试图在openGL和GLFW中绘制一个简单的立方体。
#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;
}
drawcube()
、单一责任原则等中设置矩阵。C
-前缀版本(stdio.h
->cstdio
)的C标头。改用这些。一起:
#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;
}
我对OpenGL、GLFW或GLEW没有太多的经验,所以我对这些库的故障排除能力微乎其微。我已经设法呈现了一个三角形([-1,-1,0],[1,-1,0],[0,1,0]),但是当使用顶点属性坐标和颜色属性作为立方体时,它似乎无法呈现。我的代码、着色器和矩阵运算可能有很多问题。我希望我对我所做的事情有更清楚的了解,这样我就可以对我的错误有更详细的描述。目前,上面的代码只呈现窗口。我最初遵循http
我能够创建一个窗口,并清除到所需的颜色。但无法在左下角绘制正方形。
我试图为OpenGL工作设置一个跨平台代码库,下面的代码在我硬盘的Windows7分区上绘制得很好。然而,在小牛,我只得到一个黑屏,不知道为什么。我已经尝试了指南和相关问题中建议的所有东西,但到目前为止都没有效果!希望我只是遗漏了一些明显的东西,因为我对OpenGL还很陌生。 通过Xcode编译,使用2013年Macbook Mini,Intel HD Graphics 5000。可能还值得注意的
实现的基本思路很简单,首先提供如上图所示立方体线框所有顶点的三维坐标,然后通过旋转矩阵对所有顶点进行旋转变换,最后调用绘制函数gl.drawArrays把所有点连成线渲染出来。 通过第1.3、1.4两节课案例知道,显示器上显示的实际上是平面的像素,可以简单理解为三维几何体放在你眼睛和显示器之间,几何体在显示器上的投影,视线一定的情况下, 你看到的投影效果取决几何体的位置状态,如果你学过画法几何应该
我正在为学校做一项作业,我无法弄清楚我需要用来解决这个问题的逻辑。我希望你们中的一些人能为我指出正确的方向。 我有一些类来绘制简单的形状: 根据赋值,下的每个延迟类都需要一个方法才能画特定的(,etc...)独立于。对于,我的猜测是这个方法类似于: 扩展类图显示了一个接口 ,我为每个形状创建了一个方法。JavaFX端()实现了这个接口及其所有方法。这些方法希望 shape 对象作为参数。我使用这些
绘制矩形 与其它图形库不同,LCUI 提供的图形 API 只支持矩形这一种形式的图形绘制,不支持基于路径来绘制复杂图形。因此,对于其它复杂的图形,你需要手动编写代码填充像素来绘制。 LCUI 提供了一种绘制矩形的方法: int Graph_FillRect(LCUI_Graph *graph, LCUI_Color color, LCUI_Rect *rec