The program freezes when I call glBindTexture(GL_TEXTURE_2D, _ID); in the draw(Sprite) method of the 'Renderer' class. (the actual code is sprite.getTexture()->bind(), but I have added a std::cout in that function before and after the glBindTexture() call and it only prints once).
I am struggling to understand why the program is freezing - when I call glBindTexture, the program doesn't respond and crashes. Rather than filing this page up with a long list of code, heres the link to the github: https://github.com/TheInfernalcow/OpenGL-game, the files which are relevant are mainly the src/graphics/renderer.cpp and src/graphics/texture.cpp.
If anyone has the time to read through the code and try and point me in the right direction I would be grateful, have been pondering over this for hours.
Is it Splashstate that is having issue? If so -
SplashState::SplashState(Game* game)
{
_game = game;
Texture2D backgroundTexture("res/darkguy.png", 96, 128);
Sprite _background;
_background.setTexture(backgroundTexture);
}
You are assigning a texture to the locally scoped Sprite, not the one on the SplashState -- so when you try to draw it in your render function, the class level Sprite has no texture.
Related
Context
I'm a beginner in 3D graphics and I'm starting out with Vulkan, which I already know it's not recommended save it please, currently working on a university project to develop the base of a 3D computer graphics engine based on the Vulkan API.
The problem
Example of running the app to render the classic 2D triangle
Drawing a 3D mesh after having drawn the triangle
So as you can see in the images above I want to be able to:
Run the engine.
Choose an object to be drawn.
Close the window.
Choose another object to be drawn.
Open the same window back up with only the last object chosen visible.
And the way I have been doing this is by essentially cleaning up the whole swap chain and recreating it from scratch once the window is closed and a new object has been chosen. Now I'm aware this probably sounds like terrorism for any computer graphics engineer but the reason I'm doing this is because I don't know a better way, I have just finished the vulkan tutorial.
Solutions tried
I have checked that I do a vkDestroyBuffer and vkFreeMemory on the current vertex buffer before recreating it again once I choose a different object.
I have disabled depth testing entirely in case it had something to do with it, it doesn't.
Note: The code is extensive and I really don't have a clue of which part of it could be relevant to the problem, so I opted for not cluttering the question, if there is an specific part you think it might help you find the solution please request it.
Thank you for taking the time to read my question.
A comment by user369070 ended up drawing my attention to the function I use to read OBJ files which made me realize that this function wasn't cleaning a data structure I use to store the vertices of the object chosen to be drawn before passing them to the vertex buffer.
I just had to add vertices = {}; at the top of the function to solve it.
I'm very new to OpenGL. I want to learn it from the Blue Book (OpenGL superbible 6th edition).
When I compile the first program with Visual Studio 2013, everything goe well, except that a white window appears and then the program quit with code 0.
The program is:
// Include the "sb6.h" header file
#include "sb6.h"
// Derive my_application from sb6::application
class my_application : public sb6::application
{
public:
// Our rendering function
void render(double currentTime)
{
// Simply clear the window with red
static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, red);
}
};
// Our one and only instance of DECLARE_MAIN
DECLARE_MAIN(my_application);
I think both the compiling and building process are working fine because the result code 0. But I cannot figure out why I don't see a red output window. Please help me.
You are basically dealing with a real-time application which means it takes very little time to render a frame. Rendering a frame means, OpenGL will take all the commands you used to define the scene (the geometry, the settings of the buffers, the shaders, etc.) and render 1 frame as quickly as it can. Generally, if your application needs to be real-time, it will have to be able to render this scene is more than 30 frame per second.
So what basically OpenGL does is render this frame, and then quit. Normally (you don't say in your post which framework you use to create your application, GLUT, GLFW? Well you say you use the code the blue book but not sure which OpenGL lib it uses, it's obviously a wrapper around something else), but a typical OpenGL app does this (in pseudo c/c++ code, assuming some arbitrary framework to deal with keyboard/mouse events, etc.):
bool run = true;
main() {
...
while (run) {
event e = get_event(e); // this is a key, or mouse event
process_event(e);
render_frame();
}
...
}
void processe_event(event e)
{
if (e.type == KEYBOARD_EVENT && e.value == ESC) { run = false; return; }
...
}
The idea is that you run the render function within an infinite loop. So each time the program iterates over the loop, it will render to the screen the content of your openGL scene. Of course, since it's an infinite loop, the window stays on the screen, until your decide to kill the program (or implement some mechanism in which you escape the loop when some specific keys are used, typically the escape key).
The most basic way of getting this to work, is to use an infinity loop:
while (1) {
render_frame();
}
and do a ctrl+c or interrupt/kill your program. That way you don't have to deal with keys, etc. and then you can at least see what your program does and then move on learning how to use keys.
Aslo I am not sure your code will do anything. First, if you use a double buffer (generally the default these days), you will need to switch buffers to see even the clear function doing something. Second, you will need to add some geometry to your scene. However, note that if you use OpenGL4 for example, you will need to declare and use shaders to see anything, and this is not easy to get this working the first time.
Note that maybe the infinite loop is embedded within the macro DECLARE_MAIN, however this is the problem with using code/framework like the one you are showing in your example, you don't know what's happening elsewhere in the code and how are things coded. For example maybe the buffer swap is happening in the macro DECLARE_MAIN. Generally, I understand why they use macro like that for teaching people (because it hides from you all the complexity of how to get the OpenGL app working) but the downfall is that it stops you from truly understanding the principle of how things work. I personally think this is not the best way to teach graphics especially OpenGL.
The blue book is great, but I would also recommend that you find some example on the web, which shows how to render for example a simple triangle in GL, for example:
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/
They are quite a few on the web, which are simple, well explained, etc.
You will need to choose your framework first though. I recommend GLFW if you can.
Also, while lessons on OpenGL haven't been written for this yet, I would recommend you check www.scratchapixel.com in the future. It will explain how OpenGL works and guide you step by step to get a simple app running.
If you have more questions please add them in your comments.
For my purposes, I want to clear the drawing surface of my canvas and grab the current GL2 object, save it to a managing wrapper and use it one step later, after returning from the canvas's display() method (that in turn calls the display(GLAutoDrawable drawable) method). It seems though, that after returning from the display() method, something happens that causes the GL object to defunction, like when I want to get an available textureID by calling glGenBuffers(1, buffer) I recieve 0 - not a valid textureID to load a texture into.
Is there a way to make the GL object work outside of the display method? (gl.getContext().makeCurrent() does not change anything ...)
Edit: After tinkering around, it seems that the call to glGenTextures does actually nothing - when I create a texture within the display method and then call it later from outside the call to display, I get the same textureID that was in the buffer before - so the call does not change the value within the buffer - also glGetError returns 0 ...
Edit2: Java: openGL: JOGL: What happens behind the scenes when I call the display() method? contained that question, but no answer was given on how to do it. It might be interesting to see a step by step method to do it, if having the code somewhere once, one might not need to modify it ...
Rather use GLAutoDrawable.invoke(), it's safer than trying to get the GLContext stored in the GLAutoDrawable and calling makeCurrent() on it. Anyway, you're trying to defeat the main purpose of GLEventListener which is both useless and dangerous. Finally, the best place to get advises about JOGL is the official JogAmp forum as we can't be everywhere:
http://forum.jogamp.org
When I load an image, the next image loaded for something else is the same image.
Basically:
I'm making a Pong replica and there is a texture for the paddle and for the ball (yes I have made sure that they are both different). You can find the source code here. The paddle loads an image fine, but when it comes to the ball it loads the same exact image as the paddle (Paddle = Paddle.cpp , Ball = Ball.cpp).
What could possibly be wrong with it? Am I using SOIL incorrectly?
From source:
glBindTexture(PadImg , GL_TEXTURE_2D);
This is way wrong, it should be
glBindTexture(GL_TEXTURE_2D, PadImg);
You should get familiar with glGetError, it will help you find problems like this.
Also, these calls are in the wrong order:
glVertex2f(-w,-10);
glTexCoord2f(0,0);
glVertex should always be the last function called per vertex (normals and texcoords come first).
I have a program that draws text and draws a primitive triangle. Both functions work independently. However, when I call them in a row my triangle does not draw and I then receive this error:
D3D10: ERROR: ID3D10Device::Draw: Input Assembler - Vertex Shader linkage error: Signatures between stages are incompatible. The reason is that the input stage requires Semantic/Index (POSITION,0) as input, but it is not provided by the output stage. [ EXECUTION ERROR #342: DEVICE_SHADER_LINKAGE_SEMANTICNAME_NOT_FOUND ]
I've done some research and it looks like it's caused by ID3DXFont changing the state when it renders. Sc4Freak on this thread:
http://www.gamedev.net/topic/487280-dx10-render-question/
Recommends passing a D3DX10_SPRITE_SAVE_STATE, but I don't exactly understand the relationship between spirtes and direct text, or where I should be calling it. Can anyone explain why this happens?
I figured this out. The way you do this is by calling a:
pSprite->begin(D3DX10_SPRITE_SAVE_STATE);
//your text drawtext method stuff here
pSprite->end();
Note that you'll need to initialize the sprite object. You can do this with a D3DX10CreateSprite function, which is very similar to the createtext function.
http://msdn.microsoft.com/en-us/library/bb172670(v=VS.85).aspx