How to texture Opengl glut objects (C++) - c++

I have already tried and succeeded loading a texture from a bmp file, and drawing quads and triangles with texture. However i need to apply the loaded texture to an object drawn with glutSolidDodecahedron and glutSolidSphere. How can i do this? Please include some code if possible
Note: I HAVE to use those functions, I'm not allowed to draw them from scratch.

Neither glutSolidDodecahedron nor glutSolidSphere specifies texture coordinates, at least not according to any documentation that a quick web search turns up. I had a quick look at the FreeGLUT implementations and those do indeed not specify texture coordinates.
If you can use shaders, you can derive the 2D texture coordinates from the 3D location of the vertices. Spheres and dodecahedrons are pretty regular shapes, so you can simply do a spherical projection (convert the vertex position to spherical coordinates and drop the radius component).

Related

How do I map a texture correctly onto a convex polygon in SFML or OpenGL?

I want to represent my Objects as textured convex Polygons. For the most part those will just be rotated rectangles but i want to support convex shapes too and thats where the problems arise.
I worked with Blender a while ago and there you could unwrap the 3D-Objects and explicetely tell Blender which vertex of the Shape has which Position on the Texture.
Would it maybe be better to just request the Texture to have the size of the bounding Rectangle of the Shape so I can just apply the texture with SFML?
PS: Im sorry i cant post pictures to clarify my question.
or OpenGL
In OpenGL, typically you'll have two (or more!) vertex attributes: position and texture coordinate. That's basically saying which vertex of the Shape has which Position on the Texture.
That's what SFML has to be doing internally, and since its Open-Source, you might just peek inside and see if your "bounding rectangle" idea has a chance of working (my guess is that it indeed does).

Texture Mapping without OpenGL

So I'm supposed to Texture Map a specific model I've loaded into a scene (with a Framebuffer and a Planar Pinhole Camera), however I'm not allowed to use OpenGL and I have no idea how to do it otherwise (we do use glDrawPixels for other functionality, but that's the only function we can use).
Is anyone here able enough to give me a run-through on how to texture map without OpenGL functionality?
I'm supposed to use these slides: https://www.cs.purdue.edu/cgvlab/courses/334/Fall_2014/Lectures/TMapping.pdf
But they make very little sense to me.
What I've gathered so far is the following:
You iterate over a model, and assign each triangle "texture coordinates" (which I'm not sure what those are), and then use "model space interpolation" (again, I don't understand what that is) to apply the texture with the right perspective.
I currently have my program doing the following:
TL;DR:
1. What is model space interpolation/how do I do it?
2. What explicitly are texture coordinates?
3. How, on a high level (in layman's terms) do I texture map a model without using OpenGL.
OK, let's start by making sure we're both on the same page about how the color interpolation works. Lines 125 through 143 set up three vectors redABC, greenABC and blueABC that are used to interpolate the colors across the triangle. They work one color component at a time, and each of the three vectors helps interpolate one color component.
By convention, s,t coordinates are in source texture space. As provided in the mesh data, they specify the position within the texture of that particular vertex of the triangle. The crucial thing to understand is that s,t coordinates need to be interpolated across the triangle just like colors.
So, what you want to do is set up two more ABC vectors: sABC and tABC, exactly duplicating the logic used to set up redABC, but instead of using the color components of each vertex, you just use the s,t coordinates of each vertex. Then for each pixel, instead of computing ssiRed etc. as unsigned int values, you compute ssis and ssit as floats, they should be in the range 0.0f through 1.0f assuming your source s,t values are well behaved.
Now that you have an interpolated s,t coordinate, multiply ssis by the texel width of the texture, and ssit by the texel height, and use those coordinates to fetch the texel. Then just put that on the screen.
Since you are not using OpenGL I assume you wrote your own software renderer to render that teapot?
A texture is simply an image. A texture coordinate is a 2D position in the texture. So (0,0) is bottom-left and (1,1) is top-right. For every vertex of your 3D model you should store a 2D position (u,v) in the texture. That means that at that vertex, you should use the colour the texture has at that point.
To know the UV texture coordinate of a pixel in between vertices you need to interpolate the texture coordinates of the vertices around it. Then you can use that UV to look up the colour in the texture.

Texture mapping with cylinder intermediate surface manually

I'm working on a scanline rendering for a class project. The renderer works so far, it reads in a model (using the utah teapot mostly), computes vertex/surface normals, and can do flat and phong shading. I'm now working on adding texture mapping, which is where I'm running into problems (I cannot use any OpenGL methods other than actually drawing the points on the screen).
So, I read in a texture into my app and have a 2D array of RGB values. I know that the concept is to map the texture from 2D texture space to a simple 3D object (in my case, a cylinder). I then now that you then map the intermediate surface onto the object surface.
However, I don't actually know how to do those things :). I've found some formulas as to mapping a texture to a cylinder, but they always seem to leave details out such as which values to use. I also then don't know how to take the vertex coordinate of my object and get the cylinder value for that point. There's some other StackOverflow posts about mapping to a cylinder, but they 1) deal with newer OpenGL with shaders and such and 2) don't deal with intermediate surfaces, so I'm not sure how to translate the knowledge from them.
So, any help on pseudo code for mapping a texture onto a 3D object using a cylinder as an intermediate surface would be greatly appreciated.
You keep using the phrase "intermediate surface", which does not describe the process correctly, yet hints at what you have in your head.
Basically, you're asking for a way to map every point on the teapot's surface onto a cylinder (assuming that the texture will be "wrapped" on the cylinder).
Just convert your surface point into cylindrical coordinates (r, theta, height), then use theta as u and height as v (texcoords).
This is what you are trying to achieve:

