GLFW larger window than screen resolution - c++

When rendering off-screen content with OpenGL I am trying to use a window larger than the current screen resolution. The following code fails to render correctly if so (it renders to a portion of the window), but works OK when the window size is <= the resolution. (Error checks and stuff removed).
void run(int wi2,int he2)
{
glfwInit();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
win = glfwCreateWindow(wi2, he2, "OpenGL", 0, 0);
glfwMakeContextCurrent(win);
hOpenGL = glfwGetWin32Window(win);
ShowWindow(hOpenGL, SW_HIDE);
if (!loadExtensions())
{
....
}
glEnable(GL_DEPTH_TEST); // Use the Z buffer
glfwSwapInterval(0); // Do not wait for screen refresh between frames
glfwSetWindowSize(win, wi2,he2);
glViewport(0, 0, wi2,he2);
SetEvent(hRun2);
MSG msg;
for (;;)
{
HANDLE he[2] = { hRun1,hRunE };
auto gc = MsgWaitForMultipleObjects(2, he, 0, INFINITE, QS_ALLEVENTS);
if (gc == WAIT_OBJECT_0 + 1)
{
// end
glfwDestroyWindow(win);
win = 0;
break;
}
else
if (gc == WAIT_OBJECT_0)
{
// render
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(runc.bg.r, runc.bg.g, runc.bg.b, runc.bg.a);
// Load uniforms, others are omitted for simplicity
float wh[4] = { wi2,he2,0,0 };
auto uloc =
glGetUniformLocation(runc.prg, "iResolution");
glUniform3fv(uloc, 1, wh);
glLoadIdentity();
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(-wi2 / 2, -he2 / 2);
glVertex2i(wi2 / 2, -he2 / 2);
glVertex2i(wi2 / 2, he2 / 2);
glVertex2i(-wi2 / 2, he2 / 2);
glEnd();
auto r = runc.glOut->GetRawData();
memset(r, 0, wi2 * he2 * 4);
glReadPixels(0, 0, wi2, he2, GL_BGRA, GL_UNSIGNED_BYTE, r);
glfwSwapBuffers(win);
SetEvent(hRun2);
}
else { ... }
}
glfwTerminate();
win = 0;
hOpenGL = 0;
SetEvent(hRun2);
}
Is there a way to have a window rendering larger than the current screen size? Even in software rendering.

Related

OpenGL Sphere not showing

The screenshot below shows the desired result.
below is the actual result
I used the glutWireSphere API for drawing my sphere, however, it is not showing.
#include <GL/glut.h>
const int RED_COLOR[3] = {255, 0, 0};
const int GREEN_COLOR[3] = { 0, 0, 255};
// color to draw in
void setDrawColor(const int rgb[3]) {
float d = 255.0; // d = max value in an rgb color spectrum
glColor3f(rgb[0]/d, rgb[1]/d, rgb[2]/d); // set draw color R=x/255 G=y/255 B=z/255 where x,y,z are values in rgb channel respectively
}
void drawPlane() {
GLfloat A[3] = { -1, 0, 1 };
GLfloat B[3] = { 1, 0, 1 };
GLfloat C[3] = { 1, 0, -1 };
GLfloat D[3] = { -1, 0, -1 };
glBegin(GL_POLYGON);
glVertex3fv(A);
glVertex3fv(B);
glVertex3fv(C);
glVertex3fv(D);
glEnd();
}
GLfloat CamX = 0, CamY = 2, CamZ = 2;
// Display Call Back
void draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear all drawings in buffer
glLoadIdentity();
gluLookAt(CamX, CamY, CamZ, 0, 0, 0, 0, 1, 0);
drawPlane();
setDrawColor(GREEN_COLOR); // sets the drawing color to GREEN
glutWireSphere(0.2, 20, 20);
glutSwapBuffers();// Render Now i.e convert Buffer to Picture
return;
}
// Initialization
void initialize()
{
glClearColor(0.1f, 0.1f, 0.1f, 0.1f); // Set Background Color
setDrawColor(RED_COLOR); // sets the drawing color to red
glEnable(GL_DEPTH_TEST); // enable viewing the 3d
glMatrixMode(GL_PROJECTION); // change to perspective projection
glLoadIdentity(); // what does this do?
glFrustum(-1, 1, -1, 1, 2, 10); // what does this do?
glMatrixMode(GL_MODELVIEW); // what does this do?
}
// Main
int main(int argc, char* argv[])
{
glutInit(&argc, argv); // Initialize GLUT
int x = 512, y = 512; // x and y value
glutInitWindowPosition(
(int)(glutGet(GLUT_SCREEN_WIDTH) - x) / 2,
(int)(glutGet(GLUT_SCREEN_HEIGHT) - y) / 2); // Position the window's center
glutInitWindowSize(x, y); // Set the window's initial width & height
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); // initialise the buffers needed. double buff, one for RGB color the other for x,y, z
glutCreateWindow("3D Bowling Game"); // Create a window with the given title
initialize(); // Custom initialisation
glutDisplayFunc(draw); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}
what am i doing wrong?

