# OpenGL OpenGL & Raytracing

## Recommended Posts

Hello, I was wondering, if I'd build a raytracing in software, what would be the fastest way to get my image across? glDrawPixels on the entire screen drops my application to 300 fps. And I still want to be able to use opengl commands, because I want to offload the CPU with stuff that can be done on the GPU (primairy ray intersection for instance, and shading). My ultimate goal would be to combine software and hardware. So is there any extension that would allow me proper pixel manipulation? or should I dive into shaders? Regards, Dolf

##### Share on other sites
Yes, you ca use glDrawPixels and it should be good enough and fast on todays hw.

Quote:
 So is there any extension that would allow me proper pixel manipulation? or should I dive into shaders?

If you want to do raytracing in the shader, then yes it is possible. There are some demoes out there.

http://www.geocities.com/vmelkon/glsl_raytracing.html
I don't have the source anymore.

http://www.humus.ca/index.php?page=3D&&start=16

##### Share on other sites
"Yes, you ca use glDrawPixels and it should be good enough and fast on todays hw."
I didn't like the speed of that.

Thanks for the links!

Do i have to raytrace in shaders? or can i raytrace on the cpu and give it to the gpu? using a shader instead of drawpixels?

I was thinking of a hybrid between cpu and gpu, but you recommend doing all the tracing on gpu using shaders?

##### Share on other sites
Yes, raytracing can be done in shaders and there was some other guys who had done it. They were able to do more complex models instead of just planes and spheres.

What hybrid model do you have? Because glDrawPixels just renders a ready made image.

##### Share on other sites
Why on earth are you complaining about 300fps?

##### Share on other sites
Quote:
 Original post by SiCraneWhy on earth are you complaining about 300fps?

I believe birds such as pigeons(who see somewhere between the range of 60-200 fps), who may be viewing his material may see only a slightly blurred slide show instead of the animation he requires. Wait no they wouldn't.

##### Share on other sites
Without any ray tracing done, just the pure act of sending glDrawPixels, slows me down to 300 fps.
With nothing but an empty screen I was expecting 1500, I've seen that.

Well, if you render in software to an array of bytes, and then send it to the GPU just to blend it in with the rasterized image.

I'm not sure what kind of hybrid I want yet, but I will be going for a real-time implementation.

I think I'd like to leave all the things that rasterization is good at in there for the GPU, and the things the GPU sucks at (like reflections, refractions, soft shadows and maybe global illumination) I'd like to raytrace those.
And with "raytracing" I mean either on the CPU or on the GPU, whatever turns out to be the best.

Edit: I just found out about "render to texture", maybe that would be faster than drawpixels?
Edit again: Or maybe there is a way to get direct access to the framebuffer?

[Edited by - Dolf on March 23, 2008 2:40:54 PM]

##### Share on other sites
The GL driver is tuned to execute a certain amount of commands and in the general area of 60 to 100FPS.
300 FPS is meaningless.

Someone needs to write an article on this so that people understand that they must render a lot of stuff and then worry about benchmarking.

##### Share on other sites
My point is, I've seen it with 1500 fps in some direct 3d application.

How do i get that same result in opengl? I'm not saying "it's too slow" , i am saying "it's slower than it can be".

##### Share on other sites
Quote:
 Original post by DolfMy point is, I've seen it with 1500 fps in some direct 3d application.How do i get that same result in opengl? I'm not saying "it's too slow" , i am saying "it's slower than it can be".
I'm not an OpenGL person, so this won't be that helpful, but drawing individual pixels is bound to be slow. For a 320x200 screen, you'd be drawing 64,000 individual pixels. In D3D, you'd fill a texture with pixel data, then upload that in one go to the graphics card.

I imagine doing something similar in OGL would give you better performance.

##### Share on other sites
thanks evil steve

so rendering to textures :) i'll read up and give it a go.

Result: using glTexSubImage2D every frame is slower than gldrawpixels

[Edited by - Dolf on March 23, 2008 2:15:11 PM]

##### Share on other sites
If your really worried about performance then you shouldn't be using individual draws. The best idea would be to use FrameBufferObjects for this task, they should be a hell of a lot quicker and using render to texture rather than *fake* render to texture methods by using the gl...SubImages().

You might want to take a look at the Arauna Real Time Library source code which is available from a link off this website. Type it in the search bar and you should find a few results.

Also without a GeForce 8800 GTX equivalent or a Quad/Oct Core Processor its very unlikely that a drop from 1500-300FPS is going to be a problem.

Is VSync turned on? When you get numbers of FPS above 100, the measurement becomes quite irrelevant, at those numbers we can't see a difference.

Neutrinohunter

