How to draw 3D images? - graphics

I am working on a simple 3d software renderer but one thing I'm no sure about is how to actually draw it all on the screen. What could I use to draw a wireframed cube ?
I am not asking HOW to write a complete 3D pipeline just the final step, the actual drawing on the screen.
Edit: I think I could do that with SDL.

You need to project the 3D object onto the 2D screen using a perspective transformation matrix.
This will generate a set of 2D lines etc. which get drawn in the same way as "normal" 2D lines get drawn.
However, without more information about the language and/or framework you are using, it's not easy to go into any more detail.

For the "actual drawing on screen" in Windows XP of your software-rendered wireframe 3D, call StretchDIBits with a pointer to the array of bytes that represents your pixels. This answer addresses maximum convenience; maximum efficiency is another matter.

Related

Is there any code for an interactive plotting application for a two dimensional curves

Plotting packages offer a variety of methods for displaying data. Write an interactive plotting application for two dimensionsional curves. Your application should be able to allow the user to choose the mode (line strip or polyline display of the data, bar chart or pie charts), colours, and line styles.
You should start with the GUI editation like this:
Does anyone know of a low level (no frameworks) example of a drag & drop, re-order-able list?
and change it to your primitives (more points per primitive instead of one ... handle each point as (sub)object so you can change its position later).
Then just add tools like add object,del object,... For hand drawing tool use piece wise interpolation cubics
The grid can be done like this:
How to draw dynamic 2D grid that adjusts according to camera zoom: OpenGL
Mouse zooming/panning is also important
Zooming graphics based on current mouse position
Putting all above together into simple editor looks like this:
Using GPU for curve rendering might give you some nice speed and functionality boost:
Is it possible to express "t" variable from Cubic Bezier Curve equation?
Mouse selection of objects might be a speed problem if your scene contains too many objects so in such case its best to use index buffers where you can mouse select with pixel perfect precision for almost free in O(1):
OpenGL 3D-raypicking with high poly meshes
The example is for 3D , in 2D is much simpler ...
Also do not forget to implement save/load functionality to some vector file format. I recommend using SVG it might be complicated to start with it but you can quickly check it contents in any SVG viewer or browser also in notepad as its just a text file. If you use just basic path elements and ignore the rest of SVG features you will see the parsing and creating SVG is not that hard for example See these:
Get Vertices/Edges From BMP or SVG (C#)
Discrete probability distribution plot with given values
For really big datasets you might want to use spatial subdivision techniques (Bounding (Volume)Area Hierarchy, or Quad tree) to ease up the operations...
More in depth implementation details about 2D vector gfx editors depends on language, OS, gfx api and GUI api you using and task you are aiming for ...

3d Graphing Application Questions

For one of my classes, I made a 3D graphing application (using Visual Basic). It takes in a string (z=f(x,y)) as input, parses it into RPN notation, then evaluates and graphs the equation. While it did work, it took about 20 seconds to graph. I would have liked to add slide bars to rotate the graph vertically and horizontally, but it was definitely too slow to allow that.
Does anyone know what programming languages would be best for this type of thing? Ideally, I will be able to smoothly rotate the function once it is graphed.
Also, I’m trying to find a better way to rotate the function. Right now, I evaluate it at a bunch of points, and then plot the points to the screen. Every time it is rotated, it must be re-evaluated and plot all the new points. This takes just as long as the original graph process, as it basically treats it as a completely new function.
Lastly, I need a better way to display the graph. Currently (using VB with visual studio) I plot 200,000 points to a chart, but this does not look great by any means. Eventually, I would like to be able to change color based on height, and other graphics manipulation to make it look better.
To be clear, I am not asking for someone to do any of this for me, but rather the means to go about coding this in an efficient way. I will greatly appreciate any advice anyone can give to help with any of these three concerns.
So I will explain how I would go about it using C++ and OpenGL. This doesn't mean those are the tools that you must use, it's just those are standard graphics tools.
Your function's surface is essentially a 2D manifold, which has the nice property of having an intuitive mapping to a 2D space. What is commonly referred to as UV mapping.
What you should do is pick the ranges for the rectangle domain you want to display (minimum x, maximum x, minimum y, maximum y) And make 2 nested for loops of the form:
// Pseudocode
for (x=minimum; x<maximum; x++)
for (y=minimum; y=maximum; y++)
3D point = (x,y, f(x,y))
Store all of these points into a container (std vector for c++ works fine) and this will be your "mesh".
This is done once, prior to rendering. You then render those points using, for example GL_POINTS, and rotate your graph mesh using rotations on the GPU.
This will only show scattered points, not a surface.
If you also wish to show the surface of your function, and not just the points, you can triangulate that set of points fairly easily.
Group each 4 contiguous vertices (i.e the vertices at indices <x,y>, <x+1,y>, <x+1,y>, <x+1,y+1>) and create the 2 triangles:
(<x,y>, <x+1,y>, <x,y+1>), (<x+1,y>, <x+1,y+1>, <x,y+1>)
This will fill triangulate the surface of your mesh.
Essentially you only need to build your mesh once, and this way rendering should be 60 fps for something with 20 000 vertices, regardless of whether you only render points or triangles too.
Programming language is mostly not relevant, so VB itself is probably not the issue. You can have the same issues in Python, C#, C++, etc. Of course you must master the programming language you choose.
One key aspect is using the right algorithms and data-structures. Proper use of memory allocations and memory layout for maximizing CPU (and GPU) cache are also key. Then you must take advantage of the platform and hardware capabilities (GPU and Multithreading). For the last point you definetely need to use a graphics library such as OpenGL or Vulkan.

Fastest way to draw to screen with software 3D rendering

I'm currently taking a course on polygonal 3D rendering from scratch. We write our own line drawing and clipping algorithms that are eventually used to draw polygons in 3D space using code for perspective transformations that we write ourself. The assumption of the course is that we write to 2D arrays that represent the window, viewport, or display device. In the first week of the course we wrote code to write out these 2D arrays as bitmap files so we could view the output.
Now I want to see the output of my software renderer in real-time and interact with it. What is the fastest way to draw a 2D bitmap array to the screen, in Mac OSX 10.9 for example? Linux? Windows?
I'm specifically looking for speed here, as the only thing that I want the GPU to do is draw the 2D array that I just rendered in main memory at runtime.
Without the initialization step it should be OpenGL rendering of the bitmap on screen aligned quad(What's the best way to draw a fullscreen quad in OpenGL 3.2?) Only costly operation will be uploading the bitmap but it's unavoidable anyway.

Is there any graphics library like this?

IS there any graphic library meeting the following requirement.
can draw a point, a line, and a circle.
the size of canvas can be extended automatically.
support negative coordinates.
can output to png or any vectorgraph(like svg).
can draw characters (english only) (even characters rotated at a certain angle)
for e.g.
I draw 2 point (-1,-1) (1,1).
it will output a 2x2 sized picture. This left-top is the point(-1,-1). and the right-bottom is the point (1,1)
thanks.
Yes, of course there are a couple of them. I would suggest you take a look and learn OpenGL. You can run it on Windows, Mac, Linux, iOS, Android, you name it.
It supports 2D and 3D rendering, and, even though is more complex than a regular 2D graphics library like CoreGraphics, it also more powerful.

3d icons on top representation with direct x 9 in c++

I'm doing a simulator visual where I have a lot of 2D icons representing elements of the simulation and I have a 3D (.x made with Blender) icon which represents another and more important element.
I need to make this one on top of the other 2d icons (.tga) but I cannot find the way to make this. Is it possible for Direct x to draw a 3d icon over the rest of the 2d icons?.
At this moment, the 2D icons apear on top of the 3D, always.
Could please someone help me with this. I'm programming in C++
Thanks very much in advance.
Disable Z-writes when you render the 2D icons. When you then render the 3D icons with Z-writes enabled it will render OVER the 2D icons.
Your problem at present is that when you render the 2D icons you are doing so with a z value which is the front of the z-buffer (most probably 0). You may also find you can set the z-value to 1 (again this is probably, it entirely depends on how you've set up your 2D rendering) to force the 2d icons to render at the back of the z buffer.
No entry in the Z-buffer is equivalent to it being at the back of the Z-order.
Assuming you have depth buffering enabled, you need to ensure that the Z value of your icons is further from your camera than your model. If you are using ID3DXSprite, you should be able to specify the Z position when you call the Draw function, or by using the ID3DXSprite::SetTransform() function.
Note that ID3DXSprite may draw your sprites with a different projection matrix than your model, depending on the flags you pass to ID3DXSprite::Begin(). You might want to use the D3DXSPRITE_OBJECTSPACE flag.

Resources