Why doesnt the text Render with nvgText() in my implementation but works fine in the Example?

The Example followed from the NanoVG Examples.
DemoData data;
NVGcontext* vg = NULL;
GPUtimer gpuTimer;
PerfGraph fps, cpuGraph, gpuGraph;
double prevt = 0, cpuTime = 0;
if (!glfwInit()) {
printf("Failed to init GLFW.");
return -1;
}
initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time");
initGraph(&cpuGraph, GRAPH_RENDER_MS, "CPU Time");
initGraph(&gpuGraph, GRAPH_RENDER_MS, "GPU Time");
glfwSetErrorCallback(errorcb);
#ifndef _WIN32 // don't require this on win32, and works with more cards
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
#ifdef DEMO_MSAA
glfwWindowHint(GLFW_SAMPLES, 4);
#endif
//window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwSetKeyCallback(window, key);
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
printf("Could not init glew.\n");
return -1;
}
// GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
glGetError();
//vg = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_DEBUG);
vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);
if (vg == NULL) {
printf("Could not init nanovg.\n");
return -1;
}
if (loadDemoData(vg, &data) == -1)
return -1;
glfwSwapInterval(0);
initGPUTimer(&gpuTimer);
glfwSetTime(0);
prevt = glfwGetTime();
while (!glfwWindowShouldClose(window))
{
double mx, my, t, dt;
int winWidth, winHeight;
int fbWidth, fbHeight;
float pxRatio;
float gpuTimes[3];
int i, n;
t = glfwGetTime();
dt = t - prevt;
prevt = t;
startGPUTimer(&gpuTimer);
glfwGetCursorPos(window, &mx, &my);
glfwGetWindowSize(window, &winWidth, &winHeight);
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
// Calculate pixel ration for hi-dpi devices.
pxRatio = (float)fbWidth / (float)winWidth;
// Update and render
glViewport(0, 0, fbWidth, fbHeight);
if (premult)
glClearColor(0, 0, 0, 0);
else
glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
renderDemo(vg, mx, my, winWidth, winHeight, t, blowup, &data);
//works here
//char sample[] = "Sample";
//nvgBeginPath(vg);
//nvgFontSize(vg, 18.0f);
//nvgFontFace(vg, "sans");
//nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
//nvgText(vg, 200, 200, sample, NULL);
//nvgFill(vg);
renderGraph(vg, 5, 5, &fps);
renderGraph(vg, 5 + 200 + 5, 5, &cpuGraph);
if (gpuTimer.supported)
renderGraph(vg, 5 + 200 + 5 + 200 + 5, 5, &gpuGraph);
nvgEndFrame(vg);
// Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU)
cpuTime = glfwGetTime() - t;
updateGraph(&fps, dt);
updateGraph(&cpuGraph, cpuTime);
// We may get multiple results.
n = stopGPUTimer(&gpuTimer, gpuTimes, 3);
for (i = 0; i < n; i++)
updateGraph(&gpuGraph, gpuTimes[i]);
if (screenshot) {
screenshot = 0;
saveScreenShot(fbWidth, fbHeight, premult, "dump.png");
}
glfwSwapBuffers(window);
glfwPollEvents();
}
freeDemoData(vg, &data);
nvgDeleteGL3(vg);
printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f);
printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f);
printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f);
glfwTerminate();
return 0;
This Segment works in the implementation above.
nvgBeginPath(vg);
nvgFontSize(vg, 18.0f);
nvgFontFace(vg, "sans");
nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
nvgText(vg, 200, 200, sample, NULL);
nvgFill(vg);
It Doesnt Render in the implementation below.
GLFWwindow* window;
NVGcontext* vg = NULL;
//initializing GLFW
if (!glfwInit()) {
printf("Failed to init GLFW.");
return -1;
}
glfwSetErrorCallback(errorcb);
//creating a GL-Window
window = glfwCreateWindow(1366, 768, "NanoVG", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
//glfwGetCursorPos(window, &xpos, &ypos);
glfwSetKeyCallback(window, key);
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
printf("Could not init glew.\n");
return -1;
}
glGetError();
//initialize nanovg
vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);
if (vg == NULL) {
printf("Could not init nanovg.\n");
return -1;
}
glfwSwapInterval(0);
defaultColor = nvgRGBA(255, 255, 255, 255);
defaultWidth = 1.0f;
/*glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1366, 768, 0.0f, 0.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-1366, -768, 0.0f);
glScalef(1.0, -1.0, 1.0f);*/
while (!glfwWindowShouldClose(window))
{
int winWidth, winHeight, pxRatio;
int fbWidth, fbHeight;
glfwGetCursorPos(window, &mx, &my);
glfwGetWindowSize(window, &winWidth, &winHeight);
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
pxRatio = (float)fbWidth / (float)winWidth;
glViewport(0, 0, winWidth, winHeight);
glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
//doesnt work here
char sample[] = "Sample";
nvgBeginPath(vg);
nvgFontSize(vg, 18.0f);
nvgFontFace(vg, "sans");
nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
nvgText(vg, 500, 500, sample, NULL);
nvgFill(vg);
nvgEndFrame(vg);
glfwSwapBuffers(window);
glfwPollEvents();
iteration_Number++;
}
nvgDeleteGL3(vg);
glfwTerminate();
return 0;
Am i missing something basic? Sorry i am a beginner at nanovg and opengl? I have two of the mentioned snippets in two different functions and i execute only one at once.
Anyone?
2 Problems:
1. Had to initialise the font from the disk before i could use it in a rendering context.
nvgCreateFont(vg, "sans", ".\\example\\Roboto-Regular.ttf");
Included this line and worked fine.
I didnt read the Nanovg Documentation well, and understood it partially.

