Zaki Mirza’s Blog

Icon

… About software and beyond!

Intellisense, UI Frameworks and Efficiencies – Discussions with Steve Teixeira

Ive been following Steve’s Teixeira’s (Group Program Manager for Visual C++) blog for a while now. A couple of months ago, i started a discussion with Steve Teixeira –  about intellisense support in Visual Studio .Net 2005 in C# as compared to Visual C++.

Later on his Channel9 video blogpost about vc++ product strategy, i asked about performance degradation in .Net UI frameworks compared to win32 API and MFC stuff. Steve put his point of view on that which started a debate about UI frameworks that have emerged since the advent of .Net Framework including WinForms and WPF. The discussion took a pace which you can see on his post’s comments. The discussion continued with steve’s response to comments on the earlier discussion in response to my question.

I think this kind of discussion breeds good interaction between the people in the business and their customers and users. Microsoft’s recent initiatives to get things right again with customers by starting channel9 and on10 and many different contests and has somewhat saved its reputation. Lets see if Microsoft can turn it around towards up again.

Also, these kinds of discussions are where blogs show their true potential of information sharing. Yes there have been forums which have served the purpose since ages. But i think blogs serve a different purpose than forums. While forums are for discussion, blogs are more about rapid information sharing. Things like these would take ages to make it to the market in a book that are now discussed on blogs by various people around the blogosphere.

Filed under: Blog Log, C++, software, visual studio

Quick C++ Quiz #1

Im starting a little section for C++ quizes. Im nowhere near a C++ wizard. Just something i learn from here and there and ill put em up as food for thought for all my readers. Part 2 ready as well lets c.

consider this :

//quiz.cpp

 int main(int argc, char** argv){
int a();
}

Whats the problem with above code if any. Also, what do you think the above code does (or tries to do?) what is ‘a’?

Filed under: C++, programming, quiz

using glDrawElement for more logically building 3d objects in opengl

Working on CG again, i was seeing how we have to do a lot of things in glBegin() and glEnd block to create simple to complex objects. What i just found out is the use of Vertex arrays in openGL. (checkout the Red Book Chapter 2). Ill just give one example of this.

Suppose you want to create a cube (im sure most of them are already hating the sight of the “cube” :p). Simple one would go on create the 6 faces using GL_QUADS like:

glBegin(GL_QUADS);
glVertex3i(0,0,0);
glVertex3i(2,0,0);
glVertex3i(2,2,0);
glVertex3i(0,2,0);

.. and so on for rest of the faces traversing the vertices in counter-clockwise direction. thats like, 6×4 24 function calls! (imagine the overhead, on the PC and on… well ur hands:p). So GL has this really cool mechanism of vertex arrays. I wont go in details since the red-book is “the” definitive guide on openGL stuff but heres the example. First you make an array of the vertices like: (oh im lazy so im just pasting it from my own code, basically here width,height and depth are comming in the constructor, CP is a point with x,y,z fields).

//vertex definition of cube
GLfloat va[] = {
cp.x-width/2.0f, cp.y+height/2.0f, cp.z+depth/2.0f,
cp.x+width/2.0f, cp.y+height/2.0f, cp.z+depth/2.0f,
cp.x+width/2.0f, cp.y-height/2.0f, cp.z+depth/2.0f,
cp.x-width/2.0f, cp.y-height/2.0f, cp.z+depth/2.0f,
cp.x-width/2.0f, cp.y+height/2.0f, cp.z-depth/2.0f,
cp.x+width/2.0f, cp.y+height/2.0f, cp.z-depth/2.0f,
cp.x+width/2.0f, cp.y-height/2.0f, cp.z-depth/2.0f,
cp.x-width/2.0f, cp.y-height/2.0f, cp.z-depth/2.0f
};
//face drawing indices
GLubyte ia[] = {
0,3,2,1,
/* front */
0,4,7,3, /* left */
3,7,6,2, /* botom */
3,1,5,6, /* right */
6,7,4,5, /* back */
5,4,0,3 /* top */
};
if you work these indices out, youll end up with a solid cube. (remember the join-the-dots game aah i miss them ). So now we have the vertices, we have an indices list that says how to join the dots. we know we’ll do the quads here. What now?First we enable the openGL vertex array state.glEnableClientState(GL_VERTEX_ARRAY);Then we tell where our vertices are:glVertexPointer(3,GL_FLOAT,0,_vertices);and then we draw this element using out indicesglDrawElements(_drawMode,_numIndices,GL_UNSIGNED_BYTE,_indices);(GL_UNSIGNED_BYTE tell what is the type of indices, _numIndices is the number of indices in the array. can be less than the acutal array but then only those vertices will be traversed. and _drawMode is my variable, i set it to GL_QUADS for solid cube.)So basically we have made a cube with a lot less function calls and in a lot logical manner. Check out what these functions do and how to best utilize them in the red-book chapter 2. i have the red book for version 1.4 of OpenGL (i dunno yet if they will be available  in previous versions).The possibilities are endless using this technique. There are also ways to specify color array for the element like this as well. 

