- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个用 C++、openGL 和 SFML 设置的基本游戏。
但是当我尝试在屏幕上添加文本或绘制正方形(对于 GUI)时,屏幕主要变黑,我可以看到左上角发生了一些事情,但我不知道是什么
我在控制台中收到此错误:
An internal OpenGL call failed in RenderTarget.cpp<362> : GL_INVLAD_OPERATION, the specified operation is not allowed in the current state
完整代码如下:
#include "Engine.h"
static sf::RenderWindow App(sf::VideoMode(1600, 900, 32), "SFML OpenGL");
Engine::Engine()
{
glewInit();
c_player = Player(Vector3D(0, 14, 0), 0);
c_enemy = Enemy(Vector3D(0, 14, -10), 0);
c_gui = Gui(App);
c_mouseSensitivityX = 5;
c_mouseSensitivityY = 5;
}
Engine::~Engine(void)
{
}
void Engine::init(void)
{
text.setString("Hello SFML");
text.setCharacterSize(50);
text.setPosition(10.0f, 10.0f);
c_wall.LoadModel("Wall.dae");
c_crates.LoadModel("crates.dae");
c_cargoWall.LoadModel("cargoWall.dae");
c_leftCorner.LoadModel("leftCorner.dae");
c_rightCorner.LoadModel("rightCorner.dae");
c_shipPipeWall.LoadModel("shipPipeWall.dae");
c_shipWindow.LoadModel("shipWindow.dae");
float posx = -200;
float posfar = 200;
float posnear = -200;
/*for(int i = -200; i < 200 ; i += 20)
{
c_wallProperties.push_back(wallProps(Vector3D(i, 15, posfar), 0));
c_wallProperties.push_back(wallProps(Vector3D(i, 15, posnear),0));
c_wallProperties.push_back(wallProps(Vector3D(posfar, 15, i), 90));
c_wallProperties.push_back(wallProps(Vector3D(posnear, 15, i),90));
}*/
int xflip = 1;
for (int i=0; i<2; ++i)
{
c_wallProperties.push_back(wallProps(Vector3D(70 * xflip,15,20), 0));
c_crateProperties.push_back(wallProps(Vector3D(55 * xflip,15,10), 0));
// //c_wallProperties.push_back(wallProps(Vector3D(40 * xflip,15,20), 0));
// ////c_wallProperties.push_back(wallProps(Vector3D(25 * xflip,15,20), 0));
// //c_wallProperties.push_back(wallProps(Vector3D(85 * xflip,15,20), 0));
// //c_wallProperties.push_back(wallProps(Vector3D(100 * xflip,15,20), 0));
// //c_wallProperties.push_back(wallProps(Vector3D(140 * xflip,15,20), 0));
// //c_wallProperties.push_back(wallProps(Vector3D(70 * xflip,15,-10), 0));
// //c_wallProperties.push_back(wallProps(Vector3D(55 * xflip,15,-10), 0));
// //c_wallProperties.push_back(wallProps(Vector3D(40 * xflip,15,-10), 0));
// ////c_wallProperties.push_back(wallProps(Vector3D(25 * xflip,15,-10), 0));
// //c_wallProperties.push_back(wallProps(Vector3D(85 * xflip,15,-10), 0));
// //c_wallProperties.push_back(wallProps(Vector3D(100 * xflip,15,-10), 0));
// c_wallProperties.push_back(wallProps(Vector3D(140 * xflip,15,-10), 0));
xflip = xflip * -1;
}
//c_myModels.push_back(new buildings("rightCorner.dae",Vector3D(120,15,1)));
////c_myModels.push_back(new Objects("phoenix_ugv.md2", Vector3D(-50,0,20)));
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 0.f);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat lightpos[] = {.5, 1., 1., 0.};
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
c_FPSClock.restart();
ParticleSystem newParticle;
newParticle.setMaxParticles(500);
newParticle.setParticleColour(sf::Color(143, 44, 1, 255), sf::Color(255, 127, 39, 255));
newParticle.setParticleLifespan(60, 180);
newParticle.setParticleSpawnrate(0.2, 0.3);
newParticle.setParticleVelocity(Vector3D(-10, 30, -10), Vector3D(10, 50, 10));
newParticle.setParticleAcceleration(Vector3D(0, -9.81, 0), Vector3D(0, -9.81, 0));
newParticle.setSpawnerPosition(Vector3D(0, 10, 0));
c_particleSystems.push_back(newParticle);
}
void Engine::getInput(void)
{
while (App.pollEvent(c_event))
{
// Close window : exit
if (c_event.type == sf::Event::Closed)
App.close();
// Resize event : adjust viewport
if (c_event.type == sf::Event::Resized)
glViewport(0, 0, c_event.size.width, c_event.size.height);
// Camera Controls
if (c_event.type == sf::Event::KeyPressed)
{
switch (c_event.key.code)
{
case sf::Keyboard::Escape:
App.close();
break;
case sf::Keyboard::W:
c_player.move(Vector3D(0, 0, -1));
break;
case sf::Keyboard::S:
c_player.move(Vector3D(0, 0, 1));
break;
case sf::Keyboard::A:
c_player.move(Vector3D(-1, 0, 0));
break;
case sf::Keyboard::D:
c_player.move(Vector3D(1, 0, 0));
break;
case sf::Keyboard::X:
c_camera.togglePerspective();
break;
}
}
}
}
void Engine::run()
{
while (App.isOpen())
{
while (c_FPSClock.getElapsedTime().asSeconds() < 1.f/60)
{
}
// Set the active window before using OpenGL commands
// It's useless here because active window is always the same,
// but don't forget it if you use multiple windows or controls
App.setActive();
//App.draw(rectangle);
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
getInput();
update();
render();
}
}
void Engine::update(void)
{
mouseMove();
c_worldEngine.update();
c_player.update();
c_enemy.update();
c_camera.update(c_player);
for (vector<ParticleSystem>::iterator iter = c_particleSystems.begin(); iter != c_particleSystems.end(); ++iter)
{
iter->update();
}
}
void Engine::render(void)
{
c_camera.render();
c_gui.render();
c_worldEngine.render(c_camera.getPos());
App.draw(text);
for (vector<pair<Vector3D, float>>::iterator iter = c_wallProperties.begin(); iter != c_wallProperties.end(); ++iter)
{
glEnable(GL_TEXTURE_3D);
glPushMatrix();
glTranslatef(iter->first.x, iter->first.y, iter->first.z);
glRotatef(iter->second, 0, 1, 0);
c_wall.Render();
glPopMatrix();
glDisable(GL_TEXTURE_3D);
}
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
if (c_camera.isFirstPerson() == false)
{
c_player.render();
}
c_enemy.render();
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
c_glModel.render(Vector3D(0,5,0));
for (vector<ParticleSystem>::iterator iter = c_particleSystems.begin(); iter != c_particleSystems.end(); ++iter)
{
iter->render();
}
App.display();
c_FPSClock.restart();
}
void Engine::mouseMove(void)
{
sf::Vector2u winSize = App.getSize();
sf::Vector2u winMid = sf::Vector2u(winSize.x/2, winSize.y/2);
sf::Vector2i mousePos = sf::Mouse::getPosition(App);
if ((mousePos.x == winMid.x) && (mousePos.y == winMid.y))
{
return;
}
sf::Mouse::setPosition(Vector2i(winMid.x, winMid.y), App);
sf::Vector2i mouseDiff = sf::Vector2i(winMid.x - mousePos.x, winMid.y - mousePos.y);
sf::Vector2f diffRatio = sf::Vector2f(mouseDiff.x / c_mouseSensitivityX, mouseDiff.y / c_mouseSensitivityY);
if (c_camera.isFirstPerson())
{
diffRatio.y /= 10;
}
c_player.rotate(diffRatio.x);
c_camera.rotateY(diffRatio.y);
}
在我的初始化函数中,我只是像这样设置文本属性:
text.setString("Hello SFML");
text.setCharacterSize(50);
text.setPosition(10.0f, 10.0f);
文本在我的头文件中声明为 sf::Text 文本。
我看不出有什么理由不将文本打印到屏幕上!
编辑好的,所以我研究了整个推送和弹出 openGL 状态,并按照线程上显示的方式实现了它,现在我得到了这张图片!:
所以我到了那里,正如您所看到的问题是播放器不再显示并且输入全乱了:/
这是更新后的循环:
void Engine::run()
{
while (App.isOpen())
{
while (c_FPSClock.getElapsedTime().asSeconds() < 1.f/60)
{
}
App.pushGLStates();
App.draw(text);
App.popGLStates();
// Set the active window before using OpenGL commands
// It's useless here because active window is always the same,
// but don't forget it if you use multiple windows or controls
App.setActive();
//App.draw(rectangle);
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
getInput();
update();
render();
}
}
关于加载字体,我已经实现了它,但由于某种原因,无法使用此代码找到字体:
if(!font.loadFromFile("../arial.tff"))
{
cout<<"no font"<<endl;
}
虽然文件在它指向的文件中?
最佳答案
1:如果你为那个 sf::Text 设置的 sf::Font(我假设你必须有)超出范围它不会显示它,因为它的字体引用已经消失。
2:如果你有另一个线程在运行(可能显示你的风景)并且你设置了你的 App.setActive() 另一个线程会自动停用
3:如果你在它们之间切换,openGL 状态有时会与 SFML 冲突。保存它们,用 SFML 绘制,恢复它们,然后你可以恢复 openGL 绘图。
希望对您有所帮助。
哦,嘿,看看这个精彩的页面: http://www.sfml-dev.org/tutorials/2.0/window-opengl.php
关于c++ - 向其绘制时屏幕变黑 SFML C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16108309/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!