##### Share on other sites
Well, looking at these f/s (also referred as fps) drops don't tell much about exact rendering times! It's much better to show rendering times in miliseconds. Take look at this - your drop was from 1500 f/s, so your frame was drawed every 0,666666... milisecond, to 300 f/s - means that you've rendered frame every 3,333333... miliseconds. So drawing your raytrace buffer onto screen took 2,666666... milisecond - that's not SO BIG drop.

Anyway I've done some research into combined rendering methods (rasterization and raytracing), which I use in a project that I'm working on - it's not just mine project - it's engine (can be used to create games, or just for rendering - it's still not completed). I succesfully use combined rendering to get F.e. Global Illumination, Nice soft shadows (using own PCML shadow mapping algo), etc. It's working pretty nice, framerates are realtime (Well - On lower resolutions - F.e. 800x600 or 1024x768 - even 40 or more f/s (sometimes more, sometimes less - it highly depends on complexity of scene, number of dynamic objects, etc.) on todays high-end hardware).

##### Share on other sites
Mister Vilem Otte,

I was wondering, what tasks do you do as a shader and which ones do you do on the CPU?
And am I right in assuming you are doing everything in rasterization, except: soft shadows, global illumination, reflections and refractions?

Mister Neutrinohunter,

V-sync isn't on.
I'll look into framebufferobjects and I dont think arauna will help me that much. Arauna renders in software. I'd like to create a hybrid.

##### Share on other sites
Quote:
 Original post by DolfMy point is, I've seen it with 1500 fps in some direct 3d application.How do i get that same result in opengl? I'm not saying "it's too slow" , i am saying "it's slower than it can be".

It depends on the driver's behavior. So I cannot explain why you get 1500 in D3D and 300 in GL.
All I'm saying is that you need to render a lot of stuff on screen.
You might end up with GL at 70 FPS and D3D at 50 FPS.
It very much depends on the driver and all the GL commands you call.

##### Share on other sites
So, i'll describe F.e. Ambient occlusion (It's little simplier) - in short how ambient occlusion works - tracing rays in hemisphere over point - I'm getting some data and then computing visiblity factor from "some data" on that pixel. (Where some data = collision data from those rays emitted by point in random directions of hemisphere, the more rays in random direction I trace, the higher quality ambient occlusion would be) - I hope this short explanation is understandable.

1. I'm viewing scene from camera view point and tracing primary rays if I get collision, then I calculate ambent occlusion for that pixel.
- So now I have ambient occlusion in buffer (similar to texture buffer).

2. I generate texture from the raytracing buffer, there are two ways of how-to - using glTexImage2D (respectively glTexSubImage2D) or using glDrawPixels and glCopyTexImage2D (respectively glCopyTexSubImage2D, It might be possible to use frame buffers and P-buffers for this, but I haven't tested that yet)
- Now I have ambient occlusion in texture.

3. I rasterize whole scene with textures, lighting, normal mapping (sometimes with some kind of offset mapping - F.e. Parallax occlusion mapping), etc. from camera point and I have to render this into texture (I'm using frame buffer, but you can use F.e. glCopyTexSubImage2D).
- Now I have whole rasterised scene in texture.

4. I combine these two textures (for ambient occlusion is best to multiply them) and voilá I get....

5. Final scene with raytraced ambient occlusion

- I'm not using the same texture size for "raytrace buffers" and "rasterized buffers", raytrace buffers are smaller! (with bilinear filtering it looks smooth = nice)

Anyway ambient occlusion wouldn't make much sense in these days, because it'll give you similar result as screen-space ambient occlusion, but raytraced AO would be several times slower. And the last thing - raytracing speed depends very much on resolution of "scene" - so raytrace the smallest buffer you can (anyway you need to have at least some quality).
I'm not doing just reflections, refractions, GI and soft shadows using raytracing, I can render whole scene using raytracing, but it'd be slower, than combine it. Anyway reflections, refractions and GI can't be achieved correctly with rasterization (well, they can be done, but it's damn hard and damn slow - it has sense F.e. for plane).

EDIT:
#Neutrinohunter - You can't use framebuffers until you've got it in the textures or until you draw it onto screen using glDrawPixels. And "raytrace buffers" aren't textures, they're just field of floats or ints or doubles or ...

##### Share on other sites
Similar question with some answers:

http://ompf.org/forum/viewtopic.php?f=4&t=657

FYI, doing glTexSubImage2D is much faster than glDrawPixels, if you use it correctly. At least it was for me at 1920x1200x32 resolution, and seems to be the case for other people.

##### Share on other sites
Vilem: The method I use is to render to my own image class, which allows me to perform sampling methods or blurring if wanted and then render that image as a texture to an FBO.

In his case, yes he may require to do a glDrawPixels, in the method I use I don't :)

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
627744
• Total Posts
2978901
• ### Similar Content

• Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
So, here's what the plan is so far as far as loading goes:
Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!

• I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks

• A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

-What I'm using:
C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.
-Questions
Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.

• 10
• 10
• 21
• 14
• 14