SDL not displaying shape

Within my project, I've been having trouble getting an triangle to display within OnRender(), but for some reason, nothing other than the background color (green) is visible.
int main(int argc, char **argv)
{
if (!OnInit())
return -1;
SDL_Event Event;
while (_isRunning)
{
while (SDL_PollEvent(&Event))
OnEvent(&Event);
OnRender();
OnLoop();
SDL_GL_SwapWindow(_screen);
}
OnCleanup();
return 0;
}
void generalSetup()
{
// Initialize SDL2
if (SDL_Init(SDL_INIT_VIDEO) < 0)
sdldie("Failed to initial SDL2.");
else
{
/* Request OpenGL 3.2 */
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
// Create window
_screen = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
// Create Context
_mainContext = SDL_GL_CreateContext(_screen);
// Create Surface
_surface = SDL_GetWindowSurface(_screen);
SDL_FillRect(_surface, NULL, SDL_MapRGB(_surface->format, 0xCC, 0x20, 0x20));
SDL_UpdateWindowSurface(_screen);
/* swap synchronized */
SDL_GL_SetSwapInterval(1);
// Initialize GLew 1.10
glewExperimental = GL_TRUE;
GLenum error = glewInit();
if (error != GLEW_OK)
printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
else
std::cout << "GLew Initialized" << std::endl;
glClearColor(0, 1, 0, 0);
glViewport(0, 0, 800, 600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 800 / 600, 1, 1000);
gluLookAt(0, 0, 20, 0, 0, 0, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
}
bool OnInit()
{
generalSetup();
return true;
}
void OnEvent(SDL_Event* Event)
{
if (Event->type == SDL_QUIT)
_isRunning = false;
}
void OnLoop()
{
}
void OnRender()
{
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.f, 0.f, -10.f);
glBegin(GL_TRIANGLES);
glColor3f(0.1, 0.2, 0.3);
glVertex3f(0, 0, 0);
glVertex3f(1, 0, 0);
glVertex3f(0, 1, 0);
glEnd();
glPopMatrix();
}
void OnCleanup()
{
SDL_GL_DeleteContext(_mainContext);
SDL_DestroyWindow(_screen);
SDL_Quit();
}
You requested a Core context. None of your immediate-mode (matrix stack, glBegin(), etc.) code will work.
Drop back to a compatibility context (SDL_GL_CONTEXT_PROFILE_COMPATIBILITY) or supply all the necessary shaders, vertex buffers, etc. that Core requires.
You are trying to render through the immediate mode, but it is not supported by the OpenGL 3.2. Try using the version 2.0 or 2.1, which support both shaders (if you are intending to use them) and the immediate mode.

Screen goes black when drawing to it SFML C++

I have a basic game set up in C++, openGL and SFML.
But then when i try to add text or draw a square to the screen(for the GUI) the screen goes mainly black, i can see something happens in the top left, but i don't know what
And i receive this error in the console:
An internal OpenGL call failed in RenderTarget.cpp<362> : GL_INVLAD_OPERATION,
the specified operation is not allowed in the current state
Here is the full code:
#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);
}
In my init function i simply set the text properties like so:
text.setString("Hello SFML");
text.setCharacterSize(50);
text.setPosition(10.0f, 10.0f);
text is declared in my header file as sf::Text text.
I cant see any reason for the text not to be printed to the screen!
EDIT
Ok so i looked into the whole pushing and popping openGL states and implemented it as shown on a thread and i now get this image!:
So im getting there, as you can see the problem with this is the player is no longer shown and the input is all messed up :/
Here is the updated loop:
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();
}
}
In regards to loading a font i have implemented it but for some reason the font cant be found with this code:
if(!font.loadFromFile("../arial.tff"))
{
cout<<"no font"<<endl;
}
although the file is in the file it is directed at?
1: if the sf::Font you set for that sf::Text (which i assume you must have) goes out of scope it wont display it, because its font reference is gone.
2: if you have another thread running (possibly displaying your scenery) and you set your App.setActive() the other thread is deactivated automagically
3: openGL states sometimes conflict with SFML if you're switching between them.. Save them, draw with SFML, restore them, then you can resume openGL drawing.
hope this helps a little.
oh and hey, look at this wonderful page:
http://www.sfml-dev.org/tutorials/2.0/window-opengl.php