Be cautious though. make sure you change the glVertexPointer everytime you make a new object. Also to disable the vertex array mode when you are done with it. Also dont use glDrawElements within GlBegin and glEnd because glDrawElement will do it for you. if you do it just like i wrote here, you might get a single colored tasteless cube which doesnt even look like a cube. try using GL_COLOR_ARRAY to color different vertices. Ill put of a post about lighting in openGL soon (as soon as i learn it myself :p)

Filed under: C++, CG, opengl, programming

“protected” usage

I know the title is weird, but i couldnt think of anything better. So here i was coding one of my CG assignments. It was a simple assignment though im very intrigued by game/software graphics engines so i plunged in to make my own. Its in very basic stages for now. Right now im going to talk about a problem i encountered.

Consider this, you have a class called ‘BaseObject’ which is basically for a graphical object. Now graphical objects are represented by points/vertices and how surfaces/lines are drawn between those vertices. (in my case those were indices of vertices in order. ill tell you what i mean in a sec). Now how i see it the client of the class shouldnt be allowed to create an 3d/2d object without specifing the vertices,indices and the kind of surface/line between the vertices(at the least, because otherwise it doesnt make sense for a graphical object to exists without a single vertex). The Draw() method would take care of drawing. (im using opengl as my graphics api and sdl for windowing and message handling).  So in a very usual case you’d refrain from making a default constructor of the BaseObject class and at the least it would take the array of vertices,indices and the draw method (e.g. GL_POLYGON, GL_LINES etc). So far, the code would be like this:

class BaseObject
{
public:
GLfloat* _vertices;
short _numVertices;
GLint* _indices;
short _numIndices;
GLint _drawMode;

BaseObject(
int numVertices,int numIndices,GLfloat vertices[],GLubyte indices[],GLfloat colors[],GLenum drawMode);virtual void Draw();
~BaseObject(
void);
};
 Now consider this. I want to make an specialization of this class, say a SolidCube class. So instinctivly i would inherit our SolidCube class from BaseObject class, let use tell me the center of the cube, the width,height and depth and pass that to base class which will handle the drawing. So how about:

class SolidCube : public BaseObject
{
public:
SolidCube(GLfloat width,GLfloat height,GLfloat depth,Point3D cp
);
~SolidCube();
};
Now here comes the problem. When you inherit from a class you have to initialize that in the intializer list in the constructor. Given the center point, the height, the depth and width, i know how to calculate the 8 vertices of the cube. But i have no space to do the coding between the constructor definition and the initializer of base class in initializer list. e.g.

SolidCube::SolidCube(GLfloat width,GLfloat height,GLfloat depth,Point3D cp)
: BaseObject(){}
Now we intentionally excluded that default constructor from the BaseObject definition so that noone can make an empty object. (well ofcourse they can using NULL NULL for all the arrays and all, but hey we can sort that out in some other post. this is a lame design i know). So what do we do? Well my little solution was to make use of the “protected” access modifier. It lets you secure the variable/function for the derived classes. In this case, making BaseObject’s defualt constructor protected, i make sure that the clients of the class cant use this constructor but all the derived classes can. So now the above code can work and the derived class can manually set the vertices array after working out the vertices. By the way comming back to why im using indices ill write another post on that just right now.

The visual studio will let you do something like:

BaseObject a();

and if you try to do a.Draw() or anything with “a” it will give you a compile time error. It will give you a warning though when you try to do the above thing.

Filed under: C++, Code, programming

Blog Stats

  • 104,198 landed here so far...
May 2017
M T W T F S S
« Jan    
1234567
891011121314
15161718192021
22232425262728
293031  

tweet

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

RSS Google Shared Items

  • An error has occurred; the feed is probably down. Try again later.

RSS Google Reader Starred Items

  • An error has occurred; the feed is probably down. Try again later.

Top Clicks

  • None