Convert stack of 2d images into 3d image, volume rendering

I want to do a texture based volume render of CT data. I have a stack of 2d CT images that I'd like to use as a 3d texture in opengl (jogl really). I have to do it the way with polygon proxy geometry that shifts when viewing parameters change. How can I convert the 2d images to one 3d texture? I have not been able to find anything about how opengl expects 3d images to be formatted. I saw this: https://stackoverflow.com/questions/13509191/how-to-convert-2d-image-into-3d-image , but I don't it's the same.
Also, I am still in confusion about this volume rendering technique. Is it possible to take a 3d location in the 3d texture and map it to a 2d corner of a quad? I found this example: http://www.felixgers.de/teaching/jogl/texture3D.html but I don't know if it means you have to use 3d vertices. Does anyone know more sources with explicit examples?
See
http://www.opengl.org/registry/doc/glspec40.core.20100311.pdf
section 3.8.3, on defining 3D texture images.
This results in a 3d cube of texels, and yes, you can map a 3d location in this cube to a corner of a quad.
OpenGL does know a 3D texture format where each texel is a small subvolume in a [0;1]^3 cube. When you texture a triangle or a quad with this texture, it is like if you cut out a thin slice of this volume. If you want a volumetric you must write a volume raycaster. If you Google "GPU direct volume rendering" you should find plenty of tutorials.

how to bind part of the texture opengl

I wish to bind a texture on a cube (creating cube using GlutSolidCube and not glvertex) but the whole texture is bound. In the image file I have all textures together (for speed and because the teacher requested) and I only want part of the texture to be bound. How can I do that????
Textures are the unit of texture binding. If you want to "cut out" part of a texture, you do so by adjusting the texture coordinates that you use.
Instead of using the full range of 0..1, use smaller values that match the sub-texture's location inside the texture.
What you're looking to do is not possible, because glutSolidCube does not generate texture coordinates.
However, you will also note that an answer to that question indicates that you may use the following to have OpenGL generate texture coordinates for you on a call to glutSolidCube:
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
Some more information on using OpenGL's automatic texture coordinate generation is available here. However, I would like to note that this seems to come out of the days of immediate-mode OpenGL, which is deprecated. Also, GLUT is no longer maintained, but freeglut is.
To summarize, you're better off using glVertex calls and specifying your own specific texture coordinates, as unwind has suggested. You can try OpenGL's texture coordinate generation, but it might be too strict to handle what you need.