SDL openGL issue, no openGL drawing seen

I am using cygwin SDL 1.2.15 using the latest cygwin
Here is my code using SDL and openGL
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <iostream>
size_t sx=600, sy=600, bpp=32;
void render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // set location in front of camera
//glTranslated(0, 0, -10);
glBegin(GL_QUADS); // draw a square
glColor3d(1, 0, 0);
glVertex3d(-2, 2, 0);
glVertex3d( 2, 2, 0);
glVertex3d( 2, -2, 0);
glVertex3d(-2, -2, 0);
glEnd();
glFlush();
SDL_GL_SwapBuffers();
GLenum e;
while ((e =glGetError()) != GL_NO_ERROR)
std::cout<<"Error "<< e << std::endl;
}
int input(void) {
SDL_Event event;
while (SDL_PollEvent(&event))
if (event.type == SDL_QUIT || (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE)) return 0;
return 1;
}
and this is my main function
int main(int argc, char *argv[]) {
SDL_Surface *surf;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) return 0;
if (!(surf = SDL_SetVideoMode(sx, sy, bpp, SDL_OPENGL))) return 0;
glViewport(0, 0, sx, sy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)sx / (float)sy, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 1);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
GLenum e;
while ((e =glGetError()) != GL_NO_ERROR)
std::cout<<"Error "<< e << std::endl;
for (;;) {
if (!input()) break;
render();
SDL_Delay(10);
}
SDL_FreeSurface(surf);
SDL_Quit();
return 0;
}
it compiles with no error but when I run it only the window shows up and now openGL rectangle..
You have setup a near plane to one :
gluPerspective(45.0, (float)sx / (float)sy, 1.0/*near plane*/, 100.0);
Everything that is closer to the camera is clipped.
Your quad lies in plane z = 0. Try moving it a bit backward.
glBegin(GL_QUADS); // draw a square
glColor3d(1, 0, 0);
glVertex3d(-2, 2, 5);
glVertex3d( 2, 2, 5);
glVertex3d( 2, -2, 5);
glVertex3d(-2, -2, 5);
glEnd();
I don't remember if Z is facing the camera, so you might need negative Z value.
You also need to pay attention to face culling. It might be better to deactivate it to be sure ( glDisable( GL_CULL_FACE ))
Try changing the black color of the SDL window. Some times it renders the drawing with black color...may be this helps!