Maybe its because its late but I'm having some math troubles. I want to create a selection rectangle in Java based on an original click point startX and startY and another corner point called every time the mouse is dragged. Here's what I have but it doesn't work.
[code]public void drag(int tx, int ty) {
if (tx - this.x > 0) {
width = tx - this.x;
} else {
this.x = tx;
width = startX - tx;;
}
if (ty - this.y > 0) {
height = ty - this.y;
} else {
this.y = ty;
height = startY - ty;
}
}
public void click(int x, int y) {
startX = x;
startY = y;
}[/code]
The problem is when dragging to the bottom right after having dragged to the upper left, the original point of the shape changes. Maybe someone can point out what I'm doing wrong or point me in the right direction some other way. Thanks.
[QUOTE=ryan1271;35589310]Maybe its because its late but I'm having some math troubles. I want to create a selection rectangle in Java based on an original click point startX and startY and another corner point called every time the mouse is dragged. Here's what I have but it doesn't work.
[code] [/code]
The problem is when dragging to the bottom right after having dragged to the upper left, the original point of the shape changes. Maybe someone can point out what I'm doing wrong or point me in the right direction some other way. Thanks.[/QUOTE]
You only really need to keep track of two points - the click origin, and where it's at now (or where it was when the user released their mouse). From these, you can construct a rectangle. It shouldn't matter whether the rectangle has its first point at the top left or the bottom right, in general.
If it does matter that the first point specified is always at the top left, you can construct the two points by doing
NewPointA.X = Min(PointA.X, PointB.X)
NewPointA.Y = Min(PointA.Y, PointB.Y)
NewPointB.X = Max(PointA.X, PointB.X)
NewPointB.Y = Max(PointA.Y, PointB.Y)
Min and max returns the lower or higher of their arguments, respectively.
[QUOTE=Bambo.;35585150]After spending a while trying to get SFML 2.0 working, i finally got somewhere using this for guidence:
[url]http://en.sfml-dev.org/forums/index.php?topic=6122.0[/url]
but when using the template i can only use code the example gives me, if i try to input anything not used in the template then it errors.
[t]http://puu.sh/pNuV[/t]
Any ideas what it could be? that's the example which builds and runs fine on it's own.[/QUOTE]
Use sf::ConvexShape instad of sf::Shape.
[QUOTE=mechanarchy;35590986]You only really need to keep track of two points - the click origin, and where it's at now (or where it was when the user released their mouse). From these, you can construct a rectangle. It shouldn't matter whether the rectangle has its first point at the top left or the bottom right, in general.
If it does matter that the first point specified is always at the top left, you can construct the two points by doing
NewPointA.X = Min(PointA.X, PointB.X)
NewPointA.Y = Min(PointA.Y, PointB.Y)
NewPointB.X = Max(PointA.X, PointB.X)
NewPointB.Y = Max(PointA.Y, PointB.Y)
Min and max returns the lower or higher of their arguments, respectively.[/QUOTE]
Thank you. I got it working right away with your suggestion.
[code] public void drag(int tx, int ty) {
this.x = min(startX, tx);
this.y = min(startY, ty);
width = Math.abs(startX - tx);
height = Math.abs(startY - ty);
}
public void click(int x, int y) {
startX = x;
startY = y;
}[/code]
I have got trouble running my console program on a school computer, because it doesn't have any sort of .net software installed? Any ideas on how to get it to run anyway? Installing .net is out of the question.
And no, it is not meant to demolish the windows folder..
[QUOTE=Chris220;35567932]I'm attempting to save a file using the dialog boxes provided by the Win32 API. This code works fine except for one thing; upon calling GetSaveFileName() it somehow jumps back to where my save function was called from, then calls it again... I can't really explain it but the end result is that after you close the first Save File dialog, another one opens. I really have no idea what's going on here, from what I've seen online my code is fine.
[cpp]OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(OPENFILENAME));
wchar_t filename[MAX_PATH] = L"";
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = L"OBJ Files (*.obj)\0\0";
ofn.lpstrFile = filename;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
ofn.lpstrDefExt = L"obj";
if(GetSaveFileName(&ofn))
{
char file[MAX_PATH] = "";
WideCharToMultiByte(CP_UTF8, 0, filename, MAX_PATH, file, MAX_PATH, NULL, NULL);
std::cout << "Saving to " << file << std::endl;
}[/cpp]
Any ideas?[/QUOTE]
Can anyone help with this? Sorry to keep bumping it but I need ideas fairly urgently; this is for one of my university assignments :P
Back again with another C++ issue.
I'm trying to create a GameState management system. The class "GameEngine" should store a "GameState" class, which will be the one that GameEngine will call Init, Update, Draw and Cleanup on. Right now, I have GameState as a class that the actual state-classes inherits from. For instance, the class "MenuState" inherits from "GameState". The GameEngine should have a function called "ChangeState" that will take a GameState and switch to it.
Any tips on how to actually store the GameState in the GameEngine-class, and how to change it into a different GameState-instance?
Have a pointer to a GameState object in your class, make ChangeState change that pointer.
[QUOTE=Chris220;35593846]Can anyone help with this? Sorry to keep bumping it but I need ideas fairly urgently; this is for one of my university assignments :P[/QUOTE]
I literally just wrote a similar thing to open OBJ files. I'm not using wchar_t's but here's my code:
[cpp]
inline std::string CreateOpenFileDialog(const char *filter = "All Files (*.*)\0*.*\0", const char *startPath = ".")
{
OPENFILENAMEA ofn;
char fileName[MAX_PATH];
memset(&ofn, 0, sizeof(OPENFILENAMEA));
memset(&fileName, 0, MAX_PATH);
ofn.lStructSize = sizeof(OPENFILENAMEA);
ofn.hwndOwner = /*g_pVideo->GetHandle()*/NULL;
ofn.lpstrFilter = filter;
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = /*OFN_EXPLORER | */OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
ofn.lpstrDefExt = "";
ofn.lpstrTitle = "Open File...";
ofn.lpstrInitialDir = startPath;
std::string fileNameStr;
if(GetOpenFileNameA((LPOPENFILENAMEA)&ofn))
fileNameStr = fileName;
return fileNameStr;
}
[/cpp]
You might try modeling it after this. Alternatively cut out your wchar_t stuff and use the OPENFILENAME[b]A[/b] instead to see if that helps.
GameEngine has this pointer to a GameState:
[cpp]GameState *currentState = 0;[/cpp]
This is the "ChangeState" function in GameEngine:
[cpp]void GameEngine::ChangeState(GameState *gameState)
{
if (currentState)
{
currentState->Exit();
}
currentState = gameState;
currentState->Initialize();
}[/cpp]
And this is the call to the ChangeState function:
[cpp]game.ChangeState(MenuState::Instance());[/cpp]
MenuState::Instance() is a static function that returns the address of m_menuState, which is a private static variable.
[cpp]static MenuState* Instance() { return &m_menuState; }[/cpp]
m_menuState is declared like so:
[cpp]static MenuState m_menuState;[/cpp]
and initialized like so:
[cpp]MenuState MenuState::m_menuState;[/cpp]
Would this be correct? I am unsure about how to handle the pointer in the ChangeState function specifically.
Is there a tutorial on OpenGL 2.x (not ES), because my system doesn't support GL 3.0
When programming in C, do all open files automatically close at the end of the main function?
[QUOTE=Zwolf11;35598815]When programming in C, do all open files automatically close at the end of the main function?[/QUOTE]
In practise, probably. You shouldn't rely on it.
[QUOTE=Zwolf11;35598815]When programming in C, do all open files automatically close at the end of the main function?[/QUOTE]
No. C has no such thing as destructors, so nothing is released automatically.
[QUOTE]fclose() closes the associated file. If function fclose() is not called explicitly, the operating system normally will close the file when program execution terminates. However, if the file is being used for output, some data may still be buffered and not physically written to the output file. It is for this reason that it is important to explicitly call fclose() so that any buffered data is written to the file.[/QUOTE]
[url]http://cboard.cprogramming.com/c-programming/137322-need-fclose.html[/url]
Well, the operating system will close the file when the process ends; so it is "released automatically" in that sense. The real issue is just buffering. You [i]could[/i] just call fflush() at the end of main() to flush the buffers without actually closing the files, and rely on the OS to close the files. But it's cleaner to just fclose() the files.
[QUOTE=Wyzard;35599822]Well, the operating system will close the file when the process ends; so it is "released automatically" in that sense. The real issue is just buffering. You [i]could[/i] just call fflush() at the end of main() to flush the buffers without actually closing the files, and rely on the OS to close the files. But it's cleaner to just fclose() the files.[/QUOTE]
Yes, but at abnormal program termination how likely are you to fclose the files?
In the thread abnormal program termination is specifically mentioned as causing the buffered write not complete issue
I was responding to the original question about what happens at the end of main(), not abnormal termination. But that thread also mentions that buffers are automatically flushed during normal termination, so even the fflush() at the end of main() may not be necessary.
I'm having trouble getting the angle between a vector from the center of a shape to the mouse and the x-axis through the center of the shape. See the image for more info.
[IMG]http://img215.imageshack.us/img215/2408/helpkg.png[/IMG]
[QUOTE=ryan1271;35600169]I'm having trouble getting the angle between a vector from the center of a shape to the mouse and the x-axis through the center of the shape. See the image for more info.
[IMG]http://img215.imageshack.us/img215/2408/helpkg.png[/IMG][/QUOTE]
float ang = atan2(mouse.Y - boxmiddle.Y, mouse.X - boxmiddle.X);
[QUOTE=ryan1271;35600169]I'm having trouble getting the angle between a vector from the center of a shape to the mouse and the x-axis through the center of the shape. See the image for more info.
-snip-[/QUOTE]
Should just be basic trig.
sqrt((mousex - objx)^2 + (mousey - objy)^2) = hypotenuse
then use sine law,
Sin(A)/a = Sin(B)/b
so (mousey-objy)*Sin(hyp)/90 = theta.
I am currently having this error: Unhandled exception at 0x698e91a1 in GameClient.exe: 0xC0000005: Access violation reading location 0x00000008. I am not sure how to find out what is causing this, but it seems to happen right as my main function returns 0. Furthermore, this is supposed to be caused by trying to access a null pointer, but all my pointers are either auto_ptrs or are used for functions exclusively. Any ideas what could be causing this?
[QUOTE=NovembrDobby;35600235]float ang = atan2(mouse.Y - boxmiddle.Y, mouse.X - boxmiddle.X);[/QUOTE]
atan2 is the greatest built in function ever. Especially when you discover it after trying to get atan to act the way atan2 already does...
[QUOTE=WTF Nuke;35602519]I am currently having this error: Unhandled exception at 0x698e91a1 in GameClient.exe: 0xC0000005: Access violation reading location 0x00000008. I am not sure how to find out what is causing this, but it seems to happen right as my main function returns 0. Furthermore, this is supposed to be caused by trying to access a null pointer, but all my pointers are either auto_ptrs or are used for functions exclusively. Any ideas what could be causing this?[/QUOTE]
Most likely some destructor is trying to free an object pointed at by a null pointer, but you'll probably have to run a debugger in order to figure out where it's happening. (If it's when your main function is exiting, that tells me it's likely a destructor).
[editline]16th April 2012[/editline]
[QUOTE=Larikang;35602724]atan2 is the greatest built in function ever. Especially when you discover it after trying to get atan to act the way atan2 already does...[/QUOTE]
This is how you would get atan to behave like atan2, in case you're curious:
[img]http://upload.wikimedia.org/wikipedia/en/math/b/3/7/b37c89e910eaf920daa34b79225ecbc2.png[/img]
I am learning to program in bash for some reason, Idk I think command line is interesting.
Anyways, I wanted to make a 9x9 grid of zeros which was behind the scenes a times table of arrays to be used later for a video game I'm trying to make.
I tried this code but got a few errors. Help? Where did I go wrong?
#!/bin/bash
let postitionTWO=0
let positionTHREE=1
let position = 0
while [$position>82]
do
let $positionTWO = $positionTWO + 1
while [$positionTHREE>10]
do
if [$positionTHREE==9]
then
thepoint[$positonTWO + $positionTHREE]
echo ${thepoint[$positonTWO + $positionTHREE]}
let $position = $positionTWO * $positionTHREE
break
fi
thepoint[$positionTWO * $positionTHREE]
echo -n ${thepoint[$positonTWO + $itionTHREE]}
let $positionTHREE = $positionTHREE + 1
done
done
Here is the error
./gridgame.sh: line 5: let: =: syntax error: operand expected (error token is "=")
./gridgame.sh: line 6: [: missing `]'
I'm working in C++ and I've overloaded the [] operator for a class called board, it basically takes in a coordinate object and returns a cell object contained in board.
Works fine, the problem is in a different area of code I have a pointer to a board object, and I try to use the overloaded operator like so:
board[Coordinate(col,row)]->setShip(theShip);
I get the error:
error: no match for 'operator[]' in '((Battleships::PlaceFleet*)this)->Battleships::PlaceFleet::board[Battleships::Coordinate(col, row)]'
At compiletime.
Anyone able to help me out here, I have no idea what's wrong with it.
I know quite a lot about lua, but that's mostly in Garry's Mod and Love2D, so I'd like to get started on something different, I want to try fool around with 3d, make my own library from scratch, and that stuff. But I've never understood how Direct3D does what it does in the low-end, how does it draw the triangles in a 3D space? The only way I can think of is that you make a 3D to 2D conversion for every pixel on your screen, but that sounds resource intensive as hell, so that can't be it.
Maybe lua isn't even able to do something like this, but if it is, I'd like to try to, could someone possibly write up the steps that's required, or point me to the correct guide I'd use? Thanks.
[QUOTE=Donkie;35604499]I know quite a lot about lua, but that's mostly in Garry's Mod and Love2D, so I'd like to get started on something different, I want to try fool around with 3d, make my own library from scratch, and that stuff. But I've never understood how Direct3D does what it does in the low-end, how does it draw the triangles in a 3D space? The only way I can think of is that you make a 3D to 2D conversion for every pixel on your screen, but that sounds resource intensive as hell, so that can't be it.
Maybe lua isn't even able to do something like this, but if it is, I'd like to try to, could someone possibly write up the steps that's required, or point me to the correct guide I'd use? Thanks.[/QUOTE]
Seeing as i just wrote a 3d renderer from scratch without the use of opengl (particularly), or directx, ill try and give as thorough explanation as i can as to how an example renderer might work. In this case, mine, but you can implement it in many different ways. Unfortunately i know nothing of directx, and i am using SFML for simplification of the context creation and actual drawing to the screen
The first step is to create your context. With SFML this is fairly simple, and there are helper libraries available to make it easier to set up opengl i seem to remember.
The next step (in my case, after opencl is initialised) is to load your models from whatever format you might have stored in them. For me, this is the .obj file, and requires loading 3 different things: Vertex coordinates, vertex normals, and UV coordinates. Vertex coordinates are simply the position of the corner of a triangle (or polygon, though my renderer only supports triangles), vertex normals are the point facing away from the vertex (seeing as vertex's don't technically have a normal, its easiest to think about this as part of the triangle). UV coordinates map a triangle vertex to a point on a texture
You'll also need to the load a texture and push it onto the graphics card
Having loaded all of this and stored it in whatever memory format appeals to you, the next step is to actually push all of the data to the graphics card, assuming you want to perform all the calculations there. Its easiest to do this because then the data can be stored indefinitely (until you need the memory), rather than dynamically pushing the memory when it is needed.
After all the information is pushed to the graphics card, you need to actually process the information. In my case, i pass the vertex coordinates, normals, and UV coordinates as a linear array (accessed as id*9 + objectpartid). Plus, you need camera coordinates and rotation, as well as the coordinates of any lights, and textures associated with the object. Just realised i need to pass a texture ID so that my objects know which texture is associated with it.
My renderer then rotates every vertex taking into account the cameras position and rotation. This can be done as following:
[cpp]one.x=native_cos(c_rot[0].y)*(native_sin(c_rot[0].z)+native_cos(c_rot[0].z)*(position[0].x-c_pos[0].x)) - native_sin(c_rot[0].y)*(position[0].z-c_pos[0].z);
one.y=native_sin(c_rot[0].x)*(native_cos(c_rot[0].y)*(position[0].z-c_pos[0].z)+native_sin(c_rot[0].y)*(native_sin(c_rot[0].z)*(position[0].y-c_pos[0].y)+native_cos(c_rot[0].z)*(position[0].x-c_pos[0].x)))+native_cos(c_rot[0].x)*(native_cos(c_rot[0].z)*(position[0].y-c_pos[0].y)-native_sin(c_rot[0].z)*(position[0].x-c_pos[0].x));
one.z=native_cos(c_rot[0].x)*(native_cos(c_rot[0].y)*(position[0].z-c_pos[0].z)+native_sin(c_rot[0].y)*(native_sin(c_rot[0].z)*(position[0].y-c_pos[0].y)+native_cos(c_rot[0].z)*(position[0].x-c_pos[0].x)))-native_sin(c_rot[0].x)*(native_cos(c_rot[0].z)*(position[0].y-c_pos[0].y)-native_sin(c_rot[0].z)*(position[0].x-c_pos[0].x));
float rx;
rx=one.x * (700.0f/one.z);
float ry;
ry=one.y * (700.0f/one.z);
rx+=array_width[0]/2.0f;
ry+=array_height[0]/2.0f;
one.x=rx;
one.y=ry;
one.z=dcalc(one.z); ///logarithmic calculation for depth
[/cpp]
Ignore the native bits, that's opencl jargon for "do it faster than you might do it otherwise"
Now, the x y and z coordinates are in screenspace. That is, the z coordinate is the distance of a pixel from the screen, and the x and y are relative to the top left corner of the screen
At this point, my probably renderer diverges from the standard method of rendering, but ill put it in anyway because the solution is interesting
Under OpenCL, it is impossible to perform an atomic operator where, when two values are compared, write a third value. This create the impossibility of writing colour information while depth information is being calculated, due to the highly multithreaded nature of graphics cards
After eliminating too close, too far, and triangles completely out of the screen, the solution in my case was to do this (which also demonstrates how you might fill a triangle):
[cpp]int y1;
y1 = round(one.y);
int y2;
y2 = round(two.y);
int y3;
y3 = round(three.y);
int x1;
x1 = round(one.x);
int x2;
x2 = round(two.x);
int x3;
x3 = round(three.x);
int miny;
miny=min3(y1, y2, y3);
int maxy;
maxy=max3(y1, y2, y3);
int minx;
minx=min3(x1, x2, x3);
int maxx;
maxx=max3(x1, x2, x3);
//__global ulong *tpf=&screena[miny*array_width[0] + minx];
//prefetch(tpf, abs(maxx-minx)*abs(maxy-miny));
///PASS IN ROTATED CAMERA COORDINATES
/*float4 light;
light.x=1000;
light.y=0;
light.z=0;
float4 nlight;
nlight=normalize(light);*/ ///lighting unnecessary
float4 camera=c_pos[0];
float4 ncamera;
ncamera=normalize(camera);
//float angle = dot(nlight, ncamera);
float area;
area=form(x1, y1, x2, y2, x3, y3, 0, 0, 0);
if(abs(maxx-minx) > array_width[0] || abs(maxy-miny) > array_height[0]){
isexit=true;
}
int ndrawn=0;
uint mulint=UINT_MAX;
if(!isexit){
///project depth on to depth buffer
for(int x=minx; x<maxx; x++){
for(int y=miny; y<maxy; y++){
if(x < 0 || x >= array_width[0] || y < 0 || y >= array_height[0] ){
continue;
}
float fx, fy;
fx=(float)x;
fy=(float)y;
float s1=fabs(((x2*y-x*y2)+(x3*y2-x2*y3)+(x*y3-x3*y))/2.0) + fabs(((x*y1-x1*y)+(x3*y-x*y3)+(x1*y3-x3*y1))/2.0) + fabs(((x2*y1-x1*y2)+(x*y2-x2*y)+(x1*y-x*y1))/2.0); /// Work out the area of the triangle. If anyone has a better method than the following, i will be happy to implement it
if(s1 < area + 0.001 && s1 > area - 0.001) /// DO NOT USE 0.0001 THIS CAUSES HOELELELELEL
{
int2 coord;
coord.x=x;
coord.y=y;
float f1=one.z;
float f2=two.z;
float f3=three.z;
float rconstant=(x2*y3+x1*(y2-y3)-x3*y2+(x3-x2)*y1);
if(rconstant==0){
continue;
}
float A=native_divide((f2*y3+f1*(y2-y3)-f3*y2+(f3-f2)*y1),rconstant);
float B=native_divide(-(f2*x3+f1*(x2-x3)-f3*x2+(f3-f2)*x1),rconstant);
float C=f1-A*x1 - B*y1;
float cdepth=(A*x + B*y + C);
if(cdepth<depth_cutoff){
continue;
}
uint mydepth=floor(cdepth*(mulint));
__global uint *ft=&screena[y*array_width[0] + x];
uint p=atom_min(ft, mydepth); ///atomic which one is closest to screen
//if(floor(cdepth*(mulint)) < p){
// ndrawn++;
//}
}
}
}
}
//barrier(CLK_GLOBAL_MEM_FENCE);
if(!isexit){
for(int x=minx; x<maxx; x = x + 1){
for(int y=miny; y<maxy; y = y + 1){
if(x < 0 || x >= array_width[0] || y < 0 || y >= array_height[0] ){
continue;
}
float fx, fy;
fx=(float)x;
fy=(float)y;
float s1=fabs(((x2*y-x*y2)+(x3*y2-x2*y3)+(x*y3-x3*y))/2.0) + fabs(((x*y1-x1*y)+(x3*y-x*y3)+(x1*y3-x3*y1))/2.0) + fabs(((x2*y1-x1*y2)+(x*y2-x2*y)+(x1*y-x*y1))/2.0);
if(s1 < area + 0.001 && s1 > area - 0.001) /// DO NOT USE 0.0001 THIS CAUSES HOELELELELEL
{
int2 coord;
coord.x=x;
coord.y=y;
float f1=one.z;
float f2=two.z;
float f3=three.z;
float rconstant=(x2*y3+x1*(y2-y3)-x3*y2+(x3-x2)*y1);
if(rconstant==0){
continue;
}
float A=native_divide((f2*y3+f1*(y2-y3)-f3*y2+(f3-f2)*y1),rconstant);
float B=native_divide(-(f2*x3+f1*(x2-x3)-f3*x2+(f3-f2)*x1),rconstant);
float C=f1-A*x1 - B*y1;
float cdepth=(A*x + B*y + C);
uint cordepth=screena[y*array_width[0] + x];
///If you are not the depth on the depth buffer, go away
if(cordepth>round(cdepth*(mulint)) + 50 || cordepth < round(cdepth*(mulint)) - 50){
continue;
}
ndrawn++;
}
}
}
}
if(ndrawn==0){
isexit=true; ///drew no pixels to screen
ids[i]=0;
}
else{
//PUT TRIANGLE IN BUFFER
ids[i]=1;
}
[/cpp]
This probably looks horribly complicated, but ill promptly explain
There are 3 sections
Quick edit: The method for checking whether or not a pixel is in a triangle is in by area. I used a matrix simultaneous equation solver to give me each variable. Its something like finding the plane passing through all 3 points, if you want to google it
In step 1, loop through every pixel between min(vertex1, vertex2, vertex3) and max(vertex1, vertex2, vertex3) ( one, two, three in my case due to idiocy of not using an array). Calculate the depth per pixel (interpolation of screenspace z coordinates, see this page: [url]http://www.gamedev.net/topic/380061-solve-triangle-interpolation-linear-equations/[/url] ). Use atomic functions to write a correct depth value to the screen, as atomic functions ensure that there is no shit going on. In my case, less depth indicates a closer value to the screen
Step two is remarkably similar, and initially you may be wondering what the point is. Step 2 again interpolates the depth for every pixel displayed, and adds up every pixel in a triangle which satisfies the condition of being the pixel which is drawn to the screen. This actually needs to be split into 2 kernels due to opencl, but at the moment its not an issue to a high degree. The purpose of this is to more accurately eliminate (remember, the depth written before may be overwritten by a closer pixel) triangles not drawn to the screen.
id is a buffer which is as long as all the triangles being drawn to the screen. If any number of pixels are drawn to the screen, id is set to one. This allows the next kernel (lighting and UV mapping) to only process triangles which will be drawn to the screen, instead of having threads waiting around not doing anything
Back in software, this is collapsed so that id[threadid] maps to the id of the triangle that set id[idoftriangle] to 1
Back in hardware again, we perform yet again the same method of triangle filling. It might seem horribly slow, but as far as im aware it would be slower to store the data (no dynamic memory allocation on the device, would have to use global) and reuse it, rather than simply recalculate it.
Seeing as we know the depth of the pixels that will end up drawn to the screen, we can check pixels in that x,y coordinate to see if they have that depth. If yes then -
Perform lighting calculations. Vertex normals are interpolated using the same technique (although here this is the z interpolation. No particular reason, it just c+pd the wrong bit and it doesnt make any difference):
[cpp]
float f1=one.z;
float f2=two.z;
float f3=three.z;
float rconstant=(x2*y3+x1*(y2-y3)-x3*y2+(x3-x2)*y1);
if(rconstant==0){
continue;
}
float A=native_divide((f2*y3+f1*(y2-y3)-f3*y2+(f3-f2)*y1),rconstant);
float B=native_divide(-(f2*x3+f1*(x2-x3)-f3*x2+(f3-f2)*x1),rconstant);
float C=f1-A*x1 - B*y1;
float cdepth=(A*x + B*y + C);
[/cpp]
Having obtained the interpolated normal (phong this is called), you essentially take the dot product of the normal with all of the lights, then combine them however you like (i just go with add and divide by number of lights). See here for more in depth lighting techniques [url]http://en.wikipedia.org/wiki/Specular_highlight[/url]
The next step is to interpolate UV coordinates for the pixel. It is exactly the same method, except you interpolate the reciprocal of the depth, and then divide by the reciprocal for perspective correct texture mapping. You use this UV coordinate as the position of a pixel in a texture. With mip mapping, you can interpolate to obtain a more accurate result, except as of yet i have not implemented it
[url]http://en.wikipedia.org/wiki/Texture_mapping[/url]
Having done this, you simply multiply the light at that point by the texture coordinate. This gives you a nice colour, and you put it into the correct position on your buffer
Back in software, copy everything to SFML, and get it to draw it onto the screen. Mission success
[QUOTE=shill le 2nd;35602752]Most likely some destructor is trying to free an object pointed at by a null pointer, but you'll probably have to run a debugger in order to figure out where it's happening. (If it's when your main function is exiting, that tells me it's likely a destructor).[/QUOTE]
I only defined one destructor, is it possible that a automatically generated destructor is freeing up already freed memory? I don't have any free memory calls, as the only pointers that point to things allocated on the heap are using auto_ptr which from my understanding frees itself.
For the better programmers among us, how much math do you know and how much have you used while programming?
I grasp all the concepts taught in school but I see some of the calculations in bits of code that get posted and I can't imagine how they were figured out.
so i have parent class ISceneNode
[cpp]
class ISceneNode
{
public:
ISceneNode();
~ISceneNode();
virtual void SetParent(ISceneNode* node);
virtual ISceneNode* GetParent();
protected:
ISceneNode* m_Parent;
}
// in seperate cpp file
ISceneNode::ISceneNode()
{
m_Parent = NULL;
}
ISceneNode::~ISceneNode() {}
void ISceneNode::SetParent(ISceneNode* node)
{
if (m_Parent != NULL)
{
m_Parent->RemoveChild(this);
}
m_Parent = node;
}
ISceneNode* ISceneNode::GetParent()
{
return m_Parent;
}
[/cpp]
and then i have a child class, CMeshNode
[cpp]
class CMeshNode : public ISceneNode
{
public:
CMeshNode();
~CMeshNode();
void Draw();
};
// in seperate cpp file
CMeshNode::CMeshNode()
: ISceneNode()
{
// i do some stuff unrelated to parenting
}
CMeshNode::~CMeshNode() {}
void CMeshNode::Draw()
{
if (m_Parent != NULL)
{
// do stuff (THIS GETS CALLED!!)
}
}
[/cpp]
and finally in main.cpp
[cpp]
CMeshNode* node = new CMeshNode();
CMeshNode* node2 = new CMeshNode();
node2->SetParent(node);
[/cpp]
my problem seems to be that once SetParent() is through, m_Parent goes back to being NULL in ISceneNode. however, i can access m_Parent through the child class, CMeshNode. i seriously have no idea what's going on. i've verified that both objects are valid (in main()) at the time of me trying to access m_Parent, yet as soon as SetParent() is done executing, m_Parent is back to NULL whenever i access it inside of ISceneNode methods. yet i can access it just fine through CMeshNode methods. whats the deal?
Sorry, you need to Log In to post a reply